📄 ch02_15.htm
字号:
<HTML><HEAD><TITLE>Recipe 2.14. Multiplying Matrices (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:30:19Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch02_01.htm"TITLE="2. Numbers"><LINKREL="prev"HREF="ch02_14.htm"TITLE="2.13. Taking Logarithms"><LINKREL="next"HREF="ch02_16.htm"TITLE="2.15. Using Complex Numbers"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_14.htm"TITLE="2.13. Taking Logarithms"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 2.13. Taking Logarithms"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch02_01.htm"TITLE="2. Numbers"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_16.htm"TITLE="2.15. Using Complex Numbers"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 2.15. Using Complex Numbers"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch02-79550">2.14. Multiplying Matrices</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-993">Problem <ACLASS="indexterm"NAME="ch02-idx-1000008093-0"></A><ACLASS="indexterm"NAME="ch02-idx-1000008093-1"></A><ACLASS="indexterm"NAME="ch02-idx-1000008093-2"></A><ACLASS="indexterm"NAME="ch02-idx-1000008093-3"></A><ACLASS="indexterm"NAME="ch02-idx-1000008093-4"></A></A></H3><PCLASS="para">You want to multiply a pair of two-dimensional arrays. Mathematicians and engineers often need this.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-999">Solution</A></H3><PCLASS="para">Use the <ACLASS="indexterm"NAME="ch02-idx-1000008086-0"></A>PDL modules, available from CPAN. PDL is the <CITECLASS="citetitle">Perl Data Language</CITE> - modules that give fast access to compact matrix and mathematical functions:</P><PRECLASS="programlisting">use PDL;# $a and $b are both pdl objects$c = $a * $b;</PRE><PCLASS="para">Alternatively, apply the matrix multiplication algorithm to your two-dimensional array:</P><PRECLASS="programlisting">sub mmult { my ($m1,$m2) = @_; my ($m1rows,$m1cols) = matdim($m1); my ($m2rows,$m2cols) = matdim($m2); unless ($m1cols == $m2rows) { # raise exception die "IndexError: matrices don't match: $m1cols != $m2rows"; } my $result = []; my ($i, $j, $k); for $i (range($m1rows)) { for $j (range($m2cols)) { for $k (range($m1cols)) { $result->[$i][$j] += $m1->[$i][$k] * $m2->[$k][$j]; } } } return $result;}sub range { 0 .. ($_[0] - 1) }sub veclen { my $ary_ref = $_[0]; my $type = ref $ary_ref; if ($type ne "ARRAY") { die "$type is bad array ref for $ary_ref" } return scalar(@$ary_ref);}sub matdim { my $matrix = $_[0]; my $rows = veclen($matrix); my $cols = veclen($matrix->[0]); return ($rows, $cols);}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-1087">Discussion</A></H3><PCLASS="para">If you have the PDL library installed, you can make use of its lightning-fast manipulation of numbers. This requires far less memory and CPU than Perl's array manipulation. When using PDL objects, many numeric operators (such as <CODECLASS="literal">+</CODE> and <CODECLASS="literal">*</CODE>) are overloaded and work on an element-by-element basis (e.g., <CODECLASS="literal">*</CODE> is the so-called <EMCLASS="emphasis">scalar multiplication</EM> operator). To get true matrix multiplication, use the overloaded <CODECLASS="literal">x</CODE><ACLASS="indexterm"NAME="ch02-idx-1000008097-0"></A><ACLASS="indexterm"NAME="ch02-idx-1000008097-1"></A> operator.</P><PRECLASS="programlisting">use PDL;$a = pdl [ [ 3, 2, 3 ], [ 5, 9, 8 ],];$b = pdl [ [ 4, 7 ], [ 9, 3 ], [ 8, 1 ],];$c = $a x $b; # x overload</PRE><PCLASS="para">If you don't have the PDL library, or don't feel like pulling it in for a small problem, you can always do the work yourself the good old-fashioned way.</P><PRECLASS="programlisting"># mmult() and other subroutines as above$x = [ [ 3, 2, 3 ], [ 5, 9, 8 ],];$y = [ [ 4, 7 ], [ 9, 3 ], [ 8, 1 ],];$z = mmult($x, $y);</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-1151">See Also</A></H3><PCLASS="para">The documentation with the CPAN module PDL<ACLASS="indexterm"NAME="ch02-idx-1000008088-0"></A><ACLASS="indexterm"NAME="ch02-idx-1000008088-1"></A><ACLASS="indexterm"NAME="ch02-idx-1000008088-2"></A><ACLASS="indexterm"NAME="ch02-idx-1000008088-3"></A><ACLASS="indexterm"NAME="ch02-idx-1000008088-4"></A><ACLASS="indexterm"NAME="ch02-idx-1000008088-5"></A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_14.htm"TITLE="2.13. Taking Logarithms"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 2.13. Taking Logarithms"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_16.htm"TITLE="2.15. Using Complex Numbers"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 2.15. Using Complex Numbers"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">2.13. Taking Logarithms</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">2.15. Using Complex Numbers</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -