[Numpy-svn] r6527 - in trunk/numpy/core: . tests

numpy-svn@scip... numpy-svn@scip...
Mon Mar 2 08:46:24 CST 2009


Author: cdavid
Date: 2009-03-02 08:46:20 -0600 (Mon, 02 Mar 2009)
New Revision: 6527

Modified:
   trunk/numpy/core/defmatrix.py
   trunk/numpy/core/tests/test_defmatrix.py
Log:
Add in-place power raising for matrices (#787).

Modified: trunk/numpy/core/defmatrix.py
===================================================================
--- trunk/numpy/core/defmatrix.py	2009-03-02 14:26:50 UTC (rev 6526)
+++ trunk/numpy/core/defmatrix.py	2009-03-02 14:46:20 UTC (rev 6527)
@@ -302,6 +302,10 @@
     def __pow__(self, other):
         return matrix_power(self, other)
 
+    def __ipow__(self, other):
+        self[:] = self ** other
+        return self
+
     def __rpow__(self, other):
         return NotImplemented
 

Modified: trunk/numpy/core/tests/test_defmatrix.py
===================================================================
--- trunk/numpy/core/tests/test_defmatrix.py	2009-03-02 14:26:50 UTC (rev 6526)
+++ trunk/numpy/core/tests/test_defmatrix.py	2009-03-02 14:46:20 UTC (rev 6527)
@@ -207,6 +207,19 @@
         mA2 *= 3
         assert allclose(mA2.A, 3*A)
 
+    def test_pow(self):
+        """Test raising a matrix to an integer power works as expected."""
+        m = matrix("1. 2.; 3. 4.")
+        m2 = m.copy()
+        m2 **= 2
+        mi = m.copy()
+        mi **= -1
+        m4 = m2.copy()
+        m4 **= 2
+        assert_array_almost_equal(m2, m**2)
+        assert_array_almost_equal(m4, np.dot(m2, m2))
+        assert_array_almost_equal(np.dot(mi, m), np.eye(2))
+
     def test_notimplemented(self):
         '''Check that 'not implemented' operations produce a failure.'''
         A = matrix([[1., 2.],



More information about the Numpy-svn mailing list