_matfuncs_inv_ssq.py 27 KB

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