GeoBoater has asked for the wisdom of the Perl Monks concerning the following question:

Oh wisest perl monks,

I'm trying to do some oh-so-simple matrix calculations with Math::Matrix, but the usage of the major functions (cross_product, dot_product, etc) are poorly documented on CPAN, so I'm not sure if I'm even calling them properly.
http://search.cpan.org/~ulpfr/Math-Matrix-0.5/Matrix.pm

Most other functions work, such as print, transpose, etc, but none of the more useful algorithms return anything.

Does anyone have any experience with this? Am I even asking in the right forum?

Here's what I've tried so far:
#! /usr/bin/perl use Math::Matrix; $a = new Math::Matrix ([1], [2], [3]); $x = new Math::Matrix ([4], [5], [6]); $a->print("A\n"); $x->print("X\n"); $f = $a->cross_product( $x ); print "output of a->cross_product( x ) is: '$f'\n\n"; if( defined( $f ) ) { $f->print( "Cross product is \n" ) }; $f = $a->cross_product( $a, $x ); print "output of a->cross_product( a, x ) is: '$f'\n\n"; if( defined( $f ) ) { $f->print( "Cross product is \n" ) };


Script outputs:
$ mathtest.pl A 1.00000 2.00000 3.00000 X 4.00000 5.00000 6.00000 output of a->cross_product( x ) is: '' output of a->cross_product( a, x ) is: ''

Any help would be much appreciated. Thanks!!

Ryan

Replies are listed 'Best First'.
Re: Math::Matrix anyone? (source)
by tye (Sage) on Aug 02, 2006 at 20:43 UTC

    You do realize that you have full access to the source code, right?

    sub cross_product { my $vectors = shift; my $class = ref($vectors); my $dimensions = @{$vectors->[0]}; return undef unless $dimensions == @$vectors + 1;

    indicates that cross_product() expects to receive a single (blessed) matrix that contains the vectors to be productizedmultiplied.

    I'm not saying that this access excuses unclear documentation, of course. But I suspect you can use the source to answer your own questions faster then you'll get answers from someone else. Producing a patch for the module might be a good result of your investigation.

    - tye        

      I know, I know. I gave the source code a quick look, didn't quite understand all of what it was doing (being a lowly noobie perl programmer) and begged for help here. Maybe if I have time I'll go back and play around with it.

      Thanks.

      Ryan
      The comment by tye translates into the syntax for your example:
      $a = new Math::Matrix ([1], [2], [3]); $x = new Math::Matrix ([4], [5], [6]); $f=new Math::Matrix([1, 2, 3],[4, 5, 6]); $f=$f->cross_product;
      Kind of a bizarre syntax. Thanks for everyone who's posted here for helping me put the pieces together to sort this out. I don't know how to write a patch, or I would do so...
Re: Math::Matrix anyone?
by zentara (Cardinal) on Aug 02, 2006 at 19:48 UTC
    I think Math-VectorReal will work better for you.
    #!/usr/bin/perl use warnings; use strict; use Math::VectorReal; my $a = vector( 1, 2, 3 ); my $b = vector( 4, 5, 6 ); print "Vector as string (MatrixReal default format)\n\$a => ", $a; print $a->stringify( "Formated Output \$a => { %g, %g, %g }\n" ); print "Vector as string (MatrixReal default format)\n\$b => ", $b; print $b->stringify( "Formated Output \$b => { %g, %g, %g }\n" ); print 'dot product $a . $b =', $a . $b, "\n\n"; print 'cross product $a x $b =', $a x $b, "\n\n";

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Thanks. I'll give that a try!

      Ryan
Re: Math::Matrix anyone?
by dirving (Friar) on Aug 02, 2006 at 18:48 UTC
    I'm not sure about that particular module, but if you can't get it to work you might want to try PDL, which is very well documented and supported although it contains a lot of extra functionality you might not need. In particular check PDL::Primitive for the cross and dot product functions after you've read PDL::Impatient so you understand the basics.