[pypy-commit] pypy numpy-refactor: swapaxes
fijal
noreply at buildbot.pypy.org
Thu Sep 6 16:59:19 CEST 2012
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-refactor
Changeset: r57178:a0c7dd907dfc
Date: 2012-09-05 22:24 +0200
http://bitbucket.org/pypy/pypy/changeset/a0c7dd907dfc/
Log: swapaxes
diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py b/pypy/module/micronumpy/arrayimpl/concrete.py
--- a/pypy/module/micronumpy/arrayimpl/concrete.py
+++ b/pypy/module/micronumpy/arrayimpl/concrete.py
@@ -1,7 +1,7 @@
from pypy.module.micronumpy.arrayimpl import base
from pypy.module.micronumpy import support, loop
-from pypy.module.micronumpy.base import convert_to_array
+from pypy.module.micronumpy.base import convert_to_array, W_NDimArray
from pypy.module.micronumpy.strides import calc_new_strides, shape_agreement,\
calculate_broadcast_strides, calculate_dot_strides
from pypy.module.micronumpy.iter import Chunk, Chunks, NewAxisChunk, RecordChunk
@@ -292,6 +292,16 @@
shape, skip)
return MultiDimViewIterator(self, self.start, r[0], r[1], shape)
+ def swapaxes(self, axis1, axis2):
+ shape = self.shape[:]
+ strides = self.strides[:]
+ backstrides = self.backstrides[:]
+ shape[axis1], shape[axis2] = shape[axis2], shape[axis1]
+ strides[axis1], strides[axis2] = strides[axis2], strides[axis1]
+ backstrides[axis1], backstrides[axis2] = backstrides[axis2], backstrides[axis1]
+ return W_NDimArray.new_slice(self.start, strides,
+ backstrides, shape, self)
+
class ConcreteArray(BaseConcreteArray):
def __init__(self, shape, dtype, order, strides, backstrides):
self.shape = shape
diff --git a/pypy/module/micronumpy/arrayimpl/scalar.py b/pypy/module/micronumpy/arrayimpl/scalar.py
--- a/pypy/module/micronumpy/arrayimpl/scalar.py
+++ b/pypy/module/micronumpy/arrayimpl/scalar.py
@@ -75,3 +75,6 @@
def create_axis_iter(self, shape, dim):
raise Exception("axis iter should not happen on scalar")
+
+ def swapaxes(self, axis1, axis2):
+ return self
diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -158,6 +158,20 @@
def descr_get_transpose(self, space):
return W_NDimArray(self.implementation.transpose())
+ @unwrap_spec(axis1=int, axis2=int)
+ def descr_swapaxes(self, space, axis1, axis2):
+ """a.swapaxes(axis1, axis2)
+
+ Return a view of the array with `axis1` and `axis2` interchanged.
+
+ Refer to `numpy.swapaxes` for full documentation.
+
+ See Also
+ --------
+ numpy.swapaxes : equivalent function
+ """
+ return self.implementation.swapaxes(axis1, axis2)
+
def descr_tolist(self, space):
if len(self.get_shape()) == 0:
return self.get_scalar_value().item(space)
@@ -439,6 +453,7 @@
flatten = interp2app(W_NDimArray.descr_flatten),
ravel = interp2app(W_NDimArray.descr_ravel),
repeat = interp2app(W_NDimArray.descr_repeat),
+ swapaxes = interp2app(W_NDimArray.descr_swapaxes),
)
@unwrap_spec(ndmin=int, copy=bool, subok=bool)
More information about the pypy-commit
mailing list