Math::...Complex(3pm)

πŸ‘‘ Math::Cephes::Complex (3pm) πŸ‘‘

okay bestie, sit down, hydrate, and get comfy, because today we're doing complex numbers and honestly? they ate πŸ’…

This page is filed under the User Contributed Perl Documentation in man section 3pm, and the whole thing is about the module Math::Cephes::Complex.


✨ NAME ✨

Math::Cephes::Complex β€” the Perl interface to the cephes complex number routines. that's the name, that's the vibe, no notes.

🌸 SYNOPSIS 🌸

use Math::Cephes::Complex qw(cmplx);
my $z1 = cmplx(2,3);          # $z1 = 2 + 3 i
my $z2 = cmplx(3,4);          # $z2 = 3 + 4 i
my $z3 = $z1->radd($z2);      # $z3 = $z1 + $z2

look at that πŸ¦‹ three lines in and you're already doing complex arithmetic like a main character.

🧚 DESCRIPTION 🧚

so like, this module is a layer on top of the basic routines in the cephes math library to handle complex numbers. and there are so many ways to summon one, choose your fighter:

my $f = Math::Cephes::Complex->new(3, 2);   # $f = 3 + 2 i
my $g = new Math::Cephes::Complex(5, 3);    # $g = 5 + 3 i
my $h = cmplx(7, 5);                        # $h = 7 + 5 i

that last one, cmplx, is only available if you import cmplx. and if you pass literally no arguments, like

my $h = cmplx();

then the defaults $z = 0 + 0 i are assumed. no thoughts, head empty, zero plus zero i 😌

The real and imaginary parts of a complex number are represented respectively by

$f->{r}; $f->{i};

or, as methods,

$f->r;  $f->i;

and you can set them like

$f->{r} = 4; $f->{i} = 9;

or, again, as methods,

$f->r(4);   $f->i(9);

and to print the whole thing out:

print $f->as_string;

A summary of the usage is as follows πŸ‘‡


🌈 csin: Complex circular sine

SYNOPSIS

# void csin();
# cmplx z, w;

$z = cmplx(2, 3);    # $z = 2 + 3 i
$w = $z->csin;
print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
print $w->as_string;           # prints $w as Re($w) + i Im($w)

DESCRIPTION

If

z = x + iy,

then

w = sin x  cosh y  +  i cos x sinh y.

🌈 ccos: Complex circular cosine

SYNOPSIS

# void ccos();
# cmplx z, w;

$z = cmplx(2, 3);    # $z = 2 + 3 i
$w = $z->ccos;
print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
print $w->as_string;           # prints $w as Re($w) + i Im($w)

DESCRIPTION

If

z = x + iy,

then

w = cos x  cosh y  -  i sin x sinh y.

🌈 ctan: Complex circular tangent

SYNOPSIS

# void ctan();
# cmplx z, w;

$z = cmplx(2, 3);    # $z = 2 + 3 i
$w = $z->ctan;
print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
print $w->as_string;           # prints $w as Re($w) + i Im($w)

DESCRIPTION

If

z = x + iy,

then

          sin 2x  +  i sinh 2y
    w  =  --------------------.
           cos 2x  +  cosh 2y
⚠️ On the real axis the denominator is zero at odd multiples of PI/2. The denominator is evaluated by its Taylor series near these points. so like, near the spicy spots, it's handled, but be aware πŸ’€

🌈 ccot: Complex circular cotangent

SYNOPSIS

# void ccot();
# cmplx z, w;

$z = cmplx(2, 3);    # $z = 2 + 3 i
$w = $z->ccot;
print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
print $w->as_string;           # prints $w as Re($w) + i Im($w)

DESCRIPTION

If

z = x + iy,

then

          sin 2x  -  i sinh 2y
    w  =  --------------------.
           cosh 2y  -  cos 2x
⚠️ On the real axis, the denominator has zeros at even multiples of PI/2. Near these points it is evaluated by a Taylor series. she's doing the most, but respectfully ✨

🌈 casin: Complex circular arc sine

SYNOPSIS

# void casin();
# cmplx z, w;

$z = cmplx(2, 3);    # $z = 2 + 3 i
$w = $z->casin;
print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
print $w->as_string;           # prints $w as Re($w) + i Im($w)

DESCRIPTION

Inverse complex sine:

                                      2
w = -i clog( iz + csqrt( 1 - z ) ).

🌈 cacos: Complex circular arc cosine

SYNOPSIS

# void cacos();
# cmplx z, w;

$z = cmplx(2, 3);    # $z = 2 + 3 i
$w = $z->cacos;
print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
print $w->as_string;           # prints $w as Re($w) + i Im($w)

DESCRIPTION

w = arccos z  =  PI/2 - arcsin z.

🌈 catan: Complex circular arc tangent

SYNOPSIS

# void catan();
# cmplx z, w;

$z = cmplx(2, 3);    # $z = 2 + 3 i
$w = $z->catan;
print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
print $w->as_string;           # prints $w as Re($w) + i Im($w)

DESCRIPTION

If

z = x + iy,

then

             1       (    2x     )
    Re w  =  - arctan(-----------)  +  k PI
             2       (     2    2)
                     (1 - x  - y )

                  ( 2         2)
             1    (x  +  (y+1) )
    Im w  =  - log(------------)
             4    ( 2         2)
                  (x  +  (y-1) )

Where k is an arbitrary integer.


🌈 csinh: Complex hyperbolic sine

SYNOPSIS

# void csinh();
# cmplx z, w;

$z = cmplx(2, 3);    # $z = 2 + 3 i
$w = $z->csinh;
print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
print $w->as_string;           # prints $w as Re($w) + i Im($w)

DESCRIPTION

csinh z = (cexp(z) - cexp(-z))/2
        = sinh x * cos y  +  i cosh x * sin y .

🌈 casinh: Complex inverse hyperbolic sine

SYNOPSIS

# void casinh();
# cmplx z, w;

$z = cmplx(2, 3);    # $z = 2 + 3 i
$w = $z->casinh;
print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
print $w->as_string;           # prints $w as Re($w) + i Im($w)

DESCRIPTION

casinh z = -i casin iz .

🌈 ccosh: Complex hyperbolic cosine

SYNOPSIS

# void ccosh();
# cmplx z, w;

$z = cmplx(2, 3);    # $z = 2 + 3 i
$w = $z->ccosh;
print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
print $w->as_string;           # prints $w as Re($w) + i Im($w)

DESCRIPTION

ccosh(z) = cosh x  cos y + i sinh x sin y .

🌈 cacosh: Complex inverse hyperbolic cosine

SYNOPSIS

# void cacosh();
# cmplx z, w;

$z = cmplx(2, 3);    # $z = 2 + 3 i
$w = $z->cacosh;
print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
print $w->as_string;           # prints $w as Re($w) + i Im($w)

DESCRIPTION

acosh z = i acos z .

🌈 ctanh: Complex hyperbolic tangent

SYNOPSIS

# void ctanh();
# cmplx z, w;

$z = cmplx(2, 3);    # $z = 2 + 3 i
$w = $z->ctanh;
print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
print $w->as_string;           # prints $w as Re($w) + i Im($w)

DESCRIPTION

tanh z = (sinh 2x  +  i sin 2y) / (cosh 2x + cos 2y) .

🌈 catanh: Complex inverse hyperbolic tangent

SYNOPSIS

# void catanh();
# cmplx z, w;

$z = cmplx(2, 3);    # $z = 2 + 3 i
$w = $z->catanh;
print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
print $w->as_string;           # prints $w as Re($w) + i Im($w)

DESCRIPTION

Inverse tanh, equal to -i catan (iz);


🌈 cpow: Complex power function

SYNOPSIS

# void cpow();
# cmplx a, z, w;

$a = cmplx(5, 6);    # $z = 5 + 6 i
$z = cmplx(2, 3);    # $z = 2 + 3 i
$w = $a->cpow($z);
print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
print $w->as_string;           # prints $w as Re($w) + i Im($w)

DESCRIPTION

Raises complex A to the complex Zth power. Definition is per AMS55 # 4.2.8, analytically equivalent to cpow(a,z) = cexp(z clog(a)).


🌈 cmplx: Complex number arithmetic

SYNOPSIS

# typedef struct {
#     double r;     real part
#     double i;     imaginary part
#    }cmplx;

# cmplx *a, *b, *c;

$a = cmplx(3, 5);   # $a = 3 + 5 i
$b = cmplx(2, 3);   # $b = 2 + 3 i

$c = $a->cadd( $b );  #   c = a + b
$c = $a->csub( $b );  #   c = a - b
$c = $a->cmul( $b );  #   c = a * b
$c = $a->cdiv( $b );  #   c = a / b
$c = $a->cneg;        #   c = -a
$c = $a->cmov;        #   c = a

print $c->{r}, '  ', $c->{i};   # prints real and imaginary parts of $c
print $c->as_string;           # prints $c as Re($c) + i Im($c)

DESCRIPTION

Addition:

c.r  =  b.r + a.r
c.i  =  b.i + a.i

Subtraction:

c.r  =  b.r - a.r
c.i  =  b.i - a.i

Multiplication:

c.r  =  b.r * a.r  -  b.i * a.i
c.i  =  b.r * a.i  +  b.i * a.r

Division:

d    =  a.r * a.r  +  a.i * a.i
c.r  = (b.r * a.r  + b.i * a.i)/d
c.i  = (b.i * a.r  -  b.r * a.i)/d

🌈 cabs: Complex absolute value

SYNOPSIS

# double a, cabs();
# cmplx z;

$z = cmplx(2, 3);    # $z = 2 + 3 i
$a = cabs( $z );

DESCRIPTION

If z = x + iy

then

a = sqrt( x**2 + y**2 ).

Overflow and underflow are avoided by testing the magnitudes of x and y before squaring πŸ›‘οΈ If either is outside half of the floating point full scale range, both are rescaled.


🌈 csqrt: Complex square root

SYNOPSIS

# void csqrt();
# cmplx z, w;

$z = cmplx(2, 3);    # $z = 2 + 3 i
$w = $z->csqrt;
print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
print $w->as_string;           # prints $w as Re($w) + i Im($w)

DESCRIPTION

If z = x + iy, r = |z|, then

                              1/2
Im w  =  [ (r - x)/2 ]   ,

Re w  =  y / 2 Im w.
πŸ“Œ Note that -w is also a square root of z. The root chosen is always in the upper half plane.

Because of the potential for cancellation error in r - x, the result is sharpened by doing a Heron iteration (see sqrt.c) in complex arithmetic.


πŸ’€ BUGS πŸ’€

Please report any to Randy Kobes <randy@theoryx5.uwinnipeg.ca> β€” bestie is accepting mail, be nice 🫢

πŸ‘€ SEE ALSO πŸ‘€

  • For the basic interface to the cephes complex number routines, see Math::Cephes.
  • See also Math::Complex for a more extensive interface to complex number routines.

and that's only part 1, bestie 😭 there is so much more where this came from. don't go anywhere πŸ’–

Okay bestie, legal era incoming, but it's actually cute because it's just people saying "here, take my code, be normal about it" πŸ’–

The C code for the Cephes Math Library is Copyright 1984, 1987, 1989, 2002 by Stephen L. Moshier, and is available at http://www.netlib.org/cephes/. Direct inquiries to 30 Frost Street, Cambridge, MA 02140.

The perl interface is copyright 2000, 2002 by Randy Kobes. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


No cap β€” "free software, same terms as Perl itself" is the whole vibe. Respect the OG authors, keep the notices, and you're golden 🫢

And that's the whole page, bestie!! ✨ This beauty is perl v5.42.3, dated 2026-08-19, and it lives at Math::...Complex(3pm). Go forth and conjugate πŸ’…πŸ‘‘πŸŒˆ

πŸ“¦ where this page comes from