1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createMatrixClass = void 0;
|
7 |
|
8 | var _factory = require("../../utils/factory");
|
9 |
|
10 | var name = 'Matrix';
|
11 | var dependencies = [];
|
12 | var createMatrixClass =
|
13 | /* #__PURE__ */
|
14 | (0, _factory.factory)(name, dependencies, function () {
|
15 | /**
|
16 | * @constructor Matrix
|
17 | *
|
18 | * A Matrix is a wrapper around an Array. A matrix can hold a multi dimensional
|
19 | * array. A matrix can be constructed as:
|
20 | *
|
21 | * let matrix = math.matrix(data)
|
22 | *
|
23 | * Matrix contains the functions to resize, get and set values, get the size,
|
24 | * clone the matrix and to convert the matrix to a vector, array, or scalar.
|
25 | * Furthermore, one can iterate over the matrix using map and forEach.
|
26 | * The internal Array of the Matrix can be accessed using the function valueOf.
|
27 | *
|
28 | * Example usage:
|
29 | *
|
30 | * let matrix = math.matrix([[1, 2], [3, 4]])
|
31 | * matix.size() // [2, 2]
|
32 | * matrix.resize([3, 2], 5)
|
33 | * matrix.valueOf() // [[1, 2], [3, 4], [5, 5]]
|
34 | * matrix.subset([1,2]) // 3 (indexes are zero-based)
|
35 | *
|
36 | */
|
37 | function Matrix() {
|
38 | if (!(this instanceof Matrix)) {
|
39 | throw new SyntaxError('Constructor must be called with the new operator');
|
40 | }
|
41 | }
|
42 | /**
|
43 | * Attach type information
|
44 | */
|
45 |
|
46 |
|
47 | Matrix.prototype.type = 'Matrix';
|
48 | Matrix.prototype.isMatrix = true;
|
49 | /**
|
50 | * Get the Matrix storage constructor for the given format.
|
51 | *
|
52 | * @param {string} format The Matrix storage format.
|
53 | *
|
54 | * @return {Function} The Matrix storage constructor.
|
55 | */
|
56 |
|
57 | Matrix.storage = function (format) {
|
58 | // TODO: deprecated since v6.0.0. Clean up some day
|
59 | throw new Error('Matrix.storage is deprecated since v6.0.0. ' + 'Use the factory function math.matrix instead.');
|
60 | };
|
61 | /**
|
62 | * Get the storage format used by the matrix.
|
63 | *
|
64 | * Usage:
|
65 | * const format = matrix.storage() // retrieve storage format
|
66 | *
|
67 | * @return {string} The storage format.
|
68 | */
|
69 |
|
70 |
|
71 | Matrix.prototype.storage = function () {
|
72 | // must be implemented by each of the Matrix implementations
|
73 | throw new Error('Cannot invoke storage on a Matrix interface');
|
74 | };
|
75 | /**
|
76 | * Get the datatype of the data stored in the matrix.
|
77 | *
|
78 | * Usage:
|
79 | * const format = matrix.datatype() // retrieve matrix datatype
|
80 | *
|
81 | * @return {string} The datatype.
|
82 | */
|
83 |
|
84 |
|
85 | Matrix.prototype.datatype = function () {
|
86 | // must be implemented by each of the Matrix implementations
|
87 | throw new Error('Cannot invoke datatype on a Matrix interface');
|
88 | };
|
89 | /**
|
90 | * Create a new Matrix With the type of the current matrix instance
|
91 | * @param {Array | Object} data
|
92 | * @param {string} [datatype]
|
93 | */
|
94 |
|
95 |
|
96 | Matrix.prototype.create = function (data, datatype) {
|
97 | throw new Error('Cannot invoke create on a Matrix interface');
|
98 | };
|
99 | /**
|
100 | * Get a subset of the matrix, or replace a subset of the matrix.
|
101 | *
|
102 | * Usage:
|
103 | * const subset = matrix.subset(index) // retrieve subset
|
104 | * const value = matrix.subset(index, replacement) // replace subset
|
105 | *
|
106 | * @param {Index} index
|
107 | * @param {Array | Matrix | *} [replacement]
|
108 | * @param {*} [defaultValue=0] Default value, filled in on new entries when
|
109 | * the matrix is resized. If not provided,
|
110 | * new matrix elements will be filled with zeros.
|
111 | */
|
112 |
|
113 |
|
114 | Matrix.prototype.subset = function (index, replacement, defaultValue) {
|
115 | // must be implemented by each of the Matrix implementations
|
116 | throw new Error('Cannot invoke subset on a Matrix interface');
|
117 | };
|
118 | /**
|
119 | * Get a single element from the matrix.
|
120 | * @param {number[]} index Zero-based index
|
121 | * @return {*} value
|
122 | */
|
123 |
|
124 |
|
125 | Matrix.prototype.get = function (index) {
|
126 | // must be implemented by each of the Matrix implementations
|
127 | throw new Error('Cannot invoke get on a Matrix interface');
|
128 | };
|
129 | /**
|
130 | * Replace a single element in the matrix.
|
131 | * @param {number[]} index Zero-based index
|
132 | * @param {*} value
|
133 | * @param {*} [defaultValue] Default value, filled in on new entries when
|
134 | * the matrix is resized. If not provided,
|
135 | * new matrix elements will be left undefined.
|
136 | * @return {Matrix} self
|
137 | */
|
138 |
|
139 |
|
140 | Matrix.prototype.set = function (index, value, defaultValue) {
|
141 | // must be implemented by each of the Matrix implementations
|
142 | throw new Error('Cannot invoke set on a Matrix interface');
|
143 | };
|
144 | /**
|
145 | * Resize the matrix to the given size. Returns a copy of the matrix when
|
146 | * `copy=true`, otherwise return the matrix itself (resize in place).
|
147 | *
|
148 | * @param {number[]} size The new size the matrix should have.
|
149 | * @param {*} [defaultValue=0] Default value, filled in on new entries.
|
150 | * If not provided, the matrix elements will
|
151 | * be filled with zeros.
|
152 | * @param {boolean} [copy] Return a resized copy of the matrix
|
153 | *
|
154 | * @return {Matrix} The resized matrix
|
155 | */
|
156 |
|
157 |
|
158 | Matrix.prototype.resize = function (size, defaultValue) {
|
159 | // must be implemented by each of the Matrix implementations
|
160 | throw new Error('Cannot invoke resize on a Matrix interface');
|
161 | };
|
162 | /**
|
163 | * Reshape the matrix to the given size. Returns a copy of the matrix when
|
164 | * `copy=true`, otherwise return the matrix itself (reshape in place).
|
165 | *
|
166 | * @param {number[]} size The new size the matrix should have.
|
167 | * @param {boolean} [copy] Return a reshaped copy of the matrix
|
168 | *
|
169 | * @return {Matrix} The reshaped matrix
|
170 | */
|
171 |
|
172 |
|
173 | Matrix.prototype.reshape = function (size, defaultValue) {
|
174 | // must be implemented by each of the Matrix implementations
|
175 | throw new Error('Cannot invoke reshape on a Matrix interface');
|
176 | };
|
177 | /**
|
178 | * Create a clone of the matrix
|
179 | * @return {Matrix} clone
|
180 | */
|
181 |
|
182 |
|
183 | Matrix.prototype.clone = function () {
|
184 | // must be implemented by each of the Matrix implementations
|
185 | throw new Error('Cannot invoke clone on a Matrix interface');
|
186 | };
|
187 | /**
|
188 | * Retrieve the size of the matrix.
|
189 | * @returns {number[]} size
|
190 | */
|
191 |
|
192 |
|
193 | Matrix.prototype.size = function () {
|
194 | // must be implemented by each of the Matrix implementations
|
195 | throw new Error('Cannot invoke size on a Matrix interface');
|
196 | };
|
197 | /**
|
198 | * Create a new matrix with the results of the callback function executed on
|
199 | * each entry of the matrix.
|
200 | * @param {Function} callback The callback function is invoked with three
|
201 | * parameters: the value of the element, the index
|
202 | * of the element, and the Matrix being traversed.
|
203 | * @param {boolean} [skipZeros] Invoke callback function for non-zero values only.
|
204 | *
|
205 | * @return {Matrix} matrix
|
206 | */
|
207 |
|
208 |
|
209 | Matrix.prototype.map = function (callback, skipZeros) {
|
210 | // must be implemented by each of the Matrix implementations
|
211 | throw new Error('Cannot invoke map on a Matrix interface');
|
212 | };
|
213 | /**
|
214 | * Execute a callback function on each entry of the matrix.
|
215 | * @param {Function} callback The callback function is invoked with three
|
216 | * parameters: the value of the element, the index
|
217 | * of the element, and the Matrix being traversed.
|
218 | */
|
219 |
|
220 |
|
221 | Matrix.prototype.forEach = function (callback) {
|
222 | // must be implemented by each of the Matrix implementations
|
223 | throw new Error('Cannot invoke forEach on a Matrix interface');
|
224 | };
|
225 | /**
|
226 | * Create an Array with a copy of the data of the Matrix
|
227 | * @returns {Array} array
|
228 | */
|
229 |
|
230 |
|
231 | Matrix.prototype.toArray = function () {
|
232 | // must be implemented by each of the Matrix implementations
|
233 | throw new Error('Cannot invoke toArray on a Matrix interface');
|
234 | };
|
235 | /**
|
236 | * Get the primitive value of the Matrix: a multidimensional array
|
237 | * @returns {Array} array
|
238 | */
|
239 |
|
240 |
|
241 | Matrix.prototype.valueOf = function () {
|
242 | // must be implemented by each of the Matrix implementations
|
243 | throw new Error('Cannot invoke valueOf on a Matrix interface');
|
244 | };
|
245 | /**
|
246 | * Get a string representation of the matrix, with optional formatting options.
|
247 | * @param {Object | number | Function} [options] Formatting options. See
|
248 | * lib/utils/number:format for a
|
249 | * description of the available
|
250 | * options.
|
251 | * @returns {string} str
|
252 | */
|
253 |
|
254 |
|
255 | Matrix.prototype.format = function (options) {
|
256 | // must be implemented by each of the Matrix implementations
|
257 | throw new Error('Cannot invoke format on a Matrix interface');
|
258 | };
|
259 | /**
|
260 | * Get a string representation of the matrix
|
261 | * @returns {string} str
|
262 | */
|
263 |
|
264 |
|
265 | Matrix.prototype.toString = function () {
|
266 | // must be implemented by each of the Matrix implementations
|
267 | throw new Error('Cannot invoke toString on a Matrix interface');
|
268 | };
|
269 |
|
270 | return Matrix;
|
271 | }, {
|
272 | isClass: true
|
273 | });
|
274 | exports.createMatrixClass = createMatrixClass; |
\ | No newline at end of file |