malachite-nz 0.13.0

The bignum types Natural and Integer, with efficient algorithms partially derived from GMP and FLINT.
Documentation
// Copyright © 2026 Mikhail Hogrefe
//
// 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::gaussian_integer::GaussianInteger;
use crate::gaussian_integer::exhaustive::gaussian_integer_from_pair;
use crate::integer::Integer;
use crate::integer::random::{
    RandomIntegers, StripedRandomIntegers, random_integers, striped_random_integers,
};
use core::iter::Map;
use malachite_base::num::conversion::traits::ImaginaryFrom;
use malachite_base::num::random::geometric::GeometricRandomSigneds;
use malachite_base::random::Seed;
use malachite_base::tuples::random::{RandomPairsFromSingle, random_pairs_from_single};

/// Generates random purely real [`GaussianInteger`]s, whose real parts are generated by
/// [`random_integers`] and have a specified mean bit length.
///
/// The output length is infinite.
///
/// # Expected complexity per iteration
/// Same as the complexity of [`random_integers`].
///
/// # Panics
/// Panics if `mean_bits_numerator` or `mean_bits_denominator` are zero, or, if after being reduced
/// to lowest terms, their sum is greater than or equal to $2^{64}$.///
/// # Examples
/// ```
/// use malachite_base::iterators::prefix_to_string;
/// use malachite_base::random::EXAMPLE_SEED;
/// use malachite_nz::gaussian_integer::random::*;
///
/// assert_eq!(
///     prefix_to_string(random_real_gaussian_integers(EXAMPLE_SEED, 32, 1), 6),
///     "[89270, 69403499476962893258904, 62, -1848070042786, -64671510460, -696, ...]"
/// )
/// ```
#[inline]
pub fn random_real_gaussian_integers(
    seed: Seed,
    mean_bits_numerator: u64,
    mean_bits_denominator: u64,
) -> Map<RandomIntegers<GeometricRandomSigneds<i64>>, fn(Integer) -> GaussianInteger> {
    random_integers(seed, mean_bits_numerator, mean_bits_denominator).map(GaussianInteger::from)
}

/// Generates random purely imaginary [`GaussianInteger`]s, whose imaginary parts are generated by
/// [`random_integers`] and have a specified mean bit length.
///
/// The output length is infinite.
///
/// # Expected complexity per iteration
/// Same as the complexity of [`random_integers`].
///
/// # Panics
/// Panics if `mean_bits_numerator` or `mean_bits_denominator` are zero, or, if after being reduced
/// to lowest terms, their sum is greater than or equal to $2^{64}$.///
/// # Examples
/// ```
/// use malachite_base::iterators::prefix_to_string;
/// use malachite_base::random::EXAMPLE_SEED;
/// use malachite_nz::gaussian_integer::random::*;
///
/// assert_eq!(
///     prefix_to_string(random_imaginary_gaussian_integers(EXAMPLE_SEED, 32, 1), 6),
///     "[89270i, 69403499476962893258904i, 62i, -1848070042786i, -64671510460i, -696i, ...]"
/// )
/// ```
#[inline]
pub fn random_imaginary_gaussian_integers(
    seed: Seed,
    mean_bits_numerator: u64,
    mean_bits_denominator: u64,
) -> Map<RandomIntegers<GeometricRandomSigneds<i64>>, fn(Integer) -> GaussianInteger> {
    random_integers(seed, mean_bits_numerator, mean_bits_denominator)
        .map(GaussianInteger::imaginary_from)
}

/// Generates random [`GaussianInteger`]s, whose real and imaginary parts are generated by
/// [`random_integers`] and have a specified mean bit length.
///
/// The output length is infinite.
///
/// # Expected complexity per iteration
/// Same as the complexity of [`random_integers`].
///
/// # Panics
/// Panics if `mean_bits_numerator` or `mean_bits_denominator` are zero, or, if after being reduced
/// to lowest terms, their sum is greater than or equal to $2^{64}$.///
/// # Examples
/// ```
/// use malachite_base::iterators::prefix_to_string;
/// use malachite_base::random::EXAMPLE_SEED;
/// use malachite_nz::gaussian_integer::random::*;
///
/// assert_eq!(
///     prefix_to_string(random_gaussian_integers(EXAMPLE_SEED, 32, 1), 6),
///     "[69403499476962893258904+89270i, -1848070042786+62i, -696-64671510460i, -79, \
///     7330+70819i, -424643+215441i, ...]"
/// )
/// ```
#[inline]
pub fn random_gaussian_integers(
    seed: Seed,
    mean_bits_numerator: u64,
    mean_bits_denominator: u64,
) -> Map<
    RandomPairsFromSingle<RandomIntegers<GeometricRandomSigneds<i64>>>,
    fn((Integer, Integer)) -> GaussianInteger,
> {
    random_pairs_from_single(random_integers(
        seed,
        mean_bits_numerator,
        mean_bits_denominator,
    ))
    .map(gaussian_integer_from_pair)
}

/// Generates random purely real striped [`GaussianInteger`]s, whose real parts are generated by
/// [`striped_random_integers`] and have a specified mean bit length and mean stripe length.
///
/// The output length is infinite.
///
/// # Expected complexity per iteration
/// Same as the complexity of [`striped_random_integers`].
///
/// # Panics
/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <
/// mean_stripe_denominator`, if `mean_bits_numerator` or `mean_bits_denominator` are zero, or, if
/// after being reduced to lowest terms, their sum is greater than or equal to $2^{64}$.///
/// # Examples
/// ```
/// use malachite_base::iterators::prefix_to_string;
/// use malachite_base::random::EXAMPLE_SEED;
/// use malachite_nz::gaussian_integer::random::*;
///
/// assert_eq!(
///     prefix_to_string(
///         striped_random_real_gaussian_integers(EXAMPLE_SEED, 16, 1, 32, 1),
///         6
///     ),
///     "[65536, 75521006248971741167616, 32, -2199023255520, -68719468544, -527, ...]"
/// )
/// ```
#[inline]
pub fn striped_random_real_gaussian_integers(
    seed: Seed,
    mean_stripe_numerator: u64,
    mean_stripe_denominator: u64,
    mean_bits_numerator: u64,
    mean_bits_denominator: u64,
) -> Map<StripedRandomIntegers<GeometricRandomSigneds<i64>>, fn(Integer) -> GaussianInteger> {
    striped_random_integers(
        seed,
        mean_stripe_numerator,
        mean_stripe_denominator,
        mean_bits_numerator,
        mean_bits_denominator,
    )
    .map(GaussianInteger::from)
}

/// Generates random purely imaginary striped [`GaussianInteger`]s, whose imaginary parts are
/// generated by [`striped_random_integers`] and have a specified mean bit length and mean stripe
/// length.
///
/// The output length is infinite.
///
/// # Expected complexity per iteration
/// Same as the complexity of [`striped_random_integers`].
///
/// # Panics
/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <
/// mean_stripe_denominator`, if `mean_bits_numerator` or `mean_bits_denominator` are zero, or, if
/// after being reduced to lowest terms, their sum is greater than or equal to $2^{64}$.///
/// # Examples
/// ```
/// use malachite_base::iterators::prefix_to_string;
/// use malachite_base::random::EXAMPLE_SEED;
/// use malachite_nz::gaussian_integer::random::*;
///
/// assert_eq!(
///     prefix_to_string(
///         striped_random_imaginary_gaussian_integers(EXAMPLE_SEED, 16, 1, 32, 1),
///         6
///     ),
///     "[65536i, 75521006248971741167616i, 32i, -2199023255520i, -68719468544i, -527i, ...]"
/// )
/// ```
#[inline]
pub fn striped_random_imaginary_gaussian_integers(
    seed: Seed,
    mean_stripe_numerator: u64,
    mean_stripe_denominator: u64,
    mean_bits_numerator: u64,
    mean_bits_denominator: u64,
) -> Map<StripedRandomIntegers<GeometricRandomSigneds<i64>>, fn(Integer) -> GaussianInteger> {
    striped_random_integers(
        seed,
        mean_stripe_numerator,
        mean_stripe_denominator,
        mean_bits_numerator,
        mean_bits_denominator,
    )
    .map(GaussianInteger::imaginary_from)
}

/// Generates random striped [`GaussianInteger`]s, whose real and imaginary parts are generated by
/// [`striped_random_integers`] and have a specified mean bit length and mean stripe length.
///
/// The output length is infinite.
///
/// # Expected complexity per iteration
/// Same as the complexity of [`striped_random_integers`].
///
/// # Panics
/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <
/// mean_stripe_denominator`, if `mean_bits_numerator` or `mean_bits_denominator` are zero, or, if
/// after being reduced to lowest terms, their sum is greater than or equal to $2^{64}$.///
/// # Examples
/// ```
/// use malachite_base::iterators::prefix_to_string;
/// use malachite_base::random::EXAMPLE_SEED;
/// use malachite_nz::gaussian_integer::random::*;
///
/// assert_eq!(
///     prefix_to_string(
///         striped_random_gaussian_integers(EXAMPLE_SEED, 16, 1, 32, 1),
///         6
///     ),
///     "[75521006248971741167616+65536i, -2199023255520+32i, -527-68719468544i, -112, \
///     4152+131071i, -262145+262143i, ...]"
/// )
/// ```
#[inline]
pub fn striped_random_gaussian_integers(
    seed: Seed,
    mean_stripe_numerator: u64,
    mean_stripe_denominator: u64,
    mean_bits_numerator: u64,
    mean_bits_denominator: u64,
) -> Map<
    RandomPairsFromSingle<StripedRandomIntegers<GeometricRandomSigneds<i64>>>,
    fn((Integer, Integer)) -> GaussianInteger,
> {
    random_pairs_from_single(striped_random_integers(
        seed,
        mean_stripe_numerator,
        mean_stripe_denominator,
        mean_bits_numerator,
        mean_bits_denominator,
    ))
    .map(gaussian_integer_from_pair)
}