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