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

📄 complex.html

📁 perl教程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<h1><a name="displaying">DISPLAYING</a></h1>
<p>When printed, a complex number is usually shown under its cartesian
style <em>a+bi</em>, but there are legitimate cases where the polar style
<em>[r,t]</em> is more appropriate.  The process of converting the complex
number into a string that can be displayed is known as <em>stringification</em>.</p>
<p>By calling the class method <code>Math::Complex::display_format</code> and
supplying either <code>&quot;polar&quot;</code> or <code>&quot;cartesian&quot;</code> as an argument, you
override the default display style, which is <code>&quot;cartesian&quot;</code>. Not
supplying any argument returns the current settings.</p>
<p>This default can be overridden on a per-number basis by calling the
<code>display_format</code> method instead. As before, not supplying any argument
returns the current display style for this number. Otherwise whatever you
specify will be the new display style for <em>this</em> particular number.</p>
<p>For instance:</p>
<pre>
        <span class="keyword">use</span> <span class="variable">Math::Complex</span><span class="operator">;</span>
</pre>
<pre>
        <span class="variable">Math::Complex::display_format</span><span class="operator">(</span><span class="string">'polar'</span><span class="operator">);</span>
        <span class="variable">$j</span> <span class="operator">=</span> <span class="operator">(</span><span class="variable">root</span><span class="operator">(</span><span class="number">1</span><span class="operator">,</span> <span class="number">3</span><span class="operator">))</span><span class="operator">[</span><span class="number">1</span><span class="operator">]</span><span class="operator">;</span>
        <span class="keyword">print</span> <span class="string">"j = $j\n"</span><span class="operator">;</span>               <span class="comment"># Prints "j = [1,2pi/3]"</span>
        <span class="variable">$j</span><span class="operator">-&gt;</span><span class="variable">display_format</span><span class="operator">(</span><span class="string">'cartesian'</span><span class="operator">);</span>
        <span class="keyword">print</span> <span class="string">"j = $j\n"</span><span class="operator">;</span>               <span class="comment"># Prints "j = -0.5+0.866025403784439i"</span>
</pre>
<p>The polar style attempts to emphasize arguments like <em>k*pi/n</em>
(where <em>n</em> is a positive integer and <em>k</em> an integer within [-9, +9]),
this is called <em>polar pretty-printing</em>.</p>
<p>For the reverse of stringifying, see the <code>make</code> and <code>emake</code>.</p>
<p>
</p>
<h2><a name="changed_in_perl_5_6">CHANGED IN PERL 5.6</a></h2>
<p>The <code>display_format</code> class method and the corresponding
<code>display_format</code> object method can now be called using
a parameter hash instead of just a one parameter.</p>
<p>The old display format style, which can have values <code>&quot;cartesian&quot;</code> or
<code>&quot;polar&quot;</code>, can be changed using the <code>&quot;style&quot;</code> parameter.</p>
<pre>
        <span class="variable">$j</span><span class="operator">-&gt;</span><span class="variable">display_format</span><span class="operator">(</span><span class="string">style</span> <span class="operator">=&gt;</span> <span class="string">"polar"</span><span class="operator">);</span>
</pre>
<p>The one parameter calling convention also still works.</p>
<pre>
        <span class="variable">$j</span><span class="operator">-&gt;</span><span class="variable">display_format</span><span class="operator">(</span><span class="string">"polar"</span><span class="operator">);</span>
</pre>
<p>There are two new display parameters.</p>
<p>The first one is <code>&quot;format&quot;</code>, which is a sprintf()-style format string
to be used for both numeric parts of the complex number(s).  The is
somewhat system-dependent but most often it corresponds to <code>&quot;%.15g&quot;</code>.
You can revert to the default by setting the <a href="../../lib/Pod/perlfunc.html#item_format"><code>format</code></a> to <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>.</p>
<pre>
        <span class="comment"># the $j from the above example</span>
</pre>
<pre>
        <span class="variable">$j</span><span class="operator">-&gt;</span><span class="variable">display_format</span><span class="operator">(</span><span class="string">'format'</span> <span class="operator">=&gt;</span> <span class="string">'%.5f'</span><span class="operator">);</span>
        <span class="keyword">print</span> <span class="string">"j = $j\n"</span><span class="operator">;</span>               <span class="comment"># Prints "j = -0.50000+0.86603i"</span>
        <span class="variable">$j</span><span class="operator">-&gt;</span><span class="variable">display_format</span><span class="operator">(</span><span class="string">'format'</span> <span class="operator">=&gt;</span> <span class="keyword">undef</span><span class="operator">);</span>
        <span class="keyword">print</span> <span class="string">"j = $j\n"</span><span class="operator">;</span>               <span class="comment"># Prints "j = -0.5+0.86603i"</span>
</pre>
<p>Notice that this affects also the return values of the
<code>display_format</code> methods: in list context the whole parameter hash
will be returned, as opposed to only the style parameter value.
This is a potential incompatibility with earlier versions if you
have been calling the <code>display_format</code> method in list context.</p>
<p>The second new display parameter is <code>&quot;polar_pretty_print&quot;</code>, which can
be set to true or false, the default being true.  See the previous
section for what this means.</p>
<p>
</p>
<hr />
<h1><a name="usage">USAGE</a></h1>
<p>Thanks to overloading, the handling of arithmetics with complex numbers
is simple and almost transparent.</p>
<p>Here are some examples:</p>
<pre>
        <span class="keyword">use</span> <span class="variable">Math::Complex</span><span class="operator">;</span>
</pre>
<pre>
        <span class="variable">$j</span> <span class="operator">=</span> <span class="variable">cplxe</span><span class="operator">(</span><span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="variable">*pi</span><span class="operator">/</span><span class="number">3</span><span class="operator">);</span>  <span class="comment"># $j ** 3 == 1</span>
        <span class="keyword">print</span> <span class="string">"j = $j, j**3 = "</span><span class="operator">,</span> <span class="variable">$j</span> <span class="operator">**</span> <span class="number">3</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
        <span class="keyword">print</span> <span class="string">"1 + j + j**2 = "</span><span class="operator">,</span> <span class="number">1</span> <span class="operator">+</span> <span class="variable">$j</span> <span class="operator">+</span> <span class="variable">$j</span><span class="operator">**</span><span class="number">2</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
</pre>
<pre>
        <span class="variable">$z</span> <span class="operator">=</span> <span class="operator">-</span><span class="number">16</span> <span class="operator">+</span> <span class="number">0</span><span class="variable">*i</span><span class="operator">;</span>                 <span class="comment"># Force it to be a complex</span>
        <span class="keyword">print</span> <span class="string">"sqrt($z) = "</span><span class="operator">,</span> <span class="keyword">sqrt</span><span class="operator">(</span><span class="variable">$z</span><span class="operator">),</span> <span class="string">"\n"</span><span class="operator">;</span>
</pre>
<pre>
        <span class="variable">$k</span> <span class="operator">=</span> <span class="keyword">exp</span><span class="operator">(</span><span class="variable">i</span> <span class="operator">*</span> <span class="number">2</span><span class="variable">*pi</span><span class="operator">/</span><span class="number">3</span><span class="operator">);</span>
        <span class="keyword">print</span> <span class="string">"$j - $k = "</span><span class="operator">,</span> <span class="variable">$j</span> <span class="operator">-</span> <span class="variable">$k</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
</pre>
<pre>
        <span class="variable">$z</span><span class="operator">-&gt;</span><span class="variable">Re</span><span class="operator">(</span><span class="number">3</span><span class="operator">);</span>                      <span class="comment"># Re, Im, arg, abs,</span>
        <span class="variable">$j</span><span class="operator">-&gt;</span><span class="variable">arg</span><span class="operator">(</span><span class="number">2</span><span class="operator">);</span>                     <span class="comment"># (the last two aka rho, theta)</span>
                                        <span class="comment"># can be used also as mutators.</span>
</pre>
<p>
</p>
<hr />
<h1><a name="errors_due_to_division_by_zero_or_logarithm_of_zero">ERRORS DUE TO DIVISION BY ZERO OR LOGARITHM OF ZERO</a></h1>
<p>The division (/) and the following functions</p>
<pre>
        log     ln      log10   logn
        tan     sec     csc     cot
        atan    asec    acsc    acot
        tanh    sech    csch    coth
        atanh   asech   acsch   acoth</pre>
<p>cannot be computed for all arguments because that would mean dividing
by zero or taking logarithm of zero. These situations cause fatal
runtime errors looking like this</p>
<pre>
        cot(0): Division by zero.
        (Because in the definition of cot(0), the divisor sin(0) is 0)
        Died at ...</pre>
<p>or</p>
<pre>
        atanh(-1): Logarithm of zero.
        Died at...</pre>
<p>For the <code>csc</code>, <code>cot</code>, <code>asec</code>, <code>acsc</code>, <code>acot</code>, <code>csch</code>, <code>coth</code>,
<code>asech</code>, <code>acsch</code>, the argument cannot be <code>0</code> (zero).  For the
logarithmic functions and the <code>atanh</code>, <code>acoth</code>, the argument cannot
be <code>1</code> (one).  For the <code>atanh</code>, <code>acoth</code>, the argument cannot be
<code>-1</code> (minus one).  For the <code>atan</code>, <code>acot</code>, the argument cannot be
<code>i</code> (the imaginary unit).  For the <code>atan</code>, <code>acoth</code>, the argument
cannot be <code>-i</code> (the negative imaginary unit).  For the <code>tan</code>,
<code>sec</code>, <code>tanh</code>, the argument cannot be <em>pi/2 + k * pi</em>, where <em>k</em>
is any integer.  atan2(0, 0) is undefined, and if the complex arguments
are used for atan2(), a division by zero will happen if z1**2+z2**2 == 0.</p>
<p>Note that because we are operating on approximations of real numbers,
these errors can happen when merely `too close' to the singularities
listed above.</p>
<p>
</p>
<hr />
<h1><a name="errors_due_to_indigestible_arguments">ERRORS DUE TO INDIGESTIBLE ARGUMENTS</a></h1>
<p>The <code>make</code> and <code>emake</code> accept both real and complex arguments.
When they cannot recognize the arguments they will die with error
messages like the following</p>
<pre>
    Math::Complex::make: Cannot take real part of ...
    Math::Complex::make: Cannot take real part of ...
    Math::Complex::emake: Cannot take rho of ...
    Math::Complex::emake: Cannot take theta of ...</pre>
<p>
</p>
<hr />
<h1><a name="bugs">BUGS</a></h1>
<p>Saying <code>use Math::Complex;</code> exports many mathematical routines in the
caller environment and even overrides some (<a href="../../lib/Pod/perlfunc.html#item_sqrt"><code>sqrt</code></a>, <a href="../../lib/Pod/perlfunc.html#item_log"><code>log</code></a>, <a href="../../lib/Pod/perlfunc.html#item_atan2"><code>atan2</code></a>).
This is construed as a feature by the Authors, actually... ;-)</p>
<p>All routines expect to be given real or complex numbers. Don't attempt to
use BigFloat, since Perl has currently no rule to disambiguate a '+'
operation (for instance) between two overloaded entities.</p>
<p>In Cray UNICOS there is some strange numerical instability that results
in root(), cos(), sin(), cosh(), sinh(), losing accuracy fast.  Beware.
The bug may be in UNICOS math libs, in UNICOS C compiler, in Math::Complex.
Whatever it is, it does not manifest itself anywhere else where Perl runs.</p>
<p>
</p>
<hr />
<h1><a name="authors">AUTHORS</a></h1>
<p>Daniel S. Lewart &lt;<em><a href="mailto:d-lewart@uiuc.edu">d-lewart@uiuc.edu</a></em>&gt;</p>
<p>Original authors Raphael Manfredi &lt;<em><a href="mailto:Raphael_Manfredi@pobox.com">Raphael_Manfredi@pobox.com</a></em>&gt; and
Jarkko Hietaniemi &lt;<em><a href="mailto:jhi@iki.fi">jhi@iki.fi</a></em>&gt;</p>

</body>

</html>

⌨️ 快捷键说明

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