| 1 | package Sidef::Types::Number::Fraction; | ||
| 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} => \&to_n, | ||
| 12 | q{0+} => \&to_n, | ||
| 13 | q{""} => \&__stringify__, | ||
| 14 | q{${}} => \&to_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::ZERO; | ||
| 25 | $m //= Sidef::Types::Number::Number::ONE; | ||
| 26 | |||
| 27 | bless {a => $n, b => $m}; | ||
| 28 | } | ||
| 29 | |||
| 30 | *call = \&new; | ||
| 31 | |||
| 32 | sub with_value { | ||
| 33 | my ($self, $value_1, $value_2) = @_; | ||
| 34 | __PACKAGE__->new($value_1, $value_2); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub stringify { | ||
| 38 | my ($x) = @_; | ||
| 39 | Sidef::Types::String::String->new(join('/', join('', '(', $x->{a}->stringify, ')'), join('', '(', $x->{b}->stringify, ')'))); | ||
| 40 | } | ||
| 41 | |||
| 42 | *pretty = \&stringify; | ||
| 43 | |||
| 44 | sub reduce { | ||
| 45 | my ($x) = @_; | ||
| 46 | my $g = $x->{a}->gcd($x->{b}); | ||
| 47 | ($g->is_zero or $g->is_one) and return $x; | ||
| 48 | __PACKAGE__->new($x->{a}->div($g), $x->{b}->div($g)); | ||
| 49 | } | ||
| 50 | |||
| 51 | *simplify = \&reduce; | ||
| 52 | |||
| 53 | sub is_nan { | ||
| 54 | my ($x) = @_; | ||
| 55 | if ($x->{a}->is_zero and $x->{b}->is_zero) { | ||
| 56 | return Sidef::Types::Bool::Bool::TRUE; | ||
| 57 | } | ||
| 58 | $x->{a}->is_nan or $x->{b}->is_nan; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub is_real { | ||
| 62 | my ($x) = @_; | ||
| 63 | if ($x->{a}->is_zero and $x->{b}->is_zero) { | ||
| 64 | return Sidef::Types::Bool::Bool::FALSE; | ||
| 65 | } | ||
| 66 | $x->{a}->is_real and $x->{b}->is_real; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub mediant { | ||
| 70 | my ($x, $y) = @_; | ||
| 71 | if (ref($y) ne __PACKAGE__) { | ||
| 72 | $y = __PACKAGE__->new($y); | ||
| 73 | } | ||
| 74 | __PACKAGE__->new($x->{a}->add($y->{a}), $x->{b}->add($y->{b})); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub eval { | ||
| 78 | my ($x, $v) = @_; | ||
| 79 | $x->{a}->eval($v)->div($x->{b}->eval($v)); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub lift { | ||
| 83 | my ($x) = @_; | ||
| 84 | __PACKAGE__->new($x->{a}->lift, $x->{b}->lift); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub to_n { | ||
| 88 | my ($x) = @_; | ||
| 89 | my $r = $x->{a}->to_n->div($x->{b}->to_n); | ||
| 90 | |||
| 91 | if (ref($r) ne 'Sidef::Types::Number::Number') { | ||
| 92 | return $r->to_n; | ||
| 93 | } | ||
| 94 | |||
| 95 | return $r; | ||
| 96 | } | ||
| 97 | |||
| 98 | *__boolify__ = \&to_n; | ||
| 99 | *__numify__ = \&to_n; | ||
| 100 | |||
| 101 | sub __stringify__ { | ||
| 102 | my ($x) = @_; | ||
| 103 | 'Fraction(' . join(', ', $x->{a}->dump, $x->{b}->dump) . ')'; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub to_s { | ||
| 107 | my ($x) = @_; | ||
| 108 | Sidef::Types::String::String->new($x->__stringify__); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub dump { | ||
| 112 | my ($x) = @_; | ||
| 113 | Sidef::Types::String::String->new($x->__stringify__); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub nu { | ||
| 117 | $_[0]->{a}; | ||
| 118 | } | ||
| 119 | |||
| 120 | *num = \ν | ||
| 121 | *numerator = \ν | ||
| 122 | |||
| 123 | sub de { | ||
| 124 | $_[0]->{b}; | ||
| 125 | } | ||
| 126 | |||
| 127 | *den = \&de; | ||
| 128 | *denominator = \&de; | ||
| 129 | |||
| 130 | sub nude { | ||
| 131 | ($_[0]->{a}, $_[0]->{b}); | ||
| 132 | } | ||
| 133 | |||
| 134 | sub parts { | ||
| 135 | Sidef::Types::Array::Array->new([$_[0]->nude]); | ||
| 136 | } | ||
| 137 | |||
| 138 | sub neg { | ||
| 139 | my ($x) = @_; | ||
| 140 | __PACKAGE__->new($x->{a}->neg, $x->{b}); | ||
| 141 | } | ||
| 142 | |||
| 143 | sub dec { | ||
| 144 | my ($x) = @_; | ||
| 145 | __PACKAGE__->new($x->{a}->sub($x->{b}), $x->{b},); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub inc { | ||
| 149 | my ($x) = @_; | ||
| 150 | __PACKAGE__->new($x->{a}->add($x->{b}), $x->{b},); | ||
| 151 | } | ||
| 152 | |||
| 153 | sub is_zero { | ||
| 154 | my ($x) = @_; | ||
| 155 | $x->eq(Sidef::Types::Number::Number::ZERO); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub is_one { | ||
| 159 | my ($x) = @_; | ||
| 160 | $x->eq(Sidef::Types::Number::Number::ONE); | ||
| 161 | } | ||
| 162 | |||
| 163 | sub is_mone { | ||
| 164 | my ($x) = @_; | ||
| 165 | $x->eq(Sidef::Types::Number::Number::MONE); | ||
| 166 | } | ||
| 167 | |||
| 168 | sub is_proper { | ||
| 169 | my ($x) = @_; | ||
| 170 | $x->{a}->lt($x->{b}); | ||
| 171 | } | ||
| 172 | |||
| 173 | sub is_improper { | ||
| 174 | my ($x) = @_; | ||
| 175 | $x->is_proper->not; | ||
| 176 | } | ||
| 177 | |||
| 178 | sub as_mixed { | ||
| 179 | my ($x) = @_; | ||
| 180 | my ($q, $r) = $x->{a}->divmod($x->{b}); | ||
| 181 | Sidef::Types::Array::Array->new([$q, $r, $x->{b}]); | ||
| 182 | } | ||
| 183 | |||
| 184 | sub add { | ||
| 185 | my ($x, $y) = @_; | ||
| 186 | |||
| 187 | if (ref($y) eq __PACKAGE__) { | ||
| 188 | return __PACKAGE__->new($x->{a}->mul($y->{b})->add($y->{a}->mul($x->{b})), $x->{b}->mul($y->{b})); | ||
| 189 | } | ||
| 190 | |||
| 191 | __PACKAGE__->new($x->{a}->add($y->mul($x->{b})), $x->{b}); | ||
| 192 | } | ||
| 193 | |||
| 194 | sub sub { | ||
| 195 | my ($x, $y) = @_; | ||
| 196 | $x->add($y->neg); | ||
| 197 | } | ||
| 198 | |||
| 199 | sub mul { | ||
| 200 | my ($x, $y) = @_; | ||
| 201 | |||
| 202 | if (ref($y) eq __PACKAGE__) { | ||
| 203 | return __PACKAGE__->new($x->{a}->mul($y->{a}), $x->{b}->mul($y->{b})); | ||
| 204 | } | ||
| 205 | |||
| 206 | __PACKAGE__->new($x->{a}->mul($y), $x->{b}); | ||
| 207 | } | ||
| 208 | |||
| 209 | sub sqr { | ||
| 210 | my ($x) = @_; | ||
| 211 | __PACKAGE__->new($x->{a}->mul($x->{a}), $x->{b}->mul($x->{b})); | ||
| 212 | } | ||
| 213 | |||
| 214 | sub inv { | ||
| 215 | my ($x) = @_; | ||
| 216 | __PACKAGE__->new($x->{b}, $x->{a}); | ||
| 217 | } | ||
| 218 | |||
| 219 | sub invmod { | ||
| 220 | my ($x, $n) = @_; | ||
| 221 | $x->inv->mod($n); | ||
| 222 | } | ||
| 223 | |||
| 224 | sub div { | ||
| 225 | my ($x, $y) = @_; | ||
| 226 | |||
| 227 | if (ref($y) eq __PACKAGE__) { | ||
| 228 | return __PACKAGE__->new($x->{a}->mul($y->{b}), $x->{b}->mul($y->{a})); | ||
| 229 | } | ||
| 230 | |||
| 231 | __PACKAGE__->new($x->{a}, $x->{b}->mul($y)); | ||
| 232 | } | ||
| 233 | |||
| 234 | sub pow { | ||
| 235 | my ($x, $n) = @_; | ||
| 236 | |||
| 237 | if ($n->is_neg) { | ||
| 238 | my $abs_n = $n->neg; | ||
| 239 | return __PACKAGE__->new($x->{b}->pow($abs_n), $x->{a}->pow($abs_n)); | ||
| 240 | } | ||
| 241 | |||
| 242 | __PACKAGE__->new($x->{a}->pow($n), $x->{b}->pow($n)); | ||
| 243 | } | ||
| 244 | |||
| 245 | sub powmod { | ||
| 246 | my ($x, $n, $m) = @_; | ||
| 247 | |||
| 248 | $x = $x->mod($m); | ||
| 249 | |||
| 250 | my $negative_power = 0; | ||
| 251 | |||
| 252 | if ($n->is_neg) { | ||
| 253 | $n = $n->abs; | ||
| 254 | $negative_power = 1; | ||
| 255 | } | ||
| 256 | |||
| 257 | my $c = __PACKAGE__->new(Sidef::Types::Number::Number::ONE, Sidef::Types::Number::Number::ONE); | ||
| 258 | |||
| 259 | foreach my $bit (reverse split(//, $n->as_bin)) { | ||
| 260 | |||
| 261 | # c = (c*x)%m if bit | ||
| 262 | # x = (x*x)%m | ||
| 263 | |||
| 264 | $c = $c->mul($x)->mod($m) if $bit; | ||
| 265 | $x = $x->sqr->mod($m); | ||
| 266 | } | ||
| 267 | |||
| 268 | if ($negative_power) { | ||
| 269 | $c = $c->invmod($m); | ||
| 270 | } | ||
| 271 | |||
| 272 | return $c; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub lsft { | ||
| 276 | my ($x, $n) = @_; | ||
| 277 | __PACKAGE__->new($x->{a}->lsft($n), $x->{b}); | ||
| 278 | } | ||
| 279 | |||
| 280 | sub rsft { | ||
| 281 | my ($x, $n) = @_; | ||
| 282 | __PACKAGE__->new($x->{a}, $x->{b}->lsft($n)); | ||
| 283 | } | ||
| 284 | |||
| 285 | sub floor { | ||
| 286 | my ($x) = @_; | ||
| 287 | |||
| 288 | my $y = $x->{a}->div($x->{b}); | ||
| 289 | |||
| 290 | if (ref($y) ne __PACKAGE__) { | ||
| 291 | return $y->floor; | ||
| 292 | } | ||
| 293 | |||
| 294 | $x->{a}->idiv_floor($x->{b}); | ||
| 295 | } | ||
| 296 | |||
| 297 | sub ceil { | ||
| 298 | my ($x) = @_; | ||
| 299 | |||
| 300 | my $y = $x->{a}->div($x->{b}); | ||
| 301 | |||
| 302 | if (ref($y) ne __PACKAGE__) { | ||
| 303 | return $y->ceil; | ||
| 304 | } | ||
| 305 | |||
| 306 | $x->{a}->idiv_ceil($x->{b}); | ||
| 307 | } | ||
| 308 | |||
| 309 | sub round { | ||
| 310 | my ($x) = @_; | ||
| 311 | |||
| 312 | my $y = $x->{a}->div($x->{b}); | ||
| 313 | |||
| 314 | if (ref($y) ne __PACKAGE__) { | ||
| 315 | return $y->round; | ||
| 316 | } | ||
| 317 | |||
| 318 | $x->{a}->idiv_round($x->{b}); | ||
| 319 | } | ||
| 320 | |||
| 321 | sub trunc { | ||
| 322 | my ($x) = @_; | ||
| 323 | |||
| 324 | my $y = $x->{a}->div($x->{b}); | ||
| 325 | |||
| 326 | if (ref($y) ne __PACKAGE__) { | ||
| 327 | return $y->trunc; | ||
| 328 | } | ||
| 329 | |||
| 330 | $x->{a}->idiv_trunc($x->{b}); | ||
| 331 | } | ||
| 332 | |||
| 333 | sub mod { | ||
| 334 | my ($x, $y) = @_; | ||
| 335 | |||
| 336 | if (ref($y) ne __PACKAGE__) { | ||
| 337 | return __PACKAGE__->new(Sidef::Types::Number::Mod->new($x->{a}, $y), Sidef::Types::Number::Mod->new($x->{b}, $y)); | ||
| 338 | } | ||
| 339 | |||
| 340 | $x->sub($y->mul($x->div($y)->floor)); | ||
| 341 | } | ||
| 342 | |||
| 343 | { | ||
| 344 | no strict 'refs'; | ||
| 345 | |||
| 346 | foreach my $method (qw(eq ne lt le gt ge cmp or xor and)) { | ||
| 347 | *{__PACKAGE__ . '::' . $method} = sub { | ||
| 348 | my ($x, $y) = @_; | ||
| 349 | |||
| 350 | if (ref($y) ne __PACKAGE__) { | ||
| 351 | $y = __PACKAGE__->new($y); | ||
| 352 | } | ||
| 353 | |||
| 354 | $x->{a}->mul($y->{b})->$method($x->{b}->mul($y->{a})); | ||
| 355 | }; | ||
| 356 | } | ||
| 357 | |||
| 358 | *{__PACKAGE__ . '::' . '%'} = \&mod; | ||
| 359 | *{__PACKAGE__ . '::' . '/'} = \÷ | ||
| 360 | *{__PACKAGE__ . '::' . '÷'} = \÷ | ||
| 361 | *{__PACKAGE__ . '::' . '*'} = \&mul; | ||
| 362 | *{__PACKAGE__ . '::' . '+'} = \&add; | ||
| 363 | *{__PACKAGE__ . '::' . '-'} = \⊂ | ||
| 364 | *{__PACKAGE__ . '::' . '**'} = \&pow; | ||
| 365 | *{__PACKAGE__ . '::' . '++'} = \&inc; | ||
| 366 | *{__PACKAGE__ . '::' . '--'} = \&dec; | ||
| 367 | *{__PACKAGE__ . '::' . '<'} = \< | ||
| 368 | *{__PACKAGE__ . '::' . '>'} = \> | ||
| 369 | *{__PACKAGE__ . '::' . '&'} = \∧ | ||
| 370 | *{__PACKAGE__ . '::' . '|'} = \∨ | ||
| 371 | *{__PACKAGE__ . '::' . '^'} = \&xor; | ||
| 372 | *{__PACKAGE__ . '::' . '<<'} = \&lsft; | ||
| 373 | *{__PACKAGE__ . '::' . '>>'} = \&rsft; | ||
| 374 | *{__PACKAGE__ . '::' . '<=>'} = \&cmp; | ||
| 375 | *{__PACKAGE__ . '::' . '<='} = \≤ | ||
| 376 | *{__PACKAGE__ . '::' . '≤'} = \≤ | ||
| 377 | *{__PACKAGE__ . '::' . '>='} = \≥ | ||
| 378 | *{__PACKAGE__ . '::' . '≥'} = \≥ | ||
| 379 | *{__PACKAGE__ . '::' . '=='} = \&eq; | ||
| 380 | *{__PACKAGE__ . '::' . '!='} = \≠ | ||
| 381 | } | ||
| 382 | |||
| 383 | 1 |