1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
// Copyright © 2024 Mikhail Hogrefe
//
// Uses code adopted from the GNU MP Library.
//
// Copyright © 1991-2018 Free Software Foundation, Inc.
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
use crate::integer::Integer;
use crate::natural::arithmetic::add::{limbs_add, limbs_add_limb};
use crate::natural::arithmetic::divisible_by::{
limbs_divisible_by, limbs_divisible_by_limb, limbs_divisible_by_val_ref,
};
use crate::natural::arithmetic::eq_mod::{limbs_eq_limb_mod_limb, limbs_mod_exact_odd_limb};
use crate::natural::arithmetic::mod_op::limbs_mod_limb;
use crate::natural::InnerNatural::{Large, Small};
use crate::natural::Natural;
use crate::platform::{Limb, BMOD_1_TO_MOD_1_THRESHOLD};
use malachite_base::fail_on_untested_path;
use malachite_base::num::arithmetic::traits::{
DivisibleBy, EqMod, EqModPowerOf2, NegMod, PowerOf2,
};
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::basic::traits::Zero;
use malachite_base::num::logic::traits::TrailingZeros;
// Interpreting a slice of `Limb`s as the limbs of a `Natural` in ascending order, determines
// whether that `Natural` is equal to the negative of a limb mod a given `Limb` m.
//
// This function assumes that `m` is nonzero, `limbs` has at least two elements, and the last
// element of `limbs` is nonzero.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(1)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// # Panics
// Panics if the length of `limbs` is less than 2.
//
// This is equivalent to `mpz_congruent_ui_p` from `mpz/cong_ui.c`, GMP 6.2.1, where `a` is
// negative.
pub_test! {limbs_eq_neg_limb_mod_limb(xs: &[Limb], y: Limb, m: Limb) -> bool {
limbs_eq_limb_mod_limb(xs, y.neg_mod(m), m)
}}
/// Set r to -n mod d. n >= d is allowed. Can give r > d. d cannot equal 0.
///
/// This is equivalent to `NEG_MOD` from `gmp-impl.h`, GMP 6.2.1, where `r` is returned.
const fn quick_neg_mod(n: Limb, d: Limb) -> Limb {
if n <= d {
d - n
} else {
let d = d << d.leading_zeros();
(if n <= d { d } else { d << 1 }).wrapping_sub(n)
}
}
// Interpreting two limbs `x` and `y` and slice of `Limb`s `m` as three numbers x, y, and m,
// determines whether x ≡ -y mod m.
//
// This function assumes that the input slice has at least two elements, its last element is
// nonzero, and `x` and `y` are nonzero.
//
// # Worst-case complexity
// Constant time and additional memory.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a` and `d` are
// positive, `c` is negative, `a` and `d` are one limb long, and `c` is longer than one limb.
pub_const_test! {limbs_pos_limb_eq_neg_limb_mod(x: Limb, y: Limb, ms: &[Limb]) -> bool {
// We are checking whether x ≡ -y mod m; that is, whether x + y = k * m for some k in Z. But
// because of the preconditions on m, the lowest possible value of m is `2 ^ Limb::WIDTH`, while
// the highest possible value of x + y is `2 ^ (Limb::WIDTH + 1) - 2`, so we have x + y < 2 * m.
// This means that k can only be 1, so we're actually checking whether x + y = m.
ms.len() == 2 && ms[1] == 1 && {
let (sum, overflow) = x.overflowing_add(y);
overflow && sum == ms[0]
}
}}
#[allow(clippy::absurd_extreme_comparisons)]
fn limbs_pos_eq_neg_limb_mod_helper(xs: &[Limb], y: Limb, ms: &[Limb]) -> Option<bool> {
let m_len = ms.len();
let x_len = xs.len();
assert!(m_len > 1);
assert!(x_len > 1);
assert_ne!(y, 0);
assert_ne!(*xs.last().unwrap(), 0);
assert_ne!(*ms.last().unwrap(), 0);
let m_0 = ms[0];
// Check x == y mod low zero bits of m_0. This might catch a few cases of x != y quickly.
let twos = TrailingZeros::trailing_zeros(m_0);
if !xs[0].wrapping_neg().eq_mod_power_of_2(y, twos) {
return Some(false);
}
// m_0 == 0 is avoided since we don't want to bother handling extra low zero bits if m_1 is even
// (would involve borrow if x_0, y_0 != 0).
if m_len == 2 && m_0 != 0 {
let m_1 = ms[1];
if m_1 < Limb::power_of_2(twos) {
let m_0 = (m_0 >> twos) | (m_1 << (Limb::WIDTH - twos));
let y = quick_neg_mod(y, m_0);
return Some(if x_len >= BMOD_1_TO_MOD_1_THRESHOLD {
limbs_mod_limb(xs, m_0)
== if y < m_0 {
y
} else {
fail_on_untested_path("limbs_pos_eq_neg_limb_mod_helper, y >= m_0");
y % m_0
}
} else {
let r = limbs_mod_exact_odd_limb(xs, m_0, y);
r == 0 || r == m_0
});
}
}
None
}
// Interpreting a slice of `Limb`s `xs`, a Limb `y`, and another slice of `Limb`s `m` as three
// numbers x, y, and m, determines whether x ≡ -y mod m. The second input slice is immutable.
//
// This function assumes that each of the two input slices have at least two elements, their last
// elements are nonzero, and `y` is nonzero.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log \log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// # Panics
// Panics if the length of `xs` or `ms` is less than 2, if the last element of either of the slices
// is zero, or if `y` is zero.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a` and `d` are
// positive, `c` is negative, `a` and `d` are longer than one limb, and `c` is one limb long.
pub_test! {limbs_pos_eq_neg_limb_mod_ref(xs: &[Limb], y: Limb, ms: &[Limb]) -> bool {
if let Some(equal) = limbs_pos_eq_neg_limb_mod_helper(xs, y, ms) {
return equal;
}
// calculate |x - y|. Different signs, add
let mut scratch = limbs_add_limb(xs, y);
scratch.len() >= ms.len() && limbs_divisible_by_val_ref(&mut scratch, ms)
}}
// Interpreting a slice of `Limb`s `xs`, a Limb `y`, and another slice of `Limb`s `ms` as three
// numbers x, y, and m, determines whether x ≡ -y mod m. The second input slice is mutable.
//
// This function assumes that each of the two input slices have at least two elements, their last
// elements are nonzero, and `y` is nonzero.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log \log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// # Panics
// Panics if the length of `xs` or `ms` is less than 2, if the last element of either of the slices
// is zero, or if `y` is zero.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a` and `d` are
// positive, `c` is negative, `a` and `d` are longer than one limb, and `c` is one limb long.
pub_test! {limbs_pos_eq_neg_limb_mod(xs: &[Limb], y: Limb, ms: &mut [Limb]) -> bool {
if let Some(equal) = limbs_pos_eq_neg_limb_mod_helper(xs, y, ms) {
return equal;
}
// calculate |x - y|. Different signs, add
let mut scratch = limbs_add_limb(xs, y);
scratch.len() >= ms.len() && limbs_divisible_by(&mut scratch, ms)
}}
// Interpreting two slices of `Limb`s `xs` and `ys` and a Limb `m` as three numbers x, y, and m,
// determines whether x ≡ -y mod m.
//
// This function assumes that each of the two input slices have at least two elements, their last
// elements are nonzero, and `m` is nonzero.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `max(xs.len(), ys.len())`.
//
// # Panics
// Panics if the length of `xs` or `ys` is less than 2, if the last element of either of the slices
// is zero, or if `m` is zero.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a` and `d` are
// positive, `c` is negative, `a` and `c` are longer than one limb, and `m` is one limb long.
pub_test! {limbs_pos_eq_neg_mod_limb(xs: &[Limb], ys: &[Limb], m: Limb) -> bool {
if xs.len() >= ys.len() {
limbs_pos_eq_mod_neg_limb_greater(xs, ys, m)
} else {
limbs_pos_eq_mod_neg_limb_greater(ys, xs, m)
}
}}
// xs.len() >= ys.len()
fn limbs_pos_eq_mod_neg_limb_greater(xs: &[Limb], ys: &[Limb], m: Limb) -> bool {
assert!(xs.len() > 1);
assert!(ys.len() > 1);
assert_ne!(*xs.last().unwrap(), 0);
assert_ne!(*ys.last().unwrap(), 0);
assert_ne!(m, 0);
// Check x == y mod low zero bits of m_0. This might catch a few cases of x != y quickly.
if !xs[0]
.wrapping_neg()
.eq_mod_power_of_2(ys[0], TrailingZeros::trailing_zeros(m))
{
return false;
}
// calculate |x - y|. Different signs, add
limbs_divisible_by_limb(&limbs_add(xs, ys), m)
}
fn limbs_pos_eq_neg_mod_greater_helper(xs: &[Limb], ys: &[Limb], ms: &[Limb]) -> Option<bool> {
assert!(ms.len() > 1);
assert!(xs.len() > 1);
assert!(ys.len() > 1);
assert_ne!(*xs.last().unwrap(), 0);
assert_ne!(*ys.last().unwrap(), 0);
assert_ne!(*ms.last().unwrap(), 0);
// Check x == y mod low zero bits of m_0. This might catch a few cases of x != y quickly.
if !xs[0]
.wrapping_neg()
.eq_mod_power_of_2(ys[0], TrailingZeros::trailing_zeros(ms[0]))
{
Some(false)
} else {
None
}
}
// Interpreting three slice of `Limb`s as the limbs of three `Natural`s, determines whether the
// first `Natural` is equal to the negative of the second `Natural` mod the third `Natural`. The
// second input slice is immutable.
//
// This function assumes that each of the three input slices have at least two elements, and their
// last elements are nonzero.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log \log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `max(xs.len(), ys.len())`.
//
// # Panics
// Panics if the length of `xs`, `ys`, or `ms` is less than 2, or if the last element of any of the
// slices is zero.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a` and `d` are
// positive, `c` is negative, and each is longer than one limb.
pub_test! {limbs_pos_eq_neg_mod_ref(xs: &[Limb], ys: &[Limb], ms: &[Limb]) -> bool {
if xs.len() >= ys.len() {
limbs_pos_eq_neg_mod_greater_ref(xs, ys, ms)
} else {
limbs_pos_eq_neg_mod_greater_ref(ys, xs, ms)
}
}}
// xs.len() >= ys.len()
fn limbs_pos_eq_neg_mod_greater_ref(xs: &[Limb], ys: &[Limb], ms: &[Limb]) -> bool {
if let Some(equal) = limbs_pos_eq_neg_mod_greater_helper(xs, ys, ms) {
return equal;
}
// calculate |x - y|. Different signs, add
let mut scratch = limbs_add(xs, ys);
scratch.len() >= ms.len() && limbs_divisible_by_val_ref(&mut scratch, ms)
}
// Interpreting three slice of `Limb`s as the limbs of three `Natural`s, determines whether the
// first `Natural` is equal to the negative of the second `Natural` mod the third `Natural`. The
// second input slice is mutable.
//
// This function assumes that each of the three input slices have at least two elements, and their
// last elements are nonzero.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log \log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `max(xs.len(), ys.len())`.
//
// # Panics
// Panics if the length of `xs`, `ys`, or `ms` is less than 2, or if the last element of any of the
// slices is zero.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a` and `d` are
// positive, `c` is negative, and each is longer than one limb.
pub_test! {limbs_pos_eq_neg_mod(xs: &[Limb], ys: &[Limb], ms: &mut [Limb]) -> bool {
if xs.len() >= ys.len() {
limbs_pos_eq_neg_mod_greater(xs, ys, ms)
} else {
limbs_pos_eq_neg_mod_greater(ys, xs, ms)
}
}}
// xs.len() >= ys.len()
fn limbs_pos_eq_neg_mod_greater(xs: &[Limb], ys: &[Limb], ms: &mut [Limb]) -> bool {
if let Some(equal) = limbs_pos_eq_neg_mod_greater_helper(xs, ys, ms) {
return equal;
}
// calculate |x - y|. Different signs, add
let mut scratch = limbs_add(xs, ys);
scratch.len() >= ms.len() && limbs_divisible_by(&mut scratch, ms)
}
impl Natural {
fn eq_neg_limb_mod_limb(&self, other: Limb, m: Limb) -> bool {
m != 0
&& match *self {
Natural(Small(small)) => small % m == other.neg_mod(m),
Natural(Large(ref limbs)) => limbs_eq_neg_limb_mod_limb(limbs, other, m),
}
}
fn pos_eq_neg_mod(&self, other: &Natural, m: Natural) -> bool {
match (self, other, m) {
(_, _, Natural::ZERO) => false,
(x, &Natural::ZERO, m) => x.divisible_by(m),
(&Natural::ZERO, y, m) => y.divisible_by(m),
(x, &Natural(Small(y)), Natural(Small(m))) => x.eq_neg_limb_mod_limb(y, m),
(&Natural(Small(x)), y, Natural(Small(m))) => y.eq_neg_limb_mod_limb(x, m),
(&Natural(Small(x)), &Natural(Small(y)), Natural(Large(ref m))) => {
limbs_pos_limb_eq_neg_limb_mod(x, y, m)
}
(&Natural(Large(ref xs)), &Natural(Large(ref ys)), Natural(Small(m))) => {
limbs_pos_eq_neg_mod_limb(xs, ys, m)
}
(&Natural(Large(ref xs)), &Natural(Small(y)), Natural(Large(ref mut m))) => {
limbs_pos_eq_neg_limb_mod(xs, y, m)
}
(&Natural(Small(x)), &Natural(Large(ref ys)), Natural(Large(ref mut m))) => {
limbs_pos_eq_neg_limb_mod(ys, x, m)
}
(&Natural(Large(ref xs)), &Natural(Large(ref ys)), Natural(Large(ref mut m))) => {
limbs_pos_eq_neg_mod(xs, ys, m)
}
}
}
fn pos_eq_neg_mod_ref(&self, other: &Natural, m: &Natural) -> bool {
match (self, other, m) {
(_, _, &Natural::ZERO) => false,
(x, &Natural::ZERO, m) => x.divisible_by(m),
(&Natural::ZERO, y, m) => y.divisible_by(m),
(x, &Natural(Small(y)), &Natural(Small(m))) => x.eq_neg_limb_mod_limb(y, m),
(&Natural(Small(x)), y, &Natural(Small(m))) => y.eq_neg_limb_mod_limb(x, m),
(&Natural(Small(x)), &Natural(Small(y)), &Natural(Large(ref m))) => {
limbs_pos_limb_eq_neg_limb_mod(x, y, m)
}
(&Natural(Large(ref xs)), &Natural(Large(ref ys)), &Natural(Small(m))) => {
limbs_pos_eq_neg_mod_limb(xs, ys, m)
}
(&Natural(Large(ref xs)), &Natural(Small(y)), &Natural(Large(ref m))) => {
limbs_pos_eq_neg_limb_mod_ref(xs, y, m)
}
(&Natural(Small(x)), &Natural(Large(ref ys)), &Natural(Large(ref m))) => {
limbs_pos_eq_neg_limb_mod_ref(ys, x, m)
}
(&Natural(Large(ref xs)), &Natural(Large(ref ys)), &Natural(Large(ref m))) => {
limbs_pos_eq_neg_mod_ref(xs, ys, m)
}
}
}
}
impl EqMod<Integer, Natural> for Integer {
/// Returns whether an [`Integer`] is equivalent to another [`Integer`] modulo a [`Natural`];
/// that is, whether the difference between the two [`Integer`]s is a multiple of the
/// [`Natural`]. All three numbers are taken by value.
///
/// Two [`Integer`]s are equal to each other modulo 0 iff they are equal.
///
/// $f(x, y, m) = (x \equiv y \mod m)$.
///
/// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
/// other.significant_bits())`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::EqMod;
/// use malachite_nz::integer::Integer;
/// use malachite_nz::natural::Natural;
/// use core::str::FromStr;
///
/// assert_eq!(
/// Integer::from(123).eq_mod(Integer::from(223), Natural::from(100u32)),
/// true
/// );
/// assert_eq!(
/// Integer::from_str("1000000987654").unwrap().eq_mod(
/// Integer::from_str("-999999012346").unwrap(),
/// Natural::from_str("1000000000000").unwrap()
/// ),
/// true
/// );
/// assert_eq!(
/// Integer::from_str("1000000987654").unwrap().eq_mod(
/// Integer::from_str("2000000987655").unwrap(),
/// Natural::from_str("1000000000000").unwrap()
/// ),
/// false
/// );
/// ```
fn eq_mod(self, other: Integer, m: Natural) -> bool {
if self.sign == other.sign {
self.abs.eq_mod(other.abs, m)
} else {
self.abs.pos_eq_neg_mod(&other.abs, m)
}
}
}
impl<'a> EqMod<Integer, &'a Natural> for Integer {
/// Returns whether an [`Integer`] is equivalent to another [`Integer`] modulo a [`Natural`];
/// that is, whether the difference between the two [`Integer`]s is a multiple of the
/// [`Natural`]. The first two numbers are taken by value and the third by reference.
///
/// Two [`Integer`]s are equal to each other modulo 0 iff they are equal.
///
/// $f(x, y, m) = (x \equiv y \mod m)$.
///
/// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
/// other.significant_bits())`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::EqMod;
/// use malachite_nz::integer::Integer;
/// use malachite_nz::natural::Natural;
/// use core::str::FromStr;
///
/// assert_eq!(
/// Integer::from(123).eq_mod(Integer::from(223), &Natural::from(100u32)),
/// true
/// );
/// assert_eq!(
/// Integer::from_str("1000000987654").unwrap().eq_mod(
/// Integer::from_str("-999999012346").unwrap(),
/// &Natural::from_str("1000000000000").unwrap()
/// ),
/// true
/// );
/// assert_eq!(
/// Integer::from_str("1000000987654").unwrap().eq_mod(
/// Integer::from_str("2000000987655").unwrap(),
/// &Natural::from_str("1000000000000").unwrap()
/// ),
/// false
/// );
/// ```
fn eq_mod(self, other: Integer, m: &'a Natural) -> bool {
if self.sign == other.sign {
self.abs.eq_mod(other.abs, m)
} else {
self.abs.pos_eq_neg_mod_ref(&other.abs, m)
}
}
}
impl<'a> EqMod<&'a Integer, Natural> for Integer {
/// Returns whether an [`Integer`] is equivalent to another [`Integer`] modulo a [`Natural`];
/// that is, whether the difference between the two [`Integer`]s is a multiple of the
/// [`Natural`]. The first and third numbers are taken by value and the second by reference.
///
/// Two [`Integer`]s are equal to each other modulo 0 iff they are equal.
///
/// $f(x, y, m) = (x \equiv y \mod m)$.
///
/// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
/// other.significant_bits())`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::EqMod;
/// use malachite_nz::integer::Integer;
/// use malachite_nz::natural::Natural;
/// use core::str::FromStr;
///
/// assert_eq!(
/// Integer::from(123).eq_mod(&Integer::from(223), Natural::from(100u32)),
/// true
/// );
/// assert_eq!(
/// Integer::from_str("1000000987654").unwrap().eq_mod(
/// &Integer::from_str("-999999012346").unwrap(),
/// Natural::from_str("1000000000000").unwrap()
/// ),
/// true
/// );
/// assert_eq!(
/// Integer::from_str("1000000987654").unwrap().eq_mod(
/// &Integer::from_str("2000000987655").unwrap(),
/// Natural::from_str("1000000000000").unwrap()
/// ),
/// false
/// );
/// ```
fn eq_mod(self, other: &'a Integer, m: Natural) -> bool {
if self.sign == other.sign {
self.abs.eq_mod(&other.abs, m)
} else {
self.abs.pos_eq_neg_mod(&other.abs, m)
}
}
}
impl<'a, 'b> EqMod<&'a Integer, &'b Natural> for Integer {
/// Returns whether an [`Integer`] is equivalent to another [`Integer`] modulo a [`Natural`];
/// that is, whether the difference between the two [`Integer`]s is a multiple of the
/// [`Natural`]. The first number is taken by value and the second and third by reference.
///
/// Two [`Integer`]s are equal to each other modulo 0 iff they are equal.
///
/// $f(x, y, m) = (x \equiv y \mod m)$.
///
/// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
/// other.significant_bits())`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::EqMod;
/// use malachite_nz::integer::Integer;
/// use malachite_nz::natural::Natural;
/// use core::str::FromStr;
///
/// assert_eq!(
/// Integer::from(123).eq_mod(&Integer::from(223), &Natural::from(100u32)),
/// true
/// );
/// assert_eq!(
/// Integer::from_str("1000000987654").unwrap().eq_mod(
/// &Integer::from_str("-999999012346").unwrap(),
/// &Natural::from_str("1000000000000").unwrap()
/// ),
/// true
/// );
/// assert_eq!(
/// Integer::from_str("1000000987654").unwrap().eq_mod(
/// &Integer::from_str("2000000987655").unwrap(),
/// &Natural::from_str("1000000000000").unwrap()
/// ),
/// false
/// );
/// ```
fn eq_mod(self, other: &'a Integer, m: &'b Natural) -> bool {
if self.sign == other.sign {
self.abs.eq_mod(&other.abs, m)
} else {
self.abs.pos_eq_neg_mod_ref(&other.abs, m)
}
}
}
impl<'a> EqMod<Integer, Natural> for &'a Integer {
/// Returns whether an [`Integer`] is equivalent to another [`Integer`] modulo a [`Natural`];
/// that is, whether the difference between the two [`Integer`]s is a multiple of the
/// [`Natural`]. The first number is taken by reference and the second and third by value.
///
/// Two [`Integer`]s are equal to each other modulo 0 iff they are equal.
///
/// $f(x, y, m) = (x \equiv y \mod m)$.
///
/// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
/// other.significant_bits())`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::EqMod;
/// use malachite_nz::integer::Integer;
/// use malachite_nz::natural::Natural;
/// use core::str::FromStr;
///
/// assert_eq!(
/// (&Integer::from(123)).eq_mod(Integer::from(223), Natural::from(100u32)),
/// true
/// );
/// assert_eq!(
/// (&Integer::from_str("1000000987654").unwrap()).eq_mod(
/// Integer::from_str("-999999012346").unwrap(),
/// Natural::from_str("1000000000000").unwrap()
/// ),
/// true
/// );
/// assert_eq!(
/// (&Integer::from_str("1000000987654").unwrap()).eq_mod(
/// Integer::from_str("2000000987655").unwrap(),
/// Natural::from_str("1000000000000").unwrap()
/// ),
/// false
/// );
/// ```
fn eq_mod(self, other: Integer, m: Natural) -> bool {
if self.sign == other.sign {
(&self.abs).eq_mod(other.abs, m)
} else {
self.abs.pos_eq_neg_mod(&other.abs, m)
}
}
}
impl<'a, 'b> EqMod<Integer, &'b Natural> for &'a Integer {
/// Returns whether an [`Integer`] is equivalent to another [`Integer`] modulo a [`Natural`];
/// that is, whether the difference between the two [`Integer`]s is a multiple of the
/// [`Natural`]. The first and third numbers are taken by reference and the third by value.
///
/// Two [`Integer`]s are equal to each other modulo 0 iff they are equal.
///
/// $f(x, y, m) = (x \equiv y \mod m)$.
///
/// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
/// other.significant_bits())`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::EqMod;
/// use malachite_nz::integer::Integer;
/// use malachite_nz::natural::Natural;
/// use core::str::FromStr;
///
/// assert_eq!(
/// (&Integer::from(123)).eq_mod(Integer::from(223), &Natural::from(100u32)),
/// true
/// );
/// assert_eq!(
/// (&Integer::from_str("1000000987654").unwrap()).eq_mod(
/// Integer::from_str("-999999012346").unwrap(),
/// &Natural::from_str("1000000000000").unwrap()
/// ),
/// true
/// );
/// assert_eq!(
/// (&Integer::from_str("1000000987654").unwrap()).eq_mod(
/// Integer::from_str("2000000987655").unwrap(),
/// &Natural::from_str("1000000000000").unwrap()
/// ),
/// false
/// );
/// ```
fn eq_mod(self, other: Integer, m: &'b Natural) -> bool {
if self.sign == other.sign {
(&self.abs).eq_mod(other.abs, m)
} else {
self.abs.pos_eq_neg_mod_ref(&other.abs, m)
}
}
}
impl<'a, 'b> EqMod<&'b Integer, Natural> for &'a Integer {
/// Returns whether an [`Integer`] is equivalent to another [`Integer`] modulo a [`Natural`];
/// that is, whether the difference between the two [`Integer`]s is a multiple of the
/// [`Natural`]. The first two numbers are taken by reference and the third by value.
///
/// Two [`Integer`]s are equal to each other modulo 0 iff they are equal.
///
/// $f(x, y, m) = (x \equiv y \mod m)$.
///
/// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
/// other.significant_bits())`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::EqMod;
/// use malachite_nz::integer::Integer;
/// use malachite_nz::natural::Natural;
/// use core::str::FromStr;
///
/// assert_eq!(
/// (&Integer::from(123)).eq_mod(&Integer::from(223), Natural::from(100u32)),
/// true
/// );
/// assert_eq!(
/// (&Integer::from_str("1000000987654").unwrap()).eq_mod(
/// &Integer::from_str("-999999012346").unwrap(),
/// Natural::from_str("1000000000000").unwrap()
/// ),
/// true
/// );
/// assert_eq!(
/// (&Integer::from_str("1000000987654").unwrap()).eq_mod(
/// &Integer::from_str("2000000987655").unwrap(),
/// Natural::from_str("1000000000000").unwrap()
/// ),
/// false
/// );
/// ```
fn eq_mod(self, other: &'b Integer, m: Natural) -> bool {
if self.sign == other.sign {
(&self.abs).eq_mod(&other.abs, m)
} else {
self.abs.pos_eq_neg_mod(&other.abs, m)
}
}
}
impl<'a, 'b, 'c> EqMod<&'b Integer, &'c Natural> for &'a Integer {
/// Returns whether an [`Integer`] is equivalent to another [`Integer`] modulo a [`Natural`];
/// that is, whether the difference between the two [`Integer`]s is a multiple of the
/// [`Natural`]. All three numbers are taken by reference.
///
/// Two [`Integer`]s are equal to each other modulo 0 iff they are equal.
///
/// $f(x, y, m) = (x \equiv y \mod m)$.
///
/// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
/// other.significant_bits())`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::EqMod;
/// use malachite_nz::integer::Integer;
/// use malachite_nz::natural::Natural;
/// use core::str::FromStr;
///
/// assert_eq!(
/// (&Integer::from(123)).eq_mod(&Integer::from(223), &Natural::from(100u32)),
/// true
/// );
/// assert_eq!(
/// (&Integer::from_str("1000000987654").unwrap()).eq_mod(
/// &Integer::from_str("-999999012346").unwrap(),
/// &Natural::from_str("1000000000000").unwrap()
/// ),
/// true
/// );
/// assert_eq!(
/// (&Integer::from_str("1000000987654").unwrap()).eq_mod(
/// &Integer::from_str("2000000987655").unwrap(),
/// &Natural::from_str("1000000000000").unwrap()
/// ),
/// false
/// );
/// ```
fn eq_mod(self, other: &'b Integer, m: &'c Natural) -> bool {
if self.sign == other.sign {
(&self.abs).eq_mod(&other.abs, m)
} else {
self.abs.pos_eq_neg_mod_ref(&other.abs, m)
}
}
}