⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blitz_3.html

📁 c++经典教材 Blitz++ v0.8
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<HTML><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><!-- Created on November, 4  2004 by texi2html 1.64 --><!-- Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author)            Karl Berry  <karl@freefriends.org>            Olaf Bachmann <obachman@mathematik.uni-kl.de>            and many others.Maintained by: Olaf Bachmann <obachman@mathematik.uni-kl.de>Send bugs and suggestions to <texi2html@mathematik.uni-kl.de> --><HEAD><TITLE>Blitz++: Array Expressions</TITLE><META NAME="description" CONTENT="Blitz++: Array Expressions"><META NAME="keywords" CONTENT="Blitz++: Array Expressions"><META NAME="resource-type" CONTENT="document"><META NAME="distribution" CONTENT="global"><META NAME="Generator" CONTENT="texi2html 1.64"></HEAD><BODY LANG="" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000"><A NAME="SEC74"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_2.html#SEC73"> &lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC75"> &gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC98"> &lt;&lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC98"> &gt;&gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H1> 3. Array Expressions </H1><!--docid::SEC74::--><BLOCKQUOTE><TABLE BORDER=0 CELLSPACING=0> <TR><TD ALIGN="left" VALIGN="TOP"><A HREF="blitz_3.html#SEC75">3.1 Expression evaluation order</A></TD><TD>&nbsp;&nbsp;</TD><TD ALIGN="left" VALIGN="TOP">Creating expressions with Array's</TD></TR><TR><TD ALIGN="left" VALIGN="TOP"><A HREF="blitz_3.html#SEC82">3.6 Index placeholders</A></TD><TD>&nbsp;&nbsp;</TD><TD ALIGN="left" VALIGN="TOP">Array indices functionality</TD></TR><TR><TD ALIGN="left" VALIGN="TOP"><A HREF="blitz_3.html#SEC86">3.8 Single-argument math functions</A></TD><TD>&nbsp;&nbsp;</TD><TD ALIGN="left" VALIGN="TOP">Single-argument math functions on Array's</TD></TR><TR><TD ALIGN="left" VALIGN="TOP"><A HREF="blitz_3.html#SEC89">3.9 Two-argument math functions</A></TD><TD>&nbsp;&nbsp;</TD><TD ALIGN="left" VALIGN="TOP">Two-argument math functions on Array's</TD></TR><TR><TD ALIGN="left" VALIGN="TOP"><A HREF="blitz_3.html#SEC92">3.10 Declaring your own math functions on arrays</A></TD><TD>&nbsp;&nbsp;</TD><TD ALIGN="left" VALIGN="TOP">Creating your math functions on Array's</TD></TR><TR><TD ALIGN="left" VALIGN="TOP"><A HREF="blitz_3.html#SEC97">3.15 where statements</A></TD><TD>&nbsp;&nbsp;</TD><TD ALIGN="left" VALIGN="TOP">The where statement</TD></TR></TABLE></BLOCKQUOTE><P><A NAME="IDX161"></A><A NAME="IDX162"></A><A NAME="IDX163"></A><A NAME="IDX164"></A><A NAME="IDX165"></A></P><P>Array expressions in Blitz++ are implemented using the <EM>expressiontemplates</EM> technique.  Unless otherwise noted, expression evaluation willnever generate temporaries or multiple loops; an expression such as</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>Array&#60;int,1&#62; A, B, C, D;    // ...A = B + C + D;</pre></td></tr></table></P><P>will result in code similar to</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>for (int i=A.lbound(firstDim); i &#60;= A.ubound(firstDim); ++i)    A[i] = B[i] + C[i] + D[i];</pre></td></tr></table></P><P><A NAME="Array expressions"></A><HR SIZE="6"><A NAME="SEC75"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC74"> &lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC76"> &gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC74"> &lt;&lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC74"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC98"> &gt;&gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H2> 3.1 Expression evaluation order </H2><!--docid::SEC75::--><P>A commonly asked question about Blitz++ is what order it uses to evaluatearray expressions.  For example, in code such as</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>A(Range(2,10)) = A(Range(1,9))</pre></td></tr></table></P><P>does the expression get evaluated at indices 1, 2, ..., 9 or at 9, 8, ...,1?  This makes a big difference to the result: in one case, the array willbe shifted to the right by one element; in the other case, most of the arrayelements will be set to the value in <CODE>A(1)</CODE>.</P><P>Blitz always selects the traversal order it thinks will be fastest.  For 1Darrays, this means it will go from beginning to the end of the array inmemory (see notes below).  For multidimensional arrays, it will do one oftwo things:</P><P><UL><LI>try to go through the destination array in the order it is laid outin memory (i.e.  row-major for row-major arrays, column-major forcolumn-major arrays).<P><LI>if the expression is a stencil, Blitz will do tiling to improve cacheuse.  Under some circumstances blitz will even use a traversal based on ahilbert curve (a fractal) for 3D arrays.  <P></UL><P>Because the traversal order is not always predictable, it is safest to putthe result in a new array if you are doing a stencil-style expression.Blitz guarantees this will always work correctly.  If you try to put theresult in one of the operands, you have to guess correctly which traversalorder blitz will choose.  This is easy for the 1D case, but hard for themultidimensional case.</P><P>Some special notes about 1D array traversals:</P><P><UL><LI>if your array is stored in reverse order, i.e. because of aA.reverse(firstDim) or funny storage order, blitz will go through the arrayfrom end to beginning in array coordinates, but from beginning to end inmemory locations.<P><LI>many compilers/architecture combinations are equally fast at reverseorder.  But blitz has a specialized version for stride = +1, and it would bewasteful to also specialize for the case stride = -1.  So 1D arrays aretraversed from beginning to end (in memory storage order).<P></UL><P><HR SIZE="6"><A NAME="SEC76"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC75"> &lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC77"> &gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC77"> &lt;&lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC74"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC98"> &gt;&gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H2> 3.2 Expression operands </H2><!--docid::SEC76::--><P>An expression can contain any mix of these operands:</P><P><UL><LI>An array of any type, so long as it is of the same rank.Expressions which contain a mixture of array types are handled through thetype promotion mechanism described below.<P><LI>Scalars of type <CODE>int</CODE>, <CODE>float</CODE>, <CODE>double</CODE>,<CODE>long double</CODE>, or <CODE>complex&#60;T&#62;</CODE><P><LI>Index placeholders, described below<P><LI>Other expressions (e.g. <CODE>A+(B+C)</CODE>)</UL><P><HR SIZE="6"><A NAME="SEC77"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC76"> &lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC78"> &gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC80"> &lt;&lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC74"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC80"> &gt;&gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H2> 3.3 Array operands </H2><!--docid::SEC77::--><P><HR SIZE="6"><A NAME="SEC78"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC77"> &lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC79"> &gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[ &lt;&lt; ]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[ &gt;&gt; ]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H3> Using subarrays in an expression </H3><!--docid::SEC78::--><P><A NAME="IDX166"></A></P><P>Subarrays may be used in an expression.  For example, this code exampleperforms a 5-point average on a two-dimensional array:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>Array&#60;float,2&#62; A(64,64), B(64,64);   // ...Range I(1,62), J(1,62);A(I,J) = (B(I,J) + B(I+1,J) + B(I-1,J)                  + B(I,J+1) + B(I,J-1)) / 5;</pre></td></tr></table></P><P><HR SIZE="6"><A NAME="SEC79"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC78"> &lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC80"> &gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[ &lt;&lt; ]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[ &gt;&gt; ]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H3> Mixing arrays with different storage formats </H3><!--docid::SEC79::--><P><A NAME="IDX167"></A></P><P>Arrays with different storage formats (for example, C-style andFortran-style) can be mixed in the same expression.  Blitz++ will handle thedifferent storage formats automatically.  However:</P><P><UL><LI>Evaluation may be slower, since a different traversal order may beused.<P><LI>If you are using index placeholders (see below) or reductions inthe expression, you may <STRONG>not</STRONG> mix array objects with differentstarting bases.  <P></UL><P><HR SIZE="6"><A NAME="SEC80"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC79"> &lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC81"> &gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC81"> &lt;&lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC74"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC98"> &gt;&gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H2> 3.4 Expression operators </H2><!--docid::SEC80::--><P>These binary operators are supported:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>+ - * / % &#62; &#60; &#62;= &#60;= == != &#38;&#38; || ^ &#38; | </pre></td></tr></table></P><P>Note: operator <CODE>&#60;&#60;</CODE> and <CODE>&#62;&#62;</CODE> are reserved for use in input/output.If you need a bit-shift operation on arrays, you may define one yourself;see <A HREF="blitz_3.html#SEC92">3.10 Declaring your own math functions on arrays</A>.</P><P>These unary operators are supported:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>- ~ !</pre></td></tr></table></P><P>The operators <CODE>&#62; &#60; &#62;= &#60;= == != &#38;&#38; || !</CODE> result in a bool-valuedexpression.</P><P><A NAME="IDX168"></A>All operators are applied <EM>elementwise</EM>.</P><P><A NAME="IDX169"></A>You can only use operators which are well-defined for the number type storedin the arrays.  For example, bitwise XOR (<CODE>^</CODE>) is meaningful forintegers, so this code is all right:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>Array&#60;int,3&#62; A, B, C;   // ...A = B ^ C;</pre></td></tr></table></P><P>Bitwise XOR is <EM>not</EM> meaningful on floating point types, so this codewill generate a compiler error:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>Array&#60;float,1&#62; A, B, C;   // ...C = B ^ C;</pre></td></tr></table></P><P>Here's the compiler error generated by KAI C++ for the above code:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>"../../blitz/ops.h", line 85: error: expression must have integral or enum type  BZ_DEFINE_OP(BitwiseXor,^);  ^          detected during:            instantiation of "blitz::BitwiseXor&#60;float, float&#62;::T_numtype                      blitz::BitwiseXor&#60;float, float&#62;::apply(float, float)" at                      line 210 of "../../blitz/arrayexpr.h"            instantiation of ...                     .                     .</pre></td></tr></table></P><P><A NAME="IDX170"></A>If you are creating arrays using a type you have created yourself, you willneed to overload whatever operators you want to use on arrays.  For example,if I create a class <CODE>Polynomial</CODE>, and want to write code such as:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>Array&#60;Polynomial,2&#62; A, B, C;   // ...C = A * B;</pre></td></tr></table></P><P>I would have to provide <CODE>operator*</CODE> for <CODE>Polynomial</CODE> byimplementing</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>Polynomial Polynomial::operator*(Polynomial);)</pre></td></tr></table></P><P>or</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>Polynomial operator*(Polynomial, Polynomial);)</pre></td></tr></table></P><P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -