UNPKG

7.06 kBJavaScriptView Raw
1import { isArray, isMatrix, isString, typeOf } from '../../utils/is.js';
2import { clone } from '../../utils/object.js';
3import { factory } from '../../utils/factory.js';
4var name = 'ImmutableDenseMatrix';
5var dependencies = ['smaller', 'DenseMatrix'];
6export var createImmutableDenseMatrixClass = /* #__PURE__ */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 // use DenseMatrix implementation
23 var matrix = new DenseMatrix(data, datatype); // internal structures
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 // initialize fields from JSON representation
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 // unsupported type
39 throw new TypeError('Unsupported type of data (' + typeOf(data) + ')');
40 } else {
41 // nothing provided
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 * Attach type information
53 */
54
55 ImmutableDenseMatrix.prototype.type = 'ImmutableDenseMatrix';
56 ImmutableDenseMatrix.prototype.isImmutableDenseMatrix = true;
57 /**
58 * Get a subset of the matrix, or replace a subset of the matrix.
59 *
60 * Usage:
61 * const subset = matrix.subset(index) // retrieve subset
62 * const value = matrix.subset(index, replacement) // replace subset
63 *
64 * @param {Index} index
65 * @param {Array | ImmutableDenseMatrix | *} [replacement]
66 * @param {*} [defaultValue=0] Default value, filled in on new entries when
67 * the matrix is resized. If not provided,
68 * new matrix elements will be filled with zeros.
69 */
70
71 ImmutableDenseMatrix.prototype.subset = function (index) {
72 switch (arguments.length) {
73 case 1:
74 {
75 // use base implementation
76 var m = DenseMatrix.prototype.subset.call(this, index); // check result is a matrix
77
78 if (isMatrix(m)) {
79 // return immutable matrix
80 return new ImmutableDenseMatrix({
81 data: m._data,
82 size: m._size,
83 datatype: m._datatype
84 });
85 }
86
87 return m;
88 }
89 // intentional fall through
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 * Replace a single element in the matrix.
101 * @param {Number[]} index Zero-based index
102 * @param {*} value
103 * @param {*} [defaultValue] Default value, filled in on new entries when
104 * the matrix is resized. If not provided,
105 * new matrix elements will be left undefined.
106 * @return {ImmutableDenseMatrix} self
107 */
108
109
110 ImmutableDenseMatrix.prototype.set = function () {
111 throw new Error('Cannot invoke set on an Immutable Matrix instance');
112 };
113 /**
114 * Resize the matrix to the given size. Returns a copy of the matrix when
115 * `copy=true`, otherwise return the matrix itself (resize in place).
116 *
117 * @param {Number[]} size The new size the matrix should have.
118 * @param {*} [defaultValue=0] Default value, filled in on new entries.
119 * If not provided, the matrix elements will
120 * be filled with zeros.
121 * @param {boolean} [copy] Return a resized copy of the matrix
122 *
123 * @return {Matrix} The resized matrix
124 */
125
126
127 ImmutableDenseMatrix.prototype.resize = function () {
128 throw new Error('Cannot invoke resize on an Immutable Matrix instance');
129 };
130 /**
131 * Disallows reshaping in favor of immutability.
132 *
133 * @throws {Error} Operation not allowed
134 */
135
136
137 ImmutableDenseMatrix.prototype.reshape = function () {
138 throw new Error('Cannot invoke reshape on an Immutable Matrix instance');
139 };
140 /**
141 * Create a clone of the matrix
142 * @return {ImmutableDenseMatrix} clone
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 * Get a JSON representation of the matrix
155 * @returns {Object}
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 * Generate a matrix from a JSON object
169 * @param {Object} json An object structured like
170 * `{"mathjs": "ImmutableDenseMatrix", data: [], size: []}`,
171 * where mathjs is optional
172 * @returns {ImmutableDenseMatrix}
173 */
174
175
176 ImmutableDenseMatrix.fromJSON = function (json) {
177 return new ImmutableDenseMatrix(json);
178 };
179 /**
180 * Swap rows i and j in Matrix.
181 *
182 * @param {Number} i Matrix row index 1
183 * @param {Number} j Matrix row index 2
184 *
185 * @return {Matrix} The matrix reference
186 */
187
188
189 ImmutableDenseMatrix.prototype.swapRows = function () {
190 throw new Error('Cannot invoke swapRows on an Immutable Matrix instance');
191 };
192 /**
193 * Calculate the minimum value in the set
194 * @return {Number | undefined} min
195 */
196
197
198 ImmutableDenseMatrix.prototype.min = function () {
199 // check min has been calculated before
200 if (this._min === null) {
201 // minimum
202 var m = null; // compute min
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 * Calculate the maximum value in the set
216 * @return {Number | undefined} max
217 */
218
219
220 ImmutableDenseMatrix.prototype.max = function () {
221 // check max has been calculated before
222 if (this._max === null) {
223 // maximum
224 var m = null; // compute max
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