Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Dot product of three matrices

by Angharad (Pilgrim)
on Aug 01, 2005 at 16:13 UTC ( [id://479958]=CUFP: print w/replies, xml ) Need Help??

Perl script that calculates the dot product of three matrices.
#!/usr/bin/perl @one = ( 2, 5); @two = ( -4, -3); @three = (2, -6); @result = (); $r = &dProduct(\@one,\@two,\@three); print "\n Dot Product = $r \n"; # ------------------------------------------------ # Returns dot product of three vectors. # Takes three pointers to arrays as input # Returns a scalar. sub dProduct { my ($x,$y, $z) = @_; my $sum; my $ct1 = $#{$x} + 1; # items in $x my $ct2 = $#{$y} + 1; # items in $y my $ct3 = $#{$z} + 1; # items in $z return undef if ($ct1 != $ct2) ; for ($i=0;$i<$ct1;$i++) { $sum += $$x[$i] * $$y[$i] * $$z[$i]; } return $sum; }
::Edited comments::

Replies are listed 'Best First'.
Re: Dot product of three matrices
by davidrw (Prior) on Aug 01, 2005 at 17:18 UTC
    a few comments:
    • why not use an existing solution? quick cpan search found Math::Matrix that has (among other things) a dot_product method.
    • Why are you storing arrays of the components (e.g. @x, @y, @z) instead of storing the vectors, for example:
      my @vector1 = ( 2, -4, 2 ); my @vector2 = ( 5, -3, -6);
      Obviously the logic is the same, but using vectors is much clearer code-wise and mathematically.
    • use strict; and use warnings;
    • shouldn't the length check include ct3?
      return unless $ct1 && $ct1 == $ct2 && $ct2 == $ct3;
    • Simply doing my $ct1 = scalar @$x; might be clearer that adding to $#
    • one (of many possible) more "perl-ish" solutions:
      my $sum = 0; foreach my $i ( 0 .. $#$x ){ $sum += $x->[$i] * $y->[$i] * $z->[$i]; } return $sum;
      Another:
      my $sum = 0; $sum += $x->[$_] * $y->[$_] * $z->[$_] for 0 .. $#$x; return $sum;

      It is little enough code that writing it yourself is likely often much less time-consuming than finding an appropriate solution, much less getting that solution installed. But, I'd have implemented in terms of some useful abstractions that I personally keep at-hand:

      use List::Util 'reduce'; use Algorithm::Loops 'MapCarE'; my $prod= reduce {$a+$b} MapCarE { reduce {$a*$b} @_ } \( @x, @y, @z ) +;

      - tye        

Re: Dot product of three matrices
by Anonymous Monk on Aug 01, 2005 at 18:07 UTC
    I guess I'm a little rusty in vector math, but can anyone tell me what the dot product of three matrices means? (i.e. is there a geometric interpretation?) And in what field of study does it come in handy?
      I think by "three matrices" OP meant "two vectors" .. it's just that instead of having $Ax,$Ay,$Az and $Bx, $By, $Bz, OP is instead passing [$Ax, $Bx], [$Ay,$By], [$Az,$Bz] to his sub.
        Is he?
        $sum += $$x[$i] * $$y[$i] * $$z[$i];
        The above code implies bad things but he is in fact calculating the dot product of 3 vectors that are 2D. To sum it up: not the clearest code ever :-)
Re: Dot product of three matrices
by Angharad (Pilgrim) on Aug 02, 2005 at 10:10 UTC
    Thanks everyone for the feedback. Much appreciated. I really must apologise ... originally I wrote the program with two vectors just as a tester before adding the third and I've just noticed I didnt update my comments to reflect this. Shame on me.
    It is the dot product of three matrices. I wrote it in part solution (hopefully) for a larger task I have to solve.
    Thanks again for the comments.
    p.s. I'm a she ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://479958]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found