Author: Jasper Schulz <jasper.sch...@student.hpi.uni-potsdam.de> Branch: Changeset: r55864:b145fd6ade3d Date: 2012-06-27 23:27 +0200 http://bitbucket.org/pypy/pypy/changeset/b145fd6ade3d/
Log: test and fix 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,6 +163,7 @@ 'sum': 'app_numpy.sum', 'min': 'app_numpy.min', 'identity': 'app_numpy.identity', + 'eye': 'app_numpy.eye', 'max': 'app_numpy.max', 'arange': 'app_numpy.arange', } 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 @@ -16,6 +16,26 @@ a[i][i] = 1 return a +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 sum(a,axis=None, out=None): '''sum(a, axis=None) Sum of array elements over a given axis. 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 @@ -1155,6 +1155,38 @@ assert d.shape == (3, 3) assert d.dtype == dtype('int32') assert (d == [[1, 0, 0], [0, 1, 0], [0, 0, 1]]).all() + + def test_eye(self): + from _numpypy import eye, array + from _numpypy import int32, float64, 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 _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit