1 | 'use strict'
|
2 |
|
3 | const util = require('../../utils/index')
|
4 | const DimensionError = require('../../error/DimensionError')
|
5 |
|
6 | const array = util.array
|
7 | const object = util.object
|
8 | const string = util.string
|
9 | const number = util.number
|
10 |
|
11 | const isArray = Array.isArray
|
12 | const isNumber = number.isNumber
|
13 | const isInteger = number.isInteger
|
14 | const isString = string.isString
|
15 |
|
16 | const validateIndex = array.validateIndex
|
17 |
|
18 | function factory (type, config, load, typed) {
|
19 | const Matrix = load(require('./Matrix'))
|
20 | const equalScalar = load(require('../../function/relational/equalScalar'))
|
21 | const getArrayDataType = load(require('./utils/getArrayDataType'))
|
22 |
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 | function SparseMatrix (data, datatype) {
|
29 | if (!(this instanceof SparseMatrix)) { throw new SyntaxError('Constructor must be called with the new operator') }
|
30 | if (datatype && !isString(datatype)) { throw new Error('Invalid datatype: ' + datatype) }
|
31 |
|
32 | if (type.isMatrix(data)) {
|
33 |
|
34 | _createFromMatrix(this, data, datatype)
|
35 | } else if (data && isArray(data.index) && isArray(data.ptr) && isArray(data.size)) {
|
36 |
|
37 | this._values = data.values
|
38 | this._index = data.index
|
39 | this._ptr = data.ptr
|
40 | this._size = data.size
|
41 | this._datatype = datatype || data.datatype
|
42 | } else if (isArray(data)) {
|
43 |
|
44 | _createFromArray(this, data, datatype)
|
45 | } else if (data) {
|
46 |
|
47 | throw new TypeError('Unsupported type of data (' + util.types.type(data) + ')')
|
48 | } else {
|
49 |
|
50 | this._values = []
|
51 | this._index = []
|
52 | this._ptr = [0]
|
53 | this._size = [0, 0]
|
54 | this._datatype = datatype
|
55 | }
|
56 | }
|
57 |
|
58 | function _createFromMatrix (matrix, source, datatype) {
|
59 |
|
60 | if (source.type === 'SparseMatrix') {
|
61 |
|
62 | matrix._values = source._values ? object.clone(source._values) : undefined
|
63 | matrix._index = object.clone(source._index)
|
64 | matrix._ptr = object.clone(source._ptr)
|
65 | matrix._size = object.clone(source._size)
|
66 | matrix._datatype = datatype || source._datatype
|
67 | } else {
|
68 |
|
69 | _createFromArray(matrix, source.valueOf(), datatype || source._datatype)
|
70 | }
|
71 | }
|
72 |
|
73 | function _createFromArray (matrix, data, datatype) {
|
74 |
|
75 | matrix._values = []
|
76 | matrix._index = []
|
77 | matrix._ptr = []
|
78 | matrix._datatype = datatype
|
79 |
|
80 | const rows = data.length
|
81 | let columns = 0
|
82 |
|
83 |
|
84 | let eq = equalScalar
|
85 |
|
86 | let zero = 0
|
87 |
|
88 | if (isString(datatype)) {
|
89 |
|
90 | eq = typed.find(equalScalar, [datatype, datatype]) || equalScalar
|
91 |
|
92 | zero = typed.convert(0, datatype)
|
93 | }
|
94 |
|
95 |
|
96 | if (rows > 0) {
|
97 |
|
98 | let j = 0
|
99 | do {
|
100 |
|
101 | matrix._ptr.push(matrix._index.length)
|
102 |
|
103 | for (let i = 0; i < rows; i++) {
|
104 |
|
105 | const row = data[i]
|
106 |
|
107 | if (isArray(row)) {
|
108 |
|
109 | if (j === 0 && columns < row.length) { columns = row.length }
|
110 |
|
111 | if (j < row.length) {
|
112 |
|
113 | const v = row[j]
|
114 |
|
115 | if (!eq(v, zero)) {
|
116 |
|
117 | matrix._values.push(v)
|
118 |
|
119 | matrix._index.push(i)
|
120 | }
|
121 | }
|
122 | } else {
|
123 |
|
124 | if (j === 0 && columns < 1) { columns = 1 }
|
125 |
|
126 | if (!eq(row, zero)) {
|
127 |
|
128 | matrix._values.push(row)
|
129 |
|
130 | matrix._index.push(i)
|
131 | }
|
132 | }
|
133 | }
|
134 |
|
135 | j++
|
136 | }
|
137 | while (j < columns)
|
138 | }
|
139 |
|
140 | matrix._ptr.push(matrix._index.length)
|
141 |
|
142 | matrix._size = [rows, columns]
|
143 | }
|
144 |
|
145 | SparseMatrix.prototype = new Matrix()
|
146 |
|
147 | |
148 |
|
149 |
|
150 | SparseMatrix.prototype.type = 'SparseMatrix'
|
151 | SparseMatrix.prototype.isSparseMatrix = true
|
152 |
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | SparseMatrix.prototype.getDataType = function () {
|
163 | return getArrayDataType(this._values)
|
164 | }
|
165 |
|
166 | |
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | SparseMatrix.prototype.storage = function () {
|
176 | return 'sparse'
|
177 | }
|
178 |
|
179 | |
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | SparseMatrix.prototype.datatype = function () {
|
189 | return this._datatype
|
190 | }
|
191 |
|
192 | |
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | SparseMatrix.prototype.create = function (data, datatype) {
|
199 | return new SparseMatrix(data, datatype)
|
200 | }
|
201 |
|
202 | |
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | SparseMatrix.prototype.density = function () {
|
212 |
|
213 | const rows = this._size[0]
|
214 | const columns = this._size[1]
|
215 |
|
216 | return rows !== 0 && columns !== 0 ? (this._index.length / (rows * columns)) : 0
|
217 | }
|
218 |
|
219 | |
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 | SparseMatrix.prototype.subset = function (index, replacement, defaultValue) {
|
234 | if (!this._values) { throw new Error('Cannot invoke subset on a Pattern only matrix') }
|
235 |
|
236 |
|
237 | switch (arguments.length) {
|
238 | case 1:
|
239 | return _getsubset(this, index)
|
240 |
|
241 |
|
242 | case 2:
|
243 | case 3:
|
244 | return _setsubset(this, index, replacement, defaultValue)
|
245 |
|
246 | default:
|
247 | throw new SyntaxError('Wrong number of arguments')
|
248 | }
|
249 | }
|
250 |
|
251 | function _getsubset (matrix, idx) {
|
252 |
|
253 | if (!type.isIndex(idx)) {
|
254 | throw new TypeError('Invalid index')
|
255 | }
|
256 |
|
257 | const isScalar = idx.isScalar()
|
258 | if (isScalar) {
|
259 |
|
260 | return matrix.get(idx.min())
|
261 | }
|
262 |
|
263 | const size = idx.size()
|
264 | if (size.length !== matrix._size.length) {
|
265 | throw new DimensionError(size.length, matrix._size.length)
|
266 | }
|
267 |
|
268 |
|
269 | let i, ii, k, kk
|
270 |
|
271 |
|
272 | const min = idx.min()
|
273 | const max = idx.max()
|
274 | for (i = 0, ii = matrix._size.length; i < ii; i++) {
|
275 | validateIndex(min[i], matrix._size[i])
|
276 | validateIndex(max[i], matrix._size[i])
|
277 | }
|
278 |
|
279 |
|
280 | const mvalues = matrix._values
|
281 | const mindex = matrix._index
|
282 | const mptr = matrix._ptr
|
283 |
|
284 |
|
285 | const rows = idx.dimension(0)
|
286 | const columns = idx.dimension(1)
|
287 |
|
288 |
|
289 | const w = []
|
290 | const pv = []
|
291 |
|
292 |
|
293 | rows.forEach(function (i, r) {
|
294 |
|
295 | pv[i] = r[0]
|
296 |
|
297 | w[i] = true
|
298 | })
|
299 |
|
300 |
|
301 | const values = mvalues ? [] : undefined
|
302 | const index = []
|
303 | const ptr = []
|
304 |
|
305 |
|
306 | columns.forEach(function (j) {
|
307 |
|
308 | ptr.push(index.length)
|
309 |
|
310 | for (k = mptr[j], kk = mptr[j + 1]; k < kk; k++) {
|
311 |
|
312 | i = mindex[k]
|
313 |
|
314 | if (w[i] === true) {
|
315 |
|
316 | index.push(pv[i])
|
317 |
|
318 | if (values) { values.push(mvalues[k]) }
|
319 | }
|
320 | }
|
321 | })
|
322 |
|
323 | ptr.push(index.length)
|
324 |
|
325 |
|
326 | return new SparseMatrix({
|
327 | values: values,
|
328 | index: index,
|
329 | ptr: ptr,
|
330 | size: size,
|
331 | datatype: matrix._datatype
|
332 | })
|
333 | }
|
334 |
|
335 | function _setsubset (matrix, index, submatrix, defaultValue) {
|
336 |
|
337 | if (!index || index.isIndex !== true) {
|
338 | throw new TypeError('Invalid index')
|
339 | }
|
340 |
|
341 |
|
342 | const iSize = index.size()
|
343 | const isScalar = index.isScalar()
|
344 |
|
345 |
|
346 | let sSize
|
347 | if (type.isMatrix(submatrix)) {
|
348 |
|
349 | sSize = submatrix.size()
|
350 |
|
351 | submatrix = submatrix.toArray()
|
352 | } else {
|
353 |
|
354 | sSize = array.size(submatrix)
|
355 | }
|
356 |
|
357 |
|
358 | if (isScalar) {
|
359 |
|
360 | if (sSize.length !== 0) {
|
361 | throw new TypeError('Scalar expected')
|
362 | }
|
363 |
|
364 | matrix.set(index.min(), submatrix, defaultValue)
|
365 | } else {
|
366 |
|
367 | if (iSize.length !== 1 && iSize.length !== 2) {
|
368 | throw new DimensionError(iSize.length, matrix._size.length, '<')
|
369 | }
|
370 |
|
371 |
|
372 | if (sSize.length < iSize.length) {
|
373 |
|
374 | let i = 0
|
375 | let outer = 0
|
376 | while (iSize[i] === 1 && sSize[i] === 1) {
|
377 | i++
|
378 | }
|
379 | while (iSize[i] === 1) {
|
380 | outer++
|
381 | i++
|
382 | }
|
383 |
|
384 | submatrix = array.unsqueeze(submatrix, iSize.length, outer, sSize)
|
385 | }
|
386 |
|
387 |
|
388 | if (!object.deepEqual(iSize, sSize)) {
|
389 | throw new DimensionError(iSize, sSize, '>')
|
390 | }
|
391 |
|
392 |
|
393 | const x0 = index.min()[0]
|
394 | const y0 = index.min()[1]
|
395 |
|
396 |
|
397 | const m = sSize[0]
|
398 | const n = sSize[1]
|
399 |
|
400 |
|
401 | for (let x = 0; x < m; x++) {
|
402 |
|
403 | for (let y = 0; y < n; y++) {
|
404 |
|
405 | const v = submatrix[x][y]
|
406 |
|
407 | matrix.set([x + x0, y + y0], v, defaultValue)
|
408 | }
|
409 | }
|
410 | }
|
411 | return matrix
|
412 | }
|
413 |
|
414 | |
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 | SparseMatrix.prototype.get = function (index) {
|
421 | if (!isArray(index)) { throw new TypeError('Array expected') }
|
422 | if (index.length !== this._size.length) { throw new DimensionError(index.length, this._size.length) }
|
423 |
|
424 |
|
425 | if (!this._values) { throw new Error('Cannot invoke get on a Pattern only matrix') }
|
426 |
|
427 |
|
428 | const i = index[0]
|
429 | const j = index[1]
|
430 |
|
431 |
|
432 | validateIndex(i, this._size[0])
|
433 | validateIndex(j, this._size[1])
|
434 |
|
435 |
|
436 | const k = _getValueIndex(i, this._ptr[j], this._ptr[j + 1], this._index)
|
437 |
|
438 | if (k < this._ptr[j + 1] && this._index[k] === i) { return this._values[k] }
|
439 |
|
440 | return 0
|
441 | }
|
442 |
|
443 | |
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 | SparseMatrix.prototype.set = function (index, v, defaultValue) {
|
454 | if (!isArray(index)) { throw new TypeError('Array expected') }
|
455 | if (index.length !== this._size.length) { throw new DimensionError(index.length, this._size.length) }
|
456 |
|
457 |
|
458 | if (!this._values) { throw new Error('Cannot invoke set on a Pattern only matrix') }
|
459 |
|
460 |
|
461 | const i = index[0]
|
462 | const j = index[1]
|
463 |
|
464 |
|
465 | let rows = this._size[0]
|
466 | let columns = this._size[1]
|
467 |
|
468 |
|
469 | let eq = equalScalar
|
470 |
|
471 | let zero = 0
|
472 |
|
473 | if (isString(this._datatype)) {
|
474 |
|
475 | eq = typed.find(equalScalar, [this._datatype, this._datatype]) || equalScalar
|
476 |
|
477 | zero = typed.convert(0, this._datatype)
|
478 | }
|
479 |
|
480 |
|
481 | if (i > rows - 1 || j > columns - 1) {
|
482 |
|
483 | _resize(this, Math.max(i + 1, rows), Math.max(j + 1, columns), defaultValue)
|
484 |
|
485 | rows = this._size[0]
|
486 | columns = this._size[1]
|
487 | }
|
488 |
|
489 |
|
490 | validateIndex(i, rows)
|
491 | validateIndex(j, columns)
|
492 |
|
493 |
|
494 | const k = _getValueIndex(i, this._ptr[j], this._ptr[j + 1], this._index)
|
495 |
|
496 | if (k < this._ptr[j + 1] && this._index[k] === i) {
|
497 |
|
498 | if (!eq(v, zero)) {
|
499 |
|
500 | this._values[k] = v
|
501 | } else {
|
502 |
|
503 | _remove(k, j, this._values, this._index, this._ptr)
|
504 | }
|
505 | } else {
|
506 |
|
507 | _insert(k, i, j, v, this._values, this._index, this._ptr)
|
508 | }
|
509 |
|
510 | return this
|
511 | }
|
512 |
|
513 | function _getValueIndex (i, top, bottom, index) {
|
514 |
|
515 | if (bottom - top === 0) { return bottom }
|
516 |
|
517 | for (let r = top; r < bottom; r++) {
|
518 |
|
519 | if (index[r] === i) { return r }
|
520 | }
|
521 |
|
522 | return top
|
523 | }
|
524 |
|
525 | function _remove (k, j, values, index, ptr) {
|
526 |
|
527 | values.splice(k, 1)
|
528 | index.splice(k, 1)
|
529 |
|
530 | for (let x = j + 1; x < ptr.length; x++) { ptr[x]-- }
|
531 | }
|
532 |
|
533 | function _insert (k, i, j, v, values, index, ptr) {
|
534 |
|
535 | values.splice(k, 0, v)
|
536 |
|
537 | index.splice(k, 0, i)
|
538 |
|
539 | for (let x = j + 1; x < ptr.length; x++) { ptr[x]++ }
|
540 | }
|
541 |
|
542 | |
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 | SparseMatrix.prototype.resize = function (size, defaultValue, copy) {
|
556 |
|
557 | if (!isArray(size)) { throw new TypeError('Array expected') }
|
558 | if (size.length !== 2) { throw new Error('Only two dimensions matrix are supported') }
|
559 |
|
560 |
|
561 | size.forEach(function (value) {
|
562 | if (!number.isNumber(value) || !number.isInteger(value) || value < 0) {
|
563 | throw new TypeError('Invalid size, must contain positive integers ' +
|
564 | '(size: ' + string.format(size) + ')')
|
565 | }
|
566 | })
|
567 |
|
568 |
|
569 | const m = copy ? this.clone() : this
|
570 |
|
571 | return _resize(m, size[0], size[1], defaultValue)
|
572 | }
|
573 |
|
574 | function _resize (matrix, rows, columns, defaultValue) {
|
575 |
|
576 | let value = defaultValue || 0
|
577 |
|
578 |
|
579 | let eq = equalScalar
|
580 |
|
581 | let zero = 0
|
582 |
|
583 | if (isString(matrix._datatype)) {
|
584 |
|
585 | eq = typed.find(equalScalar, [matrix._datatype, matrix._datatype]) || equalScalar
|
586 |
|
587 | zero = typed.convert(0, matrix._datatype)
|
588 |
|
589 | value = typed.convert(value, matrix._datatype)
|
590 | }
|
591 |
|
592 |
|
593 | const ins = !eq(value, zero)
|
594 |
|
595 |
|
596 | const r = matrix._size[0]
|
597 | let c = matrix._size[1]
|
598 |
|
599 | let i, j, k
|
600 |
|
601 |
|
602 | if (columns > c) {
|
603 |
|
604 | for (j = c; j < columns; j++) {
|
605 |
|
606 | matrix._ptr[j] = matrix._values.length
|
607 |
|
608 | if (ins) {
|
609 |
|
610 | for (i = 0; i < r; i++) {
|
611 |
|
612 | matrix._values.push(value)
|
613 |
|
614 | matrix._index.push(i)
|
615 | }
|
616 | }
|
617 | }
|
618 |
|
619 | matrix._ptr[columns] = matrix._values.length
|
620 | } else if (columns < c) {
|
621 |
|
622 | matrix._ptr.splice(columns + 1, c - columns)
|
623 |
|
624 | matrix._values.splice(matrix._ptr[columns], matrix._values.length)
|
625 | matrix._index.splice(matrix._ptr[columns], matrix._index.length)
|
626 | }
|
627 |
|
628 | c = columns
|
629 |
|
630 |
|
631 | if (rows > r) {
|
632 |
|
633 | if (ins) {
|
634 |
|
635 | let n = 0
|
636 |
|
637 | for (j = 0; j < c; j++) {
|
638 |
|
639 | matrix._ptr[j] = matrix._ptr[j] + n
|
640 |
|
641 | k = matrix._ptr[j + 1] + n
|
642 |
|
643 | let p = 0
|
644 |
|
645 | for (i = r; i < rows; i++, p++) {
|
646 |
|
647 | matrix._values.splice(k + p, 0, value)
|
648 |
|
649 | matrix._index.splice(k + p, 0, i)
|
650 |
|
651 | n++
|
652 | }
|
653 | }
|
654 |
|
655 | matrix._ptr[c] = matrix._values.length
|
656 | }
|
657 | } else if (rows < r) {
|
658 |
|
659 | let d = 0
|
660 |
|
661 | for (j = 0; j < c; j++) {
|
662 |
|
663 | matrix._ptr[j] = matrix._ptr[j] - d
|
664 |
|
665 | const k0 = matrix._ptr[j]
|
666 | const k1 = matrix._ptr[j + 1] - d
|
667 |
|
668 | for (k = k0; k < k1; k++) {
|
669 |
|
670 | i = matrix._index[k]
|
671 |
|
672 | if (i > rows - 1) {
|
673 |
|
674 | matrix._values.splice(k, 1)
|
675 |
|
676 | matrix._index.splice(k, 1)
|
677 |
|
678 | d++
|
679 | }
|
680 | }
|
681 | }
|
682 |
|
683 | matrix._ptr[j] = matrix._values.length
|
684 | }
|
685 |
|
686 | matrix._size[0] = rows
|
687 | matrix._size[1] = columns
|
688 |
|
689 | return matrix
|
690 | }
|
691 |
|
692 | |
693 |
|
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 |
|
700 |
|
701 |
|
702 |
|
703 |
|
704 |
|
705 |
|
706 | SparseMatrix.prototype.reshape = function (size, copy) {
|
707 |
|
708 | if (!isArray(size)) { throw new TypeError('Array expected') }
|
709 | if (size.length !== 2) { throw new Error('Sparse matrices can only be reshaped in two dimensions') }
|
710 |
|
711 |
|
712 | size.forEach(function (value) {
|
713 | if (!number.isNumber(value) || !number.isInteger(value) || value < 0) {
|
714 | throw new TypeError('Invalid size, must contain positive integers ' +
|
715 | '(size: ' + string.format(size) + ')')
|
716 | }
|
717 | })
|
718 |
|
719 |
|
720 | if (this._size[0] * this._size[1] !== size[0] * size[1]) {
|
721 | throw new Error('Reshaping sparse matrix will result in the wrong number of elements')
|
722 | }
|
723 |
|
724 |
|
725 | const m = copy ? this.clone() : this
|
726 |
|
727 |
|
728 | if (this._size[0] === size[0] && this._size[1] === size[1]) {
|
729 | return m
|
730 | }
|
731 |
|
732 |
|
733 | const colIndex = []
|
734 | for (let i = 0; i < m._ptr.length; i++) {
|
735 | for (let j = 0; j < m._ptr[i + 1] - m._ptr[i]; j++) {
|
736 | colIndex.push(i)
|
737 | }
|
738 | }
|
739 |
|
740 |
|
741 | const values = m._values.slice()
|
742 |
|
743 |
|
744 | const rowIndex = m._index.slice()
|
745 |
|
746 |
|
747 | for (let i = 0; i < m._index.length; i++) {
|
748 | const r1 = rowIndex[i]
|
749 | const c1 = colIndex[i]
|
750 | const flat = r1 * m._size[1] + c1
|
751 | colIndex[i] = flat % size[1]
|
752 | rowIndex[i] = Math.floor(flat / size[1])
|
753 | }
|
754 |
|
755 |
|
756 |
|
757 |
|
758 |
|
759 |
|
760 |
|
761 |
|
762 | m._values.length = 0
|
763 | m._index.length = 0
|
764 | m._ptr.length = size[1] + 1
|
765 | m._size = size.slice()
|
766 | for (let i = 0; i < m._ptr.length; i++) {
|
767 | m._ptr[i] = 0
|
768 | }
|
769 |
|
770 |
|
771 |
|
772 | for (let h = 0; h < values.length; h++) {
|
773 | const i = rowIndex[h]
|
774 | const j = colIndex[h]
|
775 | const v = values[h]
|
776 | const k = _getValueIndex(i, m._ptr[j], m._ptr[j + 1], m._index)
|
777 | _insert(k, i, j, v, m._values, m._index, m._ptr)
|
778 | }
|
779 |
|
780 |
|
781 |
|
782 | return m
|
783 | }
|
784 |
|
785 | |
786 |
|
787 |
|
788 |
|
789 |
|
790 | SparseMatrix.prototype.clone = function () {
|
791 | const m = new SparseMatrix({
|
792 | values: this._values ? object.clone(this._values) : undefined,
|
793 | index: object.clone(this._index),
|
794 | ptr: object.clone(this._ptr),
|
795 | size: object.clone(this._size),
|
796 | datatype: this._datatype
|
797 | })
|
798 | return m
|
799 | }
|
800 |
|
801 | |
802 |
|
803 |
|
804 |
|
805 |
|
806 | SparseMatrix.prototype.size = function () {
|
807 | return this._size.slice(0)
|
808 | }
|
809 |
|
810 | |
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
816 |
|
817 |
|
818 |
|
819 |
|
820 |
|
821 | SparseMatrix.prototype.map = function (callback, skipZeros) {
|
822 |
|
823 | if (!this._values) { throw new Error('Cannot invoke map on a Pattern only matrix') }
|
824 |
|
825 | const me = this
|
826 |
|
827 | const rows = this._size[0]
|
828 | const columns = this._size[1]
|
829 |
|
830 | const invoke = function (v, i, j) {
|
831 |
|
832 | return callback(v, [i, j], me)
|
833 | }
|
834 |
|
835 | return _map(this, 0, rows - 1, 0, columns - 1, invoke, skipZeros)
|
836 | }
|
837 |
|
838 | |
839 |
|
840 |
|
841 |
|
842 | function _map (matrix, minRow, maxRow, minColumn, maxColumn, callback, skipZeros) {
|
843 |
|
844 | const values = []
|
845 | const index = []
|
846 | const ptr = []
|
847 |
|
848 |
|
849 | let eq = equalScalar
|
850 |
|
851 | let zero = 0
|
852 |
|
853 | if (isString(matrix._datatype)) {
|
854 |
|
855 | eq = typed.find(equalScalar, [matrix._datatype, matrix._datatype]) || equalScalar
|
856 |
|
857 | zero = typed.convert(0, matrix._datatype)
|
858 | }
|
859 |
|
860 |
|
861 | const invoke = function (v, x, y) {
|
862 |
|
863 | v = callback(v, x, y)
|
864 |
|
865 | if (!eq(v, zero)) {
|
866 |
|
867 | values.push(v)
|
868 |
|
869 | index.push(x)
|
870 | }
|
871 | }
|
872 |
|
873 | for (let j = minColumn; j <= maxColumn; j++) {
|
874 |
|
875 | ptr.push(values.length)
|
876 |
|
877 | const k0 = matrix._ptr[j]
|
878 | const k1 = matrix._ptr[j + 1]
|
879 |
|
880 | let p = minRow
|
881 |
|
882 | for (let k = k0; k < k1; k++) {
|
883 |
|
884 | const i = matrix._index[k]
|
885 |
|
886 | if (i >= minRow && i <= maxRow) {
|
887 |
|
888 | if (!skipZeros) {
|
889 | for (let x = p; x < i; x++) { invoke(0, x - minRow, j - minColumn) }
|
890 | }
|
891 |
|
892 | invoke(matrix._values[k], i - minRow, j - minColumn)
|
893 | }
|
894 |
|
895 | p = i + 1
|
896 | }
|
897 |
|
898 | if (!skipZeros) {
|
899 | for (let y = p; y <= maxRow; y++) { invoke(0, y - minRow, j - minColumn) }
|
900 | }
|
901 | }
|
902 |
|
903 | ptr.push(values.length)
|
904 |
|
905 | return new SparseMatrix({
|
906 | values: values,
|
907 | index: index,
|
908 | ptr: ptr,
|
909 | size: [maxRow - minRow + 1, maxColumn - minColumn + 1]
|
910 | })
|
911 | }
|
912 |
|
913 | |
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 |
|
921 | SparseMatrix.prototype.forEach = function (callback, skipZeros) {
|
922 |
|
923 | if (!this._values) { throw new Error('Cannot invoke forEach on a Pattern only matrix') }
|
924 |
|
925 | const me = this
|
926 |
|
927 | const rows = this._size[0]
|
928 | const columns = this._size[1]
|
929 |
|
930 | for (let j = 0; j < columns; j++) {
|
931 |
|
932 | const k0 = this._ptr[j]
|
933 | const k1 = this._ptr[j + 1]
|
934 |
|
935 | let p = 0
|
936 |
|
937 | for (let k = k0; k < k1; k++) {
|
938 |
|
939 | const i = this._index[k]
|
940 |
|
941 | if (!skipZeros) {
|
942 |
|
943 | for (let x = p; x < i; x++) { callback(0, [x, j], me) }
|
944 | }
|
945 |
|
946 | callback(this._values[k], [i, j], me)
|
947 |
|
948 | p = i + 1
|
949 | }
|
950 |
|
951 | if (!skipZeros) {
|
952 |
|
953 | for (let y = p; y < rows; y++) { callback(0, [y, j], me) }
|
954 | }
|
955 | }
|
956 | }
|
957 |
|
958 | |
959 |
|
960 |
|
961 |
|
962 |
|
963 | SparseMatrix.prototype.toArray = function () {
|
964 | return _toArray(this._values, this._index, this._ptr, this._size, true)
|
965 | }
|
966 |
|
967 | |
968 |
|
969 |
|
970 |
|
971 |
|
972 | SparseMatrix.prototype.valueOf = function () {
|
973 | return _toArray(this._values, this._index, this._ptr, this._size, false)
|
974 | }
|
975 |
|
976 | function _toArray (values, index, ptr, size, copy) {
|
977 |
|
978 | const rows = size[0]
|
979 | const columns = size[1]
|
980 |
|
981 | const a = []
|
982 |
|
983 | let i, j
|
984 |
|
985 | for (i = 0; i < rows; i++) {
|
986 | a[i] = []
|
987 | for (j = 0; j < columns; j++) { a[i][j] = 0 }
|
988 | }
|
989 |
|
990 |
|
991 | for (j = 0; j < columns; j++) {
|
992 |
|
993 | const k0 = ptr[j]
|
994 | const k1 = ptr[j + 1]
|
995 |
|
996 | for (let k = k0; k < k1; k++) {
|
997 |
|
998 | i = index[k]
|
999 |
|
1000 | a[i][j] = values ? (copy ? object.clone(values[k]) : values[k]) : 1
|
1001 | }
|
1002 | }
|
1003 | return a
|
1004 | }
|
1005 |
|
1006 | |
1007 |
|
1008 |
|
1009 |
|
1010 |
|
1011 |
|
1012 |
|
1013 |
|
1014 |
|
1015 | SparseMatrix.prototype.format = function (options) {
|
1016 |
|
1017 | const rows = this._size[0]
|
1018 | const columns = this._size[1]
|
1019 |
|
1020 | const density = this.density()
|
1021 |
|
1022 | let str = 'Sparse Matrix [' + string.format(rows, options) + ' x ' + string.format(columns, options) + '] density: ' + string.format(density, options) + '\n'
|
1023 |
|
1024 | for (let j = 0; j < columns; j++) {
|
1025 |
|
1026 | const k0 = this._ptr[j]
|
1027 | const k1 = this._ptr[j + 1]
|
1028 |
|
1029 | for (let k = k0; k < k1; k++) {
|
1030 |
|
1031 | const i = this._index[k]
|
1032 |
|
1033 | str += '\n (' + string.format(i, options) + ', ' + string.format(j, options) + ') ==> ' + (this._values ? string.format(this._values[k], options) : 'X')
|
1034 | }
|
1035 | }
|
1036 | return str
|
1037 | }
|
1038 |
|
1039 | |
1040 |
|
1041 |
|
1042 |
|
1043 |
|
1044 | SparseMatrix.prototype.toString = function () {
|
1045 | return string.format(this.toArray())
|
1046 | }
|
1047 |
|
1048 | |
1049 |
|
1050 |
|
1051 |
|
1052 |
|
1053 | SparseMatrix.prototype.toJSON = function () {
|
1054 | return {
|
1055 | mathjs: 'SparseMatrix',
|
1056 | values: this._values,
|
1057 | index: this._index,
|
1058 | ptr: this._ptr,
|
1059 | size: this._size,
|
1060 | datatype: this._datatype
|
1061 | }
|
1062 | }
|
1063 |
|
1064 | |
1065 |
|
1066 |
|
1067 |
|
1068 |
|
1069 |
|
1070 |
|
1071 |
|
1072 | SparseMatrix.prototype.diagonal = function (k) {
|
1073 |
|
1074 | if (k) {
|
1075 |
|
1076 | if (type.isBigNumber(k)) { k = k.toNumber() }
|
1077 |
|
1078 | if (!isNumber(k) || !isInteger(k)) {
|
1079 | throw new TypeError('The parameter k must be an integer number')
|
1080 | }
|
1081 | } else {
|
1082 |
|
1083 | k = 0
|
1084 | }
|
1085 |
|
1086 | const kSuper = k > 0 ? k : 0
|
1087 | const kSub = k < 0 ? -k : 0
|
1088 |
|
1089 |
|
1090 | const rows = this._size[0]
|
1091 | const columns = this._size[1]
|
1092 |
|
1093 |
|
1094 | const n = Math.min(rows - kSub, columns - kSuper)
|
1095 |
|
1096 |
|
1097 | const values = []
|
1098 | const index = []
|
1099 | const ptr = []
|
1100 |
|
1101 | ptr[0] = 0
|
1102 |
|
1103 | for (let j = kSuper; j < columns && values.length < n; j++) {
|
1104 |
|
1105 | const k0 = this._ptr[j]
|
1106 | const k1 = this._ptr[j + 1]
|
1107 |
|
1108 | for (let x = k0; x < k1; x++) {
|
1109 |
|
1110 | const i = this._index[x]
|
1111 |
|
1112 | if (i === j - kSuper + kSub) {
|
1113 |
|
1114 | values.push(this._values[x])
|
1115 |
|
1116 | index[values.length - 1] = i - kSub
|
1117 |
|
1118 | break
|
1119 | }
|
1120 | }
|
1121 | }
|
1122 |
|
1123 | ptr.push(values.length)
|
1124 |
|
1125 | return new SparseMatrix({
|
1126 | values: values,
|
1127 | index: index,
|
1128 | ptr: ptr,
|
1129 | size: [n, 1]
|
1130 | })
|
1131 | }
|
1132 |
|
1133 | |
1134 |
|
1135 |
|
1136 |
|
1137 |
|
1138 |
|
1139 |
|
1140 |
|
1141 | SparseMatrix.fromJSON = function (json) {
|
1142 | return new SparseMatrix(json)
|
1143 | }
|
1144 |
|
1145 | |
1146 |
|
1147 |
|
1148 |
|
1149 |
|
1150 |
|
1151 |
|
1152 |
|
1153 |
|
1154 |
|
1155 |
|
1156 | SparseMatrix.diagonal = function (size, value, k, defaultValue, datatype) {
|
1157 | if (!isArray(size)) { throw new TypeError('Array expected, size parameter') }
|
1158 | if (size.length !== 2) { throw new Error('Only two dimensions matrix are supported') }
|
1159 |
|
1160 |
|
1161 | size = size.map(function (s) {
|
1162 |
|
1163 | if (type.isBigNumber(s)) {
|
1164 |
|
1165 | s = s.toNumber()
|
1166 | }
|
1167 |
|
1168 | if (!isNumber(s) || !isInteger(s) || s < 1) {
|
1169 | throw new Error('Size values must be positive integers')
|
1170 | }
|
1171 | return s
|
1172 | })
|
1173 |
|
1174 |
|
1175 | if (k) {
|
1176 |
|
1177 | if (type.isBigNumber(k)) { k = k.toNumber() }
|
1178 |
|
1179 | if (!isNumber(k) || !isInteger(k)) {
|
1180 | throw new TypeError('The parameter k must be an integer number')
|
1181 | }
|
1182 | } else {
|
1183 |
|
1184 | k = 0
|
1185 | }
|
1186 |
|
1187 |
|
1188 | let eq = equalScalar
|
1189 |
|
1190 | let zero = 0
|
1191 |
|
1192 | if (isString(datatype)) {
|
1193 |
|
1194 | eq = typed.find(equalScalar, [datatype, datatype]) || equalScalar
|
1195 |
|
1196 | zero = typed.convert(0, datatype)
|
1197 | }
|
1198 |
|
1199 | const kSuper = k > 0 ? k : 0
|
1200 | const kSub = k < 0 ? -k : 0
|
1201 |
|
1202 |
|
1203 | const rows = size[0]
|
1204 | const columns = size[1]
|
1205 |
|
1206 |
|
1207 | const n = Math.min(rows - kSub, columns - kSuper)
|
1208 |
|
1209 |
|
1210 | let _value
|
1211 |
|
1212 |
|
1213 | if (isArray(value)) {
|
1214 |
|
1215 | if (value.length !== n) {
|
1216 |
|
1217 | throw new Error('Invalid value array length')
|
1218 | }
|
1219 |
|
1220 | _value = function (i) {
|
1221 |
|
1222 | return value[i]
|
1223 | }
|
1224 | } else if (type.isMatrix(value)) {
|
1225 |
|
1226 | const ms = value.size()
|
1227 |
|
1228 | if (ms.length !== 1 || ms[0] !== n) {
|
1229 |
|
1230 | throw new Error('Invalid matrix length')
|
1231 | }
|
1232 |
|
1233 | _value = function (i) {
|
1234 |
|
1235 | return value.get([i])
|
1236 | }
|
1237 | } else {
|
1238 |
|
1239 | _value = function () {
|
1240 |
|
1241 | return value
|
1242 | }
|
1243 | }
|
1244 |
|
1245 |
|
1246 | const values = []
|
1247 | const index = []
|
1248 | const ptr = []
|
1249 |
|
1250 |
|
1251 | for (let j = 0; j < columns; j++) {
|
1252 |
|
1253 | ptr.push(values.length)
|
1254 |
|
1255 | const i = j - kSuper
|
1256 |
|
1257 | if (i >= 0 && i < n) {
|
1258 |
|
1259 | const v = _value(i)
|
1260 |
|
1261 | if (!eq(v, zero)) {
|
1262 |
|
1263 | index.push(i + kSub)
|
1264 |
|
1265 | values.push(v)
|
1266 | }
|
1267 | }
|
1268 | }
|
1269 |
|
1270 | ptr.push(values.length)
|
1271 |
|
1272 | return new SparseMatrix({
|
1273 | values: values,
|
1274 | index: index,
|
1275 | ptr: ptr,
|
1276 | size: [rows, columns]
|
1277 | })
|
1278 | }
|
1279 |
|
1280 | |
1281 |
|
1282 |
|
1283 |
|
1284 |
|
1285 |
|
1286 |
|
1287 |
|
1288 |
|
1289 | SparseMatrix.prototype.swapRows = function (i, j) {
|
1290 |
|
1291 | if (!isNumber(i) || !isInteger(i) || !isNumber(j) || !isInteger(j)) {
|
1292 | throw new Error('Row index must be positive integers')
|
1293 | }
|
1294 |
|
1295 | if (this._size.length !== 2) {
|
1296 | throw new Error('Only two dimensional matrix is supported')
|
1297 | }
|
1298 |
|
1299 | validateIndex(i, this._size[0])
|
1300 | validateIndex(j, this._size[0])
|
1301 |
|
1302 |
|
1303 | SparseMatrix._swapRows(i, j, this._size[1], this._values, this._index, this._ptr)
|
1304 |
|
1305 | return this
|
1306 | }
|
1307 |
|
1308 | |
1309 |
|
1310 |
|
1311 |
|
1312 |
|
1313 |
|
1314 |
|
1315 |
|
1316 |
|
1317 | SparseMatrix._forEachRow = function (j, values, index, ptr, callback) {
|
1318 |
|
1319 | const k0 = ptr[j]
|
1320 | const k1 = ptr[j + 1]
|
1321 |
|
1322 | for (let k = k0; k < k1; k++) {
|
1323 |
|
1324 | callback(index[k], values[k])
|
1325 | }
|
1326 | }
|
1327 |
|
1328 | |
1329 |
|
1330 |
|
1331 |
|
1332 |
|
1333 |
|
1334 |
|
1335 |
|
1336 |
|
1337 |
|
1338 | SparseMatrix._swapRows = function (x, y, columns, values, index, ptr) {
|
1339 |
|
1340 | for (let j = 0; j < columns; j++) {
|
1341 |
|
1342 | const k0 = ptr[j]
|
1343 | const k1 = ptr[j + 1]
|
1344 |
|
1345 | const kx = _getValueIndex(x, k0, k1, index)
|
1346 |
|
1347 | const ky = _getValueIndex(y, k0, k1, index)
|
1348 |
|
1349 | if (kx < k1 && ky < k1 && index[kx] === x && index[ky] === y) {
|
1350 |
|
1351 | if (values) {
|
1352 | const v = values[kx]
|
1353 | values[kx] = values[ky]
|
1354 | values[ky] = v
|
1355 | }
|
1356 |
|
1357 | continue
|
1358 | }
|
1359 |
|
1360 | if (kx < k1 && index[kx] === x && (ky >= k1 || index[ky] !== y)) {
|
1361 |
|
1362 | const vx = values ? values[kx] : undefined
|
1363 |
|
1364 | index.splice(ky, 0, y)
|
1365 | if (values) { values.splice(ky, 0, vx) }
|
1366 |
|
1367 | index.splice(ky <= kx ? kx + 1 : kx, 1)
|
1368 | if (values) { values.splice(ky <= kx ? kx + 1 : kx, 1) }
|
1369 |
|
1370 | continue
|
1371 | }
|
1372 |
|
1373 | if (ky < k1 && index[ky] === y && (kx >= k1 || index[kx] !== x)) {
|
1374 |
|
1375 | const vy = values ? values[ky] : undefined
|
1376 |
|
1377 | index.splice(kx, 0, x)
|
1378 | if (values) { values.splice(kx, 0, vy) }
|
1379 |
|
1380 | index.splice(kx <= ky ? ky + 1 : ky, 1)
|
1381 | if (values) { values.splice(kx <= ky ? ky + 1 : ky, 1) }
|
1382 | }
|
1383 | }
|
1384 | }
|
1385 |
|
1386 |
|
1387 | type.Matrix._storage.sparse = SparseMatrix
|
1388 |
|
1389 | return SparseMatrix
|
1390 | }
|
1391 |
|
1392 | exports.name = 'SparseMatrix'
|
1393 | exports.path = 'type'
|
1394 | exports.factory = factory
|
1395 | exports.lazy = false
|