[pypy-commit] pypy syntax_fix: fixed compilation error due to wrong varible name

2016-05-15 Thread Sergey Matyunin
Author: Sergey Matyunin <sbmatyu...@gmail.com>
Branch: syntax_fix
Changeset: r84468:b49dacefd0c7
Date: 2016-05-15 10:01 +0200
http://bitbucket.org/pypy/pypy/changeset/b49dacefd0c7/

Log:fixed compilation error due to wrong varible name

diff --git a/pypy/module/imp/importing.py b/pypy/module/imp/importing.py
--- a/pypy/module/imp/importing.py
+++ b/pypy/module/imp/importing.py
@@ -591,7 +591,7 @@
 def load_c_extension(space, filename, modulename):
 from pypy.module.cpyext.api import load_extension_module
 log_pyverbose(space, 1, "import %s # from %s\n" %
-  (modulename, pathname))
+  (modulename, filename))
 load_extension_module(space, filename, modulename)
 # NB. cpyext.api.load_extension_module() can also delegate to _cffi_backend
 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy numpy_broadcast_nd: Fixed nested call of numpy.broadcast

2016-05-05 Thread Sergey Matyunin
Author: Sergey Matyunin <sbmatyu...@gmail.com>
Branch: numpy_broadcast_nd
Changeset: r84230:b58fe1445add
Date: 2016-05-01 18:19 +0200
http://bitbucket.org/pypy/pypy/changeset/b58fe1445add/

Log:Fixed nested call of numpy.broadcast

diff --git a/pypy/module/micronumpy/broadcast.py 
b/pypy/module/micronumpy/broadcast.py
--- a/pypy/module/micronumpy/broadcast.py
+++ b/pypy/module/micronumpy/broadcast.py
@@ -37,11 +37,22 @@
 except OverflowError as e:
 raise oefmt(space.w_ValueError, "broadcast dimensions too large.")
 
-self.list_iter_state = [W_FlatIterator(arr, self.shape, 
arr.get_order() != self.order)
-for arr in self.seq]
+self.list_iter_state = self._prepare_iterators()
 
 self.done = False
 
+def _prepare_iterators(self):
+res = []
+for arr in self.seq:
+if isinstance(arr, W_Broadcast):
+res.extend([self._create_iterator(it.base) for it in 
arr.list_iter_state])
+else:
+res.append(self._create_iterator(arr))
+return res
+
+def _create_iterator(self, arr):
+return W_FlatIterator(arr, self.shape, arr.get_order() != self.order)
+
 def get_shape(self):
 return self.shape
 
@@ -49,10 +60,17 @@
 return self.order
 
 def get_dtype(self):
-return self.seq[0].get_dtype() #XXX Fixme
+return self.seq[0].get_dtype()  # XXX Fixme
 
 def get_size(self):
-return 0  #XXX Fixme
+return self.size
+
+def is_scalar(self):
+return self.ndims() == 0
+
+def ndims(self):
+return len(self.get_shape())
+ndims._always_inline_ = True
 
 def create_iter(self, shape=None, backward_broadcast=False):
 return self, self.list_iter_state # XXX Fixme
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy numpy_broadcast_nd: W_Broadcast (micronumpy) is rewritten using W_FlatIterator for implementation of iters attribute. W_FlatIterator gets optional arguments in constructor.

2016-04-30 Thread Sergey Matyunin
Author: Sergey Matyunin <sbmatyu...@gmail.com>
Branch: numpy_broadcast_nd
Changeset: r84065:c0d40603d40b
Date: 2016-04-24 13:36 +0200
http://bitbucket.org/pypy/pypy/changeset/c0d40603d40b/

Log:W_Broadcast (micronumpy) is rewritten using W_FlatIterator for
implementation of iters attribute. W_FlatIterator gets optional
arguments in constructor.

diff --git a/pypy/module/micronumpy/broadcast.py 
b/pypy/module/micronumpy/broadcast.py
--- a/pypy/module/micronumpy/broadcast.py
+++ b/pypy/module/micronumpy/broadcast.py
@@ -1,12 +1,12 @@
 import pypy.module.micronumpy.constants as NPY
-from nditer import ConcreteIter, parse_op_flag, parse_op_arg
 from pypy.interpreter.error import OperationError, oefmt
 from pypy.interpreter.gateway import interp2app
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
 from pypy.module.micronumpy import support
-from pypy.module.micronumpy.base import W_NDimArray, convert_to_array, 
W_NumpyObject
+from pypy.module.micronumpy.base import convert_to_array, W_NumpyObject
+from pypy.module.micronumpy.flatiter import W_FlatIterator
 from rpython.rlib import jit
-from strides import calculate_broadcast_strides, shape_agreement_multiple
+from strides import shape_agreement_multiple
 
 def descr_new_broadcast(space, w_subtype, __args__):
 return W_Broadcast(space, __args__.arguments_w)
@@ -26,45 +26,21 @@
 self.seq = [convert_to_array(space, w_elem)
 for w_elem in args]
 
-self.op_flags = parse_op_arg(space, 'op_flags', space.w_None,
- len(self.seq), parse_op_flag)
-
 self.shape = shape_agreement_multiple(space, self.seq, shape=None)
 self.order = NPY.CORDER
 
-self.iters = []
+self.list_iter_state = []
 self.index = 0
 
 try:
 self.size = support.product_check(self.shape)
 except OverflowError as e:
 raise oefmt(space.w_ValueError, "broadcast dimensions too large.")
-for i in range(len(self.seq)):
-it = self.get_iter(space, i)
-it.contiguous = False
-self.iters.append((it, it.reset()))
+
+self.list_iter_state = [W_FlatIterator(arr, self.shape, 
arr.get_order() != self.order)
+for arr in self.seq]
 
 self.done = False
-pass
-
-def get_iter(self, space, i):
-arr = self.seq[i]
-imp = arr.implementation
-if arr.is_scalar():
-return ConcreteIter(imp, 1, [], [], [], self.op_flags[i], self)
-shape = self.shape
-
-backward = imp.order != self.order
-
-r = calculate_broadcast_strides(imp.strides, imp.backstrides, 
imp.shape,
-shape, backward)
-
-iter_shape = shape
-if len(shape) != len(r[0]):
-# shape can be shorter when using an external loop, just return a 
view
-iter_shape = imp.shape
-return ConcreteIter(imp, imp.get_size(), iter_shape, r[0], r[1],
-self.op_flags[i], self)
 
 def descr_iter(self, space):
 return space.wrap(self)
@@ -79,28 +55,26 @@
 return space.wrap(self.index)
 
 def descr_get_numiter(self, space):
-return space.wrap(len(self.iters))
+return space.wrap(len(self.list_iter_state))
 
 def descr_get_number_of_dimensions(self, space):
 return space.wrap(len(self.shape))
 
+def descr_get_iters(self, space):
+return space.newtuple(self.list_iter_state)
+
 @jit.unroll_safe
 def descr_next(self, space):
 if self.index >= self.size:
 self.done = True
 raise OperationError(space.w_StopIteration, space.w_None)
 self.index += 1
-res = []
-for i, (it, st) in enumerate(self.iters):
-res.append(self._get_item(it, st))
-self.iters[i] = (it, it.next(st))
+res = [it.descr_next(space) for it in self.list_iter_state]
+
 if len(res) < 2:
 return res[0]
 return space.newtuple(res)
 
-def _get_item(self, it, st):
-return W_NDimArray(it.getoperand(st))
-
 
 W_Broadcast.typedef = TypeDef("numpy.broadcast",
   __new__=interp2app(descr_new_broadcast),
@@ -111,4 +85,5 @@
   
index=GetSetProperty(W_Broadcast.descr_get_index),
   
numiter=GetSetProperty(W_Broadcast.descr_get_numiter),
   
nd=GetSetProperty(W_Broadcast.descr_get_number_of_dimensions),
+  
iters=GetSetProperty(W_Broadcast.descr_get_iters),
   )
diff --git a/pypy/module/micronumpy/flatiter.py 
b/pypy/module/micronumpy/flatiter.py
--- a/pypy/module/micronumpy/flatiter.py
+++ b/pypy/module/micronumpy/flatiter.py
@@ -33,9 +33,9 @@
 
 
 class W_FlatIterator(W_NDimArray):
-def __init__(se

[pypy-commit] pypy numpy_broadcast_nd: fixed compilation error for W_Broadcast

2016-04-30 Thread Sergey Matyunin
Author: Sergey Matyunin <sbmatyu...@gmail.com>
Branch: numpy_broadcast_nd
Changeset: r84067:23fae214f255
Date: 2016-04-24 14:28 +0200
http://bitbucket.org/pypy/pypy/changeset/23fae214f255/

Log:fixed compilation error for W_Broadcast

diff --git a/pypy/module/micronumpy/broadcast.py 
b/pypy/module/micronumpy/broadcast.py
--- a/pypy/module/micronumpy/broadcast.py
+++ b/pypy/module/micronumpy/broadcast.py
@@ -69,7 +69,7 @@
 self.done = True
 raise OperationError(space.w_StopIteration, space.w_None)
 self.index += 1
-res = [it.descr_next(space) for it in self.list_iter_state]
+res = [space.call_method(it, 'next') for it in self.list_iter_state]
 
 if len(res) < 2:
 return res[0]
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy numpy_broadcast_nd: In W_Broadcast (micronumpy) implemented nd attribute

2016-04-30 Thread Sergey Matyunin
Author: Sergey Matyunin <sbmatyu...@gmail.com>
Branch: numpy_broadcast_nd
Changeset: r84064:2bcbec2ef549
Date: 2016-04-24 11:46 +0200
http://bitbucket.org/pypy/pypy/changeset/2bcbec2ef549/

Log:In W_Broadcast (micronumpy) implemented nd attribute

diff --git a/pypy/module/micronumpy/broadcast.py 
b/pypy/module/micronumpy/broadcast.py
--- a/pypy/module/micronumpy/broadcast.py
+++ b/pypy/module/micronumpy/broadcast.py
@@ -81,6 +81,9 @@
 def descr_get_numiter(self, space):
 return space.wrap(len(self.iters))
 
+def descr_get_number_of_dimensions(self, space):
+return space.wrap(len(self.shape))
+
 @jit.unroll_safe
 def descr_next(self, space):
 if self.index >= self.size:
@@ -107,4 +110,5 @@
   size=GetSetProperty(W_Broadcast.descr_get_size),
   
index=GetSetProperty(W_Broadcast.descr_get_index),
   
numiter=GetSetProperty(W_Broadcast.descr_get_numiter),
+  
nd=GetSetProperty(W_Broadcast.descr_get_number_of_dimensions),
   )
diff --git a/pypy/module/micronumpy/test/test_broadcast.py 
b/pypy/module/micronumpy/test/test_broadcast.py
--- a/pypy/module/micronumpy/test/test_broadcast.py
+++ b/pypy/module/micronumpy/test/test_broadcast.py
@@ -57,7 +57,6 @@
 
 def test_broadcast_failures(self):
 import numpy as np
-import sys
 x = np.array([1, 2, 3])
 y = np.array([4, 5])
 raises(ValueError, np.broadcast, x, y)
@@ -95,3 +94,11 @@
 else:
 mit = np.broadcast(*arrs)
 assert mit.numiter == j
+
+def test_broadcast_nd(self):
+import numpy as np
+arg1, arg2 = np.empty((6, 7)), np.empty((5, 6, 1))
+b = np.broadcast(arg1, arg2)
+
+assert hasattr(b, 'nd')
+assert b.nd == 3
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy numpy_broadcast_nd: Implemented reset for numpy broadcast object.

2016-04-30 Thread Sergey Matyunin
Author: Sergey Matyunin <sbmatyu...@gmail.com>
Branch: numpy_broadcast_nd
Changeset: r84066:d52b849b3779
Date: 2016-04-24 13:54 +0200
http://bitbucket.org/pypy/pypy/changeset/d52b849b3779/

Log:Implemented reset for numpy broadcast object.

diff --git a/pypy/module/micronumpy/broadcast.py 
b/pypy/module/micronumpy/broadcast.py
--- a/pypy/module/micronumpy/broadcast.py
+++ b/pypy/module/micronumpy/broadcast.py
@@ -75,6 +75,11 @@
 return res[0]
 return space.newtuple(res)
 
+def descr_reset(self, space):
+self.index = 0
+self.done = False
+for it in self.list_iter_state:
+it.reset()
 
 W_Broadcast.typedef = TypeDef("numpy.broadcast",
   __new__=interp2app(descr_new_broadcast),
@@ -86,4 +91,5 @@
   
numiter=GetSetProperty(W_Broadcast.descr_get_numiter),
   
nd=GetSetProperty(W_Broadcast.descr_get_number_of_dimensions),
   
iters=GetSetProperty(W_Broadcast.descr_get_iters),
+  reset=interp2app(W_Broadcast.descr_reset),
   )
diff --git a/pypy/module/micronumpy/flatiter.py 
b/pypy/module/micronumpy/flatiter.py
--- a/pypy/module/micronumpy/flatiter.py
+++ b/pypy/module/micronumpy/flatiter.py
@@ -76,7 +76,7 @@
  base.get_order(), w_instance=base)
 return loop.flatiter_getitem(res, self.iter, state, step)
 finally:
-self.iter.reset(self.state, mutate=True)
+self.reset()
 
 def descr_setitem(self, space, w_idx, w_value):
 if not (space.isinstance_w(w_idx, space.w_int) or
@@ -96,11 +96,14 @@
 arr = convert_to_array(space, w_value)
 loop.flatiter_setitem(space, dtype, arr, self.iter, state, step, 
length)
 finally:
-self.iter.reset(self.state, mutate=True)
+self.reset()
 
 def descr___array_wrap__(self, space, obj, w_context=None):
 return obj
 
+def reset(self):
+self.iter.reset(self.state, mutate=True)
+
 W_FlatIterator.typedef = TypeDef("numpy.flatiter",
 base = GetSetProperty(W_FlatIterator.descr_base),
 index = GetSetProperty(W_FlatIterator.descr_index),
diff --git a/pypy/module/micronumpy/test/test_broadcast.py 
b/pypy/module/micronumpy/test/test_broadcast.py
--- a/pypy/module/micronumpy/test/test_broadcast.py
+++ b/pypy/module/micronumpy/test/test_broadcast.py
@@ -123,3 +123,15 @@
 assert step_in_y == y[0, 0]  # == 3
 assert step_in_broadcast == (1, 3)
 assert step2_in_y == y[1, 0]  # == 4
+
+def test_broadcast_reset(self):
+import numpy as np
+x = np.array([1, 2, 3])
+y = np.array([[4], [5], [6]])
+
+b = np.broadcast(x, y)
+b.next(), b.next(), b.next()
+b.reset()
+
+assert b.index == 0
+assert b.next() == (1, 4)
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy numpy_broadcast: In W_Broadcast (micronumpy) added check for number of arguments, added numiter property

2016-04-17 Thread Sergey Matyunin
Author: Sergey Matyunin <sbmatyu...@gmail.com>
Branch: numpy_broadcast
Changeset: r83711:7f688202899a
Date: 2016-04-08 13:07 +0200
http://bitbucket.org/pypy/pypy/changeset/7f688202899a/

Log:In W_Broadcast (micronumpy) added check for number of arguments,
added numiter property

diff --git a/pypy/module/micronumpy/broadcast.py 
b/pypy/module/micronumpy/broadcast.py
--- a/pypy/module/micronumpy/broadcast.py
+++ b/pypy/module/micronumpy/broadcast.py
@@ -19,9 +19,14 @@
 def descr_new_broadcast(space, w_subtype, __args__):
 return W_Broadcast(space, __args__.arguments_w)
 
-def __init__(self, space, w_args):
+def __init__(self, space, args):
+num_args = len(args)
+if not (2 <= num_args <= NPY.MAXARGS):
+raise OperationError(space.w_ValueError,
+ space.wrap("Need at least two and fewer than 
(%d) array objects." % NPY.MAXARGS))
+
 self.seq = [convert_to_array(space, w_elem)
-for w_elem in w_args]
+for w_elem in args]
 
 self.op_flags = parse_op_arg(space, 'op_flags', space.w_None,
  len(self.seq), parse_op_flag)
@@ -72,6 +77,9 @@
 def descr_get_index(self, space):
 return space.wrap(self.index)
 
+def descr_get_numiter(self, space):
+return space.wrap(len(self.iters))
+
 @jit.unroll_safe
 def descr_next(self, space):
 if self.index >= self.size:
@@ -97,4 +105,5 @@
   
shape=GetSetProperty(W_Broadcast.descr_get_shape),
   size=GetSetProperty(W_Broadcast.descr_get_size),
   
index=GetSetProperty(W_Broadcast.descr_get_index),
+  
numiter=GetSetProperty(W_Broadcast.descr_get_numiter),
   )
diff --git a/pypy/module/micronumpy/constants.py 
b/pypy/module/micronumpy/constants.py
--- a/pypy/module/micronumpy/constants.py
+++ b/pypy/module/micronumpy/constants.py
@@ -77,6 +77,8 @@
 WRAP = 1
 RAISE = 2
 
+MAXARGS = 32
+
 # These can be requested in constructor functions and tested for
 ARRAY_C_CONTIGUOUS = 0x0001
 ARRAY_F_CONTIGUOUS = 0x0002
diff --git a/pypy/module/micronumpy/test/test_broadcast.py 
b/pypy/module/micronumpy/test/test_broadcast.py
--- a/pypy/module/micronumpy/test/test_broadcast.py
+++ b/pypy/module/micronumpy/test/test_broadcast.py
@@ -73,3 +73,16 @@
  (2, 40, 700), (2, 40, 800), (2, 50, 700), (2, 50, 800),
  (3, 40, 700), (3, 40, 800), (3, 50, 700), (3, 50, 800)]
 
+def test_number_of_arguments(self):
+"""
+Test from numpy unit tests.
+"""
+import numpy as np
+arr = np.empty((5,))
+for j in range(35):
+arrs = [arr] * j
+if j < 2 or j > 32:
+raises(ValueError, np.broadcast, *arrs)
+else:
+mit = np.broadcast(*arrs)
+assert mit.numiter == j
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy numpy_broadcast: Implemented W_Broadcast for numpy.broadcast

2016-04-17 Thread Sergey Matyunin
Author: Sergey Matyunin <sbmatyu...@gmail.com>
Branch: numpy_broadcast
Changeset: r83709:bbb4848772d2
Date: 2016-04-08 10:34 +0200
http://bitbucket.org/pypy/pypy/changeset/bbb4848772d2/

Log:Implemented W_Broadcast for numpy.broadcast

diff --git a/pypy/module/micronumpy/__init__.py 
b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -32,6 +32,7 @@
 'set_string_function': 'appbridge.set_string_function',
 'typeinfo': 'descriptor.get_dtype_cache(space).w_typeinfo',
 'nditer': 'nditer.W_NDIter',
+'broadcast': 'broadcast.W_Broadcast',
 
 'set_docstring': 'support.descr_set_docstring',
 'VisibleDeprecationWarning': 'support.W_VisibleDeprecationWarning',
diff --git a/pypy/module/micronumpy/broadcast.py 
b/pypy/module/micronumpy/broadcast.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/micronumpy/broadcast.py
@@ -0,0 +1,100 @@
+import operator
+
+import pypy.module.micronumpy.constants as NPY
+from nditer import ConcreteIter, parse_op_flag, parse_op_arg
+from pypy.interpreter.error import OperationError
+from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.typedef import TypeDef, GetSetProperty
+from pypy.module.micronumpy.base import W_NDimArray, convert_to_array, 
W_NumpyObject
+from rpython.rlib import jit
+from strides import calculate_broadcast_strides, shape_agreement_multiple
+
+
+class W_Broadcast(W_NumpyObject):
+"""
+Implementation of numpy.broadcast.
+This class is a simplified version of nditer.W_NDIter with fixed iteration 
for broadcasted arrays.
+"""
+
+@staticmethod
+def descr_new_broadcast(space, w_subtype, __args__):
+return W_Broadcast(space, __args__.arguments_w)
+
+def __init__(self, space, w_args):
+self.seq = [convert_to_array(space, w_elem)
+for w_elem in w_args]
+
+self.op_flags = parse_op_arg(space, 'op_flags', space.w_None,
+ len(self.seq), parse_op_flag)
+
+self.shape = tuple(shape_agreement_multiple(space, self.seq, 
shape=None))
+self.order = NPY.CORDER
+
+self.iters = []
+self.index = 0
+self.size = reduce(operator.mul, self.shape, 1)
+for i in range(len(self.seq)):
+it = self.get_iter(space, i)
+it.contiguous = False
+self.iters.append((it, it.reset()))
+
+self.done = False
+pass
+
+def get_iter(self, space, i):
+arr = self.seq[i]
+imp = arr.implementation
+if arr.is_scalar():
+return ConcreteIter(imp, 1, [], [], [], self.op_flags[i], self)
+shape = self.shape
+
+backward = imp.order != self.order
+
+r = calculate_broadcast_strides(imp.strides, imp.backstrides, 
imp.shape,
+shape, backward)
+
+iter_shape = shape
+if len(shape) != len(r[0]):
+# shape can be shorter when using an external loop, just return a 
view
+iter_shape = imp.shape
+return ConcreteIter(imp, imp.get_size(), iter_shape, r[0], r[1],
+self.op_flags[i], self)
+
+def descr_iter(self, space):
+return space.wrap(self)
+
+def descr_get_shape(self, space):
+return space.wrap(self.shape)
+
+def descr_get_size(self, space):
+return space.wrap(self.size)
+
+def descr_get_index(self, space):
+return space.wrap(self.index)
+
+@jit.unroll_safe
+def descr_next(self, space):
+if self.index >= self.size:
+self.done = True
+raise OperationError(space.w_StopIteration, space.w_None)
+self.index += 1
+res = []
+for i, (it, st) in enumerate(self.iters):
+res.append(self._get_item(it, st))
+self.iters[i] = (it, it.next(st))
+if len(res) < 2:
+return res[0]
+return space.newtuple(res)
+
+def _get_item(self, it, st):
+return W_NDimArray(it.getoperand(st))
+
+
+W_Broadcast.typedef = TypeDef("numpy.broadcast",
+  
__new__=interp2app(W_Broadcast.descr_new_broadcast),
+  __iter__=interp2app(W_Broadcast.descr_iter),
+  next=interp2app(W_Broadcast.descr_next),
+  
shape=GetSetProperty(W_Broadcast.descr_get_shape),
+  size=GetSetProperty(W_Broadcast.descr_get_size),
+  
index=GetSetProperty(W_Broadcast.descr_get_index),
+  )
diff --git a/pypy/module/micronumpy/test/test_broadcast.py 
b/pypy/module/micronumpy/test/test_broadcast.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/micronumpy/test/test_broadcast.py
@@ -0,0 +1,75 @@
+# -*- encoding: utf-8 -*-
+
+from pypy.module.micronum

[pypy-commit] pypy numpy_broadcast: Fixed compiling of W_Broadcast

2016-04-17 Thread Sergey Matyunin
Author: Sergey Matyunin <sbmatyu...@gmail.com>
Branch: numpy_broadcast
Changeset: r83710:7680be59c5aa
Date: 2016-04-08 11:38 +0200
http://bitbucket.org/pypy/pypy/changeset/7680be59c5aa/

Log:Fixed compiling of W_Broadcast
-fixed tuple conversion
-removed non-rpython reduce() call

diff --git a/pypy/module/micronumpy/broadcast.py 
b/pypy/module/micronumpy/broadcast.py
--- a/pypy/module/micronumpy/broadcast.py
+++ b/pypy/module/micronumpy/broadcast.py
@@ -1,10 +1,9 @@
-import operator
-
 import pypy.module.micronumpy.constants as NPY
 from nditer import ConcreteIter, parse_op_flag, parse_op_arg
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.gateway import interp2app
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
+from pypy.module.micronumpy import support
 from pypy.module.micronumpy.base import W_NDimArray, convert_to_array, 
W_NumpyObject
 from rpython.rlib import jit
 from strides import calculate_broadcast_strides, shape_agreement_multiple
@@ -27,12 +26,13 @@
 self.op_flags = parse_op_arg(space, 'op_flags', space.w_None,
  len(self.seq), parse_op_flag)
 
-self.shape = tuple(shape_agreement_multiple(space, self.seq, 
shape=None))
+self.shape = shape_agreement_multiple(space, self.seq, shape=None)
 self.order = NPY.CORDER
 
 self.iters = []
 self.index = 0
-self.size = reduce(operator.mul, self.shape, 1)
+
+self.size = support.product(self.shape)
 for i in range(len(self.seq)):
 it = self.get_iter(space, i)
 it.contiguous = False
@@ -64,7 +64,7 @@
 return space.wrap(self)
 
 def descr_get_shape(self, space):
-return space.wrap(self.shape)
+return space.newtuple([space.wrap(i) for i in self.shape])
 
 def descr_get_size(self, space):
 return space.wrap(self.size)
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy fix_transpose_for_list_v3: Fixed ndarray.transpose when argument is a list or an array

2016-03-22 Thread Sergey Matyunin
Author: Sergey Matyunin <sbmatyu...@gmail.com>
Branch: fix_transpose_for_list_v3
Changeset: r83277:ef93194a1339
Date: 2016-03-22 18:08 +0100
http://bitbucket.org/pypy/pypy/changeset/ef93194a1339/

Log:Fixed ndarray.transpose when argument is a list or an array

diff --git a/pypy/module/micronumpy/ndarray.py 
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -502,29 +502,34 @@
 return W_NDimArray(self.implementation.transpose(self, axes))
 
 def descr_transpose(self, space, args_w):
-if len(args_w) == 1 and space.isinstance_w(args_w[0], space.w_tuple):
-args_w = space.fixedview(args_w[0])
-if (len(args_w) == 0 or
-len(args_w) == 1 and space.is_none(args_w[0])):
+if len(args_w) == 0 or len(args_w) == 1 and space.is_none(args_w[0]):
 return self.descr_get_transpose(space)
 else:
-if len(args_w) != self.ndims():
-raise oefmt(space.w_ValueError, "axes don't match array")
-axes = []
-axes_seen = [False] * self.ndims()
-for w_arg in args_w:
-try:
-axis = support.index_w(space, w_arg)
-except OperationError:
-raise oefmt(space.w_TypeError, "an integer is required")
-if axis < 0 or axis >= self.ndims():
-raise oefmt(space.w_ValueError, "invalid axis for this 
array")
-if axes_seen[axis] is True:
-raise oefmt(space.w_ValueError, "repeated axis in 
transpose")
-axes.append(axis)
-axes_seen[axis] = True
-return self.descr_get_transpose(space, axes)
+if len(args_w) > 1:
+axes = args_w
+else:  # Iterable in the only argument (len(arg_w) == 1 and 
arg_w[0] is not None)
+axes = space.fixedview(args_w[0])
 
+axes = self._checked_axes(axes, space)
+return self.descr_get_transpose(space, axes)
+
+def _checked_axes(self, axes_raw, space):
+if len(axes_raw) != self.ndims():
+raise oefmt(space.w_ValueError, "axes don't match array")
+axes = []
+axes_seen = [False] * self.ndims()
+for elem in axes_raw:
+try:
+axis = support.index_w(space, elem)
+except OperationError:
+raise oefmt(space.w_TypeError, "an integer is required")
+if axis < 0 or axis >= self.ndims():
+raise oefmt(space.w_ValueError, "invalid axis for this array")
+if axes_seen[axis] is True:
+raise oefmt(space.w_ValueError, "repeated axis in transpose")
+axes.append(axis)
+axes_seen[axis] = True
+return axes
 
 @unwrap_spec(axis1=int, axis2=int)
 def descr_swapaxes(self, space, axis1, axis2):
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -2960,6 +2960,36 @@
 assert (a.transpose() == b).all()
 assert (a.transpose(None) == b).all()
 
+def test_transpose_arg_tuple(self):
+import numpy as np
+a = np.arange(24).reshape(2, 3, 4)
+transpose_args = a.transpose(1, 2, 0)
+
+transpose_test = a.transpose((1, 2, 0))
+
+assert transpose_test.shape == (3, 4, 2)
+assert (transpose_args == transpose_test).all()
+
+def test_transpose_arg_list(self):
+import numpy as np
+a = np.arange(24).reshape(2, 3, 4)
+transpose_args = a.transpose(1, 2, 0)
+
+transpose_test = a.transpose([1, 2, 0])
+
+assert transpose_test.shape == (3, 4, 2)
+assert (transpose_args == transpose_test).all()
+
+def test_transpose_arg_array(self):
+import numpy as np
+a = np.arange(24).reshape(2, 3, 4)
+transpose_args = a.transpose(1, 2, 0)
+
+transpose_test = a.transpose(np.array([1, 2, 0]))
+
+assert transpose_test.shape == (3, 4, 2)
+assert (transpose_args == transpose_test).all()
+
 def test_transpose_error(self):
 import numpy as np
 a = np.arange(24).reshape(2, 3, 4)
@@ -2968,6 +2998,11 @@
 raises(ValueError, a.transpose, 1, 0, 1)
 raises(TypeError, a.transpose, 1, 0, '2')
 
+def test_transpose_unexpected_argument(self):
+import numpy as np
+a = np.array([[1, 2], [3, 4], [5, 6]])
+raises(TypeError, 'a.transpose(axes=(1,2,0))')
+
 def test_flatiter(self):
 from numpy import array, flatiter, arange, zeros
 a = array([[10, 30], [40, 60]])
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy fix_indexing_by_numpy_int: Fixed indexing by numpy.int

2016-03-14 Thread Sergey Matyunin
Author: Sergey Matyunin <sbmatyu...@gmail.com>
Branch: fix_indexing_by_numpy_int
Changeset: r83046:889015cbbfed
Date: 2016-03-13 22:33 +0100
http://bitbucket.org/pypy/pypy/changeset/889015cbbfed/

Log:Fixed indexing by numpy.int

diff --git a/pypy/module/micronumpy/ndarray.py 
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -267,6 +267,11 @@
 "interpreted as a valid boolean index")
 elif isinstance(w_idx, boxes.W_GenericBox):
 w_ret = self.getitem_array_int(space, w_idx)
+
+if isinstance(w_idx, boxes.W_IntegerBox):
+# if w_idx is integer then getitem_array_int must contain a 
single value and we must return it.
+# Get 0-th element of the w_ret.
+w_ret = w_ret.implementation.descr_getitem(space, self, 
space.wrap(0))
 else:
 try:
 w_ret = self.implementation.descr_getitem(space, self, w_idx)
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -3397,6 +3397,22 @@
 a.itemset(1, 2, 100)
 assert a[1, 2] == 100
 
+def test_index_int64(self):
+import numpy as np
+res = np.array([10, 20, 30])[np.int64(1)]
+
+assert isinstance(res, np.int64)
+assert not isinstance(res, np.ndarray)
+assert res == 20
+
+def test_index_int32(self):
+import numpy as np
+res = np.array([10, 20, 30])[np.int32(0)]
+
+assert isinstance(res, np.int64)
+assert not isinstance(res, np.ndarray)
+assert res == 10
+
 def test_index(self):
 import numpy as np
 a = np.array([1], np.uint16)
@@ -3408,6 +3424,7 @@
 assert exc.value.message == 'only integer arrays with one element 
' \
 'can be converted to an index'
 
+
 def test_int_array_index(self):
 from numpy import array
 assert (array([])[[]] == []).all()
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy where_1_arg: Implemented where for 1 argument. Added tests.

2016-03-13 Thread Sergey Matyunin
Author: Sergey Matyunin <sbmatyu...@gmail.com>
Branch: where_1_arg
Changeset: r83020:35aba6438e37
Date: 2016-03-13 19:43 +0100
http://bitbucket.org/pypy/pypy/changeset/35aba6438e37/

Log:Implemented where for 1 argument. Added tests.

diff --git a/pypy/module/micronumpy/arrayops.py 
b/pypy/module/micronumpy/arrayops.py
--- a/pypy/module/micronumpy/arrayops.py
+++ b/pypy/module/micronumpy/arrayops.py
@@ -71,8 +71,8 @@
 """
 if space.is_none(w_y):
 if space.is_none(w_x):
-raise OperationError(space.w_NotImplementedError, space.wrap(
-"1-arg where unsupported right now"))
+arr = convert_to_array(space, w_arr)
+return arr.descr_nonzero(space)
 raise OperationError(space.w_ValueError, space.wrap(
 "Where should be called with either 1 or 3 arguments"))
 if space.is_none(w_x):
diff --git a/pypy/module/micronumpy/test/test_arrayops.py 
b/pypy/module/micronumpy/test/test_arrayops.py
--- a/pypy/module/micronumpy/test/test_arrayops.py
+++ b/pypy/module/micronumpy/test/test_arrayops.py
@@ -54,8 +54,24 @@
 assert (where(False, 1, [1, 2, 3]) == [1, 2, 3]).all()
 assert (where([1, 2, 3], True, False) == [True, True, True]).all()
 
-#def test_where_1_arg(self):
-#xxx
+def test_where_1_arg(self):
+from numpy import where, array
+
+result = where([1,0,1])
+
+assert isinstance(result, tuple)
+assert len(result) == 1
+assert (result[0] == array([0, 2])).all()
+
+def test_where_1_arg_2d(self):
+from numpy import where, array
+
+result = where([[1,0,1],[2,-1,-1]])
+
+assert isinstance(result, tuple)
+assert len(result) == 2
+assert (result[0] == array([0, 0, 1, 1, 1])).all()
+assert (result[1] == array([0, 2, 0, 1, 2])).all()
 
 def test_where_invalidates(self):
 from numpy import where, ones, zeros, array
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy numpy_partition: Added tests for ndarray.tolist for arrays containing objects

2016-03-05 Thread Sergey Matyunin
Author: Sergey Matyunin <sbmatyu...@gmail.com>
Branch: numpy_partition
Changeset: r82800:f70b91a44559
Date: 2016-03-02 22:57 +0100
http://bitbucket.org/pypy/pypy/changeset/f70b91a44559/

Log:Added tests for ndarray.tolist for arrays containing objects

diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -1936,6 +1936,22 @@
 a = array([[1, 2], [3, 4]])
 assert (a + a).tolist() == [[2, 4], [6, 8]]
 
+def test_tolist_object(self):
+from numpy import array
+a = array([0], dtype=object)
+assert a.tolist() == [0]
+
+def test_tolist_object_slice(self):
+from numpy import array
+list_expected = [slice(0, 1), 0]
+a = array(list_expected, dtype=object)
+assert a.tolist() == list_expected
+
+def test_tolist_object_slice_2d(self):
+from numpy import array
+a = array([(slice(0, 1), 1), (0, 1)], dtype=object)
+assert a.tolist() == [[slice(0, 1, None), 1], [0, 1]]
+
 def test_tolist_slice(self):
 from numpy import array
 a = array([[17.1, 27.2], [40.3, 50.3]])
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy numpy_partition: Fixed tolist for ndarrays containing objects

2016-03-05 Thread Sergey Matyunin
Author: Sergey Matyunin <sbmatyu...@gmail.com>
Branch: numpy_partition
Changeset: r82799:d4fa44a7c61f
Date: 2016-02-29 22:11 +0100
http://bitbucket.org/pypy/pypy/changeset/d4fa44a7c61f/

Log:Fixed tolist for ndarrays containing objects

diff --git a/pypy/module/micronumpy/ndarray.py 
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -534,8 +534,11 @@
 return self.get_scalar_value().item(space)
 l_w = []
 for i in range(self.get_shape()[0]):
-l_w.append(space.call_method(self.descr_getitem(space,
- space.wrap(i)), "tolist"))
+item_w = self.descr_getitem(space, space.wrap(i))
+if isinstance(item_w, W_NDimArray):
+l_w.append(space.call_method(item_w, "tolist"))
+else:
+l_w.append(item_w)
 return space.newlist(l_w)
 
 def descr_ravel(self, space, w_order=None):
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy numpy_partition: Added function for parition call from micronumpy

2016-03-05 Thread Sergey Matyunin
Author: Sergey Matyunin <sbmatyu...@gmail.com>
Branch: numpy_partition
Changeset: r82798:b0b79d1b0927
Date: 2016-02-29 22:06 +0100
http://bitbucket.org/pypy/pypy/changeset/b0b79d1b0927/

Log:Added function for parition call from micronumpy

diff --git a/pypy/module/micronumpy/appbridge.py 
b/pypy/module/micronumpy/appbridge.py
--- a/pypy/module/micronumpy/appbridge.py
+++ b/pypy/module/micronumpy/appbridge.py
@@ -9,6 +9,7 @@
 w_array_repr = None
 w_array_str = None
 w__usefields = None
+w_partition = None
 
 def __init__(self, space):
 pass
diff --git a/pypy/module/micronumpy/ndarray.py 
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -911,6 +911,10 @@
 return
 return self.implementation.sort(space, w_axis, w_order)
 
+def descr_partition(self, space, __args__):
+return get_appbridge_cache(space).call_method(
+space, 'numpy.core._partition_use', 'partition', 
__args__.prepend(self))
+
 def descr_squeeze(self, space, w_axis=None):
 cur_shape = self.get_shape()
 if not space.is_none(w_axis):
@@ -1635,6 +1639,7 @@
 
 argsort  = interp2app(W_NDimArray.descr_argsort),
 sort  = interp2app(W_NDimArray.descr_sort),
+partition  = interp2app(W_NDimArray.descr_partition),
 astype   = interp2app(W_NDimArray.descr_astype),
 base = GetSetProperty(W_NDimArray.descr_get_base),
 byteswap = interp2app(W_NDimArray.descr_byteswap),
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit