GComplex.pod 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. =encoding utf8
  2. =head1 NAME
  3. Math::GComplex - Generic complex number library.
  4. =head1 VERSION
  5. Version 0.13
  6. =head1 SYNOPSIS
  7. use 5.010;
  8. use Math::GComplex;
  9. use Math::AnyNum qw(:overload);
  10. my $x = Math::GComplex->new(3, 4);
  11. my $y = Math::GComplex->new(7, 5);
  12. say $x + $y; #=> (10 9)
  13. say $x - $y; #=> (-4 -1)
  14. say $x * $y; #=> (1 43)
  15. say $x / $y; #=> (41/74 13/74)
  16. =head1 DESCRIPTION
  17. B<Math::GComplex> is a lightweight library, providing 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 L<Math::AnyNum>.
  18. In most cases, it can be used as a drop-in replacement for L<Math::Complex>.
  19. Due to its simple and elegant design, Math::GComplex is between 2x up to 8x faster than L<Math::Complex>.
  20. =head1 EXPORT
  21. The following functions are exportable:
  22. :trig
  23. sin sinh asin asinh
  24. cos cosh acos acosh
  25. tan tanh atan atanh
  26. cot coth acot acoth
  27. sec sech asec asech
  28. csc csch acsc acsch
  29. atan2 deg2rad rad2deg
  30. :special
  31. gcd invmod powmod
  32. log logn exp pow pown sqrt cbrt root
  33. :misc
  34. cplx polar abs acmp sgn conj norm
  35. inv real imag reals floor ceil round
  36. Multiple functions can be exported as:
  37. use Math::GComplex qw(acos acosh);
  38. There is also the possibility of exporting an entire group of functions, by specifying their group name, as:
  39. use Math::GComplex qw(:trig);
  40. The imaginary unit, C<i = sqrt(-1)>, is also exportable, as:
  41. use Math::GComplex qw(i);
  42. Additionally, by specifying the C<:all> keyword, all the exportable functions, including the C<i> constant, will be exported:
  43. use Math::GComplex qw(:all);
  44. The C<:overload> keyword enables constant overloading, which makes
  45. each number a Math::GComplex object and also exports the C<i> constant:
  46. use Math::GComplex qw(:overload);
  47. CORE::say 3 + 4*i; #=> (3 4)
  48. CORE::say log(-1); #=> (0 3.14159265358979)
  49. B<NOTE:> C<:overload> is lexical to the current scope only.
  50. The syntax for disabling the C<:overload> behavior in the current scope, is:
  51. no Math::GComplex; # :overload will be disabled in the current scope
  52. Nothing is exported by default.
  53. =head1 INITIALIZATION
  54. =head2 new / cplx / make
  55. my $z = cplx($real, $imag);
  56. my $z = Math::GComplex->new($real, $imag);
  57. my $z = Math::GComplex->make($real, $imag);
  58. Create a new complex number, given its Cartesian coordinate form.
  59. =head2 cplxe / emake
  60. my $z = cplxe($r, $theta);
  61. my $z = Math::GComplex->emake($r, $theta);
  62. Create a new complex number, given its polar form.
  63. =head2 i
  64. my $i = Math::GComplex::i();
  65. Returns the imaginary unit as a B<Math::GComplex> object, equivalent with C<cplx(0, 1)>.
  66. =head1 BASIC OPERATIONS
  67. This section describes all the basic operations provided by this module.
  68. =head2 add
  69. my $z = $x + $y;
  70. my $z = $x->add($y);
  71. Addition of C<x> and C<y>, defined as:
  72. (a + b*i) + (x + y*i) = (a + x) + (b + y)*i
  73. =head2 sub
  74. my $z = $x - $y;
  75. my $z = $x->sub($y);
  76. Subtraction of C<y> from C<x>, defined as:
  77. (a + b*i) - (x + y*i) = (a - x) + (b - y)*i
  78. =head2 mul
  79. my $z = $x * $y;
  80. my $z = $x->mul($y);
  81. Multiplication of C<x> and C<y>, defined as:
  82. (a + b*i) * (x + y*i) = (a*x - b*y) + (a*y + b*x)*i
  83. =head2 div
  84. my $z = $x / $y;
  85. my $z = $x->div($y);
  86. Division of C<x> by C<y>, defined as:
  87. (a + b*i) / (x + y*i) = (a*x + b*y)/(x^2 + y^2) + (b*x - a*y)/(x^2 + y^2)*i
  88. =head2 mod
  89. my $z = $x % $y;
  90. my $z = $x->mod($y);
  91. Remainder of C<x> when divided by C<y>, defined as:
  92. mod(a, b) = a - b * floor(a/b)
  93. =head2 neg
  94. my $z = -$x;
  95. my $z = $x->neg;
  96. Additive inverse of C<x>, defined as:
  97. neg(a + b*i) = -a - b*i
  98. =head2 conj
  99. my $z = ~$x;
  100. my $z = $x->conj;
  101. Complex conjugate of C<x>, defined as:
  102. conj(a + b*i) = a - b*i
  103. =head2 inv
  104. my $z = $x->inv;
  105. Multiplicative inverse of C<x>, defined as:
  106. inv(x) = 1/x
  107. =head2 norm
  108. my $z = $x->norm;
  109. Normalized value of C<x>, defined as:
  110. norm(a + b*i) = a^2 + b^2
  111. =head2 abs
  112. my $z = $x->abs;
  113. Absolute value of C<x>, defined as:
  114. abs(a + b*i) = sqrt(a^2 + b^2)
  115. =head2 sgn
  116. my $z = $x->sgn;
  117. The sign of C<x>, defined as:
  118. sgn(x) = x / abs(x)
  119. =head1 SPECIAL FUNCTIONS
  120. This section describes the special mathematical functions provided by this module.
  121. =head2 log
  122. my $z = log($x);
  123. my $z = $x->log;
  124. Natural logarithm of C<x>, defined as:
  125. log(a + b*i) = log(a^2 + b^2)/2 + atan2(b, a) * i
  126. =head2 logn
  127. my $z = $x->logn($y);
  128. Logarithm of C<x> to base C<y>, defined as:
  129. logn(a, b) = log(a) / log(b)
  130. =head2 exp
  131. my $z = exp($x);
  132. my $z = $x->exp;
  133. Natural exponentiation of C<x>, defined as:
  134. exp(a + b*i) = exp(a) * cos(b) + exp(a) * sin(b) * i
  135. =head2 pow
  136. my $z = $x**$y;
  137. my $z = $x->pow($y);
  138. Raises C<x> to power C<y> and returns the result, defined as:
  139. a^b = exp(log(a) * b)
  140. =head2 pown
  141. my $z = $x->pown($n);
  142. Raises C<x> to power C<n>, using the exponentiation by squaring method, and returns the result, where C<n> is a native integer.
  143. =head2 powmod
  144. my $z = $x->powmod($n, $m);
  145. Modular exponentiation C<x^n mod m>, where C<n> in an arbitrary large integer.
  146. =head2 gcd
  147. my $z = $n->gcd($k);
  148. Greatest common divisors of two complex numbers.
  149. =head2 invmod
  150. my $x = $n->invmod($m);
  151. Modular multiplicative inverse of two complex numbers.
  152. The returned value is the solution to C<x> in:
  153. n*x = 1 (mod m)
  154. Returns C<undef> when a multiplicative inverse mod C<m> does not exist.
  155. =head2 root
  156. my $z = $x->root($y);
  157. Nth root of C<x>, defined as:
  158. root(a, b) = exp(log(a) / b)
  159. =head2 sqrt
  160. my $z = sqrt($x);
  161. my $z = $x->sqrt;
  162. Square root of C<x>, defined as:
  163. sqrt(x) = exp(log(x) / 2)
  164. =head2 cbrt
  165. my $z = $x->cbrt;
  166. Cube root of C<x>, defined as:
  167. cbrt(x) = exp(log(x) / 3)
  168. =head1 TRIGONOMETRIC FUNCTIONS
  169. This section includes all the trigonometric functions provided by Math::GComplex.
  170. =head2 sin / sinh / asin / asinh
  171. my $z = $x->sin;
  172. my $z = $x->sinh;
  173. my $z = $x->asin;
  174. my $z = $x->asinh;
  175. Sine, hyperbolic sine, inverse sine and inverse hyperbolic sine.
  176. Defined as:
  177. sin(x) = (exp(x * i) - exp(-i * x))/(2 * i)
  178. sinh(x) = (exp(2 * x) - 1) / (2 * exp(x))
  179. asin(x) = -i * log(i * x + sqrt(1 - x^2))
  180. asinh(x) = log(sqrt(x^2 + 1) + x)
  181. =head2 cos / cosh / acos / acosh
  182. my $z = $x->cos;
  183. my $z = $x->cosh;
  184. my $z = $x->acos;
  185. my $z = $x->acosh;
  186. Cosine, hyperbolic cosine, inverse cosine and inverse hyperbolic cosine.
  187. Defined as:
  188. cos(x) = (exp(-i * x) + exp(i * x)) / 2
  189. cosh(x) = (exp(2 * x) + 1) / (2 * exp(x))
  190. acos(x) = -2 * i * log(i * sqrt((1 - x)/2) + sqrt((1 + x)/2))
  191. acosh(x) = log(x + sqrt(x - 1) * sqrt(x + 1))
  192. =head2 tan / tanh / atan / atanh
  193. my $z = $x->tan;
  194. my $z = $x->tanh;
  195. my $z = $x->atan;
  196. my $z = $x->atanh;
  197. Tangent, hyperbolic tangent, inverse tangent and inverse hyperbolic tangent.
  198. Defined as:
  199. tan(x) = (2 * i)/(exp(2 * i * x) + 1) - i
  200. tanh(x) = (exp(2 * x) - 1) / (exp(2 * x) + 1)
  201. atan(x) = i * (log(1 - i * x) - log(1 + i * x)) / 2
  202. atanh(x) = (log(1 + x) - log(1 - x)) / 2
  203. =head2 cot / coth / acot / acoth
  204. my $z = $x->cot;
  205. my $z = $x->coth;
  206. my $z = $x->acot;
  207. my $z = $x->acoth;
  208. Cotangent, hyperbolic cotangent, inverse cotangent and inverse hyperbolic cotangent.
  209. Defined as:
  210. cot(x) = (2 * i)/(exp(2 * i * x) - 1) + i
  211. coth(x) = (exp(2 * x) + 1) / (exp(2 * x) - 1)
  212. acot(x) = atan(1/x)
  213. acoth(x) = atanh(1/x)
  214. =head2 sec / sech / asec / asech
  215. my $z = $x->sec;
  216. my $z = $x->sech;
  217. my $z = $x->asec;
  218. my $z = $x->asech;
  219. Secant, hyperbolic secant, inverse secant and inverse hyperbolic secant.
  220. Defined as:
  221. sec(x) = 2/(exp(-i * x) + exp(i * x))
  222. sech(x) = (2 * exp(x)) / (exp(2 * x) + 1)
  223. asec(x) = acos(1/x)
  224. asech(x) = acosh(1/x)
  225. =head2 csc / csch / acsc / acsch
  226. my $z = $x->csc;
  227. my $z = $x->csch;
  228. my $z = $x->acsc;
  229. my $z = $x->acsch;
  230. Cosecant, hyperbolic cosecant, inverse cosecant and inverse hyperbolic cosecant.
  231. Defined as:
  232. csc(x) = -(2 * i)/(exp(-i * x) - exp(i * x))
  233. csch(x) = (2 * exp(x)) / (exp(2 * x) - 1)
  234. acsc(x) = asin(1/x)
  235. acsch(x) = asinh(1/x)
  236. =head2 atan2
  237. my $z = atan2($x, $y);
  238. my $z = $x->atan2($y);
  239. The arc tangent of C<x> and C<y>, defined as:
  240. atan2(a, b) = -i * log((b + a*i) / sqrt(a^2 + b^2))
  241. =head2 deg2rad
  242. my $rad = $x->deg2rad;
  243. Returns the value of C<x> converted from degrees to radians.
  244. Defined as:
  245. deg2rad(x) = x / 180 * atan2(0, -abs(x))
  246. =head2 rad2deg
  247. my $deg = $x->rad2deg;
  248. Returns the value of C<x> converted from radians to degrees.
  249. Defined as:
  250. rad2deg(x) = x * 180 / atan2(0, -abs(x))
  251. =head1 MISCELLANEOUS FUNCTIONS
  252. This section describes the various useful methods provided by this module.
  253. =head2 floor
  254. my $z = $x->floor;
  255. The floor function, defined as:
  256. floor(a + b*i) = floor(a) + floor(b)*i
  257. =head2 ceil
  258. my $z = $x->ceil;
  259. The ceil function, defined as:
  260. ceil(a + b*i) = ceil(a) + ceil(b)*i
  261. =head2 round
  262. my $z = $x->round;
  263. The round function, rounding C<x> to the nearest Gaussian integer, defined as:
  264. round(a + b*i) = round(a) + round(b)*i
  265. This function uses the half-away-from-zero tie-breaking method, defined as:
  266. round(+0.5) = +1
  267. round(-0.5) = -1
  268. =head2 int
  269. my $z = int($x);
  270. my $z = $x->int;
  271. The integer-truncation function, defined as:
  272. int(a + b*i) = int(a) + int(b)*i
  273. =head2 and
  274. my $z = $x & $y;
  275. my $z = $x->and($y);
  276. Bitwise AND-logical operation, defined as:
  277. (a + b*i) & (x + y*i) = (a & x) + (b & y)*i
  278. =head2 or
  279. my $z = $x | $y;
  280. my $z = $x->or($y);
  281. Bitwise OR-logical operation, defined as:
  282. (a + b*i) | (x + y*i) = (a | x) + (b | y)*i
  283. =head2 xor
  284. my $z = $x ^ $y;
  285. my $z = $x->xor($y);
  286. Bitwise XOR-logical operation, defined as:
  287. (a + b*i) ^ (x + y*i) = (a ^ x) + (b ^ y)*i
  288. =head2 lsft
  289. my $z = $x << $n;
  290. my $z = $x->lsft($n);
  291. Bitwise left-shift operation, defined as:
  292. (a + b*i) << n = (a << n) + (b << n)*i
  293. (a + b*i) << (x + y*i) = int((a + b*i) * 2**(x + y*i))
  294. =head2 rsft
  295. my $z = $x >> $n;
  296. my $z = $x->rsft($n);
  297. Bitwise right-shift operation, defined as:
  298. (a + b*i) >> n = (a >> n) + (b >> n)*i
  299. (a + b*i) >> (x + y*i) = int((a + b*i) / 2**(x + y*i))
  300. =head2 real
  301. my $re = $x->real;
  302. Return the real part of C<x>.
  303. =head2 imag
  304. my $im = $x->imag;
  305. Returns the imaginary part of C<x>.
  306. =head2 reals
  307. my ($re, $im) = $x->reals
  308. Returns the real and the imaginary part of C<x>, as real numbers.
  309. =head1 * Comparisons
  310. =head2 eq
  311. my $bool = $x == $y;
  312. my $bool = $x->eq($y);
  313. Equality check: returns a true value when C<x> and C<y> are equal.
  314. =head2 ne
  315. my $bool = $x != $y;
  316. my $bool = $x->ne($y);
  317. Inequality check: returns a true value when C<x> and C<y> are not equal.
  318. =head2 gt
  319. my $bool = $x > $y;
  320. my $bool = $x->gt($y);
  321. Returns a true value when C<x> is greater than C<y>.
  322. =head2 ge
  323. my $bool = $x >= $y;
  324. my $bool = $x->ge($y);
  325. Returns a true value when C<x> is equal or greater than C<y>.
  326. =head2 lt
  327. my $bool = $x < $y;
  328. my $bool = $x->lt($y);
  329. Returns a true value when C<x> is less than C<y>.
  330. =head2 le
  331. my $bool = $x <= $y;
  332. my $bool = $x->le($y)
  333. Returns a true value when C<x> is equal or less than C<y>.
  334. =head2 cmp
  335. my $int = $x <=> $y;
  336. my $int = $x->cmp($y);
  337. Compares C<x> to C<y> and returns a negative value when C<x> is less than C<y>,
  338. 0 when C<x> and C<y> are equal, and a positive value when C<x> is greater than C<y>.
  339. Complex numbers are compared as:
  340. (real($x) <=> real($y)) ||
  341. (imag($x) <=> imag($y))
  342. =head2 acmp
  343. my $int = $x->acmp($y);
  344. Absolute comparison of C<x> and C<y>, defined as:
  345. acmp(a, b) = abs(a) <=> abs(b)
  346. =head1 * Conversions
  347. =head2 polar
  348. my ($rho, $theta) = $x->polar;
  349. Returns the polar form of C<x>, such that:
  350. x = rho * exp(theta * i)
  351. =head2 boolify
  352. my $bool = $x->boolify;
  353. Returns a true value when either the real part or the imaginary part of C<x> is non-zero.
  354. =head2 numify
  355. my $num = $x->numify;
  356. Returns the real part of C<x>.
  357. =head2 stringify
  358. my $str = $x->stringify;
  359. Returns a stringification version of C<x>.
  360. Example:
  361. Math::GComplex->new( 3, -4)->stringify; # "(3 -4)"
  362. Math::GComplex->new(-5, 6)->stringify; # "(-5 6)"
  363. =head1 LIMITATIONS
  364. 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.
  365. When the components of a complex number are native Perl numbers, the "division by zero" and the "logarithm of zero" cases are implicitly handled by this library.
  366. However the user may still encounter incorrect results due to rounding errors and/or overflow/underflow in some special cases, such as:
  367. coth(1e6) = (NaN NaN)
  368. cosh(1e6) = (NaN NaN)
  369. =head1 SEE ALSO
  370. =over 4
  371. =item * Other math libraries
  372. L<Math::AnyNum> - Arbitrary size precision for integers, rationals, floating-points and complex numbers.
  373. L<Math::GMP> - High speed arbitrary size integer math.
  374. L<Math::GMPz> - perl interface to the GMP library's integer (mpz) functions.
  375. L<Math::GMPq> - perl interface to the GMP library's rational (mpq) functions.
  376. L<Math::MPFR> - perl interface to the MPFR (floating point) library.
  377. L<Math::MPC> - perl interface to the MPC (multi precision complex) library.
  378. L<Math::Complex> - complex numbers and associated mathematical functions.
  379. =back
  380. =head1 REPOSITORY
  381. L<https://github.com/trizen/Math-GComplex>
  382. =head1 AUTHOR
  383. Daniel Șuteu, C<< <trizen at cpan.org> >>
  384. =head1 COPYRIGHT AND LICENSE
  385. Copyright (C) 2018-2019 Daniel Șuteu
  386. This library is free software; you can redistribute it and/or modify
  387. it under the same terms as Perl itself, either Perl version 5.22.0 or,
  388. at your option, any later version of Perl 5 you may have available.
  389. =cut