@@ -158,14 +158,32 @@ export class SparseMatrix {
158
158
const m = this . rows ;
159
159
const p = other . columns ;
160
160
161
+ const {
162
+ columns : otherCols ,
163
+ rows : otherRows ,
164
+ values : otherValues ,
165
+ } = other . getNonZeros ( ) ;
166
+ const {
167
+ columns : thisCols ,
168
+ rows : thisRows ,
169
+ values : thisValues ,
170
+ } = this . getNonZeros ( ) ;
171
+
161
172
const result = new SparseMatrix ( m , p ) ;
162
- this . withEachNonZero ( ( i , j , v1 ) => {
163
- other . withEachNonZero ( ( k , l , v2 ) => {
164
- if ( j === k ) {
165
- result . set ( i , l , result . get ( i , l ) + v1 * v2 ) ;
173
+
174
+ const nbOtherActive = otherCols . length ;
175
+ const nbThisActive = thisCols . length ;
176
+ for ( let t = 0 ; t < nbThisActive ; t ++ ) {
177
+ const i = thisRows [ t ] ;
178
+ const j = thisCols [ t ] ;
179
+ for ( let o = 0 ; o < nbOtherActive ; o ++ ) {
180
+ if ( j === otherRows [ o ] ) {
181
+ const l = otherCols [ o ] ;
182
+ result . set ( i , l , result . get ( i , l ) + otherValues [ o ] * thisValues [ t ] ) ;
166
183
}
167
- } ) ;
168
- } ) ;
184
+ }
185
+ }
186
+
169
187
return result ;
170
188
}
171
189
@@ -179,13 +197,31 @@ export class SparseMatrix {
179
197
initialCapacity : this . cardinality * other . cardinality ,
180
198
} ) ;
181
199
182
- this . withEachNonZero ( ( i , j , v1 ) => {
183
- const pi = p * i ;
184
- const qj = q * j ;
185
- other . withEachNonZero ( ( k , l , v2 ) => {
186
- result . set ( pi + k , qj + l , v1 * v2 ) ;
187
- } ) ;
188
- } ) ;
200
+ const {
201
+ columns : otherCols ,
202
+ rows : otherRows ,
203
+ values : otherValues ,
204
+ } = other . getNonZeros ( ) ;
205
+ const {
206
+ columns : thisCols ,
207
+ rows : thisRows ,
208
+ values : thisValues ,
209
+ } = this . getNonZeros ( ) ;
210
+
211
+ const nbOtherActive = otherCols . length ;
212
+ const nbThisActive = thisCols . length ;
213
+ for ( let t = 0 ; t < nbThisActive ; t ++ ) {
214
+ const pi = p * thisRows [ t ] ;
215
+ const qj = q * thisCols [ t ] ;
216
+ for ( let o = 0 ; o < nbOtherActive ; o ++ ) {
217
+ result . set (
218
+ pi + otherRows [ o ] ,
219
+ qj + otherCols [ o ] ,
220
+ otherValues [ o ] * thisValues [ t ] ,
221
+ ) ;
222
+ }
223
+ }
224
+
189
225
return result ;
190
226
}
191
227
@@ -229,12 +265,11 @@ export class SparseMatrix {
229
265
const columns = new Array ( cardinality ) ;
230
266
const values = new Array ( cardinality ) ;
231
267
let idx = 0 ;
232
- this . forEachNonZero ( ( i , j , value ) => {
268
+ this . withEachNonZero ( ( i , j , value ) => {
233
269
rows [ idx ] = i ;
234
270
columns [ idx ] = j ;
235
271
values [ idx ] = value ;
236
272
idx ++ ;
237
- return value ;
238
273
} ) ;
239
274
return { rows, columns, values } ;
240
275
}
@@ -254,9 +289,8 @@ export class SparseMatrix {
254
289
let trans = new SparseMatrix ( this . columns , this . rows , {
255
290
initialCapacity : this . cardinality ,
256
291
} ) ;
257
- this . forEachNonZero ( ( i , j , value ) => {
292
+ this . withEachNonZero ( ( i , j , value ) => {
258
293
trans . set ( j , i , value ) ;
259
- return value ;
260
294
} ) ;
261
295
return trans ;
262
296
}
0 commit comments