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