Author: Brian Kearns <[email protected]>
Branch:
Changeset: r61641:79a8a52dba4d
Date: 2013-02-23 01:30 -0500
http://bitbucket.org/pypy/pypy/changeset/79a8a52dba4d/
Log: move _numpypy.eye to numpypy.lib.twodim_base
diff --git a/lib_pypy/numpypy/core/numeric.py b/lib_pypy/numpypy/core/numeric.py
--- a/lib_pypy/numpypy/core/numeric.py
+++ b/lib_pypy/numpypy/core/numeric.py
@@ -530,5 +530,5 @@
[ 0., 0., 1.]])
"""
- from _numpypy import eye
+ from numpy import eye
return eye(n, dtype=dtype)
diff --git a/lib_pypy/numpypy/lib/__init__.py b/lib_pypy/numpypy/lib/__init__.py
--- a/lib_pypy/numpypy/lib/__init__.py
+++ b/lib_pypy/numpypy/lib/__init__.py
@@ -1,1 +1,2 @@
from .function_base import *
+from .twodim_base import *
diff --git a/lib_pypy/numpypy/lib/twodim_base.py
b/lib_pypy/numpypy/lib/twodim_base.py
new file mode 100644
--- /dev/null
+++ b/lib_pypy/numpypy/lib/twodim_base.py
@@ -0,0 +1,52 @@
+from _numpypy import zeros
+
+def eye(N, M=None, k=0, dtype=float):
+ """
+ Return a 2-D array with ones on the diagonal and zeros elsewhere.
+
+ Parameters
+ ----------
+ N : int
+ Number of rows in the output.
+ M : int, optional
+ Number of columns in the output. If None, defaults to `N`.
+ k : int, optional
+ Index of the diagonal: 0 (the default) refers to the main diagonal,
+ a positive value refers to an upper diagonal, and a negative value
+ to a lower diagonal.
+ dtype : data-type, optional
+ Data-type of the returned array.
+
+ Returns
+ -------
+ I : ndarray of shape (N,M)
+ An array where all elements are equal to zero, except for the `k`-th
+ diagonal, whose values are equal to one.
+
+ See Also
+ --------
+ identity : (almost) equivalent function
+ diag : diagonal 2-D array from a 1-D array specified by the user.
+
+ Examples
+ --------
+ >>> np.eye(2, dtype=int)
+ array([[1, 0],
+ [0, 1]])
+ >>> np.eye(3, k=1)
+ array([[ 0., 1., 0.],
+ [ 0., 0., 1.],
+ [ 0., 0., 0.]])
+
+ """
+ if M is None:
+ M = N
+ m = zeros((N, M), dtype=dtype)
+ if k >= M:
+ return m
+ if k >= 0:
+ i = k
+ else:
+ i = (-k) * M
+ m[:M-k].flat[i::M+1] = 1
+ return m
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
@@ -163,7 +163,6 @@
interpleveldefs[exposed] = "interp_ufuncs.get(space).%s" % impl
appleveldefs = {
- 'eye': 'app_numpy.eye',
'arange': 'app_numpy.arange',
}
def setup_after_space_initialization(self):
diff --git a/pypy/module/micronumpy/app_numpy.py
b/pypy/module/micronumpy/app_numpy.py
--- a/pypy/module/micronumpy/app_numpy.py
+++ b/pypy/module/micronumpy/app_numpy.py
@@ -2,26 +2,6 @@
import _numpypy
-def eye(n, m=None, k=0, dtype=None):
- if m is None:
- m = n
- a = _numpypy.zeros((n, m), dtype=dtype)
- ni = 0
- mi = 0
-
- if k < 0:
- p = n + k
- ni = -k
- else:
- p = n - k
- mi = k
-
- while ni < n and mi < m:
- a[ni][mi] = 1
- ni += 1
- mi += 1
- return a
-
def arange(start, stop=None, step=1, dtype=None):
'''arange([start], stop[, step], dtype=None)
Generate values in the half-interval [start, stop).
diff --git a/pypy/module/micronumpy/test/test_complex.py
b/pypy/module/micronumpy/test/test_complex.py
--- a/pypy/module/micronumpy/test/test_complex.py
+++ b/pypy/module/micronumpy/test/test_complex.py
@@ -382,7 +382,7 @@
def test_conjugate(self):
from _numpypy import conj, conjugate, complex128, complex64
- import _numpypy as np
+ import numpypy as np
c0 = complex128(complex(2.5, 0))
c1 = complex64(complex(1, 2))
diff --git a/pypy/module/micronumpy/test/test_numarray.py
b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -1188,35 +1188,6 @@
assert (array([[1,2],[3,4]]).prod(0) == [3, 8]).all()
assert (array([[1,2],[3,4]]).prod(1) == [2, 12]).all()
- def test_eye(self):
- from _numpypy import eye
- from _numpypy import int32, dtype
- a = eye(0)
- assert len(a) == 0
- assert a.dtype == dtype('float64')
- assert a.shape == (0, 0)
- b = eye(1, dtype=int32)
- assert len(b) == 1
- assert b[0][0] == 1
- assert b.shape == (1, 1)
- assert b.dtype == dtype('int32')
- c = eye(2)
- assert c.shape == (2, 2)
- assert (c == [[1, 0], [0, 1]]).all()
- d = eye(3, dtype='int32')
- assert d.shape == (3, 3)
- assert d.dtype == dtype('int32')
- assert (d == [[1, 0, 0], [0, 1, 0], [0, 0, 1]]).all()
- e = eye(3, 4)
- assert e.shape == (3, 4)
- assert (e == [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]).all()
- f = eye(2, 4, k=3)
- assert f.shape == (2, 4)
- assert (f == [[0, 0, 0, 1], [0, 0, 0, 0]]).all()
- g = eye(3, 4, k=-1)
- assert g.shape == (3, 4)
- assert (g == [[0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0]]).all()
-
def test_prod(self):
from _numpypy import array
a = array(range(1, 6))
diff --git a/pypy/module/test_lib_pypy/numpypy/lib/test_twodim_base.py
b/pypy/module/test_lib_pypy/numpypy/lib/test_twodim_base.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/numpypy/lib/test_twodim_base.py
@@ -0,0 +1,31 @@
+from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
+
+class AppTestFunctionBase(BaseNumpyAppTest):
+ def test_eye(self):
+ from _numpypy import int32, dtype
+ from numpypy import eye
+ a = eye(0)
+ assert len(a) == 0
+ assert a.dtype == dtype('float64')
+ assert a.shape == (0, 0)
+ b = eye(1, dtype=int32)
+ assert len(b) == 1
+ assert b[0][0] == 1
+ assert b.shape == (1, 1)
+ assert b.dtype == dtype('int32')
+ c = eye(2)
+ assert c.shape == (2, 2)
+ assert (c == [[1, 0], [0, 1]]).all()
+ d = eye(3, dtype='int32')
+ assert d.shape == (3, 3)
+ assert d.dtype == dtype('int32')
+ assert (d == [[1, 0, 0], [0, 1, 0], [0, 0, 1]]).all()
+ e = eye(3, 4)
+ assert e.shape == (3, 4)
+ assert (e == [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]).all()
+ f = eye(2, 4, k=3)
+ assert f.shape == (2, 4)
+ assert (f == [[0, 0, 0, 1], [0, 0, 0, 0]]).all()
+ g = eye(3, 4, k=-1)
+ assert g.shape == (3, 4)
+ assert (g == [[0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0]]).all()
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit