UNPKG

4.46 kBJavaScriptView Raw
1import { factory } from '../../../utils/factory.js';
2import { DimensionError } from '../../../error/DimensionError.js';
3import { scatter } from '../../../utils/collection.js';
4var name = 'algorithm06';
5var dependencies = ['typed', 'equalScalar'];
6export var createAlgorithm06 = /* #__PURE__ */factory(name, dependencies, _ref => {
7 var {
8 typed,
9 equalScalar
10 } = _ref;
11
12 /**
13 * Iterates over SparseMatrix A and SparseMatrix B nonzero items and invokes the callback function f(Aij, Bij).
14 * Callback function invoked (Anz U Bnz) times, where Anz and Bnz are the nonzero elements in both matrices.
15 *
16 *
17 * ┌ f(Aij, Bij) ; A(i,j) !== 0 && B(i,j) !== 0
18 * C(i,j) = ┤
19 * └ 0 ; otherwise
20 *
21 *
22 * @param {Matrix} a The SparseMatrix instance (A)
23 * @param {Matrix} b The SparseMatrix instance (B)
24 * @param {Function} callback The f(Aij,Bij) operation to invoke
25 *
26 * @return {Matrix} SparseMatrix (C)
27 *
28 * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97620294
29 */
30 return function algorithm06(a, b, callback) {
31 // sparse matrix arrays
32 var avalues = a._values;
33 var asize = a._size;
34 var adt = a._datatype; // sparse matrix arrays
35
36 var bvalues = b._values;
37 var bsize = b._size;
38 var bdt = b._datatype; // validate dimensions
39
40 if (asize.length !== bsize.length) {
41 throw new DimensionError(asize.length, bsize.length);
42 } // check rows & columns
43
44
45 if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) {
46 throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')');
47 } // rows & columns
48
49
50 var rows = asize[0];
51 var columns = asize[1]; // datatype
52
53 var dt; // equal signature to use
54
55 var eq = equalScalar; // zero value
56
57 var zero = 0; // callback signature to use
58
59 var cf = callback; // process data types
60
61 if (typeof adt === 'string' && adt === bdt) {
62 // datatype
63 dt = adt; // find signature that matches (dt, dt)
64
65 eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype
66
67 zero = typed.convert(0, dt); // callback
68
69 cf = typed.find(callback, [dt, dt]);
70 } // result arrays
71
72
73 var cvalues = avalues && bvalues ? [] : undefined;
74 var cindex = [];
75 var cptr = []; // workspaces
76
77 var x = cvalues ? [] : undefined; // marks indicating we have a value in x for a given column
78
79 var w = []; // marks indicating value in a given row has been updated
80
81 var u = []; // loop columns
82
83 for (var j = 0; j < columns; j++) {
84 // update cptr
85 cptr[j] = cindex.length; // columns mark
86
87 var mark = j + 1; // scatter the values of A(:,j) into workspace
88
89 scatter(a, j, w, x, u, mark, cindex, cf); // scatter the values of B(:,j) into workspace
90
91 scatter(b, j, w, x, u, mark, cindex, cf); // check we need to process values (non pattern matrix)
92
93 if (x) {
94 // initialize first index in j
95 var k = cptr[j]; // loop index in j
96
97 while (k < cindex.length) {
98 // row
99 var i = cindex[k]; // check function was invoked on current row (Aij !=0 && Bij != 0)
100
101 if (u[i] === mark) {
102 // value @ i
103 var v = x[i]; // check for zero value
104
105 if (!eq(v, zero)) {
106 // push value
107 cvalues.push(v); // increment pointer
108
109 k++;
110 } else {
111 // remove value @ i, do not increment pointer
112 cindex.splice(k, 1);
113 }
114 } else {
115 // remove value @ i, do not increment pointer
116 cindex.splice(k, 1);
117 }
118 }
119 } else {
120 // initialize first index in j
121 var p = cptr[j]; // loop index in j
122
123 while (p < cindex.length) {
124 // row
125 var r = cindex[p]; // check function was invoked on current row (Aij !=0 && Bij != 0)
126
127 if (u[r] !== mark) {
128 // remove value @ i, do not increment pointer
129 cindex.splice(p, 1);
130 } else {
131 // increment pointer
132 p++;
133 }
134 }
135 }
136 } // update cptr
137
138
139 cptr[columns] = cindex.length; // return sparse matrix
140
141 return a.createSparseMatrix({
142 values: cvalues,
143 index: cindex,
144 ptr: cptr,
145 size: [rows, columns],
146 datatype: dt
147 });
148 };
149});
\No newline at end of file