identity.js (4613B)
1 import { isBigNumber } from '../../utils/is.js'; 2 import { resize } from '../../utils/array.js'; 3 import { isInteger } from '../../utils/number.js'; 4 import { factory } from '../../utils/factory.js'; 5 var name = 'identity'; 6 var dependencies = ['typed', 'config', 'matrix', 'BigNumber', 'DenseMatrix', 'SparseMatrix']; 7 export var createIdentity = /* #__PURE__ */factory(name, dependencies, _ref => { 8 var { 9 typed, 10 config, 11 matrix, 12 BigNumber, 13 DenseMatrix, 14 SparseMatrix 15 } = _ref; 16 17 /** 18 * Create a 2-dimensional identity matrix with size m x n or n x n. 19 * The matrix has ones on the diagonal and zeros elsewhere. 20 * 21 * Syntax: 22 * 23 * math.identity(n) 24 * math.identity(n, format) 25 * math.identity(m, n) 26 * math.identity(m, n, format) 27 * math.identity([m, n]) 28 * math.identity([m, n], format) 29 * 30 * Examples: 31 * 32 * math.identity(3) // returns [[1, 0, 0], [0, 1, 0], [0, 0, 1]] 33 * math.identity(3, 2) // returns [[1, 0], [0, 1], [0, 0]] 34 * 35 * const A = [[1, 2, 3], [4, 5, 6]] 36 * math.identity(math.size(A)) // returns [[1, 0, 0], [0, 1, 0]] 37 * 38 * See also: 39 * 40 * diag, ones, zeros, size, range 41 * 42 * @param {...number | Matrix | Array} size The size for the matrix 43 * @param {string} [format] The Matrix storage format 44 * 45 * @return {Matrix | Array | number} A matrix with ones on the diagonal. 46 */ 47 return typed(name, { 48 '': function _() { 49 return config.matrix === 'Matrix' ? matrix([]) : []; 50 }, 51 string: function string(format) { 52 return matrix(format); 53 }, 54 'number | BigNumber': function numberBigNumber(rows) { 55 return _identity(rows, rows, config.matrix === 'Matrix' ? 'dense' : undefined); 56 }, 57 'number | BigNumber, string': function numberBigNumberString(rows, format) { 58 return _identity(rows, rows, format); 59 }, 60 'number | BigNumber, number | BigNumber': function numberBigNumberNumberBigNumber(rows, cols) { 61 return _identity(rows, cols, config.matrix === 'Matrix' ? 'dense' : undefined); 62 }, 63 'number | BigNumber, number | BigNumber, string': function numberBigNumberNumberBigNumberString(rows, cols, format) { 64 return _identity(rows, cols, format); 65 }, 66 Array: function Array(size) { 67 return _identityVector(size); 68 }, 69 'Array, string': function ArrayString(size, format) { 70 return _identityVector(size, format); 71 }, 72 Matrix: function Matrix(size) { 73 return _identityVector(size.valueOf(), size.storage()); 74 }, 75 'Matrix, string': function MatrixString(size, format) { 76 return _identityVector(size.valueOf(), format); 77 } 78 }); 79 80 function _identityVector(size, format) { 81 switch (size.length) { 82 case 0: 83 return format ? matrix(format) : []; 84 85 case 1: 86 return _identity(size[0], size[0], format); 87 88 case 2: 89 return _identity(size[0], size[1], format); 90 91 default: 92 throw new Error('Vector containing two values expected'); 93 } 94 } 95 /** 96 * Create an identity matrix 97 * @param {number | BigNumber} rows 98 * @param {number | BigNumber} cols 99 * @param {string} [format] 100 * @returns {Matrix} 101 * @private 102 */ 103 104 105 function _identity(rows, cols, format) { 106 // BigNumber constructor with the right precision 107 var Big = isBigNumber(rows) || isBigNumber(cols) ? BigNumber : null; 108 if (isBigNumber(rows)) rows = rows.toNumber(); 109 if (isBigNumber(cols)) cols = cols.toNumber(); 110 111 if (!isInteger(rows) || rows < 1) { 112 throw new Error('Parameters in function identity must be positive integers'); 113 } 114 115 if (!isInteger(cols) || cols < 1) { 116 throw new Error('Parameters in function identity must be positive integers'); 117 } 118 119 var one = Big ? new BigNumber(1) : 1; 120 var defaultValue = Big ? new Big(0) : 0; 121 var size = [rows, cols]; // check we need to return a matrix 122 123 if (format) { 124 // create diagonal matrix (use optimized implementation for storage format) 125 if (format === 'sparse') { 126 return SparseMatrix.diagonal(size, one, 0, defaultValue); 127 } 128 129 if (format === 'dense') { 130 return DenseMatrix.diagonal(size, one, 0, defaultValue); 131 } 132 133 throw new TypeError("Unknown matrix type \"".concat(format, "\"")); 134 } // create and resize array 135 136 137 var res = resize([], size, defaultValue); // fill in ones on the diagonal 138 139 var minimum = rows < cols ? rows : cols; // fill diagonal 140 141 for (var d = 0; d < minimum; d++) { 142 res[d][d] = one; 143 } 144 145 return res; 146 } 147 });