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-wis also a square root ofz. 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::Complexfor 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 π
π COPYRIGHT π
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
- ubuntu stonking: libmath-cephes-perl 0.5308-1build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms