Skip to main content

ComparableGaussianIntegerRef

Struct ComparableGaussianIntegerRef 

Source
pub struct ComparableGaussianIntegerRef<'a>(pub &'a GaussianInteger);
Expand description

ComparableGaussianIntegerRef is a wrapper around a GaussianInteger, taking the GaussianInteger by reference.

See the ComparableGaussianInteger documentation for details.

Tuple Fields§

§0: &'a GaussianInteger

Methods from Deref<Target = GaussianInteger>§

Source

pub fn checked_roots(&self, exp: u64) -> Vec<Self>

Returns all the $n$th roots of a GaussianInteger: none if it is not a perfect $n$th power, one if it is zero, and otherwise $\gcd(n, 4)$ of them, in the canonical order of ComparableGaussianInteger, lexicographic by real part and then imaginary part.

The principal root is the one whose argument lies in $(-\pi/g, \pi/g]$ for $g = \gcd(n, 4)$; see CheckedRoot.

$$ f(z, n) = \{ w \in \Z[i] : w^n = z \}. $$

§Worst-case complexity

$T(n) = O(n^2)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant bits of the real and imaginary parts of self.

§Panics

Panics if exp is zero.

§Examples
use malachite_base::num::basic::traits::Zero;
use malachite_nz::gaussian_integer::GaussianInteger;
use std::str::FromStr;

let roots = |s, exp| {
    GaussianInteger::from_str(s)
        .unwrap()
        .checked_roots(exp)
        .iter()
        .map(ToString::to_string)
        .collect::<Vec<_>>()
};
assert_eq!(roots("-4", 4), ["-1-i", "-1+i", "1-i", "1+i"]);
assert_eq!(roots("-4", 2), ["-2i", "2i"]);
assert_eq!(roots("-8", 3), ["-2"]);
assert_eq!(roots("3+4i", 3), Vec::<String>::new());
assert_eq!(
    GaussianInteger::ZERO.checked_roots(7),
    [GaussianInteger::ZERO]
);
Source

pub fn checked_sqrts(&self) -> Vec<Self>

Returns all the square roots of a GaussianInteger: none if it is not a perfect square, one if it is zero, and otherwise the principal root and its negative, in the canonical order of ComparableGaussianInteger, lexicographic by real part and then imaginary part.

The principal root is the one with positive real part or, if that is zero, with non-negative imaginary part; see CheckedSqrt.

$$ f(z) = \{ w \in \Z[i] : w^2 = z \}. $$

§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 the maximum number of significant bits of the real and imaginary parts of self.

§Examples
use malachite_base::num::basic::traits::Zero;
use malachite_nz::gaussian_integer::GaussianInteger;
use std::str::FromStr;

let roots = |s| {
    GaussianInteger::from_str(s)
        .unwrap()
        .checked_sqrts()
        .iter()
        .map(ToString::to_string)
        .collect::<Vec<_>>()
};
assert_eq!(roots("3+4i"), ["-2-i", "2+i"]);
assert_eq!(roots("-1"), ["-i", "i"]);
assert_eq!(roots("2+i"), Vec::<String>::new());
assert_eq!(
    GaussianInteger::ZERO.checked_sqrts(),
    [GaussianInteger::ZERO]
);
Source

pub fn remove_one_plus_i(&self) -> (Self, u64)

Removes the largest power of $1 + i$ from a GaussianInteger, taking it by reference and returning the reduced GaussianInteger together with the exponent of that power.

$1 + i$ is the Gaussian prime above 2, with $(1 + i)^2 = 2i$. If $(1 + i)^k$ is the largest power of $1 + i$ that divides self, this returns $(\text{self} / (1 + i)^k, k)$. The exponent is twice the largest power of 2 dividing both parts, plus one more when the parts have the same 2-adic valuation, since then both are odd after the shift and their sum is even. Zero is left alone, with an exponent of 0, since every power of $1 + i$ divides it.

§Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

§Examples
use malachite_base::num::basic::traits::Two;
use malachite_nz::gaussian_integer::GaussianInteger;
use std::str::FromStr;

// 6+2i = (-1-2i)(1+i)^3
let (q, k) = GaussianInteger::from_str("6+2i")
    .unwrap()
    .remove_one_plus_i();
assert_eq!(q.to_string(), "-1-2i");
assert_eq!(k, 3);

// 2 = (-i)(1+i)^2
let (q, k) = GaussianInteger::TWO.remove_one_plus_i();
assert_eq!(q.to_string(), "-i");
assert_eq!(k, 2);

// 3+2i is not divisible by 1+i
let (q, k) = GaussianInteger::from_str("3+2i")
    .unwrap()
    .remove_one_plus_i();
assert_eq!(q.to_string(), "3+2i");
assert_eq!(k, 0);
Source

pub fn max_significant_bits(&self) -> u64

Returns the larger of the numbers of significant bits of the real and imaginary parts of a GaussianInteger, each taken in absolute value.

This is the size measure that FLINT’s fmpzi_bits computes, and the one that the sizes of the parts are compared against when an algorithm is chosen; the SignificantBits implementation sums the two counts instead.

$$ f(a + bi) = \max(\operatorname{bits}(a), \operatorname{bits}(b)), $$ where $\operatorname{bits}(n)$ is the number of significant bits of $|n|$, with $\operatorname{bits}(0) = 0$.

§Worst-case complexity

Constant time and additional memory.

§Examples
use malachite_base::num::basic::traits::Zero;
use malachite_nz::gaussian_integer::GaussianInteger;
use std::str::FromStr;

assert_eq!(GaussianInteger::ZERO.max_significant_bits(), 0);
assert_eq!(
    GaussianInteger::from_str("3+4i")
        .unwrap()
        .max_significant_bits(),
    3
);
assert_eq!(
    GaussianInteger::from_str("1000000000000+i")
        .unwrap()
        .max_significant_bits(),
    40
);

Trait Implementations§

Source§

impl<'a> Clone for ComparableGaussianIntegerRef<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for ComparableGaussianIntegerRef<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for ComparableGaussianIntegerRef<'_>

Source§

fn deref(&self) -> &GaussianInteger

Allows a ComparableGaussianIntegerRef to dereference to a GaussianInteger.

use malachite_base::num::basic::traits::One;
use malachite_nz::gaussian_integer::{ComparableGaussianIntegerRef, GaussianInteger};

let x = GaussianInteger::ONE;
let y = ComparableGaussianIntegerRef(&x);
assert_eq!(*y, GaussianInteger::ONE);
Source§

type Target = GaussianInteger

The resulting type after dereferencing.
Source§

impl Display for ComparableGaussianIntegerRef<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Converts a ComparableGaussianIntegerRef to a String, writing the wrapped GaussianInteger exactly as its own Display implementation does.

§Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant bits of the real and imaginary parts.

§Examples
use malachite_base::num::basic::traits::I;
use malachite_nz::gaussian_integer::{ComparableGaussianIntegerRef, GaussianInteger};

let x = GaussianInteger::I;
assert_eq!(ComparableGaussianIntegerRef(&x).to_string(), "i");
Source§

impl<'a> Eq for ComparableGaussianIntegerRef<'a>

Source§

impl<'a> Hash for ComparableGaussianIntegerRef<'a>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for ComparableGaussianIntegerRef<'_>

Source§

fn cmp(&self, other: &Self) -> Ordering

Compares two ComparableGaussianIntegerRefs.

The order is lexicographic: real parts are compared first, and imaginary parts break ties. This is a total order, and its equality agrees with GaussianInteger equality, but it is not compatible with arithmetic: no total order on the complex numbers is. It is intended for canonically sorting Gaussian integers and for using them as keys in ordered collections.

§Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant bits of the real and imaginary parts of self and other.

§Examples
use malachite_base::num::basic::traits::{I, NegativeI, One, Zero};
use malachite_nz::gaussian_integer::{ComparableGaussianIntegerRef, GaussianInteger};

let zero = GaussianInteger::ZERO;
let one = GaussianInteger::ONE;
let i = GaussianInteger::I;
let negative_i = GaussianInteger::NEGATIVE_I;

// 0 < i, since the real parts are equal and 0 < 1
assert!(ComparableGaussianIntegerRef(&zero) < ComparableGaussianIntegerRef(&i));
// -i < i
assert!(ComparableGaussianIntegerRef(&negative_i) < ComparableGaussianIntegerRef(&i));
// i < 1, since 0 < 1 and the real parts are compared first
assert!(ComparableGaussianIntegerRef(&i) < ComparableGaussianIntegerRef(&one));
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl<'a> PartialEq for ComparableGaussianIntegerRef<'a>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for ComparableGaussianIntegerRef<'_>

Source§

fn partial_cmp( &self, other: &ComparableGaussianIntegerRef<'_>, ) -> Option<Ordering>

Compares two ComparableGaussianIntegerRefs.

See the documentation for the Ord implementation.

1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a> StructuralPartialEq for ComparableGaussianIntegerRef<'a>

Source§

impl ToLatex for ComparableGaussianIntegerRef<'_>

Source§

fn fmt_latex(&self, f: &mut Formatter<'_>) -> Result

Writes a ComparableGaussianIntegerRef as a LaTeX math-mode fragment.

The fragment is the wrapped GaussianInteger’s own: the wrapper exists to give an ordering, and does not change what the value is.

§Worst-case complexity

Same as the time and additional memory complexity of fmt_latex for GaussianInteger.

§Examples
use malachite_base::strings::latex::ToLatex;
use malachite_nz::gaussian_integer::{ComparableGaussianIntegerRef, GaussianInteger};

let x = GaussianInteger::from(2);
assert_eq!(
    ComparableGaussianIntegerRef(&x).to_latex_string(),
    x.to_latex_string()
);
Source§

fn to_latex(&self) -> LatexWrapper<'_, Self>
where Self: Sized,

Converts a value to a LaTeX math-mode fragment. Read more
Source§

fn to_latex_string(&self) -> String
where Self: Sized,

Converts a value to a LaTeX math-mode fragment, as a String. Read more
Source§

impl ToTypst for ComparableGaussianIntegerRef<'_>

Source§

fn fmt_typst(&self, f: &mut Formatter<'_>) -> Result

Writes a ComparableGaussianIntegerRef as a Typst math-mode fragment.

The fragment is the wrapped GaussianInteger’s own: the wrapper exists to give an ordering, and does not change what the value is.

§Worst-case complexity

Same as the time and additional memory complexity of fmt_typst for GaussianInteger.

§Examples
use malachite_base::strings::typst::ToTypst;
use malachite_nz::gaussian_integer::{ComparableGaussianIntegerRef, GaussianInteger};

let x = GaussianInteger::from(2);
assert_eq!(
    ComparableGaussianIntegerRef(&x).to_typst_string(),
    x.to_typst_string()
);
Source§

fn to_typst(&self) -> TypstWrapper<'_, Self>
where Self: Sized,

Converts a value to a Typst math-mode fragment. Read more
Source§

fn to_typst_string(&self) -> String
where Self: Sized,

Converts a value to a Typst math-mode fragment, as a String. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T, U> ExactFrom<T> for U
where U: TryFrom<T>,

Source§

fn exact_from(value: T) -> U

Source§

impl<T, U> ExactInto<U> for T
where U: ExactFrom<T>,

Source§

fn exact_into(self) -> U

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> ImaginaryInto<U> for T
where U: ImaginaryFrom<T>,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> OverflowingInto<U> for T
where U: OverflowingFrom<T>,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> RoundingInto<U> for T
where U: RoundingFrom<T>,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> SaturatingInto<U> for T
where U: SaturatingFrom<T>,

Source§

impl<T> ToDebugString for T
where T: Debug,

Source§

fn to_debug_string(&self) -> String

Returns the String produced by Ts Debug implementation.

§Examples
use malachite_base::strings::ToDebugString;

assert_eq!([1, 2, 3].to_debug_string(), "[1, 2, 3]");
assert_eq!(
    [vec![2, 3], vec![], vec![4]].to_debug_string(),
    "[[2, 3], [], [4]]"
);
assert_eq!(Some(5).to_debug_string(), "Some(5)");
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T, U> WrappingInto<U> for T
where U: WrappingFrom<T>,

Source§

fn wrapping_into(self) -> U