{
    "content": [
        {
            "type": "text",
            "text": "# Math::Complex (perldoc)\n\n**Summary:** Math::Complex - complex numbers and associated mathematical functions\n\n**Synopsis:** use Math::Complex;\n$z = Math::Complex->make(5, 6);\n$t = 4 - 3*i + $z;\n$j = cplxe(1, 2*pi/3);\n\n## Section Outline\n\n- **NAME** (2 lines)\n- **SYNOPSIS** (6 lines)\n- **DESCRIPTION** (150 lines)\n- **OPERATIONS** (101 lines)\n- **CREATION** (49 lines)\n- **DISPLAYING** (70 lines)\n- **USAGE** (21 lines)\n- **CONSTANTS** (7 lines) — 1 subsections\n  - Inf (24 lines)\n- **ERRORS DUE TO DIVISION BY ZERO OR LOGARITHM OF ZERO** (36 lines)\n- **ERRORS DUE TO INDIGESTIBLE ARGUMENTS** (9 lines)\n- **BUGS** (14 lines)\n- **SEE ALSO** (2 lines)\n- **AUTHORS** (4 lines)\n- **LICENSE** (3 lines)\n\n## Full Content\n\n### NAME\n\nMath::Complex - complex numbers and associated mathematical functions\n\n### SYNOPSIS\n\nuse Math::Complex;\n\n$z = Math::Complex->make(5, 6);\n$t = 4 - 3*i + $z;\n$j = cplxe(1, 2*pi/3);\n\n### DESCRIPTION\n\nThis package lets you create and manipulate complex numbers. By default,\n*Perl* limits itself to real numbers, but an extra \"use\" statement\nbrings full complex support, along with a full set of mathematical\nfunctions typically associated with and/or extended to complex numbers.\n\nIf you wonder what complex numbers are, they were invented to be able to\nsolve the following equation:\n\nx*x = -1\n\nand by definition, the solution is noted *i* (engineers use *j* instead\nsince *i* usually denotes an intensity, but the name does not matter).\nThe number *i* is a pure *imaginary* number.\n\nThe arithmetics with pure imaginary numbers works just like you would\nexpect it with real numbers... you just have to remember that\n\ni*i = -1\n\nso you have:\n\n5i + 7i = i * (5 + 7) = 12i\n4i - 3i = i * (4 - 3) = i\n4i * 2i = -8\n6i / 2i = 3\n1 / i = -i\n\nComplex numbers are numbers that have both a real part and an imaginary\npart, and are usually noted:\n\na + bi\n\nwhere \"a\" is the *real* part and \"b\" is the *imaginary* part. The\narithmetic with complex numbers is straightforward. You have to keep\ntrack of the real and the imaginary parts, but otherwise the rules used\nfor real numbers just apply:\n\n(4 + 3i) + (5 - 2i) = (4 + 5) + i(3 - 2) = 9 + i\n(2 + i) * (4 - i) = 2*4 + 4i -2i -i*i = 8 + 2i + 1 = 9 + 2i\n\nA graphical representation of complex numbers is possible in a plane\n(also called the *complex plane*, but it's really a 2D plane). The\nnumber\n\nz = a + bi\n\nis the point whose coordinates are (a, b). Actually, it would be the\nvector originating from (0, 0) to (a, b). It follows that the addition\nof two complex numbers is a vectorial addition.\n\nSince there is a bijection between a point in the 2D plane and a complex\nnumber (i.e. the mapping is unique and reciprocal), a complex number can\nalso be uniquely identified with polar coordinates:\n\n[rho, theta]\n\nwhere \"rho\" is the distance to the origin, and \"theta\" the angle between\nthe vector and the *x* axis. There is a notation for this using the\nexponential form, which is:\n\nrho * exp(i * theta)\n\nwhere *i* is the famous imaginary number introduced above. Conversion\nbetween this form and the cartesian form \"a + bi\" is immediate:\n\na = rho * cos(theta)\nb = rho * sin(theta)\n\nwhich is also expressed by this formula:\n\nz = rho * exp(i * theta) = rho * (cos theta + i * sin theta)\n\nIn other words, it's the projection of the vector onto the *x* and *y*\naxes. Mathematicians call *rho* the *norm* or *modulus* and *theta* the\n*argument* of the complex number. The *norm* of \"z\" is marked here as\nabs(z).\n\nThe polar notation (also known as the trigonometric representation) is\nmuch more handy for performing multiplications and divisions of complex\nnumbers, whilst the cartesian notation is better suited for additions\nand subtractions. Real numbers are on the *x* axis, and therefore *y* or\n*theta* is zero or *pi*.\n\nAll the common operations that can be performed on a real number have\nbeen defined to work on complex numbers as well, and are merely\n*extensions* of the operations defined on real numbers. This means they\nkeep their natural meaning when there is no imaginary part, provided the\nnumber is within their definition set.\n\nFor instance, the \"sqrt\" routine which computes the square root of its\nargument is only defined for non-negative real numbers and yields a\nnon-negative real number (it is an application from R+ to R+). If we\nallow it to return a complex number, then it can be extended to negative\nreal numbers to become an application from R to C (the set of complex\nnumbers):\n\nsqrt(x) = x >= 0 ? sqrt(x) : sqrt(-x)*i\n\nIt can also be extended to be an application from C to C, whilst its\nrestriction to R behaves as defined above by using the following\ndefinition:\n\nsqrt(z = [r,t]) = sqrt(r) * exp(i * t/2)\n\nIndeed, a negative real number can be noted \"[x,pi]\" (the modulus *x* is\nalways non-negative, so \"[x,pi]\" is really \"-x\", a negative number) and\nthe above definition states that\n\nsqrt([x,pi]) = sqrt(x) * exp(i*pi/2) = [sqrt(x),pi/2] = sqrt(x)*i\n\nwhich is exactly what we had defined for negative real numbers above.\nThe \"sqrt\" returns only one of the solutions: if you want the both, use\nthe \"root\" function.\n\nAll the common mathematical functions defined on real numbers that are\nextended to complex numbers share that same property of working *as\nusual* when the imaginary part is zero (otherwise, it would not be\ncalled an extension, would it?).\n\nA *new* operation possible on a complex number that is the identity for\nreal numbers is called the *conjugate*, and is noted with a horizontal\nbar above the number, or \"~z\" here.\n\nz = a + bi\n~z = a - bi\n\nSimple... Now look:\n\nz * ~z = (a + bi) * (a - bi) = a*a + b*b\n\nWe saw that the norm of \"z\" was noted abs(z) and was defined as the\ndistance to the origin, also known as:\n\nrho = abs(z) = sqrt(a*a + b*b)\n\nso\n\nz * ~z = abs(z)  2\n\nIf z is a pure real number (i.e. \"b == 0\"), then the above yields:\n\na * a = abs(a)  2\n\nwhich is true (\"abs\" has the regular meaning for real number, i.e.\nstands for the absolute value). This example explains why the norm of\n\"z\" is noted abs(z): it extends the \"abs\" function to complex numbers,\nyet is the regular \"abs\" we know when the complex number actually has no\nimaginary part... This justifies *a posteriori* our use of the \"abs\"\nnotation for the norm.\n\n### OPERATIONS\n\nGiven the following notations:\n\nz1 = a + bi = r1 * exp(i * t1)\nz2 = c + di = r2 * exp(i * t2)\nz = <any complex or real number>\n\nthe following (overloaded) operations are supported on complex numbers:\n\nz1 + z2 = (a + c) + i(b + d)\nz1 - z2 = (a - c) + i(b - d)\nz1 * z2 = (r1 * r2) * exp(i * (t1 + t2))\nz1 / z2 = (r1 / r2) * exp(i * (t1 - t2))\nz1  z2 = exp(z2 * log z1)\n~z = a - bi\nabs(z) = r1 = sqrt(a*a + b*b)\nsqrt(z) = sqrt(r1) * exp(i * t/2)\nexp(z) = exp(a) * exp(i * b)\nlog(z) = log(r1) + i*t\nsin(z) = 1/2i (exp(i * z1) - exp(-i * z))\ncos(z) = 1/2 (exp(i * z1) + exp(-i * z))\natan2(y, x) = atan(y / x) # Minding the right quadrant, note the order.\n\nThe definition used for complex arguments of atan2() is\n\n-i log((x + iy)/sqrt(x*x+y*y))\n\nNote that atan2(0, 0) is not well-defined.\n\nThe following extra operations are supported on both real and complex\nnumbers:\n\nRe(z) = a\nIm(z) = b\narg(z) = t\nabs(z) = r\n\ncbrt(z) = z  (1/3)\nlog10(z) = log(z) / log(10)\nlogn(z, n) = log(z) / log(n)\n\ntan(z) = sin(z) / cos(z)\n\ncsc(z) = 1 / sin(z)\nsec(z) = 1 / cos(z)\ncot(z) = 1 / tan(z)\n\nasin(z) = -i * log(i*z + sqrt(1-z*z))\nacos(z) = -i * log(z + i*sqrt(1-z*z))\natan(z) = i/2 * log((i+z) / (i-z))\n\nacsc(z) = asin(1 / z)\nasec(z) = acos(1 / z)\nacot(z) = atan(1 / z) = -i/2 * log((i+z) / (z-i))\n\nsinh(z) = 1/2 (exp(z) - exp(-z))\ncosh(z) = 1/2 (exp(z) + exp(-z))\ntanh(z) = sinh(z) / cosh(z) = (exp(z) - exp(-z)) / (exp(z) + exp(-z))\n\ncsch(z) = 1 / sinh(z)\nsech(z) = 1 / cosh(z)\ncoth(z) = 1 / tanh(z)\n\nasinh(z) = log(z + sqrt(z*z+1))\nacosh(z) = log(z + sqrt(z*z-1))\natanh(z) = 1/2 * log((1+z) / (1-z))\n\nacsch(z) = asinh(1 / z)\nasech(z) = acosh(1 / z)\nacoth(z) = atanh(1 / z) = 1/2 * log((1+z) / (z-1))\n\n*arg*, *abs*, *log*, *csc*, *cot*, *acsc*, *acot*, *csch*, *coth*,\n*acosech*, *acotanh*, have aliases *rho*, *theta*, *ln*, *cosec*,\n*cotan*, *acosec*, *acotan*, *cosech*, *cotanh*, *acosech*, *acotanh*,\nrespectively. \"Re\", \"Im\", \"arg\", \"abs\", \"rho\", and \"theta\" can be used\nalso as mutators. The \"cbrt\" returns only one of the solutions: if you\nwant all three, use the \"root\" function.\n\nThe *root* function is available to compute all the *n* roots of some\ncomplex, where *n* is a strictly positive integer. There are exactly *n*\nsuch roots, returned as a list. Getting the number mathematicians call\n\"j\" such that:\n\n1 + j + j*j = 0;\n\nis a simple matter of writing:\n\n$j = ((root(1, 3))[1];\n\nThe *k*th root for \"z = [r,t]\" is given by:\n\n(root(z, n))[k] = r(1/n) * exp(i * (t + 2*k*pi)/n)\n\nYou can return the *k*th root directly by \"root(z, n, k)\", indexing\nstarting from *zero* and ending at *n - 1*.\n\nThe *spaceship* numeric comparison operator, <=>, is also defined. In\norder to ensure its restriction to real numbers is conform to what you\nwould expect, the comparison is run on the real part of the complex\nnumber first, and imaginary parts are compared only when the real parts\nmatch.\n\n### CREATION\n\nTo create a complex number, use either:\n\n$z = Math::Complex->make(3, 4);\n$z = cplx(3, 4);\n\nif you know the cartesian form of the number, or\n\n$z = 3 + 4*i;\n\nif you like. To create a number using the polar form, use either:\n\n$z = Math::Complex->emake(5, pi/3);\n$x = cplxe(5, pi/3);\n\ninstead. The first argument is the modulus, the second is the angle (in\nradians, the full circle is 2*pi). (Mnemonic: \"e\" is used as a notation\nfor complex numbers in the polar form).\n\nIt is possible to write:\n\n$x = cplxe(-3, pi/4);\n\nbut that will be silently converted into \"[3,-3pi/4]\", since the modulus\nmust be non-negative (it represents the distance to the origin in the\ncomplex plane).\n\nIt is also possible to have a complex number as either argument of the\n\"make\", \"emake\", \"cplx\", and \"cplxe\": the appropriate component of the\nargument will be used.\n\n$z1 = cplx(-2,  1);\n$z2 = cplx($z1, 4);\n\nThe \"new\", \"make\", \"emake\", \"cplx\", and \"cplxe\" will also understand a\nsingle (string) argument of the forms\n\n2-3i\n-3i\n[2,3]\n[2,-3pi/4]\n[2]\n\nin which case the appropriate cartesian and exponential components will\nbe parsed from the string and used to create new complex numbers. The\nimaginary component and the theta, respectively, will default to zero.\n\nThe \"new\", \"make\", \"emake\", \"cplx\", and \"cplxe\" will also understand the\ncase of no arguments: this means plain zero or (0, 0).\n\n### DISPLAYING\n\nWhen printed, a complex number is usually shown under its cartesian\nstyle *a+bi*, but there are legitimate cases where the polar style\n*[r,t]* is more appropriate. The process of converting the complex\nnumber into a string that can be displayed is known as\n*stringification*.\n\nBy calling the class method \"Math::Complex::displayformat\" and\nsupplying either \"polar\" or \"cartesian\" as an argument, you override the\ndefault display style, which is \"cartesian\". Not supplying any argument\nreturns the current settings.\n\nThis default can be overridden on a per-number basis by calling the\n\"displayformat\" method instead. As before, not supplying any argument\nreturns the current display style for this number. Otherwise whatever\nyou specify will be the new display style for *this* particular number.\n\nFor instance:\n\nuse Math::Complex;\n\nMath::Complex::displayformat('polar');\n$j = (root(1, 3))[1];\nprint \"j = $j\\n\";               # Prints \"j = [1,2pi/3]\"\n$j->displayformat('cartesian');\nprint \"j = $j\\n\";               # Prints \"j = -0.5+0.866025403784439i\"\n\nThe polar style attempts to emphasize arguments like *k*pi/n* (where *n*\nis a positive integer and *k* an integer within [-9, +9]), this is\ncalled *polar pretty-printing*.\n\nFor the reverse of stringifying, see the \"make\" and \"emake\".\n\nCHANGED IN PERL 5.6\nThe \"displayformat\" class method and the corresponding \"displayformat\"\nobject method can now be called using a parameter hash instead of just a\none parameter.\n\nThe old display format style, which can have values \"cartesian\" or\n\"polar\", can be changed using the \"style\" parameter.\n\n$j->displayformat(style => \"polar\");\n\nThe one parameter calling convention also still works.\n\n$j->displayformat(\"polar\");\n\nThere are two new display parameters.\n\nThe first one is \"format\", which is a sprintf()-style format string to\nbe used for both numeric parts of the complex number(s). The is somewhat\nsystem-dependent but most often it corresponds to \"%.15g\". You can\nrevert to the default by setting the \"format\" to \"undef\".\n\n# the $j from the above example\n\n$j->displayformat('format' => '%.5f');\nprint \"j = $j\\n\";               # Prints \"j = -0.50000+0.86603i\"\n$j->displayformat('format' => undef);\nprint \"j = $j\\n\";               # Prints \"j = -0.5+0.86603i\"\n\nNotice that this affects also the return values of the \"displayformat\"\nmethods: in list context the whole parameter hash will be returned, as\nopposed to only the style parameter value. This is a potential\nincompatibility with earlier versions if you have been calling the\n\"displayformat\" method in list context.\n\nThe second new display parameter is \"polarprettyprint\", which can be\nset to true or false, the default being true. See the previous section\nfor what this means.\n\n### USAGE\n\nThanks to overloading, the handling of arithmetics with complex numbers\nis simple and almost transparent.\n\nHere are some examples:\n\nuse Math::Complex;\n\n$j = cplxe(1, 2*pi/3);  # $j  3 == 1\nprint \"j = $j, j3 = \", $j  3, \"\\n\";\nprint \"1 + j + j2 = \", 1 + $j + $j2, \"\\n\";\n\n$z = -16 + 0*i;                 # Force it to be a complex\nprint \"sqrt($z) = \", sqrt($z), \"\\n\";\n\n$k = exp(i * 2*pi/3);\nprint \"$j - $k = \", $j - $k, \"\\n\";\n\n$z->Re(3);                      # Re, Im, arg, abs,\n$j->arg(2);                     # (the last two aka rho, theta)\n# can be used also as mutators.\n\n### CONSTANTS\n\nPI\nThe constant \"pi\" and some handy multiples of it (pi2, pi4, and pip2\n(pi/2) and pip4 (pi/4)) are also available if separately exported:\n\nuse Math::Complex ':pi';\n$thirdofcircle = pi2 / 3;\n\n#### Inf\n\nThe floating point infinity can be exported as a subroutine Inf():\n\nuse Math::Complex qw(Inf sinh);\nmy $AlsoInf = Inf() + 42;\nmy $AnotherInf = sinh(1e42);\nprint \"$AlsoInf is $AnotherInf\\n\" if $AlsoInf == $AnotherInf;\n\nNote that the stringified form of infinity varies between platforms: it\ncan be for example any of\n\ninf\ninfinity\nINF\n1.#INF\n\nor it can be something else.\n\nAlso note that in some platforms trying to use the infinity in\narithmetic operations may result in Perl crashing because using an\ninfinity causes SIGFPE or its moral equivalent to be sent. The way to\nignore this is\n\nlocal $SIG{FPE} = sub { };\n\n### ERRORS DUE TO DIVISION BY ZERO OR LOGARITHM OF ZERO\n\nThe division (/) and the following functions\n\nlog     ln      log10   logn\ntan     sec     csc     cot\natan    asec    acsc    acot\ntanh    sech    csch    coth\natanh   asech   acsch   acoth\n\ncannot be computed for all arguments because that would mean dividing by\nzero or taking logarithm of zero. These situations cause fatal runtime\nerrors looking like this\n\ncot(0): Division by zero.\n(Because in the definition of cot(0), the divisor sin(0) is 0)\nDied at ...\n\nor\n\natanh(-1): Logarithm of zero.\nDied at...\n\nFor the \"csc\", \"cot\", \"asec\", \"acsc\", \"acot\", \"csch\", \"coth\", \"asech\",\n\"acsch\", the argument cannot be 0 (zero). For the logarithmic functions\nand the \"atanh\", \"acoth\", the argument cannot be 1 (one). For the\n\"atanh\", \"acoth\", the argument cannot be -1 (minus one). For the \"atan\",\n\"acot\", the argument cannot be \"i\" (the imaginary unit). For the \"atan\",\n\"acoth\", the argument cannot be \"-i\" (the negative imaginary unit). For\nthe \"tan\", \"sec\", \"tanh\", the argument cannot be *pi/2 + k * pi*, where\n*k* is any integer. atan2(0, 0) is undefined, and if the complex\narguments are used for atan2(), a division by zero will happen if\nz12+z22 == 0.\n\nNote that because we are operating on approximations of real numbers,\nthese errors can happen when merely `too close' to the singularities\nlisted above.\n\n### ERRORS DUE TO INDIGESTIBLE ARGUMENTS\n\nThe \"make\" and \"emake\" accept both real and complex arguments. When they\ncannot recognize the arguments they will die with error messages like\nthe following\n\nMath::Complex::make: Cannot take real part of ...\nMath::Complex::make: Cannot take real part of ...\nMath::Complex::emake: Cannot take rho of ...\nMath::Complex::emake: Cannot take theta of ...\n\n### BUGS\n\nSaying \"use Math::Complex;\" exports many mathematical routines in the\ncaller environment and even overrides some (\"sqrt\", \"log\", \"atan2\").\nThis is construed as a feature by the Authors, actually... ;-)\n\nAll routines expect to be given real or complex numbers. Don't attempt\nto use BigFloat, since Perl has currently no rule to disambiguate a '+'\noperation (for instance) between two overloaded entities.\n\nIn Cray UNICOS there is some strange numerical instability that results\nin root(), cos(), sin(), cosh(), sinh(), losing accuracy fast. Beware.\nThe bug may be in UNICOS math libs, in UNICOS C compiler, in\nMath::Complex. Whatever it is, it does not manifest itself anywhere else\nwhere Perl runs.\n\n### SEE ALSO\n\nMath::Trig\n\n### AUTHORS\n\nDaniel S. Lewart <lewart!at!uiuc.edu>, Jarkko Hietaniemi\n<jhi!at!iki.fi>, Raphael Manfredi <RaphaelManfredi!at!pobox.com>,\nZefram <zefram@fysh.org>\n\n### LICENSE\n\nThis library is free software; you can redistribute it and/or modify it\nunder the same terms as Perl itself.\n\n"
        }
    ],
    "structuredContent": {
        "command": "Math::Complex",
        "section": "",
        "mode": "perldoc",
        "summary": "Math::Complex - complex numbers and associated mathematical functions",
        "synopsis": "use Math::Complex;\n$z = Math::Complex->make(5, 6);\n$t = 4 - 3*i + $z;\n$j = cplxe(1, 2*pi/3);",
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 6,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 150,
                "subsections": []
            },
            {
                "name": "OPERATIONS",
                "lines": 101,
                "subsections": []
            },
            {
                "name": "CREATION",
                "lines": 49,
                "subsections": []
            },
            {
                "name": "DISPLAYING",
                "lines": 70,
                "subsections": []
            },
            {
                "name": "USAGE",
                "lines": 21,
                "subsections": []
            },
            {
                "name": "CONSTANTS",
                "lines": 7,
                "subsections": [
                    {
                        "name": "Inf",
                        "lines": 24
                    }
                ]
            },
            {
                "name": "ERRORS DUE TO DIVISION BY ZERO OR LOGARITHM OF ZERO",
                "lines": 36,
                "subsections": []
            },
            {
                "name": "ERRORS DUE TO INDIGESTIBLE ARGUMENTS",
                "lines": 9,
                "subsections": []
            },
            {
                "name": "BUGS",
                "lines": 14,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "AUTHORS",
                "lines": 4,
                "subsections": []
            },
            {
                "name": "LICENSE",
                "lines": 3,
                "subsections": []
            }
        ]
    }
}