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