Author: Alex Gaynor <alex.gay...@gmail.com>
Branch: 
Changeset: r44982:0ab53e4999f9
Date: 2011-06-16 19:16 -0700
http://bitbucket.org/pypy/pypy/changeset/0ab53e4999f9/

Log:    Added numpy.ones

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
@@ -9,6 +9,7 @@
         'array': 'interp_numarray.SingleDimArray',
         'zeros': 'interp_numarray.zeros',
         'empty': 'interp_numarray.zeros',
+        'ones': 'interp_numarray.ones',
 
         # ufuncs
         'abs': 'interp_ufuncs.absolute',
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
@@ -197,6 +197,7 @@
     Intermediate class for performing binary operations.
     """
     _immutable_fields_ = ["function", "left", "right"]
+
     def __init__(self, function, left, right, signature):
         VirtualArray.__init__(self, signature)
         self.function = function
@@ -220,9 +221,11 @@
 
 class ViewArray(BaseArray):
     """
-    Class for representing views of arrays, they will reflect changes of 
parrent arrays. Example: slices
+    Class for representing views of arrays, they will reflect changes of parent
+    arrays. Example: slices
     """
     _immutable_fields_ = ["parent"]
+
     def __init__(self, parent, signature):
         BaseArray.__init__(self)
         self.signature = signature
@@ -230,7 +233,10 @@
         self.invalidates = parent.invalidates
 
     def get_concrete(self):
-        return self # in fact, ViewArray never gets "concrete" as it never 
stores data. This implementation is needed for BaseArray getitem/setitem to 
work, can be refactored.
+        # in fact, ViewArray never gets "concrete" as it never stores data.
+        # This implementation is needed for BaseArray getitem/setitem to work,
+        # can be refactored.
+        return self
 
     def eval(self, i):
         return self.parent.eval(self.calc_index(i))
@@ -320,10 +326,16 @@
         i += 1
     return space.wrap(arr)
 
-@unwrap_spec(ObjSpace, int)
+@unwrap_spec(size=int)
 def zeros(space, size):
     return space.wrap(SingleDimArray(size))
 
+@unwrap_spec(size=int)
+def ones(space, size):
+    arr = SingleDimArray(size)
+    for i in xrange(size):
+        arr.storage[i] = 1.0
+    return space.wrap(arr)
 
 BaseArray.typedef = TypeDef(
     'numarray',
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
@@ -28,6 +28,15 @@
         a[1] = 1.0
         assert a[1] == 1.0
 
+    def test_ones(self):
+        from numpy import ones
+        a = ones(3)
+        assert len(a) == 3
+        assert a[0] == 1
+        raises(IndexError, "a[3]")
+        a[2] = 4
+        assert a[2] == 4
+
     def test_iterator_init(self):
         from numpy import array
         a = array(range(5))
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to