| 1 | package Sidef::Types::Number::Complex; | ||
| 2 | |||
| 3 | # No reference is blessed in this class. | ||
| 4 | |||
| 5 | use utf8; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use parent qw( | ||
| 9 | Sidef::Types::Number::Number | ||
| 10 | ); | ||
| 11 | |||
| 12 | our ($PREC, $ROUND); | ||
| 13 | |||
| 14 | BEGIN { | ||
| 15 | *PREC = \$Sidef::Types::Number::Number::PREC; | ||
| 16 | *ROUND = \$Sidef::Types::Number::Number::ROUND; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub new { | ||
| 20 | my (undef, $real, $imag) = @_; | ||
| 21 | |||
| 22 | $real //= Sidef::Types::Number::Number::ZERO; | ||
| 23 | $imag //= Sidef::Types::Number::Number::ZERO; | ||
| 24 | |||
| 25 | Sidef::Types::Number::Number::_valid(\$real) if ref($real); | ||
| 26 | Sidef::Types::Number::Number::_valid(\$imag) if ref($imag); | ||
| 27 | |||
| 28 | if (ref($real) eq 'Sidef::Types::Number::Number') { | ||
| 29 | $real = $$real; | ||
| 30 | } | ||
| 31 | else { | ||
| 32 | $real = | ||
| 33 | defined($real) | ||
| 34 | ? Sidef::Types::Number::Number::_str2obj("$real") | ||
| 35 | : ${(Sidef::Types::Number::Number::ZERO)}; | ||
| 36 | |||
| 37 | $real = Sidef::Types::Number::Number::_any2mpz($real) if !ref($real); | ||
| 38 | } | ||
| 39 | |||
| 40 | if (defined($imag)) { | ||
| 41 | |||
| 42 | if (ref($imag) eq 'Sidef::Types::Number::Number') { | ||
| 43 | $imag = $$imag; | ||
| 44 | } | ||
| 45 | else { | ||
| 46 | $imag = Sidef::Types::Number::Number::_str2obj("$imag"); | ||
| 47 | } | ||
| 48 | |||
| 49 | $imag = Sidef::Types::Number::Number::_any2mpz($imag) if !ref($imag); | ||
| 50 | |||
| 51 | my $c = Math::MPC::Rmpc_init2($PREC); | ||
| 52 | my $sig = join(' ', ref($real), ref($imag)); | ||
| 53 | |||
| 54 | # GMPz | ||
| 55 | if ($sig eq q(Math::GMPz Math::GMPz)) { | ||
| 56 | Math::MPC::Rmpc_set_z_z($c, $real, $imag, $ROUND); | ||
| 57 | } | ||
| 58 | elsif ($sig eq q(Math::GMPz Math::GMPq)) { | ||
| 59 | Math::MPC::Rmpc_set_z_q($c, $real, $imag, $ROUND); | ||
| 60 | } | ||
| 61 | elsif ($sig eq q(Math::GMPz Math::MPFR)) { | ||
| 62 | Math::MPC::Rmpc_set_z_fr($c, $real, $imag, $ROUND); | ||
| 63 | } | ||
| 64 | |||
| 65 | # GMPq | ||
| 66 | elsif ($sig eq q(Math::GMPq Math::GMPq)) { | ||
| 67 | Math::MPC::Rmpc_set_q_q($c, $real, $imag, $ROUND); | ||
| 68 | } | ||
| 69 | elsif ($sig eq q(Math::GMPq Math::GMPz)) { | ||
| 70 | Math::MPC::Rmpc_set_q_z($c, $real, $imag, $ROUND); | ||
| 71 | } | ||
| 72 | elsif ($sig eq q(Math::GMPq Math::MPFR)) { | ||
| 73 | Math::MPC::Rmpc_set_q_fr($c, $real, $imag, $ROUND); | ||
| 74 | } | ||
| 75 | |||
| 76 | # MPFR | ||
| 77 | elsif ($sig eq q(Math::MPFR Math::MPFR)) { | ||
| 78 | Math::MPC::Rmpc_set_fr_fr($c, $real, $imag, $ROUND); | ||
| 79 | } | ||
| 80 | elsif ($sig eq q(Math::MPFR Math::GMPz)) { | ||
| 81 | Math::MPC::Rmpc_set_fr_z($c, $real, $imag, $ROUND); | ||
| 82 | } | ||
| 83 | elsif ($sig eq q(Math::MPFR Math::GMPq)) { | ||
| 84 | Math::MPC::Rmpc_set_fr_q($c, $real, $imag, $ROUND); | ||
| 85 | } | ||
| 86 | |||
| 87 | # Anything else | ||
| 88 | else { | ||
| 89 | $real = Sidef::Types::Number::Number::_any2mpc($real); | ||
| 90 | $imag = Sidef::Types::Number::Number::_any2mpc($imag); | ||
| 91 | Math::MPC::Rmpc_set($c, $imag, $ROUND); | ||
| 92 | Math::MPC::Rmpc_mul_i($c, $c, 1, $ROUND); | ||
| 93 | Math::MPC::Rmpc_add($c, $c, $real, $ROUND); | ||
| 94 | } | ||
| 95 | |||
| 96 | bless \$c, 'Sidef::Types::Number::Number'; | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | my $c = Sidef::Types::Number::Number::_any2mpc($real); | ||
| 100 | bless \$c, 'Sidef::Types::Number::Number'; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | *call = \&new; | ||
| 105 | |||
| 106 | 1 |