UNPKG

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