| 1 | package Sidef::Types::Number::Mod; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use 5.016; | ||
| 5 | |||
| 6 | use parent qw( | ||
| 7 | Sidef::Types::Number::Number | ||
| 8 | ); | ||
| 9 | |||
| 10 | use overload | ||
| 11 | q{bool} => sub { (@_) = ($_[0]); goto &__boolify__ }, | ||
| 12 | q{0+} => sub { (@_) = ($_[0]); goto &__numify__ }, | ||
| 13 | q{""} => sub { (@_) = ($_[0]); goto &__stringify__ }, | ||
| 14 | q{${}} => sub { $_[0]{n} }; | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my (undef, $n, $m) = @_; | ||
| 18 | |||
| 19 | # Handle evaluation of polynomials | ||
| 20 | if (ref($_[0]) eq __PACKAGE__) { | ||
| 21 | return $_[0]->eval($n); | ||
| 22 | } | ||
| 23 | |||
| 24 | #$n = Sidef::Types::Number::Number->new($n) if !UNIVERSAL::isa($n, 'Sidef::Types::Number::Number'); | ||
| 25 | #$m = Sidef::Types::Number::Number->new($m) if !UNIVERSAL::isa($m, 'Sidef::Types::Number::Number'); | ||
| 26 | |||
| 27 | if (ref($n) eq __PACKAGE__) { | ||
| 28 | $n = $n->real->mod($m); | ||
| 29 | } | ||
| 30 | |||
| 31 | $n = $n->mod($m); | ||
| 32 | |||
| 33 | if (ref($n) eq 'Sidef::Types::Number::Polynomial' and ref($m) ne 'Sidef::Types::Number::Polynomial') { | ||
| 34 | return $n; | ||
| 35 | } | ||
| 36 | |||
| 37 | if (ref($n) eq 'Sidef::Types::Number::Fraction' and ref($m) ne 'Sidef::Types::Number::Fraction') { | ||
| 38 | return $n; | ||
| 39 | } | ||
| 40 | |||
| 41 | if (ref($n) eq __PACKAGE__) { | ||
| 42 | return $n; | ||
| 43 | } | ||
| 44 | |||
| 45 | bless {n => $n, m => $m}; | ||
| 46 | } | ||
| 47 | |||
| 48 | *call = \&new; | ||
| 49 | |||
| 50 | sub with_value { | ||
| 51 | my ($self, $value) = @_; | ||
| 52 | __PACKAGE__->new($value, $self->{m}); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub to_n { | ||
| 56 | $_[0]->{n}; | ||
| 57 | } | ||
| 58 | |||
| 59 | *lift = \&to_n; | ||
| 60 | |||
| 61 | sub eval { | ||
| 62 | my ($x, $v) = @_; | ||
| 63 | |||
| 64 | #$x->{n}->eval($v)->mod($x->{m}); | ||
| 65 | __PACKAGE__->new($x->{n}->eval($v), $x->{m}); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub is_zero { | ||
| 69 | my ($x) = @_; | ||
| 70 | $x->eq(Sidef::Types::Number::Number::ZERO); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub is_one { | ||
| 74 | my ($x) = @_; | ||
| 75 | $x->eq(Sidef::Types::Number::Number::ONE); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub is_mone { | ||
| 79 | my ($x) = @_; | ||
| 80 | $x->eq(Sidef::Types::Number::Number::MONE); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub is_neg { | ||
| 84 | $_[0]->{n}->is_neg; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub is_pos { | ||
| 88 | $_[0]->{n}->is_pos; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub is_nan { | ||
| 92 | $_[0]->{n}->is_nan; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub is_inf { | ||
| 96 | $_[0]->{n}->is_inf; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub is_ninf { | ||
| 100 | $_[0]->{n}->is_ninf; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub is_real { | ||
| 104 | $_[0]->{n}->is_real; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub real { | ||
| 108 | $_[0]->{n}->is_zero ? $_[0]->{m} : $_[0]->{n}; | ||
| 109 | } | ||
| 110 | |||
| 111 | *re = \ℜ | ||
| 112 | |||
| 113 | sub norm { | ||
| 114 | $_[0]->real->norm; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub modulus { | ||
| 118 | $_[0]->{m}; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub __boolify__ { | ||
| 122 | $_[0]->{n}; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub __numify__ { | ||
| 126 | $_[0]->{n}; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub __stringify__ { | ||
| 130 | my ($x) = @_; | ||
| 131 | "Mod($x->{n}, $x->{m})"; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub to_s { | ||
| 135 | my ($x) = @_; | ||
| 136 | Sidef::Types::String::String->new($x->__stringify__); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub dump { | ||
| 140 | my ($x) = @_; | ||
| 141 | Sidef::Types::String::String->new(join('', "Mod(", join(', ', $x->{n}->dump, $x->{m}->dump), ')')); | ||
| 142 | } | ||
| 143 | |||
| 144 | sub pretty { | ||
| 145 | my ($x) = @_; | ||
| 146 | Sidef::Types::String::String->new(join('', "Mod(", join(', ', $x->{n}->pretty, $x->{m}->pretty), ')')); | ||
| 147 | } | ||
| 148 | |||
| 149 | sub mod { | ||
| 150 | my ($x, $y) = @_; | ||
| 151 | __PACKAGE__->new($x->lift, $y->lift); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub div { | ||
| 155 | my ($x, $y) = @_; | ||
| 156 | |||
| 157 | if (ref($y) ne __PACKAGE__) { | ||
| 158 | $y = __PACKAGE__->new($y, $x->{m}); | ||
| 159 | } | ||
| 160 | |||
| 161 | $y = $y->{n} if (ref($y) eq __PACKAGE__); | ||
| 162 | |||
| 163 | __PACKAGE__->new($x->{n}->mul($y->invmod($x->{m})), $x->{m}); | ||
| 164 | } | ||
| 165 | |||
| 166 | sub neg { | ||
| 167 | my ($x) = @_; | ||
| 168 | __PACKAGE__->new($x->{n}->neg, $x->{m}); | ||
| 169 | } | ||
| 170 | |||
| 171 | sub abs { | ||
| 172 | my ($x) = @_; | ||
| 173 | $x->{n}->abs; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub inv { | ||
| 177 | my ($x) = @_; | ||
| 178 | __PACKAGE__->new($x->{n}->invmod($x->{m}), $x->{m}); | ||
| 179 | } | ||
| 180 | |||
| 181 | sub inc { | ||
| 182 | my ($x) = @_; | ||
| 183 | $x->add(Sidef::Types::Number::Number::ONE); | ||
| 184 | } | ||
| 185 | |||
| 186 | sub dec { | ||
| 187 | my ($x) = @_; | ||
| 188 | $x->sub(Sidef::Types::Number::Number::ONE); | ||
| 189 | } | ||
| 190 | |||
| 191 | sub chinese { | ||
| 192 | my (@values) = @_; | ||
| 193 | |||
| 194 | #<<< | ||
| 195 | my $crt = __PACKAGE__->new( | ||
| 196 | Sidef::Types::Number::Number::ZERO, | ||
| 197 | Sidef::Types::Number::Number::ONE | ||
| 198 | ); | ||
| 199 | #>>> | ||
| 200 | |||
| 201 | foreach my $mod (@values) { | ||
| 202 | |||
| 203 | ref($mod) eq __PACKAGE__ or next; | ||
| 204 | |||
| 205 | #<<< | ||
| 206 | $crt = __PACKAGE__->new( | ||
| 207 | Sidef::Math::Math->chinese( | ||
| 208 | [$crt->{n}, $crt->{m}], | ||
| 209 | [$mod->{n}, $mod->{m}] | ||
| 210 | ), | ||
| 211 | Sidef::Types::Number::Number::lcm($crt->{m}, $mod->{m}) | ||
| 212 | ); | ||
| 213 | #>>> | ||
| 214 | } | ||
| 215 | |||
| 216 | $crt; | ||
| 217 | } | ||
| 218 | |||
| 219 | sub sqrt { | ||
| 220 | my ($x) = @_; | ||
| 221 | __PACKAGE__->new($x->{n}->sqrtmod($x->{m}), $x->{m}); | ||
| 222 | } | ||
| 223 | |||
| 224 | sub sqr { | ||
| 225 | my ($x) = @_; | ||
| 226 | __PACKAGE__->new($x->{n}->sqr, $x->{m}); | ||
| 227 | } | ||
| 228 | |||
| 229 | sub znorder { | ||
| 230 | my ($x) = @_; | ||
| 231 | $x->{n}->znorder($x->{m}); | ||
| 232 | } | ||
| 233 | |||
| 234 | *multiplicative_order = \&znorder; | ||
| 235 | |||
| 236 | sub pow { | ||
| 237 | my ($x, $y) = @_; | ||
| 238 | __PACKAGE__->new($x->{n}->powmod($y, $x->{m}), $x->{m}); | ||
| 239 | } | ||
| 240 | |||
| 241 | sub factorial { | ||
| 242 | my ($x) = @_; | ||
| 243 | __PACKAGE__->new($x->{n}->factorialmod($x->{m}), $x->{m}); | ||
| 244 | } | ||
| 245 | |||
| 246 | sub lucas { | ||
| 247 | my ($x) = @_; | ||
| 248 | __PACKAGE__->new($x->{n}->lucasmod($x->{m}), $x->{m}); | ||
| 249 | } | ||
| 250 | |||
| 251 | sub lucasu { | ||
| 252 | my ($x, $P, $Q) = @_; | ||
| 253 | __PACKAGE__->new(Sidef::Types::Number::Number::lucasUmod($P, $Q, $x->{n}, $x->{m}), $x->{m}); | ||
| 254 | } | ||
| 255 | |||
| 256 | *lucasU = \&lucasu; | ||
| 257 | *LucasU = \&lucasu; | ||
| 258 | |||
| 259 | sub lucasv { | ||
| 260 | my ($x, $P, $Q) = @_; | ||
| 261 | __PACKAGE__->new(Sidef::Types::Number::Number::lucasVmod($P, $Q, $x->{n}, $x->{m}), $x->{m}); | ||
| 262 | } | ||
| 263 | |||
| 264 | *lucasV = \&lucasv; | ||
| 265 | *LucasV = \&lucasv; | ||
| 266 | |||
| 267 | sub fibonacci { | ||
| 268 | my ($x) = @_; | ||
| 269 | __PACKAGE__->new($x->{n}->fibmod($x->{m}), $x->{m}); | ||
| 270 | } | ||
| 271 | |||
| 272 | *fib = \&fibonacci; | ||
| 273 | |||
| 274 | sub chebyshevu { | ||
| 275 | my ($x, $n) = @_; | ||
| 276 | __PACKAGE__->new(Sidef::Types::Number::Number::chebyshevUmod($n, $x->{n}, $x->{m}), $x->{m}); | ||
| 277 | } | ||
| 278 | |||
| 279 | *ChebyshevU = \&chebyshevu; | ||
| 280 | *chebyshevU = \&chebyshevu; | ||
| 281 | |||
| 282 | sub chebyshevt { | ||
| 283 | my ($x, $n) = @_; | ||
| 284 | __PACKAGE__->new(Sidef::Types::Number::Number::chebyshevTmod($n, $x->{n}, $x->{m}), $x->{m}); | ||
| 285 | } | ||
| 286 | |||
| 287 | *chebyshevT = \&chebyshevt; | ||
| 288 | *ChebyshevT = \&chebyshevt; | ||
| 289 | |||
| 290 | sub cyclotomic { | ||
| 291 | my ($x, $n) = @_; | ||
| 292 | __PACKAGE__->new(Sidef::Types::Number::Number::cyclotomicmod($n, $x->{n}, $x->{m}), $x->{m}); | ||
| 293 | } | ||
| 294 | |||
| 295 | sub shift_left { # x * 2^n | ||
| 296 | my ($x, $n) = @_; | ||
| 297 | $x->mul(Sidef::Types::Number::Number::TWO->powmod($n, $x->{m})); | ||
| 298 | } | ||
| 299 | |||
| 300 | *lsft = \&shift_left; | ||
| 301 | |||
| 302 | sub shift_right { # x / 2^n | ||
| 303 | my ($x, $n) = @_; | ||
| 304 | $x->div(Sidef::Types::Number::Number::TWO->powmod($n, $x->{m})); | ||
| 305 | } | ||
| 306 | |||
| 307 | *rsft = \&shift_right; | ||
| 308 | |||
| 309 | { | ||
| 310 | no strict 'refs'; | ||
| 311 | |||
| 312 | foreach my $method (qw(eq ne lt le gt ge cmp)) { | ||
| 313 | *{__PACKAGE__ . '::' . $method} = sub { | ||
| 314 | my ($x, $y) = @_; | ||
| 315 | |||
| 316 | if (ref($y) ne __PACKAGE__) { | ||
| 317 | $y = __PACKAGE__->new($y, $x->{m}); | ||
| 318 | } | ||
| 319 | |||
| 320 | $y = $y->{n} if (ref($y) eq __PACKAGE__); | ||
| 321 | |||
| 322 | $x->{n}->$method($y); | ||
| 323 | }; | ||
| 324 | } | ||
| 325 | |||
| 326 | foreach my $method (qw(mul add sub xor or and)) { | ||
| 327 | *{__PACKAGE__ . '::' . $method} = sub { | ||
| 328 | my ($x, $y) = @_; | ||
| 329 | |||
| 330 | if (ref($y) ne __PACKAGE__) { | ||
| 331 | $y = __PACKAGE__->new($y, $x->{m}); | ||
| 332 | } | ||
| 333 | |||
| 334 | $y = $y->{n} if (ref($y) eq __PACKAGE__); | ||
| 335 | |||
| 336 | __PACKAGE__->new($x->{n}->$method($y), $x->{m}); | ||
| 337 | }; | ||
| 338 | } | ||
| 339 | |||
| 340 | *{__PACKAGE__ . '::' . '/'} = \÷ | ||
| 341 | *{__PACKAGE__ . '::' . '÷'} = \÷ | ||
| 342 | *{__PACKAGE__ . '::' . '*'} = \&mul; | ||
| 343 | *{__PACKAGE__ . '::' . '+'} = \&add; | ||
| 344 | *{__PACKAGE__ . '::' . '-'} = \⊂ | ||
| 345 | *{__PACKAGE__ . '::' . '%'} = \&mod; | ||
| 346 | *{__PACKAGE__ . '::' . '**'} = \&pow; | ||
| 347 | *{__PACKAGE__ . '::' . '++'} = \&inc; | ||
| 348 | *{__PACKAGE__ . '::' . '--'} = \&dec; | ||
| 349 | *{__PACKAGE__ . '::' . '<'} = \< | ||
| 350 | *{__PACKAGE__ . '::' . '>'} = \> | ||
| 351 | *{__PACKAGE__ . '::' . '&'} = \∧ | ||
| 352 | *{__PACKAGE__ . '::' . '|'} = \∨ | ||
| 353 | *{__PACKAGE__ . '::' . '^'} = \&xor; | ||
| 354 | *{__PACKAGE__ . '::' . '<<'} = \&lsft; | ||
| 355 | *{__PACKAGE__ . '::' . '>>'} = \&rsft; | ||
| 356 | *{__PACKAGE__ . '::' . '<=>'} = \&cmp; | ||
| 357 | *{__PACKAGE__ . '::' . '<='} = \≤ | ||
| 358 | *{__PACKAGE__ . '::' . '≤'} = \≤ | ||
| 359 | *{__PACKAGE__ . '::' . '>='} = \≥ | ||
| 360 | *{__PACKAGE__ . '::' . '≥'} = \≥ | ||
| 361 | *{__PACKAGE__ . '::' . '=='} = \&eq; | ||
| 362 | *{__PACKAGE__ . '::' . '!='} = \≠ | ||
| 363 | *{__PACKAGE__ . '::' . '!'} = \&factorial; | ||
| 364 | } | ||
| 365 | |||
| 366 | 1 |