📄 complex.html
字号:
z = a + bi
~z = a - bi</pre>
<p>Simple... Now look:</p>
<pre>
z * ~z = (a + bi) * (a - bi) = a*a + b*b</pre>
<p>We saw that the norm of <code>z</code> was noted <a href="../../lib/Pod/perlfunc.html#item_abs"><code>abs(z)</code></a> and was defined as the
distance to the origin, also known as:</p>
<pre>
rho = abs(z) = sqrt(a*a + b*b)</pre>
<p>so</p>
<pre>
z * ~z = abs(z) ** 2</pre>
<p>If z is a pure real number (i.e. <code>b == 0</code>), then the above yields:</p>
<pre>
a * a = abs(a) ** 2</pre>
<p>which is true (<a href="../../lib/Pod/perlfunc.html#item_abs"><code>abs</code></a> has the regular meaning for real number, i.e. stands
for the absolute value). This example explains why the norm of <code>z</code> is
noted <a href="../../lib/Pod/perlfunc.html#item_abs"><code>abs(z)</code></a>: it extends the <a href="../../lib/Pod/perlfunc.html#item_abs"><code>abs</code></a> function to complex numbers, yet
is the regular <a href="../../lib/Pod/perlfunc.html#item_abs"><code>abs</code></a> we know when the complex number actually has no
imaginary part... This justifies <em>a posteriori</em> our use of the <a href="../../lib/Pod/perlfunc.html#item_abs"><code>abs</code></a>
notation for the norm.</p>
<p>
</p>
<hr />
<h1><a name="operations">OPERATIONS</a></h1>
<p>Given the following notations:</p>
<pre>
z1 = a + bi = r1 * exp(i * t1)
z2 = c + di = r2 * exp(i * t2)
z = <any complex or real number></pre>
<p>the following (overloaded) operations are supported on complex numbers:</p>
<pre>
z1 + z2 = (a + c) + i(b + d)
z1 - z2 = (a - c) + i(b - d)
z1 * z2 = (r1 * r2) * exp(i * (t1 + t2))
z1 / z2 = (r1 / r2) * exp(i * (t1 - t2))
z1 ** z2 = exp(z2 * log z1)
~z = a - bi
abs(z) = r1 = sqrt(a*a + b*b)
sqrt(z) = sqrt(r1) * exp(i * t/2)
exp(z) = exp(a) * exp(i * b)
log(z) = log(r1) + i*t
sin(z) = 1/2i (exp(i * z1) - exp(-i * z))
cos(z) = 1/2 (exp(i * z1) + exp(-i * z))
atan2(y, x) = atan(y / x) # Minding the right quadrant, note the order.</pre>
<p>The definition used for complex arguments of <a href="../../lib/Pod/perlfunc.html#item_atan2"><code>atan2()</code></a> is</p>
<pre>
-i log((x + iy)/sqrt(x*x+y*y))</pre>
<p>The following extra operations are supported on both real and complex
numbers:</p>
<pre>
Re(z) = a
Im(z) = b
arg(z) = t
abs(z) = r</pre>
<pre>
cbrt(z) = z ** (1/3)
log10(z) = log(z) / log(10)
logn(z, n) = log(z) / log(n)</pre>
<pre>
tan(z) = sin(z) / cos(z)</pre>
<pre>
csc(z) = 1 / sin(z)
sec(z) = 1 / cos(z)
cot(z) = 1 / tan(z)</pre>
<pre>
asin(z) = -i * log(i*z + sqrt(1-z*z))
acos(z) = -i * log(z + i*sqrt(1-z*z))
atan(z) = i/2 * log((i+z) / (i-z))</pre>
<pre>
acsc(z) = asin(1 / z)
asec(z) = acos(1 / z)
acot(z) = atan(1 / z) = -i/2 * log((i+z) / (z-i))</pre>
<pre>
sinh(z) = 1/2 (exp(z) - exp(-z))
cosh(z) = 1/2 (exp(z) + exp(-z))
tanh(z) = sinh(z) / cosh(z) = (exp(z) - exp(-z)) / (exp(z) + exp(-z))</pre>
<pre>
csch(z) = 1 / sinh(z)
sech(z) = 1 / cosh(z)
coth(z) = 1 / tanh(z)</pre>
<pre>
asinh(z) = log(z + sqrt(z*z+1))
acosh(z) = log(z + sqrt(z*z-1))
atanh(z) = 1/2 * log((1+z) / (1-z))</pre>
<pre>
acsch(z) = asinh(1 / z)
asech(z) = acosh(1 / z)
acoth(z) = atanh(1 / z) = 1/2 * log((1+z) / (z-1))</pre>
<p><em>arg</em>, <em>abs</em>, <em>log</em>, <em>csc</em>, <em>cot</em>, <em>acsc</em>, <em>acot</em>, <em>csch</em>,
<em>coth</em>, <em>acosech</em>, <em>acotanh</em>, have aliases <em>rho</em>, <em>theta</em>, <em>ln</em>,
<em>cosec</em>, <em>cotan</em>, <em>acosec</em>, <em>acotan</em>, <em>cosech</em>, <em>cotanh</em>,
<em>acosech</em>, <em>acotanh</em>, respectively. <code>Re</code>, <code>Im</code>, <code>arg</code>, <a href="../../lib/Pod/perlfunc.html#item_abs"><code>abs</code></a>,
<code>rho</code>, and <code>theta</code> can be used also as mutators. The <code>cbrt</code>
returns only one of the solutions: if you want all three, use the
<code>root</code> function.</p>
<p>The <em>root</em> function is available to compute all the <em>n</em>
roots of some complex, where <em>n</em> is a strictly positive integer.
There are exactly <em>n</em> such roots, returned as a list. Getting the
number mathematicians call <code>j</code> such that:</p>
<pre>
<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="variable">*j</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">;</span>
</pre>
<p>is a simple matter of writing:</p>
<pre>
<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>
</pre>
<p>The <em>k</em>th root for <code>z = [r,t]</code> is given by:</p>
<pre>
(root(z, n))[k] = r**(1/n) * exp(i * (t + 2*k*pi)/n)</pre>
<p>You can return the <em>k</em>th root directly by <code>root(z, n, k)</code>,
indexing starting from <em>zero</em> and ending at <em>n - 1</em>.</p>
<p>The <em>spaceship</em> comparison operator, <=>, is also defined. In
order to ensure its restriction to real numbers is conform to what you
would expect, the comparison is run on the real part of the complex
number first, and imaginary parts are compared only when the real
parts match.</p>
<p>
</p>
<hr />
<h1><a name="creation">CREATION</a></h1>
<p>To create a complex number, use either:</p>
<pre>
<span class="variable">$z</span> <span class="operator">=</span> <span class="variable">Math::Complex</span><span class="operator">-></span><span class="variable">make</span><span class="operator">(</span><span class="number">3</span><span class="operator">,</span> <span class="number">4</span><span class="operator">);</span>
<span class="variable">$z</span> <span class="operator">=</span> <span class="variable">cplx</span><span class="operator">(</span><span class="number">3</span><span class="operator">,</span> <span class="number">4</span><span class="operator">);</span>
</pre>
<p>if you know the cartesian form of the number, or</p>
<pre>
<span class="variable">$z</span> <span class="operator">=</span> <span class="number">3</span> <span class="operator">+</span> <span class="number">4</span><span class="variable">*i</span><span class="operator">;</span>
</pre>
<p>if you like. To create a number using the polar form, use either:</p>
<pre>
<span class="variable">$z</span> <span class="operator">=</span> <span class="variable">Math::Complex</span><span class="operator">-></span><span class="variable">emake</span><span class="operator">(</span><span class="number">5</span><span class="operator">,</span> <span class="variable">pi</span><span class="operator">/</span><span class="number">3</span><span class="operator">);</span>
<span class="variable">$x</span> <span class="operator">=</span> <span class="variable">cplxe</span><span class="operator">(</span><span class="number">5</span><span class="operator">,</span> <span class="variable">pi</span><span class="operator">/</span><span class="number">3</span><span class="operator">);</span>
</pre>
<p>instead. The first argument is the modulus, the second is the angle
(in radians, the full circle is 2*pi). (Mnemonic: <code>e</code> is used as a
notation for complex numbers in the polar form).</p>
<p>It is possible to write:</p>
<pre>
<span class="variable">$x</span> <span class="operator">=</span> <span class="variable">cplxe</span><span class="operator">(-</span><span class="number">3</span><span class="operator">,</span> <span class="variable">pi</span><span class="operator">/</span><span class="number">4</span><span class="operator">);</span>
</pre>
<p>but that will be silently converted into <code>[3,-3pi/4]</code>, since the
modulus must be non-negative (it represents the distance to the origin
in the complex plane).</p>
<p>It is also possible to have a complex number as either argument of the
<code>make</code>, <code>emake</code>, <code>cplx</code>, and <code>cplxe</code>: the appropriate component of
the argument will be used.</p>
<pre>
<span class="variable">$z1</span> <span class="operator">=</span> <span class="variable">cplx</span><span class="operator">(-</span><span class="number">2</span><span class="operator">,</span> <span class="number">1</span><span class="operator">);</span>
<span class="variable">$z2</span> <span class="operator">=</span> <span class="variable">cplx</span><span class="operator">(</span><span class="variable">$z1</span><span class="operator">,</span> <span class="number">4</span><span class="operator">);</span>
</pre>
<p>The <code>new</code>, <code>make</code>, <code>emake</code>, <code>cplx</code>, and <code>cplxe</code> will also
understand a single (string) argument of the forms</p>
<pre>
2-3i
-3i
[2,3]
[2,-3pi/4]
[2]</pre>
<p>in which case the appropriate cartesian and exponential components
will be parsed from the string and used to create new complex numbers.
The imaginary component and the theta, respectively, will default to zero.</p>
<p>The <code>new</code>, <code>make</code>, <code>emake</code>, <code>cplx</code>, and <code>cplxe</code> will also
understand the case of no arguments: this means plain zero or (0, 0).</p>
<p>
</p>
<hr />
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -