CINXE.COM
Math::Complex - complex numbers and associated mathematical functions - Perldoc Browser
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Math::Complex - complex numbers and associated mathematical functions - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/Math::Complex"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" /> <style> body { background: #f4f4f5; color: #020202; } .navbar-dark { background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%); background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%); background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0); background-repeat: repeat-x; } .navbar-dark .navbar-nav .nav-link, .navbar-dark .navbar-nav .nav-link:focus { color: #fff } .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 } #wrapperlicious { margin: 0 auto; font: 0.9em 'Helvetica Neue', Helvetica, sans-serif; font-weight: normal; line-height: 1.5em; margin: 0; padding: 0; } #wrapperlicious h1 { font-size: 1.5em } #wrapperlicious h2 { font-size: 1.3em } #wrapperlicious h3 { font-size: 1.1em } #wrapperlicious h4 { font-size: 0.9em } #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3, #wrapperlicious h4, #wrapperlicious dt { color: #020202; margin-top: 1em; margin-bottom: 1em; position: relative; font-weight: bold; } #wrapperlicious a { color: inherit; text-decoration: underline } #wrapperlicious #toc { text-decoration: none } #wrapperlicious a:hover { color: #2a2a2a } #wrapperlicious a img { border: 0 } #wrapperlicious :not(pre) > code { color: inherit; background-color: rgba(0, 0, 0, 0.04); border-radius: 3px; font: 0.9em Consolas, Menlo, Monaco, monospace; padding: 0.3em; } #wrapperlicious dd { margin: 0; margin-left: 2em; } #wrapperlicious dt { color: #2a2a2a; font-weight: bold; margin-left: 0.9em; } #wrapperlicious p { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious li > p { margin-bottom: 0; margin-top: 0; } #wrapperlicious pre { border: 1px solid #c1c1c1; border-radius: 3px; font: 100% Consolas, Menlo, Monaco, monospace; margin-bottom: 1em; margin-top: 1em; } #wrapperlicious pre > code { display: block; background-color: #f6f6f6; font: 0.9em Consolas, Menlo, Monaco, monospace; line-height: 1.5em; text-align: left; white-space: pre; padding: 1em; } #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious ul { list-style-type: square; } #wrapperlicious ul ul { margin-bottom: 0px; margin-top: 0px; } #footer { font-size: 0.8em; padding-top: 0.5em; text-align: center; } #more { display: inline; font-size: 0.8em; } #perldocdiv { background-color: #fff; border: 1px solid #c1c1c1; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; margin-left: auto; margin-right: auto; padding: 3em; padding-top: 1em; max-width: 960px; } #moduleversion { float: right } #wrapperlicious .leading-notice { font-style: italic; padding-left: 1em; margin-top: 1em; margin-bottom: 1em; } #wrapperlicious .permalink { display: none; left: -0.75em; position: absolute; padding-right: 0.25em; text-decoration: none; } #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink, #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink, #wrapperlicious dt:hover .permalink { display: block; } </style> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-KVNWBNT5FB'); gtag('config', 'UA-50555-3'); </script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="/"><img src="/images/perl_camel_30.png" width="30" height="30" class="d-inline-block align-top" alt="Perl Camel Logo"> Perldoc Browser</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.8.1</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/Math::Complex">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/Math::Complex">5.40.1</a> <a class="dropdown-item" href="/5.40.0/Math::Complex">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/Math::Complex">5.38.3</a> <a class="dropdown-item" href="/5.38.2/Math::Complex">5.38.2</a> <a class="dropdown-item" href="/5.38.1/Math::Complex">5.38.1</a> <a class="dropdown-item" href="/5.38.0/Math::Complex">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/Math::Complex">5.36.3</a> <a class="dropdown-item" href="/5.36.2/Math::Complex">5.36.2</a> <a class="dropdown-item" href="/5.36.1/Math::Complex">5.36.1</a> <a class="dropdown-item" href="/5.36.0/Math::Complex">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/Math::Complex">5.34.3</a> <a class="dropdown-item" href="/5.34.2/Math::Complex">5.34.2</a> <a class="dropdown-item" href="/5.34.1/Math::Complex">5.34.1</a> <a class="dropdown-item" href="/5.34.0/Math::Complex">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/Math::Complex">5.32.1</a> <a class="dropdown-item" href="/5.32.0/Math::Complex">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/Math::Complex">5.30.3</a> <a class="dropdown-item" href="/5.30.2/Math::Complex">5.30.2</a> <a class="dropdown-item" href="/5.30.1/Math::Complex">5.30.1</a> <a class="dropdown-item" href="/5.30.0/Math::Complex">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/Math::Complex">5.28.3</a> <a class="dropdown-item" href="/5.28.2/Math::Complex">5.28.2</a> <a class="dropdown-item" href="/5.28.1/Math::Complex">5.28.1</a> <a class="dropdown-item" href="/5.28.0/Math::Complex">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/Math::Complex">5.26.3</a> <a class="dropdown-item" href="/5.26.2/Math::Complex">5.26.2</a> <a class="dropdown-item" href="/5.26.1/Math::Complex">5.26.1</a> <a class="dropdown-item" href="/5.26.0/Math::Complex">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/Math::Complex">5.24.4</a> <a class="dropdown-item" href="/5.24.3/Math::Complex">5.24.3</a> <a class="dropdown-item" href="/5.24.2/Math::Complex">5.24.2</a> <a class="dropdown-item" href="/5.24.1/Math::Complex">5.24.1</a> <a class="dropdown-item" href="/5.24.0/Math::Complex">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/Math::Complex">5.22.4</a> <a class="dropdown-item" href="/5.22.3/Math::Complex">5.22.3</a> <a class="dropdown-item" href="/5.22.2/Math::Complex">5.22.2</a> <a class="dropdown-item" href="/5.22.1/Math::Complex">5.22.1</a> <a class="dropdown-item" href="/5.22.0/Math::Complex">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/Math::Complex">5.20.3</a> <a class="dropdown-item" href="/5.20.2/Math::Complex">5.20.2</a> <a class="dropdown-item" href="/5.20.1/Math::Complex">5.20.1</a> <a class="dropdown-item" href="/5.20.0/Math::Complex">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/Math::Complex">5.18.4</a> <a class="dropdown-item" href="/5.18.3/Math::Complex">5.18.3</a> <a class="dropdown-item" href="/5.18.2/Math::Complex">5.18.2</a> <a class="dropdown-item" href="/5.18.1/Math::Complex">5.18.1</a> <a class="dropdown-item" href="/5.18.0/Math::Complex">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/Math::Complex">5.16.3</a> <a class="dropdown-item" href="/5.16.2/Math::Complex">5.16.2</a> <a class="dropdown-item" href="/5.16.1/Math::Complex">5.16.1</a> <a class="dropdown-item" href="/5.16.0/Math::Complex">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/Math::Complex">5.14.4</a> <a class="dropdown-item" href="/5.14.3/Math::Complex">5.14.3</a> <a class="dropdown-item" href="/5.14.2/Math::Complex">5.14.2</a> <a class="dropdown-item" href="/5.14.1/Math::Complex">5.14.1</a> <a class="dropdown-item" href="/5.14.0/Math::Complex">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/Math::Complex">5.12.5</a> <a class="dropdown-item" href="/5.12.4/Math::Complex">5.12.4</a> <a class="dropdown-item" href="/5.12.3/Math::Complex">5.12.3</a> <a class="dropdown-item" href="/5.12.2/Math::Complex">5.12.2</a> <a class="dropdown-item" href="/5.12.1/Math::Complex">5.12.1</a> <a class="dropdown-item" href="/5.12.0/Math::Complex">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/Math::Complex">5.10.1</a> <a class="dropdown-item" href="/5.10.0/Math::Complex">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/Math::Complex">5.8.9</a> <a class="dropdown-item" href="/5.8.8/Math::Complex">5.8.8</a> <a class="dropdown-item" href="/5.8.7/Math::Complex">5.8.7</a> <a class="dropdown-item" href="/5.8.6/Math::Complex">5.8.6</a> <a class="dropdown-item" href="/5.8.5/Math::Complex">5.8.5</a> <a class="dropdown-item" href="/5.8.4/Math::Complex">5.8.4</a> <a class="dropdown-item" href="/5.8.3/Math::Complex">5.8.3</a> <a class="dropdown-item" href="/5.8.2/Math::Complex">5.8.2</a> <a class="dropdown-item active" href="/5.8.1/Math::Complex">5.8.1</a> <a class="dropdown-item" href="/5.8.0/Math::Complex">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/Math::Complex">5.6.2</a> <a class="dropdown-item" href="/5.6.1/Math::Complex">5.6.1</a> <a class="dropdown-item" href="/5.6.0/Math::Complex">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/Math::Complex">5.005_04</a> <a class="dropdown-item" href="/5.005_03/Math::Complex">5.005_03</a> <a class="dropdown-item" href="/5.005_02/Math::Complex">5.005_02</a> <a class="dropdown-item" href="/5.005_01/Math::Complex">5.005_01</a> <a class="dropdown-item" href="/5.005/Math::Complex">5.005</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-dev"> <a class="dropdown-item" href="/blead/Math::Complex">blead</a> <a class="dropdown-item" href="/5.41.10/Math::Complex">5.41.10</a> <a class="dropdown-item" href="/5.41.9/Math::Complex">5.41.9</a> <a class="dropdown-item" href="/5.41.8/Math::Complex">5.41.8</a> <a class="dropdown-item" href="/5.41.7/Math::Complex">5.41.7</a> <a class="dropdown-item" href="/5.41.6/Math::Complex">5.41.6</a> <a class="dropdown-item" href="/5.41.5/Math::Complex">5.41.5</a> <a class="dropdown-item" href="/5.41.4/Math::Complex">5.41.4</a> <a class="dropdown-item" href="/5.41.3/Math::Complex">5.41.3</a> <a class="dropdown-item" href="/5.41.2/Math::Complex">5.41.2</a> <a class="dropdown-item" href="/5.41.1/Math::Complex">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/Math::Complex">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/Math::Complex">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/Math::Complex">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/Math::Complex">5.39.10</a> <a class="dropdown-item" href="/5.39.9/Math::Complex">5.39.9</a> <a class="dropdown-item" href="/5.39.8/Math::Complex">5.39.8</a> <a class="dropdown-item" href="/5.39.7/Math::Complex">5.39.7</a> <a class="dropdown-item" href="/5.39.6/Math::Complex">5.39.6</a> <a class="dropdown-item" href="/5.39.5/Math::Complex">5.39.5</a> <a class="dropdown-item" href="/5.39.4/Math::Complex">5.39.4</a> <a class="dropdown-item" href="/5.39.3/Math::Complex">5.39.3</a> <a class="dropdown-item" href="/5.39.2/Math::Complex">5.39.2</a> <a class="dropdown-item" href="/5.39.1/Math::Complex">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/Math::Complex">5.38.3-RC1</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-nav"> <a class="dropdown-item" href="/5.8.1/perl">Perl</a> <a class="dropdown-item" href="/5.8.1/perlintro">Intro</a> <a class="dropdown-item" href="/5.8.1/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.8.1/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.8.1/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.1/perlop">Operators</a> <a class="dropdown-item" href="/5.8.1/functions">Functions</a> <a class="dropdown-item" href="/5.8.1/variables">Variables</a> <a class="dropdown-item" href="/5.8.1/modules">Modules</a> <a class="dropdown-item" href="/5.8.1/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.1/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.8.1/perlcommunity">Community</a> <a class="dropdown-item" href="/5.8.1/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.8.1/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.8.1/Math::Complex">Math::Complex</a> <div id="more"> (<a href="/5.8.1/Math::Complex.txt">source</a>, <a href="https://metacpan.org/pod/Math::Complex">CPAN</a>) </div> <div id="moduleversion">version 1.34</div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.8.1. <a href="/Math::Complex">View the latest version</a> </div> <h1><a id="toc">CONTENTS</a></h1> <ul> <li> <a class="text-decoration-none" href="#NAME">NAME</a> </li> <li> <a class="text-decoration-none" href="#SYNOPSIS">SYNOPSIS</a> </li> <li> <a class="text-decoration-none" href="#DESCRIPTION">DESCRIPTION</a> </li> <li> <a class="text-decoration-none" href="#OPERATIONS">OPERATIONS</a> </li> <li> <a class="text-decoration-none" href="#CREATION">CREATION</a> </li> <li> <a class="text-decoration-none" href="#STRINGIFICATION">STRINGIFICATION</a> <ul> <li> <a class="text-decoration-none" href="#CHANGED-IN-PERL-5.6">CHANGED IN PERL 5.6</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#USAGE">USAGE</a> </li> <li> <a class="text-decoration-none" href="#ERRORS-DUE-TO-DIVISION-BY-ZERO-OR-LOGARITHM-OF-ZERO">ERRORS DUE TO DIVISION BY ZERO OR LOGARITHM OF ZERO</a> </li> <li> <a class="text-decoration-none" href="#ERRORS-DUE-TO-INDIGESTIBLE-ARGUMENTS">ERRORS DUE TO INDIGESTIBLE ARGUMENTS</a> </li> <li> <a class="text-decoration-none" href="#BUGS">BUGS</a> </li> <li> <a class="text-decoration-none" href="#AUTHORS">AUTHORS</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>Math::Complex - complex numbers and associated mathematical functions</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code>use Math::Complex; $z = Math::Complex->make(5, 6); $t = 4 - 3*i + $z; $j = cplxe(1, 2*pi/3);</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p>This package lets you create and manipulate complex numbers. By default, <i>Perl</i> limits itself to real numbers, but an extra <code>use</code> statement brings full complex support, along with a full set of mathematical functions typically associated with and/or extended to complex numbers.</p> <p>If you wonder what complex numbers are, they were invented to be able to solve the following equation:</p> <pre><code class="plaintext">x*x = -1</code></pre> <p>and by definition, the solution is noted <i>i</i> (engineers use <i>j</i> instead since <i>i</i> usually denotes an intensity, but the name does not matter). The number <i>i</i> is a pure <i>imaginary</i> number.</p> <p>The arithmetics with pure imaginary numbers works just like you would expect it with real numbers... you just have to remember that</p> <pre><code class="plaintext">i*i = -1</code></pre> <p>so you have:</p> <pre><code class="plaintext">5i + 7i = i * (5 + 7) = 12i 4i - 3i = i * (4 - 3) = i 4i * 2i = -8 6i / 2i = 3 1 / i = -i</code></pre> <p>Complex numbers are numbers that have both a real part and an imaginary part, and are usually noted:</p> <pre><code class="plaintext">a + bi</code></pre> <p>where <code>a</code> is the <i>real</i> part and <code>b</code> is the <i>imaginary</i> part. The arithmetic with complex numbers is straightforward. You have to keep track of the real and the imaginary parts, but otherwise the rules used for real numbers just apply:</p> <pre><code class="plaintext">(4 + 3i) + (5 - 2i) = (4 + 5) + i(3 - 2) = 9 + i (2 + i) * (4 - i) = 2*4 + 4i -2i -i*i = 8 + 2i + 1 = 9 + 2i</code></pre> <p>A graphical representation of complex numbers is possible in a plane (also called the <i>complex plane</i>, but it's really a 2D plane). The number</p> <pre><code class="plaintext">z = a + bi</code></pre> <p>is the point whose coordinates are (a, b). Actually, it would be the vector originating from (0, 0) to (a, b). It follows that the addition of two complex numbers is a vectorial addition.</p> <p>Since there is a bijection between a point in the 2D plane and a complex number (i.e. the mapping is unique and reciprocal), a complex number can also be uniquely identified with polar coordinates:</p> <pre><code class="plaintext">[rho, theta]</code></pre> <p>where <code>rho</code> is the distance to the origin, and <code>theta</code> the angle between the vector and the <i>x</i> axis. There is a notation for this using the exponential form, which is:</p> <pre><code class="plaintext">rho * exp(i * theta)</code></pre> <p>where <i>i</i> is the famous imaginary number introduced above. Conversion between this form and the cartesian form <code>a + bi</code> is immediate:</p> <pre><code class="plaintext">a = rho * cos(theta) b = rho * sin(theta)</code></pre> <p>which is also expressed by this formula:</p> <pre><code class="plaintext">z = rho * exp(i * theta) = rho * (cos theta + i * sin theta)</code></pre> <p>In other words, it's the projection of the vector onto the <i>x</i> and <i>y</i> axes. Mathematicians call <i>rho</i> the <i>norm</i> or <i>modulus</i> and <i>theta</i> the <i>argument</i> of the complex number. The <i>norm</i> of <code>z</code> will be noted <code>abs(z)</code>.</p> <p>The polar notation (also known as the trigonometric representation) is much more handy for performing multiplications and divisions of complex numbers, whilst the cartesian notation is better suited for additions and subtractions. Real numbers are on the <i>x</i> axis, and therefore <i>theta</i> is zero or <i>pi</i>.</p> <p>All the common operations that can be performed on a real number have been defined to work on complex numbers as well, and are merely <i>extensions</i> of the operations defined on real numbers. This means they keep their natural meaning when there is no imaginary part, provided the number is within their definition set.</p> <p>For instance, the <code>sqrt</code> routine which computes the square root of its argument is only defined for non-negative real numbers and yields a non-negative real number (it is an application from <b>R+</b> to <b>R+</b>). If we allow it to return a complex number, then it can be extended to negative real numbers to become an application from <b>R</b> to <b>C</b> (the set of complex numbers):</p> <pre><code class="plaintext">sqrt(x) = x >= 0 ? sqrt(x) : sqrt(-x)*i</code></pre> <p>It can also be extended to be an application from <b>C</b> to <b>C</b>, whilst its restriction to <b>R</b> behaves as defined above by using the following definition:</p> <pre><code class="plaintext">sqrt(z = [r,t]) = sqrt(r) * exp(i * t/2)</code></pre> <p>Indeed, a negative real number can be noted <code>[x,pi]</code> (the modulus <i>x</i> is always non-negative, so <code>[x,pi]</code> is really <code>-x</code>, a negative number) and the above definition states that</p> <pre><code class="plaintext">sqrt([x,pi]) = sqrt(x) * exp(i*pi/2) = [sqrt(x),pi/2] = sqrt(x)*i</code></pre> <p>which is exactly what we had defined for negative real numbers above. The <code>sqrt</code> returns only one of the solutions: if you want the both, use the <code>root</code> function.</p> <p>All the common mathematical functions defined on real numbers that are extended to complex numbers share that same property of working <i>as usual</i> when the imaginary part is zero (otherwise, it would not be called an extension, would it?).</p> <p>A <i>new</i> operation possible on a complex number that is the identity for real numbers is called the <i>conjugate</i>, and is noted with a horizontal bar above the number, or <code>~z</code> here.</p> <pre><code class="plaintext"> z = a + bi ~z = a - bi</code></pre> <p>Simple... Now look:</p> <pre><code class="plaintext">z * ~z = (a + bi) * (a - bi) = a*a + b*b</code></pre> <p>We saw that the norm of <code>z</code> was noted <code>abs(z)</code> and was defined as the distance to the origin, also known as:</p> <pre><code class="plaintext">rho = abs(z) = sqrt(a*a + b*b)</code></pre> <p>so</p> <pre><code class="plaintext">z * ~z = abs(z) ** 2</code></pre> <p>If z is a pure real number (i.e. <code>b == 0</code>), then the above yields:</p> <pre><code class="plaintext">a * a = abs(a) ** 2</code></pre> <p>which is true (<code>abs</code> 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 <code>abs(z)</code>: it extends the <code>abs</code> function to complex numbers, yet is the regular <code>abs</code> we know when the complex number actually has no imaginary part... This justifies <i>a posteriori</i> our use of the <code>abs</code> notation for the norm.</p> <h1 id="OPERATIONS"><a class="permalink" href="#OPERATIONS">#</a>OPERATIONS</h1> <p>Given the following notations:</p> <pre><code class="plaintext">z1 = a + bi = r1 * exp(i * t1) z2 = c + di = r2 * exp(i * t2) z = <any complex or real number></code></pre> <p>the following (overloaded) operations are supported on complex numbers:</p> <pre><code class="plaintext">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(z1, z2) = atan(z1/z2)</code></pre> <p>The following extra operations are supported on both real and complex numbers:</p> <pre><code class="plaintext">Re(z) = a Im(z) = b arg(z) = t abs(z) = r cbrt(z) = z ** (1/3) log10(z) = log(z) / log(10) logn(z, n) = log(z) / log(n) tan(z) = sin(z) / cos(z) csc(z) = 1 / sin(z) sec(z) = 1 / cos(z) cot(z) = 1 / tan(z) 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)) acsc(z) = asin(1 / z) asec(z) = acos(1 / z) acot(z) = atan(1 / z) = -i/2 * log((i+z) / (z-i)) 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)) csch(z) = 1 / sinh(z) sech(z) = 1 / cosh(z) coth(z) = 1 / tanh(z) 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)) acsch(z) = asinh(1 / z) asech(z) = acosh(1 / z) acoth(z) = atanh(1 / z) = 1/2 * log((1+z) / (z-1))</code></pre> <p><i>arg</i>, <i>abs</i>, <i>log</i>, <i>csc</i>, <i>cot</i>, <i>acsc</i>, <i>acot</i>, <i>csch</i>, <i>coth</i>, <i>acosech</i>, <i>acotanh</i>, have aliases <i>rho</i>, <i>theta</i>, <i>ln</i>, <i>cosec</i>, <i>cotan</i>, <i>acosec</i>, <i>acotan</i>, <i>cosech</i>, <i>cotanh</i>, <i>acosech</i>, <i>acotanh</i>, respectively. <code>Re</code>, <code>Im</code>, <code>arg</code>, <code>abs</code>, <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 <i>root</i> function is available to compute all the <i>n</i> roots of some complex, where <i>n</i> is a strictly positive integer. There are exactly <i>n</i> such roots, returned as a list. Getting the number mathematicians call <code>j</code> such that:</p> <pre><code>1 + j + j*j = 0;</code></pre> <p>is a simple matter of writing:</p> <pre><code>$j = ((root(1, 3))[1];</code></pre> <p>The <i>k</i>th root for <code>z = [r,t]</code> is given by:</p> <pre><code class="plaintext">(root(z, n))[k] = r**(1/n) * exp(i * (t + 2*k*pi)/n)</code></pre> <p>The <i>spaceship</i> 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> <h1 id="CREATION"><a class="permalink" href="#CREATION">#</a>CREATION</h1> <p>To create a complex number, use either:</p> <pre><code>$z = Math::Complex->make(3, 4); $z = cplx(3, 4);</code></pre> <p>if you know the cartesian form of the number, or</p> <pre><code>$z = 3 + 4*i;</code></pre> <p>if you like. To create a number using the polar form, use either:</p> <pre><code>$z = Math::Complex->emake(5, pi/3); $x = cplxe(5, pi/3);</code></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><code>$x = cplxe(-3, pi/4);</code></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><code>$z1 = cplx(-2, 1); $z2 = cplx($z1, 4);</code></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><code class="plaintext"> 2-3i -3i [2,3] [2]</code></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> <h1 id="STRINGIFICATION"><a class="permalink" href="#STRINGIFICATION">#</a>STRINGIFICATION</h1> <p>When printed, a complex number is usually shown under its cartesian style <i>a+bi</i>, but there are legitimate cases where the polar style <i>[r,t]</i> is more appropriate.</p> <p>By calling the class method <code>Math::Complex::display_format</code> and supplying either <code>"polar"</code> or <code>"cartesian"</code> as an argument, you override the default display style, which is <code>"cartesian"</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 <i>this</i> particular number.</p> <p>For instance:</p> <pre><code>use Math::Complex; Math::Complex::display_format('polar'); $j = (root(1, 3))[1]; print "j = $j\n"; # Prints "j = [1,2pi/3]" $j->display_format('cartesian'); print "j = $j\n"; # Prints "j = -0.5+0.866025403784439i"</code></pre> <p>The polar style attempts to emphasize arguments like <i>k*pi/n</i> (where <i>n</i> is a positive integer and <i>k</i> an integer within [-9, +9]), this is called <i>polar pretty-printing</i>.</p> <h2 id="CHANGED-IN-PERL-5.6"><a class="permalink" href="#CHANGED-IN-PERL-5.6">#</a><a id="CHANGED"></a>CHANGED IN PERL 5.6</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>"cartesian"</code> or <code>"polar"</code>, can be changed using the <code>"style"</code> parameter.</p> <pre><code>$j->display_format(style => "polar");</code></pre> <p>The one parameter calling convention also still works.</p> <pre><code>$j->display_format("polar");</code></pre> <p>There are two new display parameters.</p> <p>The first one is <code>"format"</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>"%.15g"</code>. You can revert to the default by setting the <code>format</code> to <code>undef</code>.</p> <pre><code># the $j from the above example $j->display_format('format' => '%.5f'); print "j = $j\n"; # Prints "j = -0.50000+0.86603i" $j->display_format('format' => undef); print "j = $j\n"; # Prints "j = -0.5+0.86603i"</code></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>"polar_pretty_print"</code>, which can be set to true or false, the default being true. See the previous section for what this means.</p> <h1 id="USAGE"><a class="permalink" href="#USAGE">#</a>USAGE</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><code>use Math::Complex; $j = cplxe(1, 2*pi/3); # $j ** 3 == 1 print "j = $j, j**3 = ", $j ** 3, "\n"; print "1 + j + j**2 = ", 1 + $j + $j**2, "\n"; $z = -16 + 0*i; # Force it to be a complex print "sqrt($z) = ", sqrt($z), "\n"; $k = exp(i * 2*pi/3); print "$j - $k = ", $j - $k, "\n"; $z->Re(3); # Re, Im, arg, abs, $j->arg(2); # (the last two aka rho, theta) # can be used also as mutators.</code></pre> <h1 id="ERRORS-DUE-TO-DIVISION-BY-ZERO-OR-LOGARITHM-OF-ZERO"><a class="permalink" href="#ERRORS-DUE-TO-DIVISION-BY-ZERO-OR-LOGARITHM-OF-ZERO">#</a><a id="ERRORS"></a>ERRORS DUE TO DIVISION BY ZERO OR LOGARITHM OF ZERO</h1> <p>The division (/) and the following functions</p> <pre><code class="plaintext">log ln log10 logn tan sec csc cot atan asec acsc acot tanh sech csch coth atanh asech acsch acoth</code></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><code class="plaintext">cot(0): Division by zero. (Because in the definition of cot(0), the divisor sin(0) is 0) Died at ...</code></pre> <p>or</p> <pre><code class="plaintext">atanh(-1): Logarithm of zero. Died at...</code></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 <i>pi/2 + k * pi</i>, where <i>k</i> is any integer.</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> <h1 id="ERRORS-DUE-TO-INDIGESTIBLE-ARGUMENTS"><a class="permalink" href="#ERRORS-DUE-TO-INDIGESTIBLE-ARGUMENTS">#</a><a id="ERRORS1"></a>ERRORS DUE TO INDIGESTIBLE ARGUMENTS</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><code class="plaintext">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 ...</code></pre> <h1 id="BUGS"><a class="permalink" href="#BUGS">#</a>BUGS</h1> <p>Saying <code>use Math::Complex;</code> exports many mathematical routines in the caller environment and even overrides some (<code>sqrt</code>, <code>log</code>). 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> <h1 id="AUTHORS"><a class="permalink" href="#AUTHORS">#</a>AUTHORS</h1> <p>Daniel S. Lewart <<i>d-lewart@uiuc.edu</i>></p> <p>Original authors Raphael Manfredi <<i>Raphael_Manfredi@pobox.com</i>> and Jarkko Hietaniemi <<i>jhi@iki.fi</i>></p> </div> <div id="footer"> <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p> <p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/js/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>