Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^2: Sparse Matrix Multiplication Problem

by neversaint (Deacon)
on Jan 30, 2009 at 09:07 UTC ( #740124=note: print w/replies, xml ) Need Help??

in reply to Re: Sparse Matrix Multiplication Problem
in thread Sparse Matrix Multiplication Problem

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.......

Replies are listed 'Best First'.
Re^3: Sparse Matrix Multiplication Problem
by gone2015 (Deacon) on Jan 30, 2009 at 09:50 UTC

    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>

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://740124]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2020-02-24 00:46 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (104 votes). Check out past polls.