1 | import { isArray, isMatrix, isString, typeOf } from '../../utils/is.js';
|
2 | import { clone } from '../../utils/object.js';
|
3 | import { factory } from '../../utils/factory.js';
|
4 | var name = 'ImmutableDenseMatrix';
|
5 | var dependencies = ['smaller', 'DenseMatrix'];
|
6 | export var createImmutableDenseMatrixClass = factory(name, dependencies, _ref => {
|
7 | var {
|
8 | smaller,
|
9 | DenseMatrix
|
10 | } = _ref;
|
11 |
|
12 | function ImmutableDenseMatrix(data, datatype) {
|
13 | if (!(this instanceof ImmutableDenseMatrix)) {
|
14 | throw new SyntaxError('Constructor must be called with the new operator');
|
15 | }
|
16 |
|
17 | if (datatype && !isString(datatype)) {
|
18 | throw new Error('Invalid datatype: ' + datatype);
|
19 | }
|
20 |
|
21 | if (isMatrix(data) || isArray(data)) {
|
22 |
|
23 | var matrix = new DenseMatrix(data, datatype);
|
24 |
|
25 | this._data = matrix._data;
|
26 | this._size = matrix._size;
|
27 | this._datatype = matrix._datatype;
|
28 | this._min = null;
|
29 | this._max = null;
|
30 | } else if (data && isArray(data.data) && isArray(data.size)) {
|
31 |
|
32 | this._data = data.data;
|
33 | this._size = data.size;
|
34 | this._datatype = data.datatype;
|
35 | this._min = typeof data.min !== 'undefined' ? data.min : null;
|
36 | this._max = typeof data.max !== 'undefined' ? data.max : null;
|
37 | } else if (data) {
|
38 |
|
39 | throw new TypeError('Unsupported type of data (' + typeOf(data) + ')');
|
40 | } else {
|
41 |
|
42 | this._data = [];
|
43 | this._size = [0];
|
44 | this._datatype = datatype;
|
45 | this._min = null;
|
46 | this._max = null;
|
47 | }
|
48 | }
|
49 |
|
50 | ImmutableDenseMatrix.prototype = new DenseMatrix();
|
51 | |
52 |
|
53 |
|
54 |
|
55 | ImmutableDenseMatrix.prototype.type = 'ImmutableDenseMatrix';
|
56 | ImmutableDenseMatrix.prototype.isImmutableDenseMatrix = true;
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | ImmutableDenseMatrix.prototype.subset = function (index) {
|
72 | switch (arguments.length) {
|
73 | case 1:
|
74 | {
|
75 |
|
76 | var m = DenseMatrix.prototype.subset.call(this, index);
|
77 |
|
78 | if (isMatrix(m)) {
|
79 |
|
80 | return new ImmutableDenseMatrix({
|
81 | data: m._data,
|
82 | size: m._size,
|
83 | datatype: m._datatype
|
84 | });
|
85 | }
|
86 |
|
87 | return m;
|
88 | }
|
89 |
|
90 |
|
91 | case 2:
|
92 | case 3:
|
93 | throw new Error('Cannot invoke set subset on an Immutable Matrix instance');
|
94 |
|
95 | default:
|
96 | throw new SyntaxError('Wrong number of arguments');
|
97 | }
|
98 | };
|
99 | |
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | ImmutableDenseMatrix.prototype.set = function () {
|
111 | throw new Error('Cannot invoke set on an Immutable Matrix instance');
|
112 | };
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | ImmutableDenseMatrix.prototype.resize = function () {
|
128 | throw new Error('Cannot invoke resize on an Immutable Matrix instance');
|
129 | };
|
130 | |
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | ImmutableDenseMatrix.prototype.reshape = function () {
|
138 | throw new Error('Cannot invoke reshape on an Immutable Matrix instance');
|
139 | };
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | ImmutableDenseMatrix.prototype.clone = function () {
|
147 | return new ImmutableDenseMatrix({
|
148 | data: clone(this._data),
|
149 | size: clone(this._size),
|
150 | datatype: this._datatype
|
151 | });
|
152 | };
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | ImmutableDenseMatrix.prototype.toJSON = function () {
|
160 | return {
|
161 | mathjs: 'ImmutableDenseMatrix',
|
162 | data: this._data,
|
163 | size: this._size,
|
164 | datatype: this._datatype
|
165 | };
|
166 | };
|
167 | |
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | ImmutableDenseMatrix.fromJSON = function (json) {
|
177 | return new ImmutableDenseMatrix(json);
|
178 | };
|
179 | |
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | ImmutableDenseMatrix.prototype.swapRows = function () {
|
190 | throw new Error('Cannot invoke swapRows on an Immutable Matrix instance');
|
191 | };
|
192 | |
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | ImmutableDenseMatrix.prototype.min = function () {
|
199 |
|
200 | if (this._min === null) {
|
201 |
|
202 | var m = null;
|
203 |
|
204 | this.forEach(function (v) {
|
205 | if (m === null || smaller(v, m)) {
|
206 | m = v;
|
207 | }
|
208 | });
|
209 | this._min = m !== null ? m : undefined;
|
210 | }
|
211 |
|
212 | return this._min;
|
213 | };
|
214 | |
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | ImmutableDenseMatrix.prototype.max = function () {
|
221 |
|
222 | if (this._max === null) {
|
223 |
|
224 | var m = null;
|
225 |
|
226 | this.forEach(function (v) {
|
227 | if (m === null || smaller(m, v)) {
|
228 | m = v;
|
229 | }
|
230 | });
|
231 | this._max = m !== null ? m : undefined;
|
232 | }
|
233 |
|
234 | return this._max;
|
235 | };
|
236 |
|
237 | return ImmutableDenseMatrix;
|
238 | }, {
|
239 | isClass: true
|
240 | }); |
\ | No newline at end of file |