sqrtm.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createSqrtm = void 0;
  6. var _is = require("../../utils/is.js");
  7. var _string = require("../../utils/string.js");
  8. var _array = require("../../utils/array.js");
  9. var _factory = require("../../utils/factory.js");
  10. var name = 'sqrtm';
  11. var dependencies = ['typed', 'abs', 'add', 'multiply', 'map', 'sqrt', 'subtract', 'inv', 'size', 'max', 'identity'];
  12. var createSqrtm = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  13. var typed = _ref.typed,
  14. abs = _ref.abs,
  15. add = _ref.add,
  16. multiply = _ref.multiply,
  17. map = _ref.map,
  18. sqrt = _ref.sqrt,
  19. subtract = _ref.subtract,
  20. inv = _ref.inv,
  21. size = _ref.size,
  22. max = _ref.max,
  23. identity = _ref.identity;
  24. var _maxIterations = 1e3;
  25. var _tolerance = 1e-6;
  26. /**
  27. * Calculate the principal square root matrix using the Denman–Beavers iterative method
  28. *
  29. * https://en.wikipedia.org/wiki/Square_root_of_a_matrix#By_Denman–Beavers_iteration
  30. *
  31. * @param {Array | Matrix} A The square matrix `A`
  32. * @return {Array | Matrix} The principal square root of matrix `A`
  33. * @private
  34. */
  35. function _denmanBeavers(A) {
  36. var error;
  37. var iterations = 0;
  38. var Y = A;
  39. var Z = identity(size(A));
  40. do {
  41. var Yk = Y;
  42. Y = multiply(0.5, add(Yk, inv(Z)));
  43. Z = multiply(0.5, add(Z, inv(Yk)));
  44. error = max(abs(subtract(Y, Yk)));
  45. if (error > _tolerance && ++iterations > _maxIterations) {
  46. throw new Error('computing square root of matrix: iterative method could not converge');
  47. }
  48. } while (error > _tolerance);
  49. return Y;
  50. }
  51. /**
  52. * Calculate the principal square root of a square matrix.
  53. * The principal square root matrix `X` of another matrix `A` is such that `X * X = A`.
  54. *
  55. * https://en.wikipedia.org/wiki/Square_root_of_a_matrix
  56. *
  57. * Syntax:
  58. *
  59. * X = math.sqrtm(A)
  60. *
  61. * Examples:
  62. *
  63. * math.sqrtm([[33, 24], [48, 57]]) // returns [[5, 2], [4, 7]]
  64. *
  65. * See also:
  66. *
  67. * sqrt, pow
  68. *
  69. * @param {Array | Matrix} A The square matrix `A`
  70. * @return {Array | Matrix} The principal square root of matrix `A`
  71. */
  72. return typed(name, {
  73. 'Array | Matrix': function ArrayMatrix(A) {
  74. var size = (0, _is.isMatrix)(A) ? A.size() : (0, _array.arraySize)(A);
  75. switch (size.length) {
  76. case 1:
  77. // Single element Array | Matrix
  78. if (size[0] === 1) {
  79. return map(A, sqrt);
  80. } else {
  81. throw new RangeError('Matrix must be square ' + '(size: ' + (0, _string.format)(size) + ')');
  82. }
  83. case 2:
  84. {
  85. // Two-dimensional Array | Matrix
  86. var rows = size[0];
  87. var cols = size[1];
  88. if (rows === cols) {
  89. return _denmanBeavers(A);
  90. } else {
  91. throw new RangeError('Matrix must be square ' + '(size: ' + (0, _string.format)(size) + ')');
  92. }
  93. }
  94. default:
  95. // Multi dimensional array
  96. throw new RangeError('Matrix must be at most two dimensional ' + '(size: ' + (0, _string.format)(size) + ')');
  97. }
  98. }
  99. });
  100. });
  101. exports.createSqrtm = createSqrtm;