| 1 | package Sidef::Types::Number::Quaternion; | ||
| 2 | |||
| 3 | # Reference: | ||
| 4 | # https://en.wikipedia.org/wiki/Quaternion | ||
| 5 | |||
| 6 | use utf8; | ||
| 7 | use 5.016; | ||
| 8 | |||
| 9 | use parent qw( | ||
| 10 | Sidef::Types::Number::Number | ||
| 11 | ); | ||
| 12 | |||
| 13 | use overload | ||
| 14 | q{bool} => sub { (@_) = ($_[0]); goto &__boolify__ }, | ||
| 15 | q{""} => sub { (@_) = ($_[0]); goto &__stringify__ }, | ||
| 16 | q{0+} => \&to_n, | ||
| 17 | q{${}} => \&to_n; | ||
| 18 | |||
| 19 | sub new { | ||
| 20 | my (undef, $A, $B, $C, $D) = @_; | ||
| 21 | |||
| 22 | # Handle evaluation of polynomials | ||
| 23 | if (ref($_[0]) eq __PACKAGE__) { | ||
| 24 | return $_[0]->eval($A); | ||
| 25 | } | ||
| 26 | |||
| 27 | $A //= Sidef::Types::Number::Number::ZERO; | ||
| 28 | $B //= Sidef::Types::Number::Number::ZERO; | ||
| 29 | $C //= Sidef::Types::Number::Number::ZERO; | ||
| 30 | $D //= Sidef::Types::Number::Number::ZERO; | ||
| 31 | |||
| 32 | $A = Sidef::Types::Number::Number->new($A) if !UNIVERSAL::isa($A, 'Sidef::Types::Number::Number'); | ||
| 33 | $B = Sidef::Types::Number::Number->new($B) if !UNIVERSAL::isa($B, 'Sidef::Types::Number::Number'); | ||
| 34 | $C = Sidef::Types::Number::Number->new($C) if !UNIVERSAL::isa($C, 'Sidef::Types::Number::Number'); | ||
| 35 | $D = Sidef::Types::Number::Number->new($D) if !UNIVERSAL::isa($D, 'Sidef::Types::Number::Number'); | ||
| 36 | |||
| 37 | bless {a => $A, b => $B, c => $C, d => $D}; | ||
| 38 | } | ||
| 39 | |||
| 40 | *call = \&new; | ||
| 41 | |||
| 42 | sub with_value { | ||
| 43 | my ($self, $value_1, $value_2, $value_3, $value_4) = @_; | ||
| 44 | __PACKAGE__->new($value_1, $value_2, $value_3, $value_4); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub eval { | ||
| 48 | my ($x, $v) = @_; | ||
| 49 | __PACKAGE__->new($x->{a}->eval($v), $x->{b}->eval($v), $x->{c}->eval($v), $x->{d}->eval($v)); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub lift { | ||
| 53 | my ($x) = @_; | ||
| 54 | __PACKAGE__->new($x->{a}->lift, $x->{b}->lift, $x->{c}->lift, $x->{d}->lift); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub a { | ||
| 58 | $_[0]->{a}; | ||
| 59 | } | ||
| 60 | |||
| 61 | *re = \&a; | ||
| 62 | *real = \&a; | ||
| 63 | |||
| 64 | sub b { | ||
| 65 | $_[0]->{b}; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub c { | ||
| 69 | $_[0]->{c}; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub d { | ||
| 73 | $_[0]->{d}; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub reals { | ||
| 77 | ($_[0]->{a}, $_[0]->{b}, $_[0]->{c}, $_[0]->{d}); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub parts { | ||
| 81 | Sidef::Types::Array::Array->new($_[0]->reals); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub __boolify__ { | ||
| 85 | $_[0]->{a}; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub __numify__ { | ||
| 89 | $_[0]->{a}; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub __stringify__ { | ||
| 93 | my ($x) = @_; | ||
| 94 | 'Quaternion(' . join(', ', $x->{a}->dump, $x->{b}->dump, $x->{c}->dump, $x->{d}->dump) . ')'; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub to_s { | ||
| 98 | my ($x) = @_; | ||
| 99 | Sidef::Types::String::String->new($x->__stringify__); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub dump { | ||
| 103 | my ($x) = @_; | ||
| 104 | Sidef::Types::String::String->new($x->__stringify__); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub to_gauss { | ||
| 108 | my ($x) = @_; | ||
| 109 | #<<< | ||
| 110 | Sidef::Types::Number::Gauss->new( | ||
| 111 | Sidef::Types::Number::Gauss->new($x->{a}, $x->{b}), | ||
| 112 | Sidef::Types::Number::Gauss->new($x->{c}, $x->{d}), | ||
| 113 | ); | ||
| 114 | #>>> | ||
| 115 | } | ||
| 116 | |||
| 117 | sub stringify { | ||
| 118 | my ($x) = @_; | ||
| 119 | $x->to_gauss->stringify; | ||
| 120 | } | ||
| 121 | |||
| 122 | *pretty = \&stringify; | ||
| 123 | |||
| 124 | sub to_n { | ||
| 125 | my ($x) = @_; | ||
| 126 | $x->to_gauss->to_n; | ||
| 127 | } | ||
| 128 | |||
| 129 | *to_c = \&to_n; | ||
| 130 | |||
| 131 | sub norm { | ||
| 132 | my ($x) = @_; | ||
| 133 | $x->{a}->sqr->add($x->{b}->sqr)->add($x->{c}->sqr)->add($x->{d}->sqr); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub abs { | ||
| 137 | my ($x) = @_; | ||
| 138 | $x->norm->sqrt; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub sgn { | ||
| 142 | my ($x) = @_; | ||
| 143 | $x->div($x->abs); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub neg { | ||
| 147 | my ($x) = @_; | ||
| 148 | #<<< | ||
| 149 | __PACKAGE__->new( | ||
| 150 | $x->{a}->neg, | ||
| 151 | $x->{b}->neg, | ||
| 152 | $x->{c}->neg, | ||
| 153 | $x->{d}->neg, | ||
| 154 | ); | ||
| 155 | #>>> | ||
| 156 | } | ||
| 157 | |||
| 158 | sub conj { | ||
| 159 | my ($x) = @_; | ||
| 160 | #<<< | ||
| 161 | __PACKAGE__->new( | ||
| 162 | $x->{a}, | ||
| 163 | $x->{b}->neg, | ||
| 164 | $x->{c}->neg, | ||
| 165 | $x->{d}->neg, | ||
| 166 | ); | ||
| 167 | #>>> | ||
| 168 | } | ||
| 169 | |||
| 170 | sub sqr { | ||
| 171 | my ($x) = @_; | ||
| 172 | $x->mul($x); | ||
| 173 | } | ||
| 174 | |||
| 175 | sub add { | ||
| 176 | my ($x, $y) = @_; | ||
| 177 | |||
| 178 | #<<< | ||
| 179 | if (ref($y) eq __PACKAGE__) { | ||
| 180 | return __PACKAGE__->new( | ||
| 181 | $x->{a}->add($y->{a}), | ||
| 182 | $x->{b}->add($y->{b}), | ||
| 183 | $x->{c}->add($y->{c}), | ||
| 184 | $x->{d}->add($y->{d}), | ||
| 185 | ); | ||
| 186 | } | ||
| 187 | #>>> | ||
| 188 | |||
| 189 | __PACKAGE__->new($x->{a}->add($y), $x->{b}, $x->{c}, $x->{d}); | ||
| 190 | } | ||
| 191 | |||
| 192 | sub sub { | ||
| 193 | my ($x, $y) = @_; | ||
| 194 | |||
| 195 | #<<< | ||
| 196 | if (ref($y) eq __PACKAGE__) { | ||
| 197 | return __PACKAGE__->new( | ||
| 198 | $x->{a}->sub($y->{a}), | ||
| 199 | $x->{b}->sub($y->{b}), | ||
| 200 | $x->{c}->sub($y->{c}), | ||
| 201 | $x->{d}->sub($y->{d}), | ||
| 202 | ); | ||
| 203 | } | ||
| 204 | #>>> | ||
| 205 | |||
| 206 | __PACKAGE__->new($x->{a}->sub($y), $x->{b}, $x->{c}, $x->{d}); | ||
| 207 | } | ||
| 208 | |||
| 209 | sub mul { | ||
| 210 | my ($x, $y) = @_; | ||
| 211 | |||
| 212 | if (ref($y) eq __PACKAGE__) { | ||
| 213 | return __PACKAGE__->new( | ||
| 214 | |||
| 215 | # Quaternion(a,b,c,d) * Quaternion(a',b',c',d') = Quaternion( | ||
| 216 | # a*a' - b*b' - c*c' - d*d', | ||
| 217 | # a*b' + b*a' + c*d' - d*c', | ||
| 218 | # a*c' - b*d' + c*a' + d*b', | ||
| 219 | # a*d' + b*c' - c*b' + d*a', | ||
| 220 | #) | ||
| 221 | |||
| 222 | $x->{a}->mul($y->{a})->sub($x->{b}->mul($y->{b}))->sub($x->{c}->mul($y->{c}))->sub($x->{d}->mul($y->{d})), | ||
| 223 | $x->{a}->mul($y->{b})->add($x->{b}->mul($y->{a}))->add($x->{c}->mul($y->{d}))->sub($x->{d}->mul($y->{c})), | ||
| 224 | $x->{a}->mul($y->{c})->sub($x->{b}->mul($y->{d}))->add($x->{c}->mul($y->{a}))->add($x->{d}->mul($y->{b})), | ||
| 225 | $x->{a}->mul($y->{d})->add($x->{b}->mul($y->{c}))->sub($x->{c}->mul($y->{b}))->add($x->{d}->mul($y->{a})), | ||
| 226 | ); | ||
| 227 | } | ||
| 228 | |||
| 229 | #<<< | ||
| 230 | __PACKAGE__->new( | ||
| 231 | $x->{a}->mul($y), | ||
| 232 | $x->{b}->mul($y), | ||
| 233 | $x->{c}->mul($y), | ||
| 234 | $x->{d}->mul($y), | ||
| 235 | ); | ||
| 236 | #>>> | ||
| 237 | } | ||
| 238 | |||
| 239 | sub div { | ||
| 240 | my ($x, $y) = @_; | ||
| 241 | $x->mul($y->inv); | ||
| 242 | } | ||
| 243 | |||
| 244 | sub float { | ||
| 245 | my ($x) = @_; | ||
| 246 | #<<< | ||
| 247 | __PACKAGE__->new( | ||
| 248 | $x->{a}->float, | ||
| 249 | $x->{b}->float, | ||
| 250 | $x->{c}->float, | ||
| 251 | $x->{d}->float, | ||
| 252 | ); | ||
| 253 | #>>> | ||
| 254 | } | ||
| 255 | |||
| 256 | sub floor { | ||
| 257 | my ($x) = @_; | ||
| 258 | #<<< | ||
| 259 | __PACKAGE__->new( | ||
| 260 | $x->{a}->floor, | ||
| 261 | $x->{b}->floor, | ||
| 262 | $x->{c}->floor, | ||
| 263 | $x->{d}->floor, | ||
| 264 | ); | ||
| 265 | #>>> | ||
| 266 | } | ||
| 267 | |||
| 268 | sub ceil { | ||
| 269 | my ($x) = @_; | ||
| 270 | #<<< | ||
| 271 | __PACKAGE__->new( | ||
| 272 | $x->{a}->ceil, | ||
| 273 | $x->{b}->ceil, | ||
| 274 | $x->{c}->ceil, | ||
| 275 | $x->{d}->ceil, | ||
| 276 | ); | ||
| 277 | #>>> | ||
| 278 | } | ||
| 279 | |||
| 280 | sub round { | ||
| 281 | my ($x, $r) = @_; | ||
| 282 | #<<< | ||
| 283 | __PACKAGE__->new( | ||
| 284 | $x->{a}->round($r), | ||
| 285 | $x->{b}->round($r), | ||
| 286 | $x->{c}->round($r), | ||
| 287 | $x->{d}->round($r), | ||
| 288 | ); | ||
| 289 | #>>> | ||
| 290 | } | ||
| 291 | |||
| 292 | sub mod { | ||
| 293 | my ($x, $y) = @_; | ||
| 294 | |||
| 295 | if (ref($y) eq 'Sidef::Types::Number::Number') { | ||
| 296 | return __PACKAGE__->new($x->{a}->mod($y), $x->{b}->mod($y), $x->{c}->mod($y), $x->{d}->mod($y)); | ||
| 297 | } | ||
| 298 | |||
| 299 | # mod(a, b) = a - b * floor(a/b) | ||
| 300 | $x->sub($y->mul($x->div($y)->floor)); | ||
| 301 | } | ||
| 302 | |||
| 303 | sub inv { | ||
| 304 | my ($x) = @_; | ||
| 305 | $x->conj->mul($x->{a}->sqr->add($x->{b}->sqr)->add($x->{c}->sqr)->add($x->{d}->sqr)->inv); | ||
| 306 | } | ||
| 307 | |||
| 308 | sub invmod { | ||
| 309 | my ($x, $m) = @_; | ||
| 310 | $x = $x->mod($m); | ||
| 311 | $x->conj->mul($x->{a}->sqr->add($x->{b}->sqr)->add($x->{c}->sqr)->add($x->{d}->sqr)->invmod($m))->mod($m); | ||
| 312 | } | ||
| 313 | |||
| 314 | sub is_zero { | ||
| 315 | my ($x) = @_; | ||
| 316 | my $bool = $x->{a}->is_zero; | ||
| 317 | $bool || return $bool; | ||
| 318 | $x->{b}->is_zero; | ||
| 319 | } | ||
| 320 | |||
| 321 | sub is_one { | ||
| 322 | my ($x) = @_; | ||
| 323 | my $bool = $x->{b}->is_zero; | ||
| 324 | $bool || return $bool; | ||
| 325 | $x->{a}->is_one; | ||
| 326 | } | ||
| 327 | |||
| 328 | sub is_mone { | ||
| 329 | my ($x) = @_; | ||
| 330 | my $bool = $x->{b}->is_zero; | ||
| 331 | $bool || return $bool; | ||
| 332 | $x->{a}->is_mone; | ||
| 333 | } | ||
| 334 | |||
| 335 | sub is_coprime { | ||
| 336 | my ($n, $k) = @_; | ||
| 337 | $n->norm->gcd($k->norm)->is_one; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub inc { | ||
| 341 | my ($x) = @_; | ||
| 342 | __PACKAGE__->new($x->{a}->inc, $x->{b}, $x->{c}, $x->{d}); | ||
| 343 | } | ||
| 344 | |||
| 345 | sub dec { | ||
| 346 | my ($x) = @_; | ||
| 347 | __PACKAGE__->new($x->{a}->dec, $x->{b}, $x->{c}, $x->{d}); | ||
| 348 | } | ||
| 349 | |||
| 350 | sub pow { | ||
| 351 | my ($x, $n) = @_; | ||
| 352 | |||
| 353 | $n->is_int || return $x->to_n->pow($n); | ||
| 354 | |||
| 355 | my $negative_power = 0; | ||
| 356 | |||
| 357 | if ($n->is_neg) { | ||
| 358 | $n = $n->abs; | ||
| 359 | $negative_power = 1; | ||
| 360 | } | ||
| 361 | |||
| 362 | my $c = __PACKAGE__->new(Sidef::Types::Number::Number::ONE); | ||
| 363 | |||
| 364 | foreach my $bit (reverse split(//, $n->as_bin)) { | ||
| 365 | |||
| 366 | # c *= x if bit | ||
| 367 | # x *= x | ||
| 368 | |||
| 369 | $c = $c->mul($x) if $bit; | ||
| 370 | $x = $x->mul($x); | ||
| 371 | } | ||
| 372 | |||
| 373 | if ($negative_power) { | ||
| 374 | $c = $c->inv; | ||
| 375 | } | ||
| 376 | |||
| 377 | return $c; | ||
| 378 | } | ||
| 379 | |||
| 380 | sub powmod { | ||
| 381 | my ($x, $n, $m) = @_; | ||
| 382 | |||
| 383 | $x = $x->mod($m); | ||
| 384 | |||
| 385 | my $negative_power = 0; | ||
| 386 | |||
| 387 | if ($n->is_neg) { | ||
| 388 | $n = $n->abs; | ||
| 389 | $negative_power = 1; | ||
| 390 | } | ||
| 391 | |||
| 392 | my $c = __PACKAGE__->new(Sidef::Types::Number::Number::ONE); | ||
| 393 | |||
| 394 | foreach my $bit (reverse split(//, $n->as_bin)) { | ||
| 395 | |||
| 396 | # c = (c*x)%m if bit | ||
| 397 | # x = (x*x)%m | ||
| 398 | |||
| 399 | $c = $c->mul($x)->mod($m) if $bit; | ||
| 400 | $x = $x->mul($x)->mod($m); | ||
| 401 | } | ||
| 402 | |||
| 403 | if ($negative_power) { | ||
| 404 | $c = $c->invmod($m); | ||
| 405 | } | ||
| 406 | |||
| 407 | return $c; | ||
| 408 | } | ||
| 409 | |||
| 410 | sub cmp { | ||
| 411 | my ($x, $y) = @_; | ||
| 412 | |||
| 413 | if (ref($y) eq __PACKAGE__) { | ||
| 414 | my $cmp = $x->{a}->cmp($y->{a}) // return undef; | ||
| 415 | $cmp && return $cmp; | ||
| 416 | $cmp = $x->{b}->cmp($y->{b}) // return undef; | ||
| 417 | $cmp && return $cmp; | ||
| 418 | $cmp = $x->{c}->cmp($y->{c}) // return undef; | ||
| 419 | $cmp && return $cmp; | ||
| 420 | return $x->{d}->cmp($y->{d}); | ||
| 421 | } | ||
| 422 | |||
| 423 | my $cmp = $x->{a}->cmp($y) // return undef; | ||
| 424 | $cmp && return $cmp; | ||
| 425 | $x->{b}->cmp(Sidef::Types::Number::Number::ZERO); | ||
| 426 | } | ||
| 427 | |||
| 428 | sub eq { | ||
| 429 | my ($x, $y) = @_; | ||
| 430 | |||
| 431 | if (ref($y) eq __PACKAGE__) { | ||
| 432 | my $bool = $x->{a}->eq($y->{a}) // return undef; | ||
| 433 | $bool || return $bool; | ||
| 434 | $bool = $x->{b}->eq($y->{b}) // return undef; | ||
| 435 | $bool || return $bool; | ||
| 436 | $bool = $x->{c}->eq($y->{c}) // return undef; | ||
| 437 | $bool || return $bool; | ||
| 438 | return $x->{d}->eq($y->{d}); | ||
| 439 | } | ||
| 440 | |||
| 441 | my $bool = $x->{a}->eq($y) // return undef; | ||
| 442 | $bool || return $bool; | ||
| 443 | $x->{b}->is_zero; | ||
| 444 | } | ||
| 445 | |||
| 446 | sub ne { | ||
| 447 | my ($x, $y) = @_; | ||
| 448 | |||
| 449 | if (ref($y) eq __PACKAGE__) { | ||
| 450 | my $bool = $x->{a}->ne($y->{a}); | ||
| 451 | $bool && return $bool; | ||
| 452 | $bool = $x->{b}->ne($y->{b}); | ||
| 453 | $bool && return $bool; | ||
| 454 | $bool = $x->{c}->ne($y->{c}); | ||
| 455 | $bool && return $bool; | ||
| 456 | return ($x->{d}->ne($y->{d})); | ||
| 457 | } | ||
| 458 | |||
| 459 | my $bool = $x->{a}->ne($y); | ||
| 460 | $bool && return $bool; | ||
| 461 | $x->{b}->is_zero->not; | ||
| 462 | } | ||
| 463 | |||
| 464 | sub shift_left { # x * 2^n | ||
| 465 | my ($x, $n) = @_; | ||
| 466 | $x->mul(Sidef::Types::Number::Number::TWO->pow($n)); | ||
| 467 | } | ||
| 468 | |||
| 469 | *lsft = \&shift_left; | ||
| 470 | |||
| 471 | sub shift_right { # x / 2^n | ||
| 472 | my ($x, $n) = @_; | ||
| 473 | $x->div(Sidef::Types::Number::Number::TWO->pow($n)); | ||
| 474 | } | ||
| 475 | |||
| 476 | *rsft = \&shift_right; | ||
| 477 | |||
| 478 | { | ||
| 479 | no strict 'refs'; | ||
| 480 | |||
| 481 | foreach my $method (qw(ge gt lt le)) { | ||
| 482 | *{__PACKAGE__ . '::' . $method} = sub { | ||
| 483 | my ($x, $y) = @_; | ||
| 484 | ($x->cmp($y) // return undef)->$method(Sidef::Types::Number::Number::ZERO); | ||
| 485 | }; | ||
| 486 | } | ||
| 487 | |||
| 488 | foreach my $method (qw(and xor or)) { | ||
| 489 | *{__PACKAGE__ . '::' . $method} = sub { | ||
| 490 | my ($x, $y) = @_; | ||
| 491 | |||
| 492 | #<<< | ||
| 493 | if (ref($y) eq __PACKAGE__) { | ||
| 494 | return __PACKAGE__->new( | ||
| 495 | $x->{a}->$method($y->{a}), | ||
| 496 | $x->{b}->$method($y->{b}), | ||
| 497 | $x->{c}->$method($y->{c}), | ||
| 498 | $x->{d}->$method($y->{d}), | ||
| 499 | ); | ||
| 500 | } | ||
| 501 | #>>> | ||
| 502 | |||
| 503 | __PACKAGE__->new($x->{a}->$method($y), $x->{b}, $x->{c}, $x->{d}); | ||
| 504 | }; | ||
| 505 | } | ||
| 506 | |||
| 507 | *{__PACKAGE__ . '::' . '/'} = \÷ | ||
| 508 | *{__PACKAGE__ . '::' . '÷'} = \÷ | ||
| 509 | *{__PACKAGE__ . '::' . '*'} = \&mul; | ||
| 510 | *{__PACKAGE__ . '::' . '%'} = \&mod; | ||
| 511 | *{__PACKAGE__ . '::' . '+'} = \&add; | ||
| 512 | *{__PACKAGE__ . '::' . '-'} = \⊂ | ||
| 513 | *{__PACKAGE__ . '::' . '**'} = \&pow; | ||
| 514 | *{__PACKAGE__ . '::' . '++'} = \&inc; | ||
| 515 | *{__PACKAGE__ . '::' . '--'} = \&dec; | ||
| 516 | *{__PACKAGE__ . '::' . '<'} = \< | ||
| 517 | *{__PACKAGE__ . '::' . '>'} = \> | ||
| 518 | *{__PACKAGE__ . '::' . '&'} = \∧ | ||
| 519 | *{__PACKAGE__ . '::' . '|'} = \∨ | ||
| 520 | *{__PACKAGE__ . '::' . '^'} = \&xor; | ||
| 521 | *{__PACKAGE__ . '::' . '<<'} = \&lsft; | ||
| 522 | *{__PACKAGE__ . '::' . '>>'} = \&rsft; | ||
| 523 | *{__PACKAGE__ . '::' . '<=>'} = \&cmp; | ||
| 524 | *{__PACKAGE__ . '::' . '<='} = \≤ | ||
| 525 | *{__PACKAGE__ . '::' . '≤'} = \≤ | ||
| 526 | *{__PACKAGE__ . '::' . '>='} = \≥ | ||
| 527 | *{__PACKAGE__ . '::' . '≥'} = \≥ | ||
| 528 | *{__PACKAGE__ . '::' . '=='} = \&eq; | ||
| 529 | *{__PACKAGE__ . '::' . '!='} = \≠ | ||
| 530 | } | ||
| 531 | |||
| 532 | 1 |