in reply to Sparse Matrix Multiplication Problem

So, you want to multiply your sparse array by a not-sparse vector @p. Seems straightforward enough:

my @r = (0) x scalar(@p) ; for my $i (0..$#row_index) { $r[$row_index[$i]-1] += $realval[$i] * $p[$col_index[$i]-1] ; } ;

Replies are listed 'Best First'.
Re^2: Sparse Matrix Multiplication Problem
by neversaint (Deacon) on Feb 02, 2009 at 02:58 UTC
    Dear Oshalla,
    Just to let you know. I've ported your ingenious code to C++. They are so indispensable! Thanks a million again.
    #include <iostream> #include <vector> #include <boost/assign/std/vector.hpp> #include <vector> #include <stdio.h> #include <stdlib.h> using namespace boost::assign; using namespace std; int main ( int arg_count, char *arg_vec[] ) { vector <double> realValue; realValue += 0.994704478, 0.989459074, 0.994717023, 1.000000000, 1.000000000, 0.002647761, 0.005282977, 0.000882587, 0.005270463, 0.000882587, 0.002635231, 0.000882587, 0.002635231; vector<int>rowIndex; rowIndex += 1, 2, 3, 4, 5, 1, 3, 1, 2, 1, 2, 1, 2; vector<int>colIndex; colIndex += 1, 2, 3, 4 ,5, 5, 3, 2, 1, 3, 3, 4, 4; vector<int>matDim; matDim += 5,5; // M always equal to N vector <double> theP; theP += 0.4, 0.2, 0.2, 0.2, 0.2; vector <double> Result; Result.assign(theP.size(), 0); for (int i= 0; i < rowIndex.size(); i++) { Result[rowIndex[i]-1] += realValue[i] * theP[colIndex[i]-1]; } // print it for (int k =0; k < Result.size(); k++){ cout << Result[k] << endl; } return 0; }


    ---
    neversaint and everlastingly indebted.......
Re^2: Sparse Matrix Multiplication Problem
by neversaint (Deacon) on Jan 30, 2009 at 09:07 UTC
    Dear oshalla,
    Thank you so much for your reply. It is truly useful.

    I also tried to modify your code to do multiplication of transpose of the sparse matrix. With the following code:
    use Data::Dumper; my @realval = ( 0.994704478, 0.989459074, 0.994717023, 1.000000000, 1.000000000, 0.002647761, 0.005282977, 0.000882587, 0.005270463, 0.000882587, 0.002635231, 0.000882587, 0.002635231,); my @row_index = qw( 1 2 3 4 5 1 3 1 2 1 2 1 2); my @col_index = qw(1 2 3 4 5 5 3 2 1 3 3 4 4); my @mat_dim = (5,5); # M always equal to N my @trans_r = (0) x scalar(@p); for my $j (0..$#row_index) { $trans_r[$col_index[$i]-1] += $realval[$i] * $p[$row_index[$i]-1] ; } print Dumper \@trans_r;
    Not sure why it doesn't give this desired result:
    @result = (0.3989359, 0.1982448, 0.2008801, 0.2008801, 0.2010591);
    Truly need your expert advice on this. Thanks and hope to hear from you again.

    ---
    neversaint and everlastingly indebted.......

      You really should make friends with use strict and its cousin use warnings... With the code as given:

      • use strict complains about $i, which is used in the loop, but doesn't seem to be declared anywhere

        -- that's what it means by 'Global symbol "$i" requires explicit package name'... we just have to accept that when looked at from the correct angle, this makes perfect sense.

      • use warnings would warn you that each time $i is used it is uninitialised (though with use strict you wouldn't get that far !).</c>