1 | import { isBigNumber } from '../../utils/is';
|
2 | import { resize } from '../../utils/array';
|
3 | import { isInteger } from '../../utils/number';
|
4 | import { factory } from '../../utils/factory';
|
5 | var name = 'identity';
|
6 | var dependencies = ['typed', 'config', 'matrix', 'BigNumber', 'DenseMatrix', 'SparseMatrix'];
|
7 | export var createIdentity = 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 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
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 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | function _identity(rows, cols, format) {
|
104 |
|
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];
|
120 |
|
121 | if (format) {
|
122 |
|
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 | }
|
133 |
|
134 |
|
135 | var res = resize([], size, defaultValue);
|
136 |
|
137 | var minimum = rows < cols ? rows : cols;
|
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 |