complex_exponentiation_in_real_numbers.pl 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 13 August 2017
  4. # https://github.com/trizen
  5. # Identity for complex exponentiation in real numbers, based on the identity:
  6. #
  7. # exp(x*i) = cos(x) + sin(x)*i
  8. #
  9. use 5.010;
  10. use strict;
  11. use warnings;
  12. #
  13. ## Real base and complex exponent
  14. #
  15. sub complex_power {
  16. my ($x, $r, $i) = @_;
  17. (
  18. $x**$r * cos(log($x) * $i),
  19. $x**$r * sin(log($x) * $i),
  20. )
  21. }
  22. #
  23. ## Complex base and complex exponent
  24. #
  25. sub complex_power2 {
  26. my ($x, $y, $r, $i) = @_;
  27. ($x, $y) = (log($x*$x + $y*$y) / 2, atan2($y, $x)); # log($x + $y*i)
  28. ($x, $y) = ($x*$r - $y*$i, $x*$i + $y*$r); # ($x + $y*i) * ($r + $i*i)
  29. (exp($x) * cos($y), exp($x) * sin($y)); # exp($x + $y*i)
  30. }
  31. #
  32. ## Example for 12^(3+4i)
  33. #
  34. {
  35. # base
  36. my $x = 12;
  37. # exponent
  38. my $r = 3;
  39. my $i = 4;
  40. my ($real, $imag) = complex_power($x, $r, $i);
  41. say "$x^($r + $i*i) = $real + $imag*i"; #=> -1503.99463080925 + -850.872581822307*i
  42. }
  43. #
  44. ## Example for (5+2i)^(3+7i)
  45. #
  46. {
  47. # base
  48. my $x = 5;
  49. my $y = 2;
  50. # exponent
  51. my $r = 3;
  52. my $i = 7;
  53. my ($real, $imag) = complex_power2($x, $y, $r, $i);
  54. say "($x + $y*i)^($r + $i*i) = $real + $imag*i"; #=> 10.1847486230437 + 3.84152292303168*i
  55. }