UNPKG

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