Math::GComplex - Generic complex number library.
Version 0.02
Math::GComplex provides a generic interface to complex number operations, accepting any type of number as a component of a complex number, including native Perl numbers and numerical objects provided by other mathematical libraries, such as Math::AnyNum.
use 5.014; use Math::GComplex; use Math::AnyNum qw(:overload); my $x = Math::GComplex->new(3, 4); my $y = Math::GComplex->new(7, 5); say $x + $y; #=> (10 9) say $x - $y; #=> (-4 -1) say $x * $y; #=> (1 43) say $x / $y; #=> (41/74 13/74)
Math::GComplex is a lightweight library, which focuses primarily on providing a friendly interface to complex number operations and good performance.
In most cases, it can be used as drop-in replacement for Math::Complex.
Due to its simple and elegant design, this library is between 2x up to 8x faster than Math::Complex.
The following functions are exportable:
:trig sin sinh asin asinh cos cosh acos acosh tan tanh atan atanh cot coth acot acoth sec sech asec asech csc csch acsc acsch atan2 :special log logn exp sqrt cbrt root :misc cplx abs acmp sgn conj inv real imag reals
Each function can be exported individually, as:
use Math::GComplex qw(acosh);
There is also the possibility of exporting an entire group of functions, as:
use Math::GComplex qw(:trig);
The imaginary unit, i = sqrt(-1), is also exportable, as:
i = sqrt(-1)
use Math::GComplex qw(i);
Additionally, by specifying the :all keyword, all the exportable functions, including the i constant, will be exported:
:all
i
use Math::GComplex qw(:all);
The :overload keyword enables constant overloading, which makes each number a Math::GComplex object and also exports the i constant:
:overload
use Math::GComplex qw(:overload); CORE::say 3 + 4*i; #=> (3 4) CORE::say log(-1); #=> (0 3.14159265358979)
Nothing is exported by default.
my $z = Math::GComplex->new($real, $imag);
Creates and returns a new Math::GComplex object.
my $z = cplx($real, $imag);
my $i = Math::GComplex::i();
Returns the imaginary unit as a Math::GComplex object, equivalent with cplx(0, 1).
cplx(0, 1)
This section describes all the basic operations provided by this module.
$x + $y
Addition of $x and $y.
$x
$y
$x - $y
Subtraction of $y from $x.
$x * $y
Multiplication of $x and $y.
$x / $y
Division of $x by $y.
$x % $y
Remainder of $x when divided by $y, defined as:
$x % $y = $x - $y*floor($x/$y)
-$x $x->neg
Additive inverse of $x.
~$x $x->conj
Complex conjugate of $x.
$x->inv
Multiplicative inverse of $x, defined as 1/$x.
1/$x
$x->abs
Absolute value of $x.
$x->sgn
The sign of $x, defined as $x/abs($x).
$x/abs($x)
This section describes the special mathematical functions provided by this module.
$x->log
Natural logarithm of $x.
$x->logn($y)
Logarithm for $x to base $y.
Defined as:
logn(a, b) = log(a) / log(b)
$x->exp
Natural exponentiation of $x.
$x->sqrt
Square root of $x. Equivalent with $x**(1/2).
$x**(1/2)
$x->cbrt
Cube root of $x. Equivalent with $x**(1/3).
$x**(1/3)
$x->root($y)
Nth root of $x. Equivalent with $x**(1/$y).
$x**(1/$y)
$x**$y $x->pow($y)
Raises $x to power $y and returns the result.
a**b = exp(log(a) * b)
This section includes all the trigonometric functions provied by Math::GComplex.
$x->sin $x->sinh $x->asin $x->asinh
Sine, hyperbolic sine, inverse sine and inverse hyperbolic sine.
$x->cos $x->cosh $x->acos $x->acosh
Cosine, hyperbolic cosine, inverse cosine and inverse hyperbolic cosine.
$x->tan $x->tanh $x->atan $x->atanh
Tangent, hyperbolic tangent, inverse tangent and inverse hyperbolic tangent.
$x->cot $x->coth $x->acot $x->acoth
Cotangent, hyperbolic cotangent, inverse cotangent and inverse hyperbolic cotangent.
$x->sec $x->sech $x->asec $x->asech
Secant, hyperbolic secant, inverse secant and inverse hyperbolic secant.
$x->csc $x->csch $x->acsc $x->acsch
Cosecant, hyperbolic cosecant, inverse cosecant and inverse hyperbolic cosecant.
$x->atan2($y)
The arctangent of the quotient of its arguments (i.e.: atan(x/y)).
atan(x/y)
This section describes the various useful methods provided by this module.
$x->floor
The floor function, defined for a complex number as:
floor(a + b*i) = floor(a) + floor(b)*i
$x->ceil
The ceil function, defined for a complex number as:
ceil(a + b*i) = ceil(a) + ceil(b)*i
$x->int
The integer-truncation function, defined for a complex number as:
int(a + b*i) = int(a) + int(b)*i
$x->real
Return the real part of $x.
$x->imag
Returns the imaginary part of $x.
($real, $imag) = $x->reals
Returns the real and the imaginary part of $x, as real numbers.
$x == $y $x->eq($y)
Equality check: returns a true value when $x and $y are equal.
$x != $y $x->ne($y)
Inequality check: returns a true value when $x and $y are not equal.
$x > $y $x->gt($y)
Returns a true value when $x is greater than $y.
$x >= $y $x->ge($y)
Returns a true value when $x is equal or greater than $y.
$x < $y $x->lt($y)
Returns a true value when $x is less than $y.
$x <= $y $x->le($y)
Returns a true value when $x is equal or less than $y.
$x <=> $y $x->cmp($y)
Compares $x to $y and returns a negative value when $x is less than $y, 0 when $x and $y are equal, and a positive value when $x is greater than $y.
Complex numbers are compared as:
(real($x) <=> real($y)) || (imag($x) <=> imag($y))
$x->acmp($y)
Absolute comparison of $x and $y.
acmp(x, y) = abs(x) <=> abs(y)
$x->boolify
Returns a true value when either the real part or the imaginary part of $x is non-zero.
$x->numify
Returns the real part of $x.
$x->stringify
Returns a stringification version of $x.
Example:
Math::GComplex->new(3, -4)->stringify; # "(3 -4)" Math::GComplex->new(-3, 4)->stringify; # "(-3 4)"
Being a generic interface, it assumes that all the special cases (such as division by zero) are handled by the library of which type the components of a complex number are.
When the components of a complex number are native Perl integers, the user may encounter errors due to division by zero or incorrect results due to overflow or underflow in some special cases.
Daniel "Trizen" Șuteu, <trizen at protonmail.com>
<trizen at protonmail.com>
Please report any bugs or feature requests at https://github.com/trizen/Math-GComplex/issues. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc Math::GComplex
You can also look for information at:
Github
https://github.com/trizen/Math-GComplex
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Math-GComplex
CPAN Ratings
http://cpanratings.perl.org/d/Math-GComplex
Search CPAN
http://search.cpan.org/dist/Math-GComplex/
Other math libraries
Math::AnyNum - Arbitrary size precision for integers, rationals, floating-points and complex numbers.
Math::GMP - High speed arbitrary size integer math.
Math::GMPz - perl interface to the GMP library's integer (mpz) functions.
Math::GMPq - perl interface to the GMP library's rational (mpq) functions.
Math::MPFR - perl interface to the MPFR (floating point) library.
Math::MPC - perl interface to the MPC (multi precision complex) library.
Math::Complex - complex numbers and associated mathematical functions.
Copyright 2018 Daniel "Trizen" Șuteu.
This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:
http://www.perlfoundation.org/artistic_license_2_0
Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
To install Math::GComplex, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Math::GComplex
CPAN shell
perl -MCPAN -e shell install Math::GComplex
For more information on module installation, please visit the detailed CPAN module installation guide.