Sparse Matrix functions and a ccsScatter() feature request

52 views
Skip to first unread message

Yotam G

unread,
Oct 22, 2014, 8:42:56 PM10/22/14
to nume...@googlegroups.com
I love numeric.js. I am using it for a programming assignment, and we're making heavy use of sparse matrices. I gave my students the following extra functions. It would be great if they were included in a future version of numeric.js.

// Patch numeric to add a sparse matrix transpose routine.
numeric
.ccsTranspose = function( A )
{
   
var rows_cols_vals = numeric.ccsGather( A );
   
// Swap the rows and cols.
   
return numeric.ccsScatter( [ rows_cols_vals[1], rows_cols_vals[0], rows_cols_vals[2] ] );
}
// Patch numeric to add a sparse matrix diagonal routine.
numeric
.ccsDiag = function( diag )
{
   
var ij = [];
   
for( var i = 0; i < diag.length; ++i ) ij.push( i );
   
return numeric.ccsScatter( [ ij, ij, diag ] );
}
// Patch numeric to add a sparse matrix identity.
numeric
.ccsIdentity = function( N )
{
   
return numeric.ccsDiag( numeric.rep( [N], 1. ) );
}
// Patch numeric to add a sparse matrix column sum.
// NOTE: There is no sparse matrix row sum, because it would have to first compute the transpose each time.
numeric
.ccsSumColumn = function( A, col )
{
   
var sum = 0.;
   
for( var k = A[0][col]; k < A[0][col+1]; ++k ) sum += A[2][k];
   
return sum;
}

I have a feature request related to sparse matrices. The ccsScatter/ccsGather routines are incredibly useful. I've found with compiled code that triplets are often the fastest way to construct matrices. numeric.ccsScatter() behaves slightly differently than most forms I am used to; usually, if the same i,j appears multiply times, all values are summed together in the final matrix. In numeric.ccsScatter(), it seems only the final value it used. The usual behavior is actually very convenient, so it would be great if numeric adopted it.

Also, a note about the documentation for ccsScatter/ccsGather. It would be clearer if the second line read "SX = numeric.ccsScatter(X); numeric.ccsFull(SX);", because displaying the result of ccsScatter() displays a ccs matrix, in which it is difficult to see the relationship between the three i,j,value arrays and the matrix that is constructed.

Thanks again,
Yotam

Yotam G

unread,
Oct 23, 2014, 2:03:10 AM10/23/14
to nume...@googlegroups.com
Two more requests: A sparse matrix * vector operation, and a way to tell ccsScatter what the dimensions of the matrix are, because the number of rows or columns may be larger than the maximum row or column that appears in the triplets.

Yotam

Sébastien Loisel

unread,
Oct 24, 2014, 5:47:34 AM10/24/14
to nume...@googlegroups.com

Hi Yotam,

Thanks for your emails. What you send looks good. I will try to do this by the holidays!

S

--
You received this message because you are subscribed to the Google Groups "numericjs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numericjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages