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

📄 blitz_3.html

📁 c++经典教材 Blitz++ v0.8
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<HR SIZE="6"><A NAME="SEC81"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC80"> &lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC82"> &gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC82"> &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.5 Assignment operators </H2><!--docid::SEC81::--><P><A NAME="IDX171"></A>These assignment operators are supported:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>= += -= *= /= %= ^= &#38;= |= &#62;&#62;= &#60;&#60;=</pre></td></tr></table></P><P>An array object should appear on the left side of the operator.  The rightside can be:</P><P><UL><LI>A constant (or literal) of type <CODE>T_numtype</CODE><P><LI>An array of appropriate rank, possibly of a different numeric type<P><LI>An array expression, with appropriate rank and shape<P></UL><P><A NAME="Index placeholders"></A><HR SIZE="6"><A NAME="SEC82"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC81"> &lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC83"> &gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC83"> &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.6 Index placeholders </H2><!--docid::SEC82::--><P>Blitz++ provides objects called <EM>index placeholders</EM> which representarray indices.  They can be used directly in expressions.</P><P>There is a distinct index placeholder type associated with each dimension ofan array.  The types are called <CODE>firstIndex</CODE>, <CODE>secondIndex</CODE>,<CODE>thirdIndex</CODE>, ..., <CODE>tenthIndex</CODE>, <CODE>eleventhIndex</CODE>.<A NAME="IDX172"></A><A NAME="IDX173"></A><A NAME="IDX174"></A><A NAME="IDX175"></A>Here's an example of using an index placeholder:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>Array&#60;float,1&#62; A(10);firstIndex i;A = i;</pre></td></tr></table></P><P>This generates code which is similar to:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>for (int i=0; i &#60; A.length(); ++i)    A(i) = i;</pre></td></tr></table></P><P>Here's an example which fills an array with a sampled sine wave:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>Array&#60;float,1&#62; A(16);firstIndex i;A = sin(2 * M_PI * i / 16.);</pre></td></tr></table></P><P>If your destination array has rank greater than 1, you may usemultiple index placeholders:</P><P><A NAME="IDX176"></A></P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>// Fill a two-dimensional array with a radially// symmetric, decaying sinusoid// Create the arrayint N = 64;           Array&#60;float,2&#62; F(N,N);// Some parametersfloat midpoint = (N-1)/2.;int cycles = 3;float omega = 2.0 * M_PI * cycles / double(N);float tau = - 10.0 / N;// Index placeholdersfirstIndex i;secondIndex j;// Fill the arrayF = cos(omega * sqrt(pow2(i-midpoint) + pow2(j-midpoint)))    * exp(tau * sqrt(pow2(i-midpoint) + pow2(j-midpoint)));</pre></td></tr></table></P><P>Here's a plot of the resulting array:</P><P><center> <CENTER><IMG SRC="sinsoid.gif" ALT="sinsoid"></CENTER></center><center> Array filled using an index placeholder expression.</center></P><P>You can use index placeholder expressions in up to 11 dimensions.Here's a three dimensional example:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>// Fill a three-dimensional array with a Gaussian functionArray&#60;float,3&#62; A(16,16,16);firstIndex i;secondIndex j;thirdIndex k;float midpoint = 15/2.;float c = - 1/3.0;A = exp(c * (sqr(i-midpoint) + sqr(j-midpoint)     + sqr(k-midpoint)));</pre></td></tr></table></P><P>You can mix array operands and index placeholders:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>Array&#60;int,1&#62; A(5), B(5);firstIndex i;A = 0, 1, 1, 0, 2;B = i * A;          // Results in [ 0, 1, 2, 0, 8 ]</pre></td></tr></table></P><P>For your convenience, there is a namespace within blitzcalled <CODE>tensor</CODE> which declares all the index placeholders:</P><P><A NAME="IDX177"></A><A NAME="IDX178"></A><A NAME="IDX179"></A><A NAME="IDX180"></A><A NAME="IDX181"></A><A NAME="IDX182"></A><A NAME="IDX183"></A></P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>namespace blitz {  namespace tensor {    firstIndex i;    secondIndex j;    thirdIndex k;     ...    eleventhIndex t;  }}</pre></td></tr></table></P><P>So instead of declaring your own index placeholder objects,you can just say </P><P><A NAME="IDX184"></A></P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>namespace blitz::tensor;</pre></td></tr></table>  when you would like to use them.  Alternately, you can just preface all theindex placeholders with <CODE>tensor::</CODE>, for example:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>A = sin(2 * M_PI * tensor::i / 16.);</pre></td></tr></table></P><P>This will make your code more readable, since it is immediately clear that<CODE>i</CODE> is an index placeholder, rather than a scalar value.</P><P><HR SIZE="6"><A NAME="SEC83"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC82"> &lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC84"> &gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC86"> &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#SEC86"> &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.7 Type promotion </H2><!--docid::SEC83::--><P>When operands of different numeric types are used in an expression, theresult gets promoted according to the usual C-style type promotion.  Forexample, the result of adding an <CODE>Array&#60;int&#62;</CODE> to an<CODE>Arrray&#60;float&#62;</CODE> will be promoted to <CODE>float</CODE>.  Generally, theresult is promoted to whichever type has greater precision.</P><P><HR SIZE="6"><A NAME="SEC84"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC83"> &lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC85"> &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> Type promotion for user-defined types </H3><!--docid::SEC84::--><P><A NAME="IDX185"></A><A NAME="IDX186"></A></P><P>The rules for type promotion of user-defined types (or types from anotherlibrary) are a bit complicated.  Here's how a pair of operand types arepromoted:</P><P><UL><LI>If both types are intrinsic (e.g. bool, int, float) then typepromotion follows the standard C rules.  This generally means that theresult will be promoted to whichever type has greater precision.  InBlitz++, these rules have been extended to incorporate<CODE>complex&#60;float&#62;</CODE>, <CODE>complex&#60;double&#62;</CODE>, and <CODE>complex&#60;longdouble&#62;</CODE>.<P><LI>If one of the types is intrinsic (or complex), and the other is auser-defined type, then the result is promoted to the user-defined type.<P><LI>If both types are user-defined, then the result is promoted towhichever type requires more storage space (as determined by<CODE>sizeof()</CODE>).  The rationale is that more storage space probablyindicates more precision.<P></UL><P>If you wish to alter the default type promotion rules above, you have twochoices:</P><P><UL><A NAME="IDX187"></A><LI>If the type promotion behaviour isn't dependent on the type ofoperation performed, then you can provide appropriate specializations forthe class <CODE>promote_trait&#60;A,B&#62;</CODE> which is declared in<CODE>&#60;blitz/promote.h&#62;</CODE>.<P><LI>If type promotion does depend on the type of operation, then youwill need to specialize the appropriate function objects in<CODE>&#60;blitz/ops.h&#62;</CODE>.<P></UL><P>Note that you can do these specializations in your own header files (youdon't have to edit <TT>`promote.h'</TT> or <TT>`ops.h'</TT>).</P><P><HR SIZE="6"><A NAME="SEC85"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC84"> &lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC86"> &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> Manual casts </H3><!--docid::SEC85::--><P><A NAME="IDX188"></A><A NAME="IDX189"></A></P><P>There are some inconvenient aspects of C-style type promotion.  For example,when you divide two integers in C, the result gets truncated.  The sameproblem occurs when dividing two integer arrays in Blitz++:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>Array&#60;int,1&#62; A(4), B(4);Array&#60;float,1&#62; C(4);A = 1, 2, 3, 5;B = 2, 2, 2, 7;C = A / B;      // Result:  [ 0  1  1  0 ]</pre></td></tr></table></P><P>The usual solution to this problem is to cast one of the operands to afloating type.  For this purpose, Blitz++ provides a function<CODE>cast(expr,type)</CODE> which will cast the result of <EM>expr</EM> as<EM>type</EM>:</P><P><A NAME="IDX190"></A></P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>C = A / cast(B, float());   // Result: [ 0.5  1  1.5  0.714 ]</pre></td></tr></table></P><P>The first argument to <CODE>cast()</CODE> is an array or expression.  The secondargument is a dummy object of the type to which you want to cast.  Oncecompilers support templates more thoroughly, it will be possible to use thiscast syntax:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>C = A / cast&#60;float&#62;(B);</pre></td></tr></table></P><P>But this is not yet supported.</P><P><A NAME="Math functions 1"></A><HR SIZE="6"><A NAME="SEC86"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC85"> &lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC87"> &gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC89"> &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#SEC89"> &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>

⌨️ 快捷键说明

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