malachite_nz/
lib.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9//! This crate defines [`Natural`](natural::Natural)s (non-negative integers) and
10//! [`Integer`](integer::Integer)s. Unlike primitive integers ([`u32`], [`i32`], and so on), these
11//! may be arbitrarily large. The name of this crate refers to the mathematical symbols for natural
12//! numbers and integers, $\N$ and $\Z$.
13//! - There are many functions defined on [`Natural`](natural::Natural)s and
14//!   [`Integer`](integer::Integer)s. These include
15//!   - All the ones you'd expect, like addition, subtraction, multiplication, and integer
16//!     division;
17//!   - Implementations of [`DivRound`](malachite_base::num::arithmetic::traits::DivRound), which
18//!     provides division that rounds according to a specified
19//!     [`RoundingMode`](malachite_base::rounding_modes::RoundingMode);
20//!   - Various mathematical functions, like implementations of
21//!     [`FloorSqrt`](malachite_base::num::arithmetic::traits::FloorSqrt) and
22//!     [`Gcd`](malachite_base::num::arithmetic::traits::Gcd);
23//!   - Modular arithmetic functions, like implementations of
24//!     [`ModAdd`](malachite_base::num::arithmetic::traits::ModAdd) and
25//!     [`ModPow`](malachite_base::num::arithmetic::traits::ModPow), and of traits for arithmetic
26//!     modulo a power of 2, like
27//!     [`ModPowerOf2Add`](malachite_base::num::arithmetic::traits::ModPowerOf2Add) and
28//!     [`ModPowerOf2Pow`](malachite_base::num::arithmetic::traits::ModPowerOf2Pow);
29//!   - Various functions for logic and bit manipulation, like [`BitAnd`](core::ops::BitAnd) and
30//!     [`BitAccess`](malachite_base::num::logic::traits::BitAccess).
31//! - The implementations of these functions use high-performance algorithms that work efficiently
32//!   for large numbers. For example, multiplication uses the naive quadratic algorithm, or one of
33//!   13 variants of
34//!   [Toom-Cook multiplication](https://en.wikipedia.org/wiki/Toom%E2%80%93Cook_multiplication),
35//!   or
36//!   [Schönhage-Strassen (FFT) multiplication](https://en.wikipedia.org/wiki/Schonhage-Strassen_algorithm),
37//!   depending on the input size.
38//! - Small numbers are also handled efficiently. Any [`Natural`](natural::Natural) smaller than
39//!   $2^{64}$ does not use any allocated memory, and working with such numbers is almost as fast
40//!   as working with primitive integers. As a result, Malachite does not provide implementations
41//!   for _e.g._ adding a [`Natural`](natural::Natural) to a [`u64`], since the [`u64`] can be
42//!   converted to a [`Natural`](natural::Natural) very cheaply.
43//! - Malachite handles memory intelligently. Consider the problem of adding a 1000-bit
44//!   [`Natural`](natural::Natural) and a 500-bit [`Natural`](natural::Natural). If we only have
45//!   references to the [`Natural`](natural::Natural)s, then we must allocate new memory for the
46//!   result, and this is what the `&Natural + &Natural` implementation does. However, if we can
47//!   take the first (larger) [`Natural`](natural::Natural) by value, then we do not need to
48//!   allocate any memory (except in the unlikely case of a carry): we can reuse the memory of the
49//!   first [`Natural`](natural::Natural) to store the result, and this is what the
50//!   `Natural + &Natural` implementation does. On the other hand, if we can only take the second
51//!   (smaller) [`Natural`](natural::Natural) by value, then we only have 500 bits of memory
52//!   available, which is not enough to store the sum. In this case, the [`Vec`] containing the
53//!   smaller [`Natural`](natural::Natural)'s data can be extended to hold 1000 bits, in hopes that
54//!   this will be more efficient than allocating 1000 bits in a completely new [`Vec`]. Finally,
55//!   if both [`Natural`](natural::Natural)s are taken by value, then the `Natural + Natural`
56//!   implementation chooses to reuse the memory of the larger one.
57//!
58//!   Now consider what happens when evaluating the expression `&x + &y + &z`, where each
59//!   [`Natural`](natural::Natural) has $n$ bits. Malachite must allocate about $n$ bits for the
60//!   result, but what about the intermediate sum `&x + &y`? Does Malachite need to allocate
61//!   another $n$ bits for that, for a total of $2n$ bits? No! Malachite first allocates $n$ bits
62//!   for `&x + &y`, but then that partial sum is taken by _value_ using the `Natural + &Natural`
63//!   implementation described above; so those $n$ bits are reused for the final sum.
64//!
65//! # Limbs
66//! Large [`Natural`](natural::Natural)s and [`Integer`](integer::Integer)s store their data as
67//! [`Vec`]s of some primitive type. The elements of these
68//! [`Vec`]s are called "limbs" in GMP terminology, since they're large digits.
69//! By default, the type of a `Limb` is [`u64`], but you can set it to [`u32`] using the
70//! `32_bit_limbs` feature.
71//!
72//! # Demos and benchmarks
73//! This crate comes with a `bin` target that can be used for running demos and benchmarks.
74//! - Almost all of the public functions in this crate have an associated demo. Running a demo
75//!   shows you a function's behavior on a large number of inputs. For example, to demo the
76//!   [`mod_pow`](malachite_base::num::arithmetic::traits::ModPow::mod_pow) function on
77//!   [`Natural`](natural::Natural)s, you can use the following command:
78//!   ```text
79//!   cargo run --features bin_build --release -- -l 10000 -m exhaustive -d demo_natural_mod_pow
80//!   ```
81//!   This command uses the `exhaustive` mode, which generates every possible input, generally
82//!   starting with the simplest input and progressing to more complex ones. Another mode is
83//!   `random`. The `-l` flag specifies how many inputs should be generated.
84//! - You can use a similar command to run benchmarks. The following command benchmarks various
85//!   GCD algorithms for [`u64`]s:
86//!   ```text
87//!   cargo run --features bin_build --release -- -l 1000000 -m random -b \
88//!       benchmark_natural_gcd_algorithms -o gcd-bench.gp
89//!   ```
90//!   or GCD implementations of other libraries:
91//!   ```text
92//!   cargo run --features bin_build --release -- -l 1000000 -m random -b \
93//!       benchmark_natural_gcd_library_comparison -o gcd-bench.gp
94//!   ```
95//!   This creates a file called gcd-bench.gp. You can use gnuplot to create an SVG from it like
96//!   so:
97//!   ```text
98//!   gnuplot -e "set terminal svg; l \"gcd-bench.gp\"" > gcd-bench.svg
99//!   ```
100//!
101//! The list of available demos and benchmarks is not documented anywhere; you must find them by
102//! browsing through
103//! [`bin_util/demo_and_bench`](https://github.com/mhogrefe/malachite/tree/master/malachite-nz/src/bin_util/demo_and_bench).
104//!
105//! # Features
106//! - `32_bit_limbs`: Sets the type of [`Limb`](crate#limbs) to [`u32`] instead of the default,
107//!   [`u64`].
108//! - `test_build`: A large proportion of the code in this crate is only used for testing. For a
109//!   typical user, building this code would result in an unnecessarily long compilation time and
110//!   an unnecessarily large binary. Some of it is also used for testing `malachite-q`, so it can't
111//!   just be confined to the `tests` directory. My solution is to only build this code when the
112//!   `test_build` feature is enabled. If you want to run unit tests, you must enable `test_build`.
113//!   However, doctests don't require it, since they only test the public interface.
114//! - `bin_build`: This feature is used to build the code for demos and benchmarks, which also
115//!   takes a long time to build. Enabling this feature also enables `test_build`.
116
117#![allow(
118    unstable_name_collisions,
119    clippy::assertions_on_constants,
120    clippy::cognitive_complexity,
121    clippy::many_single_char_names,
122    clippy::range_plus_one,
123    clippy::suspicious_arithmetic_impl,
124    clippy::suspicious_op_assign_impl,
125    clippy::too_many_arguments,
126    clippy::type_complexity,
127    clippy::upper_case_acronyms,
128    clippy::multiple_bound_locations
129)]
130#![warn(
131    clippy::cast_lossless,
132    clippy::explicit_into_iter_loop,
133    clippy::explicit_iter_loop,
134    clippy::filter_map_next,
135    clippy::large_digit_groups,
136    clippy::manual_filter_map,
137    clippy::manual_find_map,
138    clippy::map_flatten,
139    clippy::map_unwrap_or,
140    clippy::match_same_arms,
141    clippy::missing_const_for_fn,
142    clippy::mut_mut,
143    clippy::needless_borrow,
144    clippy::needless_continue,
145    clippy::needless_pass_by_value,
146    clippy::print_stdout,
147    clippy::redundant_closure_for_method_calls,
148    clippy::single_match_else,
149    clippy::trait_duplication_in_bounds,
150    clippy::type_repetition_in_bounds,
151    clippy::uninlined_format_args,
152    clippy::unused_self,
153    clippy::if_not_else,
154    clippy::manual_assert,
155    clippy::range_plus_one,
156    clippy::redundant_else,
157    clippy::semicolon_if_nothing_returned,
158    clippy::cloned_instead_of_copied,
159    clippy::flat_map_option,
160    clippy::unnecessary_wraps,
161    clippy::unnested_or_patterns,
162    clippy::use_self,
163    clippy::trivially_copy_pass_by_ref
164)]
165#![cfg_attr(
166    not(any(feature = "test_build", feature = "random", feature = "std")),
167    no_std
168)]
169
170#[macro_use]
171extern crate alloc;
172
173extern crate itertools;
174#[macro_use]
175extern crate malachite_base;
176#[cfg(feature = "serde")]
177#[macro_use]
178extern crate serde;
179
180#[cfg(feature = "test_build")]
181extern crate num;
182#[cfg(feature = "test_build")]
183extern crate rug;
184
185#[doc(hidden)]
186#[cfg(not(feature = "32_bit_limbs"))]
187pub use crate::platform_64 as platform;
188#[doc(hidden)]
189#[cfg(feature = "32_bit_limbs")]
190pub use platform_32 as platform;
191
192#[doc(hidden)]
193#[cfg(feature = "32_bit_limbs")]
194pub mod platform_32;
195#[doc(hidden)]
196#[cfg(not(feature = "32_bit_limbs"))]
197pub mod platform_64;
198
199#[cfg(feature = "doc-images")]
200extern crate embed_doc_image;
201
202/// [`Natural`](natural::Natural), a type representing arbitrarily large non-negative integers.
203#[macro_use]
204pub mod natural;
205/// [`Integer`](integer::Integer), a type representing integers with arbitrarily large absolute
206/// values.
207pub mod integer;
208
209#[cfg(feature = "test_build")]
210pub mod test_util;