@@ -170,14 +170,32 @@ export class SparseMatrix {
170
170
const m = this . rows ;
171
171
const p = other . columns ;
172
172
173
+ const {
174
+ columns : otherCols ,
175
+ rows : otherRows ,
176
+ values : otherValues ,
177
+ } = other . getNonZeros ( ) ;
178
+ const {
179
+ columns : thisCols ,
180
+ rows : thisRows ,
181
+ values : thisValues ,
182
+ } = this . getNonZeros ( ) ;
183
+
173
184
const result = new SparseMatrix ( m , p ) ;
174
- this . withEachNonZero ( ( i , j , v1 ) => {
175
- other . withEachNonZero ( ( k , l , v2 ) => {
176
- if ( j === k ) {
177
- result . set ( i , l , result . get ( i , l ) + v1 * v2 ) ;
185
+
186
+ const nbOtherActive = otherCols . length ;
187
+ const nbThisActive = thisCols . length ;
188
+ for ( let t = 0 ; t < nbThisActive ; t ++ ) {
189
+ const i = thisRows [ t ] ;
190
+ const j = thisCols [ t ] ;
191
+ for ( let o = 0 ; o < nbOtherActive ; o ++ ) {
192
+ if ( j === otherRows [ o ] ) {
193
+ const l = otherCols [ o ] ;
194
+ result . set ( i , l , result . get ( i , l ) + otherValues [ o ] * thisValues [ t ] ) ;
178
195
}
179
- } ) ;
180
- } ) ;
196
+ }
197
+ }
198
+
181
199
return result ;
182
200
}
183
201
@@ -195,13 +213,31 @@ export class SparseMatrix {
195
213
initialCapacity : this . cardinality * other . cardinality ,
196
214
} ) ;
197
215
198
- this . withEachNonZero ( ( i , j , v1 ) => {
199
- const pi = p * i ;
200
- const qj = q * j ;
201
- other . withEachNonZero ( ( k , l , v2 ) => {
202
- result . set ( pi + k , qj + l , v1 * v2 ) ;
203
- } ) ;
204
- } ) ;
216
+ const {
217
+ columns : otherCols ,
218
+ rows : otherRows ,
219
+ values : otherValues ,
220
+ } = other . getNonZeros ( ) ;
221
+ const {
222
+ columns : thisCols ,
223
+ rows : thisRows ,
224
+ values : thisValues ,
225
+ } = this . getNonZeros ( ) ;
226
+
227
+ const nbOtherActive = otherCols . length ;
228
+ const nbThisActive = thisCols . length ;
229
+ for ( let t = 0 ; t < nbThisActive ; t ++ ) {
230
+ const pi = p * thisRows [ t ] ;
231
+ const qj = q * thisCols [ t ] ;
232
+ for ( let o = 0 ; o < nbOtherActive ; o ++ ) {
233
+ result . set (
234
+ pi + otherRows [ o ] ,
235
+ qj + otherCols [ o ] ,
236
+ otherValues [ o ] * thisValues [ t ] ,
237
+ ) ;
238
+ }
239
+ }
240
+
205
241
return result ;
206
242
}
207
243
@@ -258,12 +294,11 @@ export class SparseMatrix {
258
294
/** @type {number[] } */
259
295
const values = new Array ( cardinality ) ;
260
296
let idx = 0 ;
261
- this . forEachNonZero ( ( i , j , value ) => {
297
+ this . withEachNonZero ( ( i , j , value ) => {
262
298
rows [ idx ] = i ;
263
299
columns [ idx ] = j ;
264
300
values [ idx ] = value ;
265
301
idx ++ ;
266
- return value ;
267
302
} ) ;
268
303
return { rows, columns, values } ;
269
304
}
@@ -287,9 +322,8 @@ export class SparseMatrix {
287
322
let trans = new SparseMatrix ( this . columns , this . rows , {
288
323
initialCapacity : this . cardinality ,
289
324
} ) ;
290
- this . forEachNonZero ( ( i , j , value ) => {
325
+ this . withEachNonZero ( ( i , j , value ) => {
291
326
trans . set ( j , i , value ) ;
292
- return value ;
293
327
} ) ;
294
328
return trans ;
295
329
}
0 commit comments