UNPKG

7.35 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createImmutableDenseMatrixClass = void 0;
7
8var _is = require("../../utils/is");
9
10var _object = require("../../utils/object");
11
12var _factory = require("../../utils/factory");
13
14var name = 'ImmutableDenseMatrix';
15var dependencies = ['smaller', 'DenseMatrix'];
16var createImmutableDenseMatrixClass = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
17 var smaller = _ref.smaller,
18 DenseMatrix = _ref.DenseMatrix;
19
20 function ImmutableDenseMatrix(data, datatype) {
21 if (!(this instanceof ImmutableDenseMatrix)) {
22 throw new SyntaxError('Constructor must be called with the new operator');
23 }
24
25 if (datatype && !(0, _is.isString)(datatype)) {
26 throw new Error('Invalid datatype: ' + datatype);
27 }
28
29 if ((0, _is.isMatrix)(data) || (0, _is.isArray)(data)) {
30 // use DenseMatrix implementation
31 var matrix = new DenseMatrix(data, datatype); // internal structures
32
33 this._data = matrix._data;
34 this._size = matrix._size;
35 this._datatype = matrix._datatype;
36 this._min = null;
37 this._max = null;
38 } else if (data && (0, _is.isArray)(data.data) && (0, _is.isArray)(data.size)) {
39 // initialize fields from JSON representation
40 this._data = data.data;
41 this._size = data.size;
42 this._datatype = data.datatype;
43 this._min = typeof data.min !== 'undefined' ? data.min : null;
44 this._max = typeof data.max !== 'undefined' ? data.max : null;
45 } else if (data) {
46 // unsupported type
47 throw new TypeError('Unsupported type of data (' + (0, _is.typeOf)(data) + ')');
48 } else {
49 // nothing provided
50 this._data = [];
51 this._size = [0];
52 this._datatype = datatype;
53 this._min = null;
54 this._max = null;
55 }
56 }
57
58 ImmutableDenseMatrix.prototype = new DenseMatrix();
59 /**
60 * Attach type information
61 */
62
63 ImmutableDenseMatrix.prototype.type = 'ImmutableDenseMatrix';
64 ImmutableDenseMatrix.prototype.isImmutableDenseMatrix = true;
65 /**
66 * Get a subset of the matrix, or replace a subset of the matrix.
67 *
68 * Usage:
69 * const subset = matrix.subset(index) // retrieve subset
70 * const value = matrix.subset(index, replacement) // replace subset
71 *
72 * @param {Index} index
73 * @param {Array | ImmutableDenseMatrix | *} [replacement]
74 * @param {*} [defaultValue=0] Default value, filled in on new entries when
75 * the matrix is resized. If not provided,
76 * new matrix elements will be filled with zeros.
77 */
78
79 ImmutableDenseMatrix.prototype.subset = function (index) {
80 switch (arguments.length) {
81 case 1:
82 {
83 // use base implementation
84 var m = DenseMatrix.prototype.subset.call(this, index); // check result is a matrix
85
86 if ((0, _is.isMatrix)(m)) {
87 // return immutable matrix
88 return new ImmutableDenseMatrix({
89 data: m._data,
90 size: m._size,
91 datatype: m._datatype
92 });
93 }
94
95 return m;
96 }
97 // intentional fall through
98
99 case 2:
100 case 3:
101 throw new Error('Cannot invoke set subset on an Immutable Matrix instance');
102
103 default:
104 throw new SyntaxError('Wrong number of arguments');
105 }
106 };
107 /**
108 * Replace a single element in the matrix.
109 * @param {Number[]} index Zero-based index
110 * @param {*} value
111 * @param {*} [defaultValue] Default value, filled in on new entries when
112 * the matrix is resized. If not provided,
113 * new matrix elements will be left undefined.
114 * @return {ImmutableDenseMatrix} self
115 */
116
117
118 ImmutableDenseMatrix.prototype.set = function () {
119 throw new Error('Cannot invoke set on an Immutable Matrix instance');
120 };
121 /**
122 * Resize the matrix to the given size. Returns a copy of the matrix when
123 * `copy=true`, otherwise return the matrix itself (resize in place).
124 *
125 * @param {Number[]} size The new size the matrix should have.
126 * @param {*} [defaultValue=0] Default value, filled in on new entries.
127 * If not provided, the matrix elements will
128 * be filled with zeros.
129 * @param {boolean} [copy] Return a resized copy of the matrix
130 *
131 * @return {Matrix} The resized matrix
132 */
133
134
135 ImmutableDenseMatrix.prototype.resize = function () {
136 throw new Error('Cannot invoke resize on an Immutable Matrix instance');
137 };
138 /**
139 * Disallows reshaping in favor of immutability.
140 *
141 * @throws {Error} Operation not allowed
142 */
143
144
145 ImmutableDenseMatrix.prototype.reshape = function () {
146 throw new Error('Cannot invoke reshape on an Immutable Matrix instance');
147 };
148 /**
149 * Create a clone of the matrix
150 * @return {ImmutableDenseMatrix} clone
151 */
152
153
154 ImmutableDenseMatrix.prototype.clone = function () {
155 return new ImmutableDenseMatrix({
156 data: (0, _object.clone)(this._data),
157 size: (0, _object.clone)(this._size),
158 datatype: this._datatype
159 });
160 };
161 /**
162 * Get a JSON representation of the matrix
163 * @returns {Object}
164 */
165
166
167 ImmutableDenseMatrix.prototype.toJSON = function () {
168 return {
169 mathjs: 'ImmutableDenseMatrix',
170 data: this._data,
171 size: this._size,
172 datatype: this._datatype
173 };
174 };
175 /**
176 * Generate a matrix from a JSON object
177 * @param {Object} json An object structured like
178 * `{"mathjs": "ImmutableDenseMatrix", data: [], size: []}`,
179 * where mathjs is optional
180 * @returns {ImmutableDenseMatrix}
181 */
182
183
184 ImmutableDenseMatrix.fromJSON = function (json) {
185 return new ImmutableDenseMatrix(json);
186 };
187 /**
188 * Swap rows i and j in Matrix.
189 *
190 * @param {Number} i Matrix row index 1
191 * @param {Number} j Matrix row index 2
192 *
193 * @return {Matrix} The matrix reference
194 */
195
196
197 ImmutableDenseMatrix.prototype.swapRows = function () {
198 throw new Error('Cannot invoke swapRows on an Immutable Matrix instance');
199 };
200 /**
201 * Calculate the minimum value in the set
202 * @return {Number | undefined} min
203 */
204
205
206 ImmutableDenseMatrix.prototype.min = function () {
207 // check min has been calculated before
208 if (this._min === null) {
209 // minimum
210 var m = null; // compute min
211
212 this.forEach(function (v) {
213 if (m === null || smaller(v, m)) {
214 m = v;
215 }
216 });
217 this._min = m !== null ? m : undefined;
218 }
219
220 return this._min;
221 };
222 /**
223 * Calculate the maximum value in the set
224 * @return {Number | undefined} max
225 */
226
227
228 ImmutableDenseMatrix.prototype.max = function () {
229 // check max has been calculated before
230 if (this._max === null) {
231 // maximum
232 var m = null; // compute max
233
234 this.forEach(function (v) {
235 if (m === null || smaller(m, v)) {
236 m = v;
237 }
238 });
239 this._max = m !== null ? m : undefined;
240 }
241
242 return this._max;
243 };
244
245 return ImmutableDenseMatrix;
246}, {
247 isClass: true
248});
249exports.createImmutableDenseMatrixClass = createImmutableDenseMatrixClass;
\No newline at end of file