1 | import { isArray, isMatrix, isString, typeOf } from '../../utils/is'
|
2 | import { clone } from '../../utils/object'
|
3 | import { factory } from '../../utils/factory'
|
4 |
|
5 | const name = 'ImmutableDenseMatrix'
|
6 | const dependencies = [
|
7 | 'smaller',
|
8 | 'DenseMatrix'
|
9 | ]
|
10 |
|
11 | export const createImmutableDenseMatrixClass = factory(name, dependencies, ({ smaller, DenseMatrix }) => {
|
12 | function ImmutableDenseMatrix (data, datatype) {
|
13 | if (!(this instanceof ImmutableDenseMatrix)) { throw new SyntaxError('Constructor must be called with the new operator') }
|
14 | if (datatype && !isString(datatype)) { throw new Error('Invalid datatype: ' + datatype) }
|
15 |
|
16 | if (isMatrix(data) || isArray(data)) {
|
17 |
|
18 | const matrix = new DenseMatrix(data, datatype)
|
19 |
|
20 | this._data = matrix._data
|
21 | this._size = matrix._size
|
22 | this._datatype = matrix._datatype
|
23 | this._min = null
|
24 | this._max = null
|
25 | } else if (data && isArray(data.data) && isArray(data.size)) {
|
26 |
|
27 | this._data = data.data
|
28 | this._size = data.size
|
29 | this._datatype = data.datatype
|
30 | this._min = typeof data.min !== 'undefined' ? data.min : null
|
31 | this._max = typeof data.max !== 'undefined' ? data.max : null
|
32 | } else if (data) {
|
33 |
|
34 | throw new TypeError('Unsupported type of data (' + typeOf(data) + ')')
|
35 | } else {
|
36 |
|
37 | this._data = []
|
38 | this._size = [0]
|
39 | this._datatype = datatype
|
40 | this._min = null
|
41 | this._max = null
|
42 | }
|
43 | }
|
44 |
|
45 | ImmutableDenseMatrix.prototype = new DenseMatrix()
|
46 |
|
47 | |
48 |
|
49 |
|
50 | ImmutableDenseMatrix.prototype.type = 'ImmutableDenseMatrix'
|
51 | ImmutableDenseMatrix.prototype.isImmutableDenseMatrix = true
|
52 |
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | ImmutableDenseMatrix.prototype.subset = function (index) {
|
67 | switch (arguments.length) {
|
68 | case 1:
|
69 | {
|
70 |
|
71 | const m = DenseMatrix.prototype.subset.call(this, index)
|
72 |
|
73 | if (isMatrix(m)) {
|
74 |
|
75 | return new ImmutableDenseMatrix({
|
76 | data: m._data,
|
77 | size: m._size,
|
78 | datatype: m._datatype
|
79 | })
|
80 | }
|
81 | return m
|
82 | }
|
83 |
|
84 | case 2:
|
85 | case 3:
|
86 | throw new Error('Cannot invoke set subset on an Immutable Matrix instance')
|
87 |
|
88 | default:
|
89 | throw new SyntaxError('Wrong number of arguments')
|
90 | }
|
91 | }
|
92 |
|
93 | |
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | ImmutableDenseMatrix.prototype.set = function () {
|
103 | throw new Error('Cannot invoke set on an Immutable Matrix instance')
|
104 | }
|
105 |
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | ImmutableDenseMatrix.prototype.resize = function () {
|
119 | throw new Error('Cannot invoke resize on an Immutable Matrix instance')
|
120 | }
|
121 |
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 | ImmutableDenseMatrix.prototype.reshape = function () {
|
128 | throw new Error('Cannot invoke reshape on an Immutable Matrix instance')
|
129 | }
|
130 |
|
131 | |
132 |
|
133 |
|
134 |
|
135 | ImmutableDenseMatrix.prototype.clone = function () {
|
136 | return new ImmutableDenseMatrix({
|
137 | data: clone(this._data),
|
138 | size: clone(this._size),
|
139 | datatype: this._datatype
|
140 | })
|
141 | }
|
142 |
|
143 | |
144 |
|
145 |
|
146 |
|
147 | ImmutableDenseMatrix.prototype.toJSON = function () {
|
148 | return {
|
149 | mathjs: 'ImmutableDenseMatrix',
|
150 | data: this._data,
|
151 | size: this._size,
|
152 | datatype: this._datatype
|
153 | }
|
154 | }
|
155 |
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | ImmutableDenseMatrix.fromJSON = function (json) {
|
164 | return new ImmutableDenseMatrix(json)
|
165 | }
|
166 |
|
167 | |
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | ImmutableDenseMatrix.prototype.swapRows = function () {
|
176 | throw new Error('Cannot invoke swapRows on an Immutable Matrix instance')
|
177 | }
|
178 |
|
179 | |
180 |
|
181 |
|
182 |
|
183 | ImmutableDenseMatrix.prototype.min = function () {
|
184 |
|
185 | if (this._min === null) {
|
186 |
|
187 | let m = null
|
188 |
|
189 | this.forEach(function (v) {
|
190 | if (m === null || smaller(v, m)) { m = v }
|
191 | })
|
192 | this._min = m !== null ? m : undefined
|
193 | }
|
194 | return this._min
|
195 | }
|
196 |
|
197 | |
198 |
|
199 |
|
200 |
|
201 | ImmutableDenseMatrix.prototype.max = function () {
|
202 |
|
203 | if (this._max === null) {
|
204 |
|
205 | let m = null
|
206 |
|
207 | this.forEach(function (v) {
|
208 | if (m === null || smaller(m, v)) { m = v }
|
209 | })
|
210 | this._max = m !== null ? m : undefined
|
211 | }
|
212 | return this._max
|
213 | }
|
214 |
|
215 | return ImmutableDenseMatrix
|
216 | }, { isClass: true })
|