test_matfuncs.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. #
  2. # Created by: Pearu Peterson, March 2002
  3. #
  4. """ Test functions for scipy.linalg._matfuncs module
  5. """
  6. import math
  7. import numpy as np
  8. from numpy import array, eye, exp, random
  9. from numpy.linalg import matrix_power
  10. from numpy.testing import (
  11. assert_allclose, assert_, assert_array_almost_equal, assert_equal,
  12. assert_array_almost_equal_nulp, suppress_warnings)
  13. from scipy.sparse import csc_matrix, SparseEfficiencyWarning
  14. from scipy.sparse._construct import eye as speye
  15. from scipy.sparse.linalg._matfuncs import (expm, _expm,
  16. ProductOperator, MatrixPowerOperator,
  17. _onenorm_matrix_power_nnm)
  18. from scipy.sparse._sputils import matrix
  19. from scipy.linalg import logm
  20. from scipy.special import factorial, binom
  21. import scipy.sparse
  22. import scipy.sparse.linalg
  23. def _burkardt_13_power(n, p):
  24. """
  25. A helper function for testing matrix functions.
  26. Parameters
  27. ----------
  28. n : integer greater than 1
  29. Order of the square matrix to be returned.
  30. p : non-negative integer
  31. Power of the matrix.
  32. Returns
  33. -------
  34. out : ndarray representing a square matrix
  35. A Forsythe matrix of order n, raised to the power p.
  36. """
  37. # Input validation.
  38. if n != int(n) or n < 2:
  39. raise ValueError('n must be an integer greater than 1')
  40. n = int(n)
  41. if p != int(p) or p < 0:
  42. raise ValueError('p must be a non-negative integer')
  43. p = int(p)
  44. # Construct the matrix explicitly.
  45. a, b = divmod(p, n)
  46. large = np.power(10.0, -n*a)
  47. small = large * np.power(10.0, -n)
  48. return np.diag([large]*(n-b), b) + np.diag([small]*b, b-n)
  49. def test_onenorm_matrix_power_nnm():
  50. np.random.seed(1234)
  51. for n in range(1, 5):
  52. for p in range(5):
  53. M = np.random.random((n, n))
  54. Mp = np.linalg.matrix_power(M, p)
  55. observed = _onenorm_matrix_power_nnm(M, p)
  56. expected = np.linalg.norm(Mp, 1)
  57. assert_allclose(observed, expected)
  58. class TestExpM:
  59. def test_zero_ndarray(self):
  60. a = array([[0.,0],[0,0]])
  61. assert_array_almost_equal(expm(a),[[1,0],[0,1]])
  62. def test_zero_sparse(self):
  63. a = csc_matrix([[0.,0],[0,0]])
  64. assert_array_almost_equal(expm(a).toarray(),[[1,0],[0,1]])
  65. def test_zero_matrix(self):
  66. a = matrix([[0.,0],[0,0]])
  67. assert_array_almost_equal(expm(a),[[1,0],[0,1]])
  68. def test_misc_types(self):
  69. A = expm(np.array([[1]]))
  70. assert_allclose(expm(((1,),)), A)
  71. assert_allclose(expm([[1]]), A)
  72. assert_allclose(expm(matrix([[1]])), A)
  73. assert_allclose(expm(np.array([[1]])), A)
  74. assert_allclose(expm(csc_matrix([[1]])).A, A)
  75. B = expm(np.array([[1j]]))
  76. assert_allclose(expm(((1j,),)), B)
  77. assert_allclose(expm([[1j]]), B)
  78. assert_allclose(expm(matrix([[1j]])), B)
  79. assert_allclose(expm(csc_matrix([[1j]])).A, B)
  80. def test_bidiagonal_sparse(self):
  81. A = csc_matrix([
  82. [1, 3, 0],
  83. [0, 1, 5],
  84. [0, 0, 2]], dtype=float)
  85. e1 = math.exp(1)
  86. e2 = math.exp(2)
  87. expected = np.array([
  88. [e1, 3*e1, 15*(e2 - 2*e1)],
  89. [0, e1, 5*(e2 - e1)],
  90. [0, 0, e2]], dtype=float)
  91. observed = expm(A).toarray()
  92. assert_array_almost_equal(observed, expected)
  93. def test_padecases_dtype_float(self):
  94. for dtype in [np.float32, np.float64]:
  95. for scale in [1e-2, 1e-1, 5e-1, 1, 10]:
  96. A = scale * eye(3, dtype=dtype)
  97. observed = expm(A)
  98. expected = exp(scale, dtype=dtype) * eye(3, dtype=dtype)
  99. assert_array_almost_equal_nulp(observed, expected, nulp=100)
  100. def test_padecases_dtype_complex(self):
  101. for dtype in [np.complex64, np.complex128]:
  102. for scale in [1e-2, 1e-1, 5e-1, 1, 10]:
  103. A = scale * eye(3, dtype=dtype)
  104. observed = expm(A)
  105. expected = exp(scale, dtype=dtype) * eye(3, dtype=dtype)
  106. assert_array_almost_equal_nulp(observed, expected, nulp=100)
  107. def test_padecases_dtype_sparse_float(self):
  108. # float32 and complex64 lead to errors in spsolve/UMFpack
  109. dtype = np.float64
  110. for scale in [1e-2, 1e-1, 5e-1, 1, 10]:
  111. a = scale * speye(3, 3, dtype=dtype, format='csc')
  112. e = exp(scale, dtype=dtype) * eye(3, dtype=dtype)
  113. with suppress_warnings() as sup:
  114. sup.filter(SparseEfficiencyWarning,
  115. "Changing the sparsity structure of a csc_matrix is expensive.")
  116. exact_onenorm = _expm(a, use_exact_onenorm=True).toarray()
  117. inexact_onenorm = _expm(a, use_exact_onenorm=False).toarray()
  118. assert_array_almost_equal_nulp(exact_onenorm, e, nulp=100)
  119. assert_array_almost_equal_nulp(inexact_onenorm, e, nulp=100)
  120. def test_padecases_dtype_sparse_complex(self):
  121. # float32 and complex64 lead to errors in spsolve/UMFpack
  122. dtype = np.complex128
  123. for scale in [1e-2, 1e-1, 5e-1, 1, 10]:
  124. a = scale * speye(3, 3, dtype=dtype, format='csc')
  125. e = exp(scale) * eye(3, dtype=dtype)
  126. with suppress_warnings() as sup:
  127. sup.filter(SparseEfficiencyWarning,
  128. "Changing the sparsity structure of a csc_matrix is expensive.")
  129. assert_array_almost_equal_nulp(expm(a).toarray(), e, nulp=100)
  130. def test_logm_consistency(self):
  131. random.seed(1234)
  132. for dtype in [np.float64, np.complex128]:
  133. for n in range(1, 10):
  134. for scale in [1e-4, 1e-3, 1e-2, 1e-1, 1, 1e1, 1e2]:
  135. # make logm(A) be of a given scale
  136. A = (eye(n) + random.rand(n, n) * scale).astype(dtype)
  137. if np.iscomplexobj(A):
  138. A = A + 1j * random.rand(n, n) * scale
  139. assert_array_almost_equal(expm(logm(A)), A)
  140. def test_integer_matrix(self):
  141. Q = np.array([
  142. [-3, 1, 1, 1],
  143. [1, -3, 1, 1],
  144. [1, 1, -3, 1],
  145. [1, 1, 1, -3]])
  146. assert_allclose(expm(Q), expm(1.0 * Q))
  147. def test_integer_matrix_2(self):
  148. # Check for integer overflows
  149. Q = np.array([[-500, 500, 0, 0],
  150. [0, -550, 360, 190],
  151. [0, 630, -630, 0],
  152. [0, 0, 0, 0]], dtype=np.int16)
  153. assert_allclose(expm(Q), expm(1.0 * Q))
  154. Q = csc_matrix(Q)
  155. assert_allclose(expm(Q).A, expm(1.0 * Q).A)
  156. def test_triangularity_perturbation(self):
  157. # Experiment (1) of
  158. # Awad H. Al-Mohy and Nicholas J. Higham (2012)
  159. # Improved Inverse Scaling and Squaring Algorithms
  160. # for the Matrix Logarithm.
  161. A = np.array([
  162. [3.2346e-1, 3e4, 3e4, 3e4],
  163. [0, 3.0089e-1, 3e4, 3e4],
  164. [0, 0, 3.221e-1, 3e4],
  165. [0, 0, 0, 3.0744e-1]],
  166. dtype=float)
  167. A_logm = np.array([
  168. [-1.12867982029050462e+00, 9.61418377142025565e+04,
  169. -4.52485573953179264e+09, 2.92496941103871812e+14],
  170. [0.00000000000000000e+00, -1.20101052953082288e+00,
  171. 9.63469687211303099e+04, -4.68104828911105442e+09],
  172. [0.00000000000000000e+00, 0.00000000000000000e+00,
  173. -1.13289322264498393e+00, 9.53249183094775653e+04],
  174. [0.00000000000000000e+00, 0.00000000000000000e+00,
  175. 0.00000000000000000e+00, -1.17947533272554850e+00]],
  176. dtype=float)
  177. assert_allclose(expm(A_logm), A, rtol=1e-4)
  178. # Perturb the upper triangular matrix by tiny amounts,
  179. # so that it becomes technically not upper triangular.
  180. random.seed(1234)
  181. tiny = 1e-17
  182. A_logm_perturbed = A_logm.copy()
  183. A_logm_perturbed[1, 0] = tiny
  184. with suppress_warnings() as sup:
  185. sup.filter(RuntimeWarning, "Ill-conditioned.*")
  186. A_expm_logm_perturbed = expm(A_logm_perturbed)
  187. rtol = 1e-4
  188. atol = 100 * tiny
  189. assert_(not np.allclose(A_expm_logm_perturbed, A, rtol=rtol, atol=atol))
  190. def test_burkardt_1(self):
  191. # This matrix is diagonal.
  192. # The calculation of the matrix exponential is simple.
  193. #
  194. # This is the first of a series of matrix exponential tests
  195. # collected by John Burkardt from the following sources.
  196. #
  197. # Alan Laub,
  198. # Review of "Linear System Theory" by Joao Hespanha,
  199. # SIAM Review,
  200. # Volume 52, Number 4, December 2010, pages 779--781.
  201. #
  202. # Cleve Moler and Charles Van Loan,
  203. # Nineteen Dubious Ways to Compute the Exponential of a Matrix,
  204. # Twenty-Five Years Later,
  205. # SIAM Review,
  206. # Volume 45, Number 1, March 2003, pages 3--49.
  207. #
  208. # Cleve Moler,
  209. # Cleve's Corner: A Balancing Act for the Matrix Exponential,
  210. # 23 July 2012.
  211. #
  212. # Robert Ward,
  213. # Numerical computation of the matrix exponential
  214. # with accuracy estimate,
  215. # SIAM Journal on Numerical Analysis,
  216. # Volume 14, Number 4, September 1977, pages 600--610.
  217. exp1 = np.exp(1)
  218. exp2 = np.exp(2)
  219. A = np.array([
  220. [1, 0],
  221. [0, 2],
  222. ], dtype=float)
  223. desired = np.array([
  224. [exp1, 0],
  225. [0, exp2],
  226. ], dtype=float)
  227. actual = expm(A)
  228. assert_allclose(actual, desired)
  229. def test_burkardt_2(self):
  230. # This matrix is symmetric.
  231. # The calculation of the matrix exponential is straightforward.
  232. A = np.array([
  233. [1, 3],
  234. [3, 2],
  235. ], dtype=float)
  236. desired = np.array([
  237. [39.322809708033859, 46.166301438885753],
  238. [46.166301438885768, 54.711576854329110],
  239. ], dtype=float)
  240. actual = expm(A)
  241. assert_allclose(actual, desired)
  242. def test_burkardt_3(self):
  243. # This example is due to Laub.
  244. # This matrix is ill-suited for the Taylor series approach.
  245. # As powers of A are computed, the entries blow up too quickly.
  246. exp1 = np.exp(1)
  247. exp39 = np.exp(39)
  248. A = np.array([
  249. [0, 1],
  250. [-39, -40],
  251. ], dtype=float)
  252. desired = np.array([
  253. [
  254. 39/(38*exp1) - 1/(38*exp39),
  255. -np.expm1(-38) / (38*exp1)],
  256. [
  257. 39*np.expm1(-38) / (38*exp1),
  258. -1/(38*exp1) + 39/(38*exp39)],
  259. ], dtype=float)
  260. actual = expm(A)
  261. assert_allclose(actual, desired)
  262. def test_burkardt_4(self):
  263. # This example is due to Moler and Van Loan.
  264. # The example will cause problems for the series summation approach,
  265. # as well as for diagonal Pade approximations.
  266. A = np.array([
  267. [-49, 24],
  268. [-64, 31],
  269. ], dtype=float)
  270. U = np.array([[3, 1], [4, 2]], dtype=float)
  271. V = np.array([[1, -1/2], [-2, 3/2]], dtype=float)
  272. w = np.array([-17, -1], dtype=float)
  273. desired = np.dot(U * np.exp(w), V)
  274. actual = expm(A)
  275. assert_allclose(actual, desired)
  276. def test_burkardt_5(self):
  277. # This example is due to Moler and Van Loan.
  278. # This matrix is strictly upper triangular
  279. # All powers of A are zero beyond some (low) limit.
  280. # This example will cause problems for Pade approximations.
  281. A = np.array([
  282. [0, 6, 0, 0],
  283. [0, 0, 6, 0],
  284. [0, 0, 0, 6],
  285. [0, 0, 0, 0],
  286. ], dtype=float)
  287. desired = np.array([
  288. [1, 6, 18, 36],
  289. [0, 1, 6, 18],
  290. [0, 0, 1, 6],
  291. [0, 0, 0, 1],
  292. ], dtype=float)
  293. actual = expm(A)
  294. assert_allclose(actual, desired)
  295. def test_burkardt_6(self):
  296. # This example is due to Moler and Van Loan.
  297. # This matrix does not have a complete set of eigenvectors.
  298. # That means the eigenvector approach will fail.
  299. exp1 = np.exp(1)
  300. A = np.array([
  301. [1, 1],
  302. [0, 1],
  303. ], dtype=float)
  304. desired = np.array([
  305. [exp1, exp1],
  306. [0, exp1],
  307. ], dtype=float)
  308. actual = expm(A)
  309. assert_allclose(actual, desired)
  310. def test_burkardt_7(self):
  311. # This example is due to Moler and Van Loan.
  312. # This matrix is very close to example 5.
  313. # Mathematically, it has a complete set of eigenvectors.
  314. # Numerically, however, the calculation will be suspect.
  315. exp1 = np.exp(1)
  316. eps = np.spacing(1)
  317. A = np.array([
  318. [1 + eps, 1],
  319. [0, 1 - eps],
  320. ], dtype=float)
  321. desired = np.array([
  322. [exp1, exp1],
  323. [0, exp1],
  324. ], dtype=float)
  325. actual = expm(A)
  326. assert_allclose(actual, desired)
  327. def test_burkardt_8(self):
  328. # This matrix was an example in Wikipedia.
  329. exp4 = np.exp(4)
  330. exp16 = np.exp(16)
  331. A = np.array([
  332. [21, 17, 6],
  333. [-5, -1, -6],
  334. [4, 4, 16],
  335. ], dtype=float)
  336. desired = np.array([
  337. [13*exp16 - exp4, 13*exp16 - 5*exp4, 2*exp16 - 2*exp4],
  338. [-9*exp16 + exp4, -9*exp16 + 5*exp4, -2*exp16 + 2*exp4],
  339. [16*exp16, 16*exp16, 4*exp16],
  340. ], dtype=float) * 0.25
  341. actual = expm(A)
  342. assert_allclose(actual, desired)
  343. def test_burkardt_9(self):
  344. # This matrix is due to the NAG Library.
  345. # It is an example for function F01ECF.
  346. A = np.array([
  347. [1, 2, 2, 2],
  348. [3, 1, 1, 2],
  349. [3, 2, 1, 2],
  350. [3, 3, 3, 1],
  351. ], dtype=float)
  352. desired = np.array([
  353. [740.7038, 610.8500, 542.2743, 549.1753],
  354. [731.2510, 603.5524, 535.0884, 542.2743],
  355. [823.7630, 679.4257, 603.5524, 610.8500],
  356. [998.4355, 823.7630, 731.2510, 740.7038],
  357. ], dtype=float)
  358. actual = expm(A)
  359. assert_allclose(actual, desired)
  360. def test_burkardt_10(self):
  361. # This is Ward's example #1.
  362. # It is defective and nonderogatory.
  363. A = np.array([
  364. [4, 2, 0],
  365. [1, 4, 1],
  366. [1, 1, 4],
  367. ], dtype=float)
  368. assert_allclose(sorted(scipy.linalg.eigvals(A)), (3, 3, 6))
  369. desired = np.array([
  370. [147.8666224463699, 183.7651386463682, 71.79703239999647],
  371. [127.7810855231823, 183.7651386463682, 91.88256932318415],
  372. [127.7810855231824, 163.6796017231806, 111.9681062463718],
  373. ], dtype=float)
  374. actual = expm(A)
  375. assert_allclose(actual, desired)
  376. def test_burkardt_11(self):
  377. # This is Ward's example #2.
  378. # It is a symmetric matrix.
  379. A = np.array([
  380. [29.87942128909879, 0.7815750847907159, -2.289519314033932],
  381. [0.7815750847907159, 25.72656945571064, 8.680737820540137],
  382. [-2.289519314033932, 8.680737820540137, 34.39400925519054],
  383. ], dtype=float)
  384. assert_allclose(scipy.linalg.eigvalsh(A), (20, 30, 40))
  385. desired = np.array([
  386. [
  387. 5.496313853692378E+15,
  388. -1.823188097200898E+16,
  389. -3.047577080858001E+16],
  390. [
  391. -1.823188097200899E+16,
  392. 6.060522870222108E+16,
  393. 1.012918429302482E+17],
  394. [
  395. -3.047577080858001E+16,
  396. 1.012918429302482E+17,
  397. 1.692944112408493E+17],
  398. ], dtype=float)
  399. actual = expm(A)
  400. assert_allclose(actual, desired)
  401. def test_burkardt_12(self):
  402. # This is Ward's example #3.
  403. # Ward's algorithm has difficulty estimating the accuracy
  404. # of its results.
  405. A = np.array([
  406. [-131, 19, 18],
  407. [-390, 56, 54],
  408. [-387, 57, 52],
  409. ], dtype=float)
  410. assert_allclose(sorted(scipy.linalg.eigvals(A)), (-20, -2, -1))
  411. desired = np.array([
  412. [-1.509644158793135, 0.3678794391096522, 0.1353352811751005],
  413. [-5.632570799891469, 1.471517758499875, 0.4060058435250609],
  414. [-4.934938326088363, 1.103638317328798, 0.5413411267617766],
  415. ], dtype=float)
  416. actual = expm(A)
  417. assert_allclose(actual, desired)
  418. def test_burkardt_13(self):
  419. # This is Ward's example #4.
  420. # This is a version of the Forsythe matrix.
  421. # The eigenvector problem is badly conditioned.
  422. # Ward's algorithm has difficulty esimating the accuracy
  423. # of its results for this problem.
  424. #
  425. # Check the construction of one instance of this family of matrices.
  426. A4_actual = _burkardt_13_power(4, 1)
  427. A4_desired = [[0, 1, 0, 0],
  428. [0, 0, 1, 0],
  429. [0, 0, 0, 1],
  430. [1e-4, 0, 0, 0]]
  431. assert_allclose(A4_actual, A4_desired)
  432. # Check the expm for a few instances.
  433. for n in (2, 3, 4, 10):
  434. # Approximate expm using Taylor series.
  435. # This works well for this matrix family
  436. # because each matrix in the summation,
  437. # even before dividing by the factorial,
  438. # is entrywise positive with max entry 10**(-floor(p/n)*n).
  439. k = max(1, int(np.ceil(16/n)))
  440. desired = np.zeros((n, n), dtype=float)
  441. for p in range(n*k):
  442. Ap = _burkardt_13_power(n, p)
  443. assert_equal(np.min(Ap), 0)
  444. assert_allclose(np.max(Ap), np.power(10, -np.floor(p/n)*n))
  445. desired += Ap / factorial(p)
  446. actual = expm(_burkardt_13_power(n, 1))
  447. assert_allclose(actual, desired)
  448. def test_burkardt_14(self):
  449. # This is Moler's example.
  450. # This badly scaled matrix caused problems for MATLAB's expm().
  451. A = np.array([
  452. [0, 1e-8, 0],
  453. [-(2e10 + 4e8/6.), -3, 2e10],
  454. [200./3., 0, -200./3.],
  455. ], dtype=float)
  456. desired = np.array([
  457. [0.446849468283175, 1.54044157383952e-09, 0.462811453558774],
  458. [-5743067.77947947, -0.0152830038686819, -4526542.71278401],
  459. [0.447722977849494, 1.54270484519591e-09, 0.463480648837651],
  460. ], dtype=float)
  461. actual = expm(A)
  462. assert_allclose(actual, desired)
  463. def test_pascal(self):
  464. # Test pascal triangle.
  465. # Nilpotent exponential, used to trigger a failure (gh-8029)
  466. for scale in [1.0, 1e-3, 1e-6]:
  467. for n in range(0, 80, 3):
  468. sc = scale ** np.arange(n, -1, -1)
  469. if np.any(sc < 1e-300):
  470. break
  471. A = np.diag(np.arange(1, n + 1), -1) * scale
  472. B = expm(A)
  473. got = B
  474. expected = binom(np.arange(n + 1)[:,None],
  475. np.arange(n + 1)[None,:]) * sc[None,:] / sc[:,None]
  476. atol = 1e-13 * abs(expected).max()
  477. assert_allclose(got, expected, atol=atol)
  478. def test_matrix_input(self):
  479. # Large np.matrix inputs should work, gh-5546
  480. A = np.zeros((200, 200))
  481. A[-1,0] = 1
  482. B0 = expm(A)
  483. with suppress_warnings() as sup:
  484. sup.filter(DeprecationWarning, "the matrix subclass.*")
  485. sup.filter(PendingDeprecationWarning, "the matrix subclass.*")
  486. B = expm(np.matrix(A))
  487. assert_allclose(B, B0)
  488. def test_exp_sinch_overflow(self):
  489. # Check overflow in intermediate steps is fixed (gh-11839)
  490. L = np.array([[1.0, -0.5, -0.5, 0.0, 0.0, 0.0, 0.0],
  491. [0.0, 1.0, 0.0, -0.5, -0.5, 0.0, 0.0],
  492. [0.0, 0.0, 1.0, 0.0, 0.0, -0.5, -0.5],
  493. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  494. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  495. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  496. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]])
  497. E0 = expm(-L)
  498. E1 = expm(-2**11 * L)
  499. E2 = E0
  500. for j in range(11):
  501. E2 = E2 @ E2
  502. assert_allclose(E1, E2)
  503. class TestOperators:
  504. def test_product_operator(self):
  505. random.seed(1234)
  506. n = 5
  507. k = 2
  508. nsamples = 10
  509. for i in range(nsamples):
  510. A = np.random.randn(n, n)
  511. B = np.random.randn(n, n)
  512. C = np.random.randn(n, n)
  513. D = np.random.randn(n, k)
  514. op = ProductOperator(A, B, C)
  515. assert_allclose(op.matmat(D), A.dot(B).dot(C).dot(D))
  516. assert_allclose(op.T.matmat(D), (A.dot(B).dot(C)).T.dot(D))
  517. def test_matrix_power_operator(self):
  518. random.seed(1234)
  519. n = 5
  520. k = 2
  521. p = 3
  522. nsamples = 10
  523. for i in range(nsamples):
  524. A = np.random.randn(n, n)
  525. B = np.random.randn(n, k)
  526. op = MatrixPowerOperator(A, p)
  527. assert_allclose(op.matmat(B), matrix_power(A, p).dot(B))
  528. assert_allclose(op.T.matmat(B), matrix_power(A, p).T.dot(B))