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