| 215 | |
| 216 | class TestAlgebra: |
| 217 | def test_basic(self): |
| 218 | import numpy.linalg as linalg |
| 219 | |
| 220 | A = np.array([[1., 2.], [3., 4.]]) |
| 221 | mA = matrix(A) |
| 222 | |
| 223 | B = np.identity(2) |
| 224 | for i in range(6): |
| 225 | assert_(np.allclose((mA ** i).A, B)) |
| 226 | B = np.dot(B, A) |
| 227 | |
| 228 | Ainv = linalg.inv(A) |
| 229 | B = np.identity(2) |
| 230 | for i in range(6): |
| 231 | assert_(np.allclose((mA ** -i).A, B)) |
| 232 | B = np.dot(B, Ainv) |
| 233 | |
| 234 | assert_(np.allclose((mA * mA).A, np.dot(A, A))) |
| 235 | assert_(np.allclose((mA + mA).A, (A + A))) |
| 236 | assert_(np.allclose((3 * mA).A, (3 * A))) |
| 237 | |
| 238 | mA2 = matrix(A) |
| 239 | mA2 *= 3 |
| 240 | assert_(np.allclose(mA2.A, 3 * A)) |
| 241 | |
| 242 | def test_pow(self): |
| 243 | """Test raising a matrix to an integer power works as expected.""" |