| 1 | package Math::Sidef; | ||
| 2 | |||
| 3 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use 5.016; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | our $VERSION = '0.05'; | ||
| 11 | |||
| 12 | 0 | ▶ | use Sidef; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | use Math::AnyNum; | ||
| 14 | |||
| 15 | use Sidef::Types::Number::Number; | ||
| 16 | use Sidef::Types::Number::Gauss; | ||
| 17 | use Sidef::Types::Number::Quadratic; | ||
| 18 | use Sidef::Types::Number::Quaternion; | ||
| 19 | use Sidef::Types::Number::Mod; | ||
| 20 | use Sidef::Types::Number::Polynomial; | ||
| 21 | use Sidef::Types::Number::Fraction; | ||
| 22 | |||
| 23 | my $sidef_number = 'Sidef::Types::Number::Number'; | ||
| 24 | my $sidef_gauss = 'Sidef::Types::Number::Gauss'; | ||
| 25 | my $sidef_quadratic = 'Sidef::Types::Number::Quadratic'; | ||
| 26 | my $sidef_quaternion = 'Sidef::Types::Number::Quaternion'; | ||
| 27 | my $sidef_mod = 'Sidef::Types::Number::Mod'; | ||
| 28 | my $sidef_polynomial = 'Sidef::Types::Number::Polynomial'; | ||
| 29 | my $sidef_fraction = 'Sidef::Types::Number::Fraction'; | ||
| 30 | my $sidef_array = 'Sidef::Types::Array::Array'; | ||
| 31 | my $sidef_string = 'Sidef::Types::String::String'; | ||
| 32 | my $sidef_bool = 'Sidef::Types::Bool::Bool'; | ||
| 33 | |||
| 34 | my @number_methods = grep { /^\w+\z/ } keys %{$sidef_number->methods->get_value}; | ||
| 35 | my @gauss_methods = grep { /^\w+\z/ } keys %{$sidef_gauss->methods->get_value}; | ||
| 36 | my @quadratic_methods = grep { /^\w+\z/ } keys %{$sidef_quadratic->methods->get_value}; | ||
| 37 | my @quaternion_methods = grep { /^\w+\z/ } keys %{$sidef_quaternion->methods->get_value}; | ||
| 38 | my @mod_methods = grep { /^\w+\z/ } keys %{$sidef_mod->methods->get_value}; | ||
| 39 | my @polynomial_methods = grep { /^\w+\z/ } keys %{$sidef_polynomial->methods->get_value}; | ||
| 40 | my @fraction_methods = grep { /^\w+\z/ } keys %{$sidef_fraction->methods->get_value}; | ||
| 41 | |||
| 42 | my @names = ( | ||
| 43 | @number_methods, @gauss_methods, @quadratic_methods, @quaternion_methods, | ||
| 44 | @mod_methods, @polynomial_methods, @fraction_methods | ||
| 45 | ); | ||
| 46 | |||
| 47 | @names = do { # remove duplicates | ||
| 48 | my %seen; | ||
| 49 | grep { !$seen{$_}++ } @names; | ||
| 50 | }; | ||
| 51 | |||
| 52 | my @constructors = qw(Number Gauss Quadratic Quaternion Mod Poly Polynomial Fraction); | ||
| 53 | |||
| 54 | our @ISA = qw(Exporter); | ||
| 55 | our @EXPORT_OK = (@names, @constructors); | ||
| 56 | our %EXPORT_TAGS = ( | ||
| 57 | number => ['Number', @number_methods], | ||
| 58 | gauss => ['Gauss', @gauss_methods], | ||
| 59 | quadratic => ['Quadratic', @quadratic_methods], | ||
| 60 | quaternion => ['Quaternion', @quaternion_methods], | ||
| 61 | mod => ['Mod', @mod_methods], | ||
| 62 | poly => ['Poly', @polynomial_methods], | ||
| 63 | polynomial => ['Polynomial', @polynomial_methods], | ||
| 64 | fraction => ['Fraction', @fraction_methods], | ||
| 65 | all => [@names, @constructors], | ||
| 66 | ); | ||
| 67 | |||
| 68 | sub Number { | ||
| 69 | _pack_value(@_); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub Gauss { | ||
| 73 | $sidef_gauss->new(map { _pack_value($_) } @_); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub Quadratic { | ||
| 77 | $sidef_quadratic->new(map { _pack_value($_) } @_); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub Quaternion { | ||
| 81 | $sidef_quaternion->new(map { _pack_value($_) } @_); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub Mod { | ||
| 85 | $sidef_mod->new(map { _pack_value($_) } @_); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub Polynomial { | ||
| 89 | $sidef_polynomial->new( | ||
| 90 | map { | ||
| 91 | ref($_) eq 'ARRAY' | ||
| 92 | ? $sidef_array->new([map { _pack_value($_) } @$_]) | ||
| 93 | : _pack_value($_) | ||
| 94 | } @_ | ||
| 95 | ); | ||
| 96 | } | ||
| 97 | |||
| 98 | *Poly = \&Polynomial; | ||
| 99 | |||
| 100 | sub Fraction { | ||
| 101 | $sidef_fraction->new(map { _pack_value($_) } @_); | ||
| 102 | } | ||
| 103 | |||
| 104 | sub _unpack_value { | ||
| 105 | my ($r) = @_; | ||
| 106 | |||
| 107 | my $ref = ref($r // return undef); | ||
| 108 | |||
| 109 | if ($ref eq $sidef_array) { | ||
| 110 | return [map { __SUB__->($_) } @$r]; | ||
| 111 | } | ||
| 112 | |||
| 113 | if ($ref eq $sidef_number) { | ||
| 114 | return Math::AnyNum->new($$_); | ||
| 115 | } | ||
| 116 | |||
| 117 | if ($ref eq $sidef_bool) { | ||
| 118 | return $$r; | ||
| 119 | } | ||
| 120 | |||
| 121 | if ($ref eq $sidef_string) { | ||
| 122 | return $$r; | ||
| 123 | } | ||
| 124 | |||
| 125 | return $r; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub _pack_value { | ||
| 129 | my ($r) = @_; | ||
| 130 | |||
| 131 | my $ref = ref($r // return undef); | ||
| 132 | |||
| 133 | if ($ref eq 'Math::AnyNum') { | ||
| 134 | return $sidef_number->new($$r); | ||
| 135 | } | ||
| 136 | |||
| 137 | if ( $ref eq $sidef_gauss | ||
| 138 | or $ref eq $sidef_quadratic | ||
| 139 | or $ref eq $sidef_quaternion | ||
| 140 | or $ref eq $sidef_mod | ||
| 141 | or $ref eq $sidef_fraction | ||
| 142 | or $ref eq $sidef_polynomial | ||
| 143 | or $ref eq $sidef_number) { | ||
| 144 | return $r; | ||
| 145 | } | ||
| 146 | |||
| 147 | return $sidef_number->new($r); | ||
| 148 | } | ||
| 149 | |||
| 150 | { | ||
| 151 | no strict 'refs'; | ||
| 152 | foreach my $name (@names) { | ||
| 153 | |||
| 154 | *{__PACKAGE__ . '::' . $name} = sub { | ||
| 155 | my (@args) = @_; | ||
| 156 | |||
| 157 | @args = map { | ||
| 158 | ref($_) eq 'Math::AnyNum' | ||
| 159 | ? $sidef_number->new($$_) | ||
| 160 | : ref($_) eq 'CODE' ? Sidef::Types::Block::Block->new( | ||
| 161 | code => do { | ||
| 162 | my $v = $_; | ||
| 163 | sub { | ||
| 164 | _pack_value(scalar $v->(map { _unpack_value($_) } @_)); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | ) | ||
| 168 | : ref($_) eq 'ARRAY' | ||
| 169 | ? [map { ref($_) eq 'Math::AnyNum' ? $sidef_number->new($$_) : _pack_value($_) } @$_] | ||
| 170 | : _pack_value($_) | ||
| 171 | } @args; | ||
| 172 | |||
| 173 | my $self = shift(@args); | ||
| 174 | my @r = $self->$name(@args); | ||
| 175 | |||
| 176 | #my @r = &{$sidef_number . '::' . $name}(@args); | ||
| 177 | |||
| 178 | if (scalar(@r) == 1) { | ||
| 179 | |||
| 180 | my $r = $r[0]; | ||
| 181 | |||
| 182 | if (ref($r // return undef) eq $sidef_number) { | ||
| 183 | return Math::AnyNum->new($$r); | ||
| 184 | } | ||
| 185 | |||
| 186 | if (ref($r) eq $sidef_bool) { | ||
| 187 | return $$r; | ||
| 188 | } | ||
| 189 | |||
| 190 | if (ref($r) eq $sidef_array) { | ||
| 191 | return map { ref($r) eq $sidef_number ? Math::AnyNum->new($$_) : _unpack_value($_) } @$r; | ||
| 192 | } | ||
| 193 | |||
| 194 | return _unpack_value($r); | ||
| 195 | } | ||
| 196 | |||
| 197 | map { _unpack_value($_) } @r; | ||
| 198 | }; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | 1; | ||
| 203 | __END__ | ||