expm.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { isSparseMatrix } from '../../utils/is.js';
  2. import { format } from '../../utils/string.js';
  3. import { factory } from '../../utils/factory.js';
  4. var name = 'expm';
  5. var dependencies = ['typed', 'abs', 'add', 'identity', 'inv', 'multiply'];
  6. export var createExpm = /* #__PURE__ */factory(name, dependencies, _ref => {
  7. var {
  8. typed,
  9. abs,
  10. add,
  11. identity,
  12. inv,
  13. multiply
  14. } = _ref;
  15. /**
  16. * Compute the matrix exponential, expm(A) = e^A. The matrix must be square.
  17. * Not to be confused with exp(a), which performs element-wise
  18. * exponentiation.
  19. *
  20. * The exponential is calculated using the Padé approximant with scaling and
  21. * squaring; see "Nineteen Dubious Ways to Compute the Exponential of a
  22. * Matrix," by Moler and Van Loan.
  23. *
  24. * Syntax:
  25. *
  26. * math.expm(x)
  27. *
  28. * Examples:
  29. *
  30. * const A = [[0,2],[0,0]]
  31. * math.expm(A) // returns [[1,2],[0,1]]
  32. *
  33. * See also:
  34. *
  35. * exp
  36. *
  37. * @param {Matrix} x A square Matrix
  38. * @return {Matrix} The exponential of x
  39. */
  40. return typed(name, {
  41. Matrix: function Matrix(A) {
  42. // Check matrix size
  43. var size = A.size();
  44. if (size.length !== 2 || size[0] !== size[1]) {
  45. throw new RangeError('Matrix must be square ' + '(size: ' + format(size) + ')');
  46. }
  47. var n = size[0];
  48. // Desired accuracy of the approximant (The actual accuracy
  49. // will be affected by round-off error)
  50. var eps = 1e-15;
  51. // The Padé approximant is not so accurate when the values of A
  52. // are "large", so scale A by powers of two. Then compute the
  53. // exponential, and square the result repeatedly according to
  54. // the identity e^A = (e^(A/m))^m
  55. // Compute infinity-norm of A, ||A||, to see how "big" it is
  56. var infNorm = infinityNorm(A);
  57. // Find the optimal scaling factor and number of terms in the
  58. // Padé approximant to reach the desired accuracy
  59. var params = findParams(infNorm, eps);
  60. var q = params.q;
  61. var j = params.j;
  62. // The Pade approximation to e^A is:
  63. // Rqq(A) = Dqq(A) ^ -1 * Nqq(A)
  64. // where
  65. // Nqq(A) = sum(i=0, q, (2q-i)!p! / [ (2q)!i!(q-i)! ] A^i
  66. // Dqq(A) = sum(i=0, q, (2q-i)!q! / [ (2q)!i!(q-i)! ] (-A)^i
  67. // Scale A by 1 / 2^j
  68. var Apos = multiply(A, Math.pow(2, -j));
  69. // The i=0 term is just the identity matrix
  70. var N = identity(n);
  71. var D = identity(n);
  72. // Initialization (i=0)
  73. var factor = 1;
  74. // Initialization (i=1)
  75. var AposToI = Apos; // Cloning not necessary
  76. var alternate = -1;
  77. for (var i = 1; i <= q; i++) {
  78. if (i > 1) {
  79. AposToI = multiply(AposToI, Apos);
  80. alternate = -alternate;
  81. }
  82. factor = factor * (q - i + 1) / ((2 * q - i + 1) * i);
  83. N = add(N, multiply(factor, AposToI));
  84. D = add(D, multiply(factor * alternate, AposToI));
  85. }
  86. var R = multiply(inv(D), N);
  87. // Square j times
  88. for (var _i = 0; _i < j; _i++) {
  89. R = multiply(R, R);
  90. }
  91. return isSparseMatrix(A) ? A.createSparseMatrix(R) : R;
  92. }
  93. });
  94. function infinityNorm(A) {
  95. var n = A.size()[0];
  96. var infNorm = 0;
  97. for (var i = 0; i < n; i++) {
  98. var rowSum = 0;
  99. for (var j = 0; j < n; j++) {
  100. rowSum += abs(A.get([i, j]));
  101. }
  102. infNorm = Math.max(rowSum, infNorm);
  103. }
  104. return infNorm;
  105. }
  106. /**
  107. * Find the best parameters for the Pade approximant given
  108. * the matrix norm and desired accuracy. Returns the first acceptable
  109. * combination in order of increasing computational load.
  110. */
  111. function findParams(infNorm, eps) {
  112. var maxSearchSize = 30;
  113. for (var k = 0; k < maxSearchSize; k++) {
  114. for (var q = 0; q <= k; q++) {
  115. var j = k - q;
  116. if (errorEstimate(infNorm, q, j) < eps) {
  117. return {
  118. q,
  119. j
  120. };
  121. }
  122. }
  123. }
  124. throw new Error('Could not find acceptable parameters to compute the matrix exponential (try increasing maxSearchSize in expm.js)');
  125. }
  126. /**
  127. * Returns the estimated error of the Pade approximant for the given
  128. * parameters.
  129. */
  130. function errorEstimate(infNorm, q, j) {
  131. var qfac = 1;
  132. for (var i = 2; i <= q; i++) {
  133. qfac *= i;
  134. }
  135. var twoqfac = qfac;
  136. for (var _i2 = q + 1; _i2 <= 2 * q; _i2++) {
  137. twoqfac *= _i2;
  138. }
  139. var twoqp1fac = twoqfac * (2 * q + 1);
  140. return 8.0 * Math.pow(infNorm / Math.pow(2, j), 2 * q) * qfac * qfac / (twoqfac * twoqp1fac);
  141. }
  142. });