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

📄 blitz_5.html

📁 A C++ class library for scientific computing
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H3> 5.1.2 Special support for complex arrays </H3><!--docid::SEC124::--><P><A NAME="IDX317"></A><A NAME="IDX318"></A></P><P>Since complex arrays are used frequently, Blitz++ provides two specialmethods for getting the real and imaginary components:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>Array&#60;complex&#60;float&#62;,2&#62; A(32,32);real(A) = 1.0;imag(A) = 0.0;</pre></td></tr></table></P><P>The function <CODE>real(A)</CODE> returns an array view of the real component;<CODE>imag(A)</CODE> returns a view of the imaginary component.</P><P>Note: Blitz++ provides numerous math functions defined over complex-valuedarrays, such as <CODE>conj</CODE>, <CODE>polar</CODE>, <CODE>arg</CODE>, <CODE>abs</CODE>,<CODE>cos</CODE>, <CODE>pow</CODE>, etc.  See the section on math functions(<A HREF="blitz_3.html#SEC92">3.8 Single-argument math functions</A>) for details.</P><P><HR SIZE="6"><A NAME="SEC125"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC124"> &lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC126"> &gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC121"> &lt;&lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC122"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC126"> &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><H3> 5.1.3 Zipping together expressions </H3><!--docid::SEC125::--><P>Blitz++ provides a function <CODE>zip()</CODE> which lets you combine two or moreexpressions into a single component.  For example, you can combine two realexpressions into a complex expression, or three integer expressions into anHSV24 expression.  The function has this syntax:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>resultexpr zip(expr1, expr2, T_element)resultexpr zip(expr1, expr2, expr3, T_element)         ** not available yetresultexpr zip(expr1, expr2, expr3, expr4, T_element)  ** not available yet</pre></td></tr></table></P><P>The types <CODE>resultexpr</CODE>, <CODE>expr1</CODE> and <CODE>expr2</CODE> are arrayexpressions.  The third argument is the type you want to create.  Forexample:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>int N = 16;Array&#60;complex&#60;float&#62;,1&#62; A(N);Array&#60;float,1&#62; theta(N); ...A = zip(cos(theta), sin(theta), complex&#60;float&#62;());</pre></td></tr></table></P><P>The above line is equivalent to:</P><P><TABLE><tr><td>&nbsp;</td><td class=example><pre>for (int i=0; i &#60; N; ++i)   A[i] = complex&#60;float&#62;(cos(theta[i]), sin(theta[i]));</pre></td></tr></table></P><P><A NAME="Array usertype"></A><HR SIZE="6"><A NAME="SEC126"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC125"> &lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_6.html#SEC127"> &gt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC121"> &lt;&lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC121"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_6.html#SEC127"> &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> 5.2 Creating arrays of a user type </H2><!--docid::SEC126::--><P>You can use the <CODE>Array</CODE> class with types you have created yourself, ortypes from another library.  If you want to do arithmetic on the array,whatever operators you use on the arrays have to be defined on theunderlying type.</P><P>For example, here's a simple class for doing fixed point computations in theinterval [0,1]:</P><P><TABLE><tr><td>&nbsp;</td><td class=smallexample><FONT SIZE=-1><pre>#include &#60;blitz/array.h&#62;#include &#60;blitz/numinquire.h&#62; // for huge()using namespace blitz;// A simple fixed point arithmetic class which represents a point// in the interval [0,1].class FixedPoint {public:	  // The type to use for the mantissa    typedef unsigned int T_mantissa;    FixedPoint() { }    FixedPoint(T_mantissa mantissa)    {          mantissa_ = mantissa;    }    FixedPoint(double value)    {        assert((value &#62;= 0.0) &#38;&#38; (value &#60;= 1.0));        mantissa_ = static_cast&#60;T_mantissa&#62;(value * huge(T_mantissa()));    }       FixedPoint operator+(FixedPoint x)    { return FixedPoint(mantissa_ + x.mantissa_); }    double value() const    { return mantissa_ / double(huge(T_mantissa())); }private:    T_mantissa mantissa_;};ostream&#38; operator&#60;&#60;(ostream&#38; os, const FixedPoint&#38; a){    os &#60;&#60; a.value();    return os;}</FONT></pre></td></tr></table></P><P>The function <CODE>huge(T)</CODE> returns the largest representable value for typeT; in the example above, it's equal to <CODE>UINT_MAX</CODE>.</P><P>The <CODE>FixedPoint</CODE> class declares three useful operations: conversionfrom <CODE>double</CODE>, addition, and outputing to an <CODE>ostream</CODE>.  We canuse all of these operations on an <CODE>Array&#60;FixedPoint&#62;</CODE> object:</P><P><TABLE><tr><td>&nbsp;</td><td class=smallexample><FONT SIZE=-1><pre>#include &#60;fixed-point.h&#62; // FixedPoint classint main(){    // Create an array using the FixedPoint class:    Array&#60;FixedPoint, 2&#62; A(4,4), B(4,4);    A = 0.5, 0.3, 0.8, 0.2,        0.1, 0.3, 0.2, 0.9,        0.0, 1.0, 0.7, 0.4,        0.2, 0.3, 0.8, 0.4;    B = A + 0.05;    cout &#60;&#60; "B = " &#60;&#60; B &#60;&#60; endl;    return 0;}</FONT></pre></td></tr></table></P><P>Note that the array <CODE>A</CODE> is initialized using a comma-delimited list of<CODE>double</CODE>; this makes use of the constructor <CODE>FixedPoint(double)</CODE>.The assignment <CODE>B = A + 0.05</CODE> uses<CODE>FixedPoint::operator+(FixedPoint)</CODE>, with an implicit conversion from<CODE>double</CODE> to <CODE>FixedPoint</CODE>.  Formatting the array <CODE>B</CODE> onto thestandard output stream is done using the output operator defined for<CODE>FixedPoint</CODE>.</P><P>Here's the program output:</P><P><TABLE><tr><td>&nbsp;</td><td class=smallexample><FONT SIZE=-1><pre>B = 4 x 4[      0.55      0.35      0.85      0.25        0.15      0.35      0.25      0.95        0.05      0.05      0.75      0.45        0.25      0.35      0.85      0.45 ]</FONT></pre></td></tr></table></P><P><A NAME="Indirection"></A><HR SIZE="6"><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC121"> &lt;&lt; </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_6.html#SEC127"> &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"> &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><BR>  <FONT SIZE="-1">This document was generatedby <I>Julian Cummings</I> on <I>October, 14  2005</I>using <A HREF="http://www.mathematik.uni-kl.de/~obachman/Texi2html"><I>texi2html</I></A></BODY></HTML>

⌨️ 快捷键说明

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