diff -r 0202bb3a6d74 pypy/module/micronumpy/numarray.py --- a/pypy/module/micronumpy/numarray.py Wed May 04 23:20:11 2011 +0200 +++ b/pypy/module/micronumpy/numarray.py Wed May 04 15:57:55 2011 -0600 @@ -125,6 +125,16 @@ b = frame.popvalue() a = frame.popvalue() frame.pushvalue(a * b) + elif opcode == 's': + # Subtract. + a = frame.popvalue() + b = frame.popvalue() + frame.pushvalue(a - b) + elif opcode == 'd': + # Divide. + a = frame.popvalue() + b = frame.popvalue() + frame.pushvalue(a / b) else: raise NotImplementedError( "Can't handle bytecode instruction %s" % opcode) @@ -145,6 +155,20 @@ # (we still have to compile new bytecode, but too bad) return compute(code) + def descr_sub(self, space, w_other): + if isinstance(w_other, BaseArray): + return space.wrap(BinOp('s', self, w_other)) + else: + return space.wrap(BinOp('s', self, + FloatWrapper(space.float_w(w_other)))) + + def descr_div(self, space, w_other): + if isinstance(w_other, BaseArray): + return space.wrap(BinOp('d', self, w_other)) + else: + return space.wrap(BinOp('d', self, + FloatWrapper(space.float_w(w_other)))) + def descr_add(self, space, w_other): if isinstance(w_other, BaseArray): return space.wrap(BinOp('a', self, w_other)) @@ -192,7 +216,9 @@ 'Operation', force = interp2app(BaseArray.force), __add__ = interp2app(BaseArray.descr_add), + __sub__ = interp2app(BaseArray.descr_sub), __mul__ = interp2app(BaseArray.descr_mul), + __div__ = interp2app(BaseArray.descr_div), ) class SingleDimArray(BaseArray): @@ -251,6 +277,8 @@ __getitem__ = interp2app(SingleDimArray.descr_getitem), __setitem__ = interp2app(SingleDimArray.descr_setitem), __add__ = interp2app(BaseArray.descr_add), + __sub__ = interp2app(BaseArray.descr_sub), __mul__ = interp2app(BaseArray.descr_mul), + __div__ = interp2app(BaseArray.descr_div), force = interp2app(SingleDimArray.force), ) diff -r 0202bb3a6d74 pypy/module/micronumpy/test/test_numpy.py --- a/pypy/module/micronumpy/test/test_numpy.py Wed May 04 23:20:11 2011 +0200 +++ b/pypy/module/micronumpy/test/test_numpy.py Wed May 04 15:57:55 2011 -0600 @@ -27,6 +27,21 @@ for i in range(5): assert b[i] == i + i + def test_subtract(self): + from numpy import array + a = array(range(5)) + b = (a - a).force() + b = b.force() + for i in range(5): + assert b[i] == 0 + + def test_divide(self): + from numpy import array + a = array(range(1, 6)) + b = (a / a).force() + for i in range(5): + assert b[i] == 1 + def test_add_other(self): from numpy import array a = array(range(5)) @@ -36,6 +51,26 @@ for i in range(5): assert c[i] == 4 + def test_subtract_other(self): + from numpy import array + a = array(range(5)) + b = array([1, 1, 1, 1, 1]) + c = (a - b).force() + for i in range(5): + assert c[i] == (i - 1), (i, c[i]) + + d = (b - a).force() + for i in range(5): + assert d[i] == (1 - i), (i, c[i]) + + def test_divide_other(self): + from numpy import array + a = array(range(5)) + b = array([2.0, 2.0, 2.0, 2.0, 2.0]) + c = (a / b).force() + for i in range(5): + assert c[i] == (i / 2.0), (i, c[i]) + def test_add_constant(self): from numpy import array a = array(range(5)) @@ -44,6 +79,21 @@ for i in range(5): assert b[i] == i + 5 + def test_subtract_constant(self): + from numpy import array + a = array(range(5)) + b = a - 5 + b = b.force() + for i in range(5): + assert b[i] == i - 5 + + def test_divide_constant(self): + from numpy import array + a = array(range(5)) + b = (a / 5.0).force() + for i in range(5): + assert b[i] == i / 5.0 + def test_mul(self): from numpy import array a = array(range(5))