_matfuncs_inv_ssq.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. """
  2. Matrix functions that use Pade approximation with inverse scaling and squaring.
  3. """
  4. from __future__ import division, print_function, absolute_import
  5. import warnings
  6. import numpy as np
  7. from scipy.linalg._matfuncs_sqrtm import SqrtmError, _sqrtm_triu
  8. from scipy.linalg.decomp_schur import schur, rsf2csf
  9. from scipy.linalg.matfuncs import funm
  10. from scipy.linalg import svdvals, solve_triangular
  11. from scipy.sparse.linalg.interface import LinearOperator
  12. from scipy.sparse.linalg import onenormest
  13. import scipy.special
  14. class LogmRankWarning(UserWarning):
  15. pass
  16. class LogmExactlySingularWarning(LogmRankWarning):
  17. pass
  18. class LogmNearlySingularWarning(LogmRankWarning):
  19. pass
  20. class LogmError(np.linalg.LinAlgError):
  21. pass
  22. class FractionalMatrixPowerError(np.linalg.LinAlgError):
  23. pass
  24. #TODO renovate or move this class when scipy operators are more mature
  25. class _MatrixM1PowerOperator(LinearOperator):
  26. """
  27. A representation of the linear operator (A - I)^p.
  28. """
  29. def __init__(self, A, p):
  30. if A.ndim != 2 or A.shape[0] != A.shape[1]:
  31. raise ValueError('expected A to be like a square matrix')
  32. if p < 0 or p != int(p):
  33. raise ValueError('expected p to be a non-negative integer')
  34. self._A = A
  35. self._p = p
  36. self.ndim = A.ndim
  37. self.shape = A.shape
  38. def _matvec(self, x):
  39. for i in range(self._p):
  40. x = self._A.dot(x) - x
  41. return x
  42. def _rmatvec(self, x):
  43. for i in range(self._p):
  44. x = x.dot(self._A) - x
  45. return x
  46. def _matmat(self, X):
  47. for i in range(self._p):
  48. X = self._A.dot(X) - X
  49. return X
  50. def _adjoint(self):
  51. return _MatrixM1PowerOperator(self._A.T, self._p)
  52. #TODO renovate or move this function when scipy operators are more mature
  53. def _onenormest_m1_power(A, p,
  54. t=2, itmax=5, compute_v=False, compute_w=False):
  55. """
  56. Efficiently estimate the 1-norm of (A - I)^p.
  57. Parameters
  58. ----------
  59. A : ndarray
  60. Matrix whose 1-norm of a power is to be computed.
  61. p : int
  62. Non-negative integer power.
  63. t : int, optional
  64. A positive parameter controlling the tradeoff between
  65. accuracy versus time and memory usage.
  66. Larger values take longer and use more memory
  67. but give more accurate output.
  68. itmax : int, optional
  69. Use at most this many iterations.
  70. compute_v : bool, optional
  71. Request a norm-maximizing linear operator input vector if True.
  72. compute_w : bool, optional
  73. Request a norm-maximizing linear operator output vector if True.
  74. Returns
  75. -------
  76. est : float
  77. An underestimate of the 1-norm of the sparse matrix.
  78. v : ndarray, optional
  79. The vector such that ||Av||_1 == est*||v||_1.
  80. It can be thought of as an input to the linear operator
  81. that gives an output with particularly large norm.
  82. w : ndarray, optional
  83. The vector Av which has relatively large 1-norm.
  84. It can be thought of as an output of the linear operator
  85. that is relatively large in norm compared to the input.
  86. """
  87. return onenormest(_MatrixM1PowerOperator(A, p),
  88. t=t, itmax=itmax, compute_v=compute_v, compute_w=compute_w)
  89. def _unwindk(z):
  90. """
  91. Compute the scalar unwinding number.
  92. Uses Eq. (5.3) in [1]_, and should be equal to (z - log(exp(z)) / (2 pi i).
  93. Note that this definition differs in sign from the original definition
  94. in equations (5, 6) in [2]_. The sign convention is justified in [3]_.
  95. Parameters
  96. ----------
  97. z : complex
  98. A complex number.
  99. Returns
  100. -------
  101. unwinding_number : integer
  102. The scalar unwinding number of z.
  103. References
  104. ----------
  105. .. [1] Nicholas J. Higham and Lijing lin (2011)
  106. "A Schur-Pade Algorithm for Fractional Powers of a Matrix."
  107. SIAM Journal on Matrix Analysis and Applications,
  108. 32 (3). pp. 1056-1078. ISSN 0895-4798
  109. .. [2] Robert M. Corless and David J. Jeffrey,
  110. "The unwinding number." Newsletter ACM SIGSAM Bulletin
  111. Volume 30, Issue 2, June 1996, Pages 28-35.
  112. .. [3] Russell Bradford and Robert M. Corless and James H. Davenport and
  113. David J. Jeffrey and Stephen M. Watt,
  114. "Reasoning about the elementary functions of complex analysis"
  115. Annals of Mathematics and Artificial Intelligence,
  116. 36: 303-318, 2002.
  117. """
  118. return int(np.ceil((z.imag - np.pi) / (2*np.pi)))
  119. def _briggs_helper_function(a, k):
  120. """
  121. Computes r = a^(1 / (2^k)) - 1.
  122. This is algorithm (2) of [1]_.
  123. The purpose is to avoid a danger of subtractive cancellation.
  124. For more computational efficiency it should probably be cythonized.
  125. Parameters
  126. ----------
  127. a : complex
  128. A complex number.
  129. k : integer
  130. A nonnegative integer.
  131. Returns
  132. -------
  133. r : complex
  134. The value r = a^(1 / (2^k)) - 1 computed with less cancellation.
  135. Notes
  136. -----
  137. The algorithm as formulated in the reference does not handle k=0 or k=1
  138. correctly, so these are special-cased in this implementation.
  139. This function is intended to not allow `a` to belong to the closed
  140. negative real axis, but this constraint is relaxed.
  141. References
  142. ----------
  143. .. [1] Awad H. Al-Mohy (2012)
  144. "A more accurate Briggs method for the logarithm",
  145. Numerical Algorithms, 59 : 393--402.
  146. """
  147. if k < 0 or int(k) != k:
  148. raise ValueError('expected a nonnegative integer k')
  149. if k == 0:
  150. return a - 1
  151. elif k == 1:
  152. return np.sqrt(a) - 1
  153. else:
  154. k_hat = k
  155. if np.angle(a) >= np.pi / 2:
  156. a = np.sqrt(a)
  157. k_hat = k - 1
  158. z0 = a - 1
  159. a = np.sqrt(a)
  160. r = 1 + a
  161. for j in range(1, k_hat):
  162. a = np.sqrt(a)
  163. r = r * (1 + a)
  164. r = z0 / r
  165. return r
  166. def _fractional_power_superdiag_entry(l1, l2, t12, p):
  167. """
  168. Compute a superdiagonal entry of a fractional matrix power.
  169. This is Eq. (5.6) in [1]_.
  170. Parameters
  171. ----------
  172. l1 : complex
  173. A diagonal entry of the matrix.
  174. l2 : complex
  175. A diagonal entry of the matrix.
  176. t12 : complex
  177. A superdiagonal entry of the matrix.
  178. p : float
  179. A fractional power.
  180. Returns
  181. -------
  182. f12 : complex
  183. A superdiagonal entry of the fractional matrix power.
  184. Notes
  185. -----
  186. Care has been taken to return a real number if possible when
  187. all of the inputs are real numbers.
  188. References
  189. ----------
  190. .. [1] Nicholas J. Higham and Lijing lin (2011)
  191. "A Schur-Pade Algorithm for Fractional Powers of a Matrix."
  192. SIAM Journal on Matrix Analysis and Applications,
  193. 32 (3). pp. 1056-1078. ISSN 0895-4798
  194. """
  195. if l1 == l2:
  196. f12 = t12 * p * l1**(p-1)
  197. elif abs(l2 - l1) > abs(l1 + l2) / 2:
  198. f12 = t12 * ((l2**p) - (l1**p)) / (l2 - l1)
  199. else:
  200. # This is Eq. (5.5) in [1].
  201. z = (l2 - l1) / (l2 + l1)
  202. log_l1 = np.log(l1)
  203. log_l2 = np.log(l2)
  204. arctanh_z = np.arctanh(z)
  205. tmp_a = t12 * np.exp((p/2)*(log_l2 + log_l1))
  206. tmp_u = _unwindk(log_l2 - log_l1)
  207. if tmp_u:
  208. tmp_b = p * (arctanh_z + np.pi * 1j * tmp_u)
  209. else:
  210. tmp_b = p * arctanh_z
  211. tmp_c = 2 * np.sinh(tmp_b) / (l2 - l1)
  212. f12 = tmp_a * tmp_c
  213. return f12
  214. def _logm_superdiag_entry(l1, l2, t12):
  215. """
  216. Compute a superdiagonal entry of a matrix logarithm.
  217. This is like Eq. (11.28) in [1]_, except the determination of whether
  218. l1 and l2 are sufficiently far apart has been modified.
  219. Parameters
  220. ----------
  221. l1 : complex
  222. A diagonal entry of the matrix.
  223. l2 : complex
  224. A diagonal entry of the matrix.
  225. t12 : complex
  226. A superdiagonal entry of the matrix.
  227. Returns
  228. -------
  229. f12 : complex
  230. A superdiagonal entry of the matrix logarithm.
  231. Notes
  232. -----
  233. Care has been taken to return a real number if possible when
  234. all of the inputs are real numbers.
  235. References
  236. ----------
  237. .. [1] Nicholas J. Higham (2008)
  238. "Functions of Matrices: Theory and Computation"
  239. ISBN 978-0-898716-46-7
  240. """
  241. if l1 == l2:
  242. f12 = t12 / l1
  243. elif abs(l2 - l1) > abs(l1 + l2) / 2:
  244. f12 = t12 * (np.log(l2) - np.log(l1)) / (l2 - l1)
  245. else:
  246. z = (l2 - l1) / (l2 + l1)
  247. u = _unwindk(np.log(l2) - np.log(l1))
  248. if u:
  249. f12 = t12 * 2 * (np.arctanh(z) + np.pi*1j*u) / (l2 - l1)
  250. else:
  251. f12 = t12 * 2 * np.arctanh(z) / (l2 - l1)
  252. return f12
  253. def _inverse_squaring_helper(T0, theta):
  254. """
  255. A helper function for inverse scaling and squaring for Pade approximation.
  256. Parameters
  257. ----------
  258. T0 : (N, N) array_like upper triangular
  259. Matrix involved in inverse scaling and squaring.
  260. theta : indexable
  261. The values theta[1] .. theta[7] must be available.
  262. They represent bounds related to Pade approximation, and they depend
  263. on the matrix function which is being computed.
  264. For example, different values of theta are required for
  265. matrix logarithm than for fractional matrix power.
  266. Returns
  267. -------
  268. R : (N, N) array_like upper triangular
  269. Composition of zero or more matrix square roots of T0, minus I.
  270. s : non-negative integer
  271. Number of square roots taken.
  272. m : positive integer
  273. The degree of the Pade approximation.
  274. Notes
  275. -----
  276. This subroutine appears as a chunk of lines within
  277. a couple of published algorithms; for example it appears
  278. as lines 4--35 in algorithm (3.1) of [1]_, and
  279. as lines 3--34 in algorithm (4.1) of [2]_.
  280. The instances of 'goto line 38' in algorithm (3.1) of [1]_
  281. probably mean 'goto line 36' and have been intepreted accordingly.
  282. References
  283. ----------
  284. .. [1] Nicholas J. Higham and Lijing Lin (2013)
  285. "An Improved Schur-Pade Algorithm for Fractional Powers
  286. of a Matrix and their Frechet Derivatives."
  287. .. [2] Awad H. Al-Mohy and Nicholas J. Higham (2012)
  288. "Improved Inverse Scaling and Squaring Algorithms
  289. for the Matrix Logarithm."
  290. SIAM Journal on Scientific Computing, 34 (4). C152-C169.
  291. ISSN 1095-7197
  292. """
  293. if len(T0.shape) != 2 or T0.shape[0] != T0.shape[1]:
  294. raise ValueError('expected an upper triangular square matrix')
  295. n, n = T0.shape
  296. T = T0
  297. # Find s0, the smallest s such that the spectral radius
  298. # of a certain diagonal matrix is at most theta[7].
  299. # Note that because theta[7] < 1,
  300. # this search will not terminate if any diagonal entry of T is zero.
  301. s0 = 0
  302. tmp_diag = np.diag(T)
  303. if np.count_nonzero(tmp_diag) != n:
  304. raise Exception('internal inconsistency')
  305. while np.max(np.absolute(tmp_diag - 1)) > theta[7]:
  306. tmp_diag = np.sqrt(tmp_diag)
  307. s0 += 1
  308. # Take matrix square roots of T.
  309. for i in range(s0):
  310. T = _sqrtm_triu(T)
  311. # Flow control in this section is a little odd.
  312. # This is because I am translating algorithm descriptions
  313. # which have GOTOs in the publication.
  314. s = s0
  315. k = 0
  316. d2 = _onenormest_m1_power(T, 2) ** (1/2)
  317. d3 = _onenormest_m1_power(T, 3) ** (1/3)
  318. a2 = max(d2, d3)
  319. m = None
  320. for i in (1, 2):
  321. if a2 <= theta[i]:
  322. m = i
  323. break
  324. while m is None:
  325. if s > s0:
  326. d3 = _onenormest_m1_power(T, 3) ** (1/3)
  327. d4 = _onenormest_m1_power(T, 4) ** (1/4)
  328. a3 = max(d3, d4)
  329. if a3 <= theta[7]:
  330. j1 = min(i for i in (3, 4, 5, 6, 7) if a3 <= theta[i])
  331. if j1 <= 6:
  332. m = j1
  333. break
  334. elif a3 / 2 <= theta[5] and k < 2:
  335. k += 1
  336. T = _sqrtm_triu(T)
  337. s += 1
  338. continue
  339. d5 = _onenormest_m1_power(T, 5) ** (1/5)
  340. a4 = max(d4, d5)
  341. eta = min(a3, a4)
  342. for i in (6, 7):
  343. if eta <= theta[i]:
  344. m = i
  345. break
  346. if m is not None:
  347. break
  348. T = _sqrtm_triu(T)
  349. s += 1
  350. # The subtraction of the identity is redundant here,
  351. # because the diagonal will be replaced for improved numerical accuracy,
  352. # but this formulation should help clarify the meaning of R.
  353. R = T - np.identity(n)
  354. # Replace the diagonal and first superdiagonal of T0^(1/(2^s)) - I
  355. # using formulas that have less subtractive cancellation.
  356. # Skip this step if the principal branch
  357. # does not exist at T0; this happens when a diagonal entry of T0
  358. # is negative with imaginary part 0.
  359. has_principal_branch = all(x.real > 0 or x.imag != 0 for x in np.diag(T0))
  360. if has_principal_branch:
  361. for j in range(n):
  362. a = T0[j, j]
  363. r = _briggs_helper_function(a, s)
  364. R[j, j] = r
  365. p = np.exp2(-s)
  366. for j in range(n-1):
  367. l1 = T0[j, j]
  368. l2 = T0[j+1, j+1]
  369. t12 = T0[j, j+1]
  370. f12 = _fractional_power_superdiag_entry(l1, l2, t12, p)
  371. R[j, j+1] = f12
  372. # Return the T-I matrix, the number of square roots, and the Pade degree.
  373. if not np.array_equal(R, np.triu(R)):
  374. raise Exception('internal inconsistency')
  375. return R, s, m
  376. def _fractional_power_pade_constant(i, t):
  377. # A helper function for matrix fractional power.
  378. if i < 1:
  379. raise ValueError('expected a positive integer i')
  380. if not (-1 < t < 1):
  381. raise ValueError('expected -1 < t < 1')
  382. if i == 1:
  383. return -t
  384. elif i % 2 == 0:
  385. j = i // 2
  386. return (-j + t) / (2 * (2*j - 1))
  387. elif i % 2 == 1:
  388. j = (i - 1) // 2
  389. return (-j - t) / (2 * (2*j + 1))
  390. else:
  391. raise Exception('internal error')
  392. def _fractional_power_pade(R, t, m):
  393. """
  394. Evaluate the Pade approximation of a fractional matrix power.
  395. Evaluate the degree-m Pade approximation of R
  396. to the fractional matrix power t using the continued fraction
  397. in bottom-up fashion using algorithm (4.1) in [1]_.
  398. Parameters
  399. ----------
  400. R : (N, N) array_like
  401. Upper triangular matrix whose fractional power to evaluate.
  402. t : float
  403. Fractional power between -1 and 1 exclusive.
  404. m : positive integer
  405. Degree of Pade approximation.
  406. Returns
  407. -------
  408. U : (N, N) array_like
  409. The degree-m Pade approximation of R to the fractional power t.
  410. This matrix will be upper triangular.
  411. References
  412. ----------
  413. .. [1] Nicholas J. Higham and Lijing lin (2011)
  414. "A Schur-Pade Algorithm for Fractional Powers of a Matrix."
  415. SIAM Journal on Matrix Analysis and Applications,
  416. 32 (3). pp. 1056-1078. ISSN 0895-4798
  417. """
  418. if m < 1 or int(m) != m:
  419. raise ValueError('expected a positive integer m')
  420. if not (-1 < t < 1):
  421. raise ValueError('expected -1 < t < 1')
  422. R = np.asarray(R)
  423. if len(R.shape) != 2 or R.shape[0] != R.shape[1]:
  424. raise ValueError('expected an upper triangular square matrix')
  425. n, n = R.shape
  426. ident = np.identity(n)
  427. Y = R * _fractional_power_pade_constant(2*m, t)
  428. for j in range(2*m - 1, 0, -1):
  429. rhs = R * _fractional_power_pade_constant(j, t)
  430. Y = solve_triangular(ident + Y, rhs)
  431. U = ident + Y
  432. if not np.array_equal(U, np.triu(U)):
  433. raise Exception('internal inconsistency')
  434. return U
  435. def _remainder_matrix_power_triu(T, t):
  436. """
  437. Compute a fractional power of an upper triangular matrix.
  438. The fractional power is restricted to fractions -1 < t < 1.
  439. This uses algorithm (3.1) of [1]_.
  440. The Pade approximation itself uses algorithm (4.1) of [2]_.
  441. Parameters
  442. ----------
  443. T : (N, N) array_like
  444. Upper triangular matrix whose fractional power to evaluate.
  445. t : float
  446. Fractional power between -1 and 1 exclusive.
  447. Returns
  448. -------
  449. X : (N, N) array_like
  450. The fractional power of the matrix.
  451. References
  452. ----------
  453. .. [1] Nicholas J. Higham and Lijing Lin (2013)
  454. "An Improved Schur-Pade Algorithm for Fractional Powers
  455. of a Matrix and their Frechet Derivatives."
  456. .. [2] Nicholas J. Higham and Lijing lin (2011)
  457. "A Schur-Pade Algorithm for Fractional Powers of a Matrix."
  458. SIAM Journal on Matrix Analysis and Applications,
  459. 32 (3). pp. 1056-1078. ISSN 0895-4798
  460. """
  461. m_to_theta = {
  462. 1: 1.51e-5,
  463. 2: 2.24e-3,
  464. 3: 1.88e-2,
  465. 4: 6.04e-2,
  466. 5: 1.24e-1,
  467. 6: 2.00e-1,
  468. 7: 2.79e-1,
  469. }
  470. n, n = T.shape
  471. T0 = T
  472. T0_diag = np.diag(T0)
  473. if np.array_equal(T0, np.diag(T0_diag)):
  474. U = np.diag(T0_diag ** t)
  475. else:
  476. R, s, m = _inverse_squaring_helper(T0, m_to_theta)
  477. # Evaluate the Pade approximation.
  478. # Note that this function expects the negative of the matrix
  479. # returned by the inverse squaring helper.
  480. U = _fractional_power_pade(-R, t, m)
  481. # Undo the inverse scaling and squaring.
  482. # Be less clever about this
  483. # if the principal branch does not exist at T0;
  484. # this happens when a diagonal entry of T0
  485. # is negative with imaginary part 0.
  486. eivals = np.diag(T0)
  487. has_principal_branch = all(x.real > 0 or x.imag != 0 for x in eivals)
  488. for i in range(s, -1, -1):
  489. if i < s:
  490. U = U.dot(U)
  491. else:
  492. if has_principal_branch:
  493. p = t * np.exp2(-i)
  494. U[np.diag_indices(n)] = T0_diag ** p
  495. for j in range(n-1):
  496. l1 = T0[j, j]
  497. l2 = T0[j+1, j+1]
  498. t12 = T0[j, j+1]
  499. f12 = _fractional_power_superdiag_entry(l1, l2, t12, p)
  500. U[j, j+1] = f12
  501. if not np.array_equal(U, np.triu(U)):
  502. raise Exception('internal inconsistency')
  503. return U
  504. def _remainder_matrix_power(A, t):
  505. """
  506. Compute the fractional power of a matrix, for fractions -1 < t < 1.
  507. This uses algorithm (3.1) of [1]_.
  508. The Pade approximation itself uses algorithm (4.1) of [2]_.
  509. Parameters
  510. ----------
  511. A : (N, N) array_like
  512. Matrix whose fractional power to evaluate.
  513. t : float
  514. Fractional power between -1 and 1 exclusive.
  515. Returns
  516. -------
  517. X : (N, N) array_like
  518. The fractional power of the matrix.
  519. References
  520. ----------
  521. .. [1] Nicholas J. Higham and Lijing Lin (2013)
  522. "An Improved Schur-Pade Algorithm for Fractional Powers
  523. of a Matrix and their Frechet Derivatives."
  524. .. [2] Nicholas J. Higham and Lijing lin (2011)
  525. "A Schur-Pade Algorithm for Fractional Powers of a Matrix."
  526. SIAM Journal on Matrix Analysis and Applications,
  527. 32 (3). pp. 1056-1078. ISSN 0895-4798
  528. """
  529. # This code block is copied from numpy.matrix_power().
  530. A = np.asarray(A)
  531. if len(A.shape) != 2 or A.shape[0] != A.shape[1]:
  532. raise ValueError('input must be a square array')
  533. # Get the number of rows and columns.
  534. n, n = A.shape
  535. # Triangularize the matrix if necessary,
  536. # attempting to preserve dtype if possible.
  537. if np.array_equal(A, np.triu(A)):
  538. Z = None
  539. T = A
  540. else:
  541. if np.isrealobj(A):
  542. T, Z = schur(A)
  543. if not np.array_equal(T, np.triu(T)):
  544. T, Z = rsf2csf(T, Z)
  545. else:
  546. T, Z = schur(A, output='complex')
  547. # Zeros on the diagonal of the triangular matrix are forbidden,
  548. # because the inverse scaling and squaring cannot deal with it.
  549. T_diag = np.diag(T)
  550. if np.count_nonzero(T_diag) != n:
  551. raise FractionalMatrixPowerError(
  552. 'cannot use inverse scaling and squaring to find '
  553. 'the fractional matrix power of a singular matrix')
  554. # If the triangular matrix is real and has a negative
  555. # entry on the diagonal, then force the matrix to be complex.
  556. if np.isrealobj(T) and np.min(T_diag) < 0:
  557. T = T.astype(complex)
  558. # Get the fractional power of the triangular matrix,
  559. # and de-triangularize it if necessary.
  560. U = _remainder_matrix_power_triu(T, t)
  561. if Z is not None:
  562. ZH = np.conjugate(Z).T
  563. return Z.dot(U).dot(ZH)
  564. else:
  565. return U
  566. def _fractional_matrix_power(A, p):
  567. """
  568. Compute the fractional power of a matrix.
  569. See the fractional_matrix_power docstring in matfuncs.py for more info.
  570. """
  571. A = np.asarray(A)
  572. if len(A.shape) != 2 or A.shape[0] != A.shape[1]:
  573. raise ValueError('expected a square matrix')
  574. if p == int(p):
  575. return np.linalg.matrix_power(A, int(p))
  576. # Compute singular values.
  577. s = svdvals(A)
  578. # Inverse scaling and squaring cannot deal with a singular matrix,
  579. # because the process of repeatedly taking square roots
  580. # would not converge to the identity matrix.
  581. if s[-1]:
  582. # Compute the condition number relative to matrix inversion,
  583. # and use this to decide between floor(p) and ceil(p).
  584. k2 = s[0] / s[-1]
  585. p1 = p - np.floor(p)
  586. p2 = p - np.ceil(p)
  587. if p1 * k2 ** (1 - p1) <= -p2 * k2:
  588. a = int(np.floor(p))
  589. b = p1
  590. else:
  591. a = int(np.ceil(p))
  592. b = p2
  593. try:
  594. R = _remainder_matrix_power(A, b)
  595. Q = np.linalg.matrix_power(A, a)
  596. return Q.dot(R)
  597. except np.linalg.LinAlgError:
  598. pass
  599. # If p is negative then we are going to give up.
  600. # If p is non-negative then we can fall back to generic funm.
  601. if p < 0:
  602. X = np.empty_like(A)
  603. X.fill(np.nan)
  604. return X
  605. else:
  606. p1 = p - np.floor(p)
  607. a = int(np.floor(p))
  608. b = p1
  609. R, info = funm(A, lambda x: pow(x, b), disp=False)
  610. Q = np.linalg.matrix_power(A, a)
  611. return Q.dot(R)
  612. def _logm_triu(T):
  613. """
  614. Compute matrix logarithm of an upper triangular matrix.
  615. The matrix logarithm is the inverse of
  616. expm: expm(logm(`T`)) == `T`
  617. Parameters
  618. ----------
  619. T : (N, N) array_like
  620. Upper triangular matrix whose logarithm to evaluate
  621. Returns
  622. -------
  623. logm : (N, N) ndarray
  624. Matrix logarithm of `T`
  625. References
  626. ----------
  627. .. [1] Awad H. Al-Mohy and Nicholas J. Higham (2012)
  628. "Improved Inverse Scaling and Squaring Algorithms
  629. for the Matrix Logarithm."
  630. SIAM Journal on Scientific Computing, 34 (4). C152-C169.
  631. ISSN 1095-7197
  632. .. [2] Nicholas J. Higham (2008)
  633. "Functions of Matrices: Theory and Computation"
  634. ISBN 978-0-898716-46-7
  635. .. [3] Nicholas J. Higham and Lijing lin (2011)
  636. "A Schur-Pade Algorithm for Fractional Powers of a Matrix."
  637. SIAM Journal on Matrix Analysis and Applications,
  638. 32 (3). pp. 1056-1078. ISSN 0895-4798
  639. """
  640. T = np.asarray(T)
  641. if len(T.shape) != 2 or T.shape[0] != T.shape[1]:
  642. raise ValueError('expected an upper triangular square matrix')
  643. n, n = T.shape
  644. # Construct T0 with the appropriate type,
  645. # depending on the dtype and the spectrum of T.
  646. T_diag = np.diag(T)
  647. keep_it_real = np.isrealobj(T) and np.min(T_diag) >= 0
  648. if keep_it_real:
  649. T0 = T
  650. else:
  651. T0 = T.astype(complex)
  652. # Define bounds given in Table (2.1).
  653. theta = (None,
  654. 1.59e-5, 2.31e-3, 1.94e-2, 6.21e-2,
  655. 1.28e-1, 2.06e-1, 2.88e-1, 3.67e-1,
  656. 4.39e-1, 5.03e-1, 5.60e-1, 6.09e-1,
  657. 6.52e-1, 6.89e-1, 7.21e-1, 7.49e-1)
  658. R, s, m = _inverse_squaring_helper(T0, theta)
  659. # Evaluate U = 2**s r_m(T - I) using the partial fraction expansion (1.1).
  660. # This requires the nodes and weights
  661. # corresponding to degree-m Gauss-Legendre quadrature.
  662. # These quadrature arrays need to be transformed from the [-1, 1] interval
  663. # to the [0, 1] interval.
  664. nodes, weights = scipy.special.p_roots(m)
  665. nodes = nodes.real
  666. if nodes.shape != (m,) or weights.shape != (m,):
  667. raise Exception('internal error')
  668. nodes = 0.5 + 0.5 * nodes
  669. weights = 0.5 * weights
  670. ident = np.identity(n)
  671. U = np.zeros_like(R)
  672. for alpha, beta in zip(weights, nodes):
  673. U += solve_triangular(ident + beta*R, alpha*R)
  674. U *= np.exp2(s)
  675. # Skip this step if the principal branch
  676. # does not exist at T0; this happens when a diagonal entry of T0
  677. # is negative with imaginary part 0.
  678. has_principal_branch = all(x.real > 0 or x.imag != 0 for x in np.diag(T0))
  679. if has_principal_branch:
  680. # Recompute diagonal entries of U.
  681. U[np.diag_indices(n)] = np.log(np.diag(T0))
  682. # Recompute superdiagonal entries of U.
  683. # This indexing of this code should be renovated
  684. # when newer np.diagonal() becomes available.
  685. for i in range(n-1):
  686. l1 = T0[i, i]
  687. l2 = T0[i+1, i+1]
  688. t12 = T0[i, i+1]
  689. U[i, i+1] = _logm_superdiag_entry(l1, l2, t12)
  690. # Return the logm of the upper triangular matrix.
  691. if not np.array_equal(U, np.triu(U)):
  692. raise Exception('internal inconsistency')
  693. return U
  694. def _logm_force_nonsingular_triangular_matrix(T, inplace=False):
  695. # The input matrix should be upper triangular.
  696. # The eps is ad hoc and is not meant to be machine precision.
  697. tri_eps = 1e-20
  698. abs_diag = np.absolute(np.diag(T))
  699. if np.any(abs_diag == 0):
  700. exact_singularity_msg = 'The logm input matrix is exactly singular.'
  701. warnings.warn(exact_singularity_msg, LogmExactlySingularWarning)
  702. if not inplace:
  703. T = T.copy()
  704. n = T.shape[0]
  705. for i in range(n):
  706. if not T[i, i]:
  707. T[i, i] = tri_eps
  708. elif np.any(abs_diag < tri_eps):
  709. near_singularity_msg = 'The logm input matrix may be nearly singular.'
  710. warnings.warn(near_singularity_msg, LogmNearlySingularWarning)
  711. return T
  712. def _logm(A):
  713. """
  714. Compute the matrix logarithm.
  715. See the logm docstring in matfuncs.py for more info.
  716. Notes
  717. -----
  718. In this function we look at triangular matrices that are similar
  719. to the input matrix. If any diagonal entry of such a triangular matrix
  720. is exactly zero then the original matrix is singular.
  721. The matrix logarithm does not exist for such matrices,
  722. but in such cases we will pretend that the diagonal entries that are zero
  723. are actually slightly positive by an ad-hoc amount, in the interest
  724. of returning something more useful than NaN. This will cause a warning.
  725. """
  726. A = np.asarray(A)
  727. if len(A.shape) != 2 or A.shape[0] != A.shape[1]:
  728. raise ValueError('expected a square matrix')
  729. # If the input matrix dtype is integer then copy to a float dtype matrix.
  730. if issubclass(A.dtype.type, np.integer):
  731. A = np.asarray(A, dtype=float)
  732. keep_it_real = np.isrealobj(A)
  733. try:
  734. if np.array_equal(A, np.triu(A)):
  735. A = _logm_force_nonsingular_triangular_matrix(A)
  736. if np.min(np.diag(A)) < 0:
  737. A = A.astype(complex)
  738. return _logm_triu(A)
  739. else:
  740. if keep_it_real:
  741. T, Z = schur(A)
  742. if not np.array_equal(T, np.triu(T)):
  743. T, Z = rsf2csf(T, Z)
  744. else:
  745. T, Z = schur(A, output='complex')
  746. T = _logm_force_nonsingular_triangular_matrix(T, inplace=True)
  747. U = _logm_triu(T)
  748. ZH = np.conjugate(Z).T
  749. return Z.dot(U).dot(ZH)
  750. except (SqrtmError, LogmError):
  751. X = np.empty_like(A)
  752. X.fill(np.nan)
  753. return X