pub struct Q { /* private fields */ }Expand description
Q is any rational value.
Attributes:
§Implicit Typecasting
Most of our functions take as input values of type Into<Q>.
These capture all types that can be turned into a Q value.
The types are Q,Z,
Modulus, i8,i16,i32,
i64,u8,u16,u32,u64,f32,f64
and the references of all of these types.
These types are then implicitly casted to a Q before the desired action is
performed.
§Examples
use qfall_math::rational::Q;
use std::str::FromStr;
// instantiations
let a = Q::from_str("-876543/235")?;
let b = Q::from(21);
let zero = Q::default();
let _ = a.clone();
// arithmetics
let _ = &a + &zero;
let _ = &a * &b;
// to_string incl. (de-)serialization
assert_eq!("-876543/235", &a.to_string());
assert_eq!(
"{\"value\":\"-876543/235\"}",
serde_json::to_string(&a).unwrap()
);
// comparison
assert_ne!(a, b);Implementations§
Source§impl Q
impl Q
Sourcepub fn div_safe(&self, divisor: &Q) -> Result<Q, MathError>
pub fn div_safe(&self, divisor: &Q) -> Result<Q, MathError>
Implements division for two borrowed Q values.
Parameters:
divisor: specifies the valueselfis divided by.
Returns the result ot the division as a Q or an error if division by zero occurs.
§Examples
use qfall_math::rational::Q;
use std::str::FromStr;
let a: Q = Q::from(42);
let b: Q = Q::from(24);
let c: Q = a.div_safe(&b).unwrap();§Errors
- Returns a
MathErrorof typeMathError::DivisionByZeroErrorif thedivisoris0.
Source§impl Q
impl Q
Sourcepub fn exp(&self) -> Self
pub fn exp(&self) -> Self
Computes e^self.
For exponents below 709, the value is converted to f64 for the calculation.
As a result, the precision is limited to the precision of f64.
This means that values smaller than e^-745 are rounded to 0.
The f64 calculation is relatively fast.
For exponents above 709, a different algorithm is used.
Its error bound is not calculated at this point, but probably below 10%.
Returns e^self.
§Examples
use qfall_math::rational::Q;
let evaluation = Q::from((17, 3)).exp();
let e = Q::ONE.exp();§Panics …
- if the exponent is too large (exponent*log_2(e)) >= 2^36. This would use up more than 64 GB of RAM.
Sourcepub fn exp_taylor(&self, length_taylor_polynomial: impl Into<u32>) -> Self
pub fn exp_taylor(&self, length_taylor_polynomial: impl Into<u32>) -> Self
Computes e^self using taylor series approximation of the exponential function.
Parameters:
length_taylor_polynomial: the length of the taylor series approximation of the exponential function
Returns e^self.
§Examples
use qfall_math::rational::Q;
use std::str::FromStr;
// sum_{k=0}^999 (17/3)^k/k!
let evaluation = Q::from((17, 3)).exp_taylor(1000_u32);Source§impl Q
impl Q
Sourcepub fn ln(&self) -> Result<Self, MathError>
pub fn ln(&self) -> Result<Self, MathError>
Computes the natural logarithm of a positive rational number
approximated as an f64 and returned as a Q.
Warning: It assumes that the return value does not overflow an f64.
Returns the double precision approximation of the natural logarithm of self
as a Q instance or a MathError, if self is smaller than 1.
§Examples
use qfall_math::rational::Q;
let value = Q::from(1);
let log = value.ln().unwrap();
assert_eq!(Q::ZERO, log);§Errors and Failures
- Returns a
MathErrorof typeNonPositiveifselfis not greater than0.
Sourcepub fn log(&self, base: impl Into<Z>) -> Result<Q, MathError>
pub fn log(&self, base: impl Into<Z>) -> Result<Q, MathError>
Computes the logarithm of a positive rational number
with an integer base greater than 1 approximated as an f64
and returned as a Q.
Warning: It assumes that the return value does not overflow an f64.
Parameters:
base: the base of the logarithm
Returns log_base(self) as a Q instance or a MathError,
if at least one of the conditions base > 1 and self > 0 isn’t met.
§Examples
use qfall_math::rational::Q;
let value = Q::from(2);
let log = value.log(2).unwrap();
assert_eq!(Q::ONE, log);§Errors and Failures
- Returns a
MathErrorof typeInvalidIntegerInputif thebaseis smaller than2. - Returns a
MathErrorof typeNonPositiveifselfis not greater than0.
Source§impl Q
impl Q
Sourcepub fn sqrt(&self) -> Q
pub fn sqrt(&self) -> Q
Calculate the square root with a fixed relative precision.
The maximum error can be described by:
Q::sqrt(x/y) = a/b the maximum error to the true square root result
is limited by a/b * (b + 1) * 10⁻⁹/(b-10⁻⁹)
which is less than 2 * a/b * 10^-9.
The actual result may be more accurate.
§Examples
use qfall_math::rational::Q;
let value = Q::from((9, 4));
let root = value.sqrt();
assert_eq!(&root, &Q::from((3, 2)));§Panics …
- if the provided value is negative.
Sourcepub fn sqrt_precision(&self, precision: &Z) -> Result<Q, MathError>
pub fn sqrt_precision(&self, precision: &Z) -> Result<Q, MathError>
Calculate the square root with a specified minimum precision.
Given Q::sqrt_precision(x/y, precision) = a/b the maximum error to
the true square root result is a/b * (b + 1) * p/(b-p)
with p = 1/(2*precision).
The actual result may be more accurate.
Parameters:
precisionspecifies the upper limit of the error. The precision must larger than zero.
Returns the square root of the value as a Q instance or a MathError,
if the precision is not larger than zero, or the parameter of the square root is negative.
§Examples
use qfall_math::integer::Z;
use qfall_math::rational::Q;
let precision = Z::from(1000);
let value = Q::from((9, 4));
let root = value.sqrt_precision(&precision).unwrap();
assert_eq!(&root, &Q::from((3, 2)));§Errors and Failures
- Returns a
MathErrorof typeMathError::NonPositiveif the precision is not larger than zero. - Returns a
MathErrorof typeMathError::NonPositiveif the parameter of the square root is negative.
Source§impl Q
impl Q
Source§impl Q
impl Q
Sourcepub fn from_f64(value: f64) -> Self
pub fn from_f64(value: f64) -> Self
Creates a rational number of type Q from a f64.
This function works with the exact float it received as input.
Many numbers like 0.1 are not exactly representable as floats and
will therefore not be instantiated as 1/10.
Input parameters:
value: the value the rational number will have, provided as af64
Returns a Q.
§Examples
use qfall_math::rational::Q;
let a: Q = Q::from_f64(0.3);
let a: Q = Q::from_f64(-123.4567);Source§impl Q
impl Q
Sourcepub fn get_denominator(&self) -> Z
pub fn get_denominator(&self) -> Z
Returns the denominator
§Examples
use qfall_math::rational::Q;
use qfall_math::integer::Z;
let value = Q::from((2, 20));
let den = value.get_denominator();
assert_eq!(den, Z::from(10));Sourcepub fn get_numerator(&self) -> Z
pub fn get_numerator(&self) -> Z
Returns the numerator
§Examples
use qfall_math::rational::Q;
use qfall_math::integer::Z;
let value = Q::from((2, 20));
let num = value.get_numerator();
assert_eq!(num, Z::from(1));Source§impl Q
impl Q
Source§impl Q
impl Q
Sourcepub fn floor(&self) -> Z
pub fn floor(&self) -> Z
Rounds the given rational Q down to the next integer Z.
§Examples
use qfall_math::rational::Q;
use qfall_math::integer::Z;
let value = Q::from((5, 2));
assert_eq!(Z::from(2), value.floor());
let value = Q::from((-5, 2));
assert_eq!(Z::from(-3), value.floor());
let value = Q::from(2);
assert_eq!(Z::from(2), value.floor());Sourcepub fn ceil(&self) -> Z
pub fn ceil(&self) -> Z
Rounds the given rational Q up to the next integer Z.
§Examples
use qfall_math::rational::Q;
use qfall_math::integer::Z;
let value = Q::from((5, 2));
assert_eq!(Z::from(3), value.ceil());
let value = Q::from((-5, 2));
assert_eq!(Z::from(-2), value.ceil());
let value = Q::from(2);
assert_eq!(Z::from(2), value.ceil());Sourcepub fn round(&self) -> Z
pub fn round(&self) -> Z
Rounds the given rational Q to the closest integer Z.
If the distance is equal, it rounds up.
§Examples
use qfall_math::rational::Q;
use qfall_math::integer::Z;
let value = Q::from((5, 2));
assert_eq!(Z::from(3), value.round());
let value = Q::from((-5, 2));
assert_eq!(Z::from(-2), value.round());
let value = Q::from(2);
assert_eq!(Z::from(2), value.round());Sourcepub fn simplify(&self, precision: impl Into<Q>) -> Self
pub fn simplify(&self, precision: impl Into<Q>) -> Self
Returns the smallest rational with the smallest denominator in the range
\[self - |precision|, self + |precision|\].
This function allows to free memory in exchange for the specified loss of precision (see Example 3). Be aware that this loss of precision is propagated by arithmetic operations and can be significantly increased depending on the performed operations.
This function ensures that simplifying does not change the sign of self.
Parameters:
precision: the precision the new value can differ fromself. Note that the absolute value is relevant, not the sign.
Returns the simplest Q within the defined range.
§Examples
use qfall_math::rational::Q;
let value = Q::from((17, 20));
let precision = Q::from((1, 20));
let simplified = Q::from((4, 5));
assert_eq!(simplified, value.simplify(&precision));use qfall_math::rational::Q;
let value = Q::from((3, 2));
assert_eq!(Q::ONE, value.simplify(0.5));§Simplify with reasonable precision loss
This example uses Q::INV_MAX32, i.e. a loss of precision of at most 1 / 2^31 - 2 behind the decimal point.
If you require higher precision, Q::INV_MAX62 is available.
use qfall_math::rational::Q;
let value = Q::PI;
let simplified = value.simplify(Q::INV_MAX32);
assert_ne!(&Q::PI, &simplified);
assert!(&simplified >= &(Q::PI - Q::INV_MAX32));
assert!(&simplified <= &(Q::PI + Q::INV_MAX32));Sourcepub fn randomized_rounding(&self, r: impl Into<Q>) -> Result<Z, MathError>
pub fn randomized_rounding(&self, r: impl Into<Q>) -> Result<Z, MathError>
Performs the randomized rounding algorithm
by sampling from a discrete Gaussian over the integers shifted
by self with gaussian parameter r.
Parameters:
r: specifies the Gaussian parameter, which is proportional to the standard deviationsigma * sqrt(2 * pi) = r
Returns the rounded value as an Z or an error if r < 0.
§Examples
use qfall_math::rational::Q;
let value = Q::from((5, 2));
let rounded = value.randomized_rounding(3).unwrap();§Errors and Failures
- Returns a
MathErrorof typeInvalidIntegerInputifr < 0.
This function implements randomized rounding according to:
- Peikert, C. (2010, August). An efficient and parallel Gaussian sampler for lattices. In: Annual Cryptology Conference (pp. 80-97). https://link.springer.com/chapter/10.1007/978-3-642-14623-7_5
Source§impl Q
impl Q
Sourcepub fn sample_gauss(
center: impl Into<Q>,
sigma: impl Into<f64>,
) -> Result<Q, MathError>
pub fn sample_gauss( center: impl Into<Q>, sigma: impl Into<f64>, ) -> Result<Q, MathError>
Chooses a Q instance according to the continuous Gaussian distribution.
Parameters:
center: specifies the position of the centersigma: specifies the standard deviation
Returns new Q sample chosen according to the specified continuous Gaussian
distribution or a MathError if the specified parameters were not chosen
appropriately (sigma > 0).
§Examples
use qfall_math::rational::Q;
let sample = Q::sample_gauss(0, 1).unwrap();§Errors and Failures
- Returns a
MathErrorof typeNonPositiveifsigma <= 0.
Source§impl Q
impl Q
Sourcepub fn to_string_decimal(&self, nr_decimal_digits: usize) -> String
pub fn to_string_decimal(&self, nr_decimal_digits: usize) -> String
Outputs the decimal representation of a Q with
the specified number of decimal digits.
If self can’t be represented exactly, it provides the
closest value representable with nr_decimal_digits rounded
towards the next representable number.
Notice that, e.g., 0.5 is represented as 0.499... as f64.
Therefore, rounding with nr_decimal_digits = 0 will output 0.
WARNING: This function converts the Q value into an f64 before
outputting the decimal representation. Thus, values that can’t be represented exactly
by an f64 will lose some precision. For large values, e.g. of size 2^64
the deviation to the original value might be within the size of 1_000.
Parameters:
nr_decimal_digits: specifies the number of decimal digits that will be a part of the outputString
Returns a String of the form "10.25" if nr_decimal_digits = 2.
§Examples
use qfall_math::rational::Q;
use std::str::FromStr;
let rational = Q::from_str("6/7").unwrap();
let decimal_repr = rational.to_string_decimal(3);Source§impl Q
impl Q
Sourcepub unsafe fn get_fmpq(&mut self) -> &mut fmpq
pub unsafe fn get_fmpq(&mut self) -> &mut fmpq
Returns a mutable reference to the field value of type fmpq.
WARNING: The returned struct is part of flint_sys.
Any changes to this object are unsafe and may introduce memory leaks.
This function is a passthrough to enable users of this library to use flint_sys
and with that FLINT functions that might not be covered in our library yet.
If this is the case, please consider contributing to this open-source project
by opening a Pull Request at qfall_math
to provide this feature in the future.
§Safety
Any flint_sys struct and function is part of a FFI to the C-library FLINT.
As FLINT is a C-library, it does not provide all memory safety features
that Rust and our Wrapper provide.
Thus, using functions of flint_sys can introduce memory leaks.
Source§impl Q
impl Q
Sourcepub unsafe fn set_fmpq(&mut self, flint_struct: fmpq)
pub unsafe fn set_fmpq(&mut self, flint_struct: fmpq)
Sets the field value of type fmpq to flint_struct.
Parameters:
flint_struct: value to set the attribute to
This function is a passthrough to enable users of this library to use flint_sys
and with that FLINT functions that might not be covered in our library yet.
If this is the case, please consider contributing to this open-source project
by opening a Pull Request at qfall_math
to provide this feature in the future.
§Safety
Ensure that the old struct does not share any memory with any other structs that might be used in the future. The memory of the old struct is freed using this function.
Any flint_sys struct and function is part of a FFI to the C-library FLINT.
As FLINT is a C-library, it does not provide all memory safety features
that Rust and our Wrapper provide.
Thus, using functions of flint_sys can introduce memory leaks.
Trait Implementations§
Source§impl Add<&Q> for &Z
impl Add<&Q> for &Z
Source§fn add(self, other: &Q) -> Self::Output
fn add(self, other: &Q) -> Self::Output
Implements the Add trait for Z and Q values.
Add is implemented for any combination of owned and borrowed values.
Parameters:
other: specifies the value to add toself
Returns the sum of both numbers as a Q.
§Examples
use qfall_math::rational::Q;
use qfall_math::integer::Z;
use std::str::FromStr;
let a: Z = Z::from(-42);
let b: Q = Q::from((42, 19));
let c: Q = &a + &b;
let d: Q = a + b;
let e: Q = &Z::from(42) + d;
let f: Q = Z::from(42) + &e;Source§impl Add<&Z> for &Q
impl Add<&Z> for &Q
Source§fn add(self, other: &Z) -> Self::Output
fn add(self, other: &Z) -> Self::Output
Implements the Add trait for Q and Z values.
Add is implemented for any combination of owned and borrowed values.
Parameters:
other: specifies the value to add toself
Returns the sum of both numbers as a Q.
§Examples
use qfall_math::rational::Q;
use qfall_math::integer::Z;
use std::str::FromStr;
let a: Q = Q::from((42, 19));
let b: Z = Z::from(-42);
let c: Q = &a + &b;
let d: Q = a + b;
let e: Q = &c + Z::from(42);
let f: Q = c + &Z::from(42);Source§impl Add for &Q
impl Add for &Q
Source§fn add(self, other: Self) -> Self::Output
fn add(self, other: Self) -> Self::Output
Implements the Add trait for two Q values.
Add is implemented for any combination of Q and borrowed Q.
Parameters:
other: specifies the value to add toself
Returns the sum of both rationals as a Q.
§Examples
use qfall_math::rational::Q;
use std::str::FromStr;
let a: Q = Q::from(42);
let b: Q = Q::from((-42, 2));
let c: Q = &a + &b;
let d: Q = a + b;
let e: Q = &c + d;
let f: Q = c + &e;Source§impl AddAssign<&Q> for Q
impl AddAssign<&Q> for Q
Source§fn add_assign(&mut self, other: &Self)
fn add_assign(&mut self, other: &Self)
Computes the addition of self and other reusing
the memory of self.
AddAssign can be used on Q in combination with
Q, Z, f64, f32, i64, i32, i16, i8, u64, u32, u16 and u8.
Parameters:
other: specifies the value to add toself
Returns the sum of both rationals as a Q.
§Examples
use qfall_math::{rational::Q, integer::Z};
let mut a: Q = Q::from(42);
let b: Q = Q::from((-42, 2));
let c: Z = Z::from(5);
a += &b;
a += b;
a += 5;
a += c;
a += 5.0;Source§impl AddAssign<&Z> for Q
impl AddAssign<&Z> for Q
Source§fn add_assign(&mut self, other: &Z)
fn add_assign(&mut self, other: &Z)
Documentation at Q::add_assign.
Source§impl AddAssign<Z> for Q
impl AddAssign<Z> for Q
Source§fn add_assign(&mut self, other: Z)
fn add_assign(&mut self, other: Z)
Documentation at Q::add_assign.
Source§impl AddAssign<f32> for Q
impl AddAssign<f32> for Q
Source§fn add_assign(&mut self, other: f32)
fn add_assign(&mut self, other: f32)
Documentation at Q::add_assign.
Source§impl AddAssign<f64> for Q
impl AddAssign<f64> for Q
Source§fn add_assign(&mut self, other: f64)
fn add_assign(&mut self, other: f64)
Documentation at Q::add_assign.
Source§impl AddAssign<i16> for Q
impl AddAssign<i16> for Q
Source§fn add_assign(&mut self, other: i16)
fn add_assign(&mut self, other: i16)
Documentation at Q::add_assign.
Source§impl AddAssign<i32> for Q
impl AddAssign<i32> for Q
Source§fn add_assign(&mut self, other: i32)
fn add_assign(&mut self, other: i32)
Documentation at Q::add_assign.
Source§impl AddAssign<i64> for Q
impl AddAssign<i64> for Q
Source§fn add_assign(&mut self, other: i64)
fn add_assign(&mut self, other: i64)
Documentation at Q::add_assign.
Source§impl AddAssign<i8> for Q
impl AddAssign<i8> for Q
Source§fn add_assign(&mut self, other: i8)
fn add_assign(&mut self, other: i8)
Documentation at Q::add_assign.
Source§impl AddAssign<u16> for Q
impl AddAssign<u16> for Q
Source§fn add_assign(&mut self, other: u16)
fn add_assign(&mut self, other: u16)
Documentation at Q::add_assign.
Source§impl AddAssign<u32> for Q
impl AddAssign<u32> for Q
Source§fn add_assign(&mut self, other: u32)
fn add_assign(&mut self, other: u32)
Documentation at Q::add_assign.
Source§impl AddAssign<u64> for Q
impl AddAssign<u64> for Q
Source§fn add_assign(&mut self, other: u64)
fn add_assign(&mut self, other: u64)
Documentation at Q::add_assign.
Source§impl AddAssign<u8> for Q
impl AddAssign<u8> for Q
Source§fn add_assign(&mut self, other: u8)
fn add_assign(&mut self, other: u8)
Documentation at Q::add_assign.
Source§impl AddAssign for Q
impl AddAssign for Q
Source§fn add_assign(&mut self, other: Q)
fn add_assign(&mut self, other: Q)
Documentation at Q::add_assign.
Source§impl Clone for Q
impl Clone for Q
Source§impl<'de> Deserialize<'de> for Q
impl<'de> Deserialize<'de> for Q
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Implements the deserialize option. This allows to create a Q from a given Json-object.
Source§impl Display for Q
impl Display for Q
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Allows to convert a rational of type Q into a String.
Returns the rational in form of a String. For rational 1/2
the String looks like this 1/2.
§Examples
use std::str::FromStr;
use qfall_math::rational::Q;
use core::fmt;
let rational = Q::from((-1, 235));
println!("{rational}");use std::str::FromStr;
use qfall_math::rational::Q;
use core::fmt;
let rational = Q::from((-1, 235));
let integer_string = rational.to_string();Source§impl<Rational: Into<Q>> Distance<Rational> for Q
impl<Rational: Into<Q>> Distance<Rational> for Q
Source§fn distance(&self, other: Rational) -> Self::Output
fn distance(&self, other: Rational) -> Self::Output
Computes the absolute distance between a Q instance and a value that
implements Into<Q>.
Parameters:
other: specifies one of theQvalues whose distance is calculated toself
Returns the absolute difference, i.e. distance between the two given Q
instances as a new Q instance.
§Examples
use qfall_math::rational::Q;
use qfall_math::traits::*;
let a = Q::from(1);
let distance_0 = a.distance(5);
let distance_1 = a.distance(10);
type Output = Q
Source§impl Div<&Q> for &MatQ
impl Div<&Q> for &MatQ
Source§fn div(self, scalar: &Q) -> Self::Output
fn div(self, scalar: &Q) -> Self::Output
Implements the Div trait for a MatQ by a Q rational.
Div is implemented for any combination of owned and borrowed values.
Parameters:
scalar: specifies the scalar by which the matrix is divided
Returns the division of self by scalar as a MatQ.
§Examples
use qfall_math::rational::{MatQ, Q};
use std::str::FromStr;
let matq_1 = MatQ::from_str("[[1, 2, 3],[4, 5/4, -1]]").unwrap();
let rational = Q::from((2,3));
let matq_2 = &matq_1 / &rational;§Panics …
- if the
scalaris0.
Source§impl Div<&Q> for &PolyOverQ
impl Div<&Q> for &PolyOverQ
Source§fn div(self, scalar: &Q) -> Self::Output
fn div(self, scalar: &Q) -> Self::Output
Implements the Div trait for a PolyOverQ by a Q rational.
Div is implemented for any combination of owned and borrowed values.
Parameters:
scalar: specifies the scalar by which the polynomial is divided
Returns the division of self by scalar as a PolyOverQ.
§Examples
use qfall_math::rational::{PolyOverQ, Q};
use std::str::FromStr;
let poly_1 = PolyOverQ::from_str("4 1/2 2 3/4 4").unwrap();
let rational = Q::from((2,3));
&poly_1 / &rational;
&poly_1 / rational;
&poly_1 / 2.0_f32;
&poly_1 / -2.0_f64;§Panics …
- if
scalaris0.
Source§impl Div<&Q> for &Z
impl Div<&Q> for &Z
Source§fn div(self, other: &Q) -> Self::Output
fn div(self, other: &Q) -> Self::Output
Implements the Div trait for Z and Q values.
Div is implemented for any combination of owned and borrowed values.
Parameters:
other: specifies the value to divideselfby
Returns the ratio of both numbers as a Q.
§Examples
use qfall_math::rational::Q;
use qfall_math::integer::Z;
use std::str::FromStr;
let a: Z = Z::from(-42);
let b: Q = Q::from((42, 19));
let c: Q = &a / &b;
let d: Q = a / b;
let e: Q = &Z::from(42) / d;
let f: Q = Z::from(42) / &e;§Panics …
- if the divisor is
0.
Source§impl Div<&Z> for &Q
impl Div<&Z> for &Q
Source§fn div(self, other: &Z) -> Self::Output
fn div(self, other: &Z) -> Self::Output
Implements the Div trait for Q and Z values.
Div is implemented for any combination of owned and borrowed values.
Parameters:
other: specifies the value to divideselfby
Returns the ratio of both numbers as a Q.
§Examples
use qfall_math::rational::Q;
use qfall_math::integer::Z;
use std::str::FromStr;
let a: Q = Q::from((42, 19));
let b: Z = Z::from(-42);
let c: Q = &a / &b;
let d: Q = a / b;
let e: Q = &c / Z::from(42);
let f: Q = c / &Z::from(42);Source§impl Div for &Q
impl Div for &Q
Source§fn div(self, other: Self) -> Self::Output
fn div(self, other: Self) -> Self::Output
Implements the Div trait for two Q values.
Div is implemented for any combination of Q and borrowed Q.
Parameters:
other: specifies the valueselfis divided by.
Returns the result ot the division as a Q.
§Examples
use qfall_math::rational::Q;
use std::str::FromStr;
let a: Q = Q::from(42);
let b: Q = Q::from(24);
let c: Q = &a / &b;
let d: Q = a / b;
let e: Q = &c / d;
let f: Q = c / &e;§Panics …
- if the
othervalue is0.
Source§impl DivAssign<&Q> for MatQ
impl DivAssign<&Q> for MatQ
Source§fn div_assign(&mut self, scalar: &Q)
fn div_assign(&mut self, scalar: &Q)
Computes the scalar multiplication of self and other reusing
the memory of self.
Parameters:
other: specifies the value to multiply toself
Divides self coordinate-wise by other returning a MatQ.
§Examples
use qfall_math::rational::{Q, MatQ};
use qfall_math::integer::{Z};
use std::str::FromStr;
let mut matq = MatQ::from_str(&format!("[[1, 2, 3],[4, 5/4, -1]]")).unwrap();
let q = Q::from((3, 4));
let z = Z::from(5);
matq /= &q;
matq /= q;
matq /= &z;
matq /= z;
matq /= -1;
matq /= 2;§Panics …
- if the
scalaris0.
Source§impl DivAssign<&Q> for PolyOverQ
impl DivAssign<&Q> for PolyOverQ
Source§fn div_assign(&mut self, scalar: &Q)
fn div_assign(&mut self, scalar: &Q)
Divides the polynomial coefficient-wise.
Parameters:
scalar: specifies the value to multiply toself
Divides self coefficient-wise by scalar returning a PolyOverQ.
§Examples
use qfall_math::rational::{Q, PolyOverQ};
use std::str::FromStr;
let mut polyq = PolyOverQ::from_str(&format!("3 1/3 -5 99/2")).unwrap();
let q = Q::from((3, 4));
polyq /= &q;
polyq /= q;
polyq /= 2_f32;
polyq /= -2_f64;§Panics …
- if the
scalaris0.
Source§impl DivAssign<&Q> for Q
impl DivAssign<&Q> for Q
Source§fn div_assign(&mut self, other: &Self)
fn div_assign(&mut self, other: &Self)
Computes the division of self and other reusing
the memory of self.
DivAssign can be used on Q in combination with
Q, Z, f64, f32, i64, i32, i16, i8, u64, u32, u16 and u8.
Parameters:
other: specifies the value to divideselfby
Returns the division of both rationals as a Q.
§Examples
use qfall_math::{rational::Q, integer::Z};
let mut a: Q = Q::from(42);
let b: Q = Q::from((-42, 2));
let c: Z = Z::from(5);
a /= &b;
a /= b;
a /= 5;
a /= c;
a /= 5.0;Source§impl DivAssign<&Z> for Q
impl DivAssign<&Z> for Q
Source§fn div_assign(&mut self, other: &Z)
fn div_assign(&mut self, other: &Z)
Documentation at Q::div_assign.
Source§impl DivAssign<Q> for MatQ
impl DivAssign<Q> for MatQ
Source§fn div_assign(&mut self, other: Q)
fn div_assign(&mut self, other: Q)
Documentation at MatQ::div_assign.
Source§impl DivAssign<Q> for PolyOverQ
impl DivAssign<Q> for PolyOverQ
Source§fn div_assign(&mut self, other: Q)
fn div_assign(&mut self, other: Q)
Documentation at PolyOverQ::div_assign.
Source§impl DivAssign<Z> for Q
impl DivAssign<Z> for Q
Source§fn div_assign(&mut self, other: Z)
fn div_assign(&mut self, other: Z)
Documentation at Q::div_assign.
Source§impl DivAssign<f32> for Q
impl DivAssign<f32> for Q
Source§fn div_assign(&mut self, other: f32)
fn div_assign(&mut self, other: f32)
Documentation at Q::div_assign.
Source§impl DivAssign<f64> for Q
impl DivAssign<f64> for Q
Source§fn div_assign(&mut self, other: f64)
fn div_assign(&mut self, other: f64)
Documentation at Q::div_assign.
Source§impl DivAssign<i16> for Q
impl DivAssign<i16> for Q
Source§fn div_assign(&mut self, other: i16)
fn div_assign(&mut self, other: i16)
Documentation at Q::div_assign.
Source§impl DivAssign<i32> for Q
impl DivAssign<i32> for Q
Source§fn div_assign(&mut self, other: i32)
fn div_assign(&mut self, other: i32)
Documentation at Q::div_assign.
Source§impl DivAssign<i64> for Q
impl DivAssign<i64> for Q
Source§fn div_assign(&mut self, other: i64)
fn div_assign(&mut self, other: i64)
Documentation at Q::div_assign.
Source§impl DivAssign<i8> for Q
impl DivAssign<i8> for Q
Source§fn div_assign(&mut self, other: i8)
fn div_assign(&mut self, other: i8)
Documentation at Q::div_assign.
Source§impl DivAssign<u16> for Q
impl DivAssign<u16> for Q
Source§fn div_assign(&mut self, other: u16)
fn div_assign(&mut self, other: u16)
Documentation at Q::div_assign.
Source§impl DivAssign<u32> for Q
impl DivAssign<u32> for Q
Source§fn div_assign(&mut self, other: u32)
fn div_assign(&mut self, other: u32)
Documentation at Q::div_assign.
Source§impl DivAssign<u64> for Q
impl DivAssign<u64> for Q
Source§fn div_assign(&mut self, other: u64)
fn div_assign(&mut self, other: u64)
Documentation at Q::div_assign.
Source§impl DivAssign<u8> for Q
impl DivAssign<u8> for Q
Source§fn div_assign(&mut self, other: u8)
fn div_assign(&mut self, other: u8)
Documentation at Q::div_assign.
Source§impl DivAssign for Q
impl DivAssign for Q
Source§fn div_assign(&mut self, other: Q)
fn div_assign(&mut self, other: Q)
Documentation at Q::div_assign.
Source§impl Drop for Q
impl Drop for Q
Source§fn drop(&mut self)
fn drop(&mut self)
Drops the given reference to the fmpq element
and frees the allocated memory if no references are left.
§Examples
use qfall_math::rational::Q;
use std::str::FromStr;
{
let a = Q::from((3, 4));
} // as a's scope ends here, it get's droppeduse qfall_math::rational::Q;
use std::str::FromStr;
let a = Q::from((3, 4));
drop(a); // explicitly drops a's valueSource§impl<Rational: Into<Q>> Evaluate<Rational, Q> for PolyOverQ
impl<Rational: Into<Q>> Evaluate<Rational, Q> for PolyOverQ
Source§fn evaluate(&self, value: Rational) -> Q
fn evaluate(&self, value: Rational) -> Q
Evaluates a PolyOverQ on a given input.
Parameters:
value: the value with which to evaluate the polynomial.
Returns the evaluation of the polynomial as a Q.
§Examples
use qfall_math::traits::*;
use qfall_math::rational::Q;
use qfall_math::rational::PolyOverQ;
use std::str::FromStr;
let poly = PolyOverQ::from_str("5 0 1 2/3 -3/2 1").unwrap();
let value = Q::from((3, 2));
let res = poly.evaluate(&value);Source§impl<Rational: Into<Q>> Evaluate<Rational, Q> for PolyOverZ
impl<Rational: Into<Q>> Evaluate<Rational, Q> for PolyOverZ
Source§fn evaluate(&self, value: Rational) -> Q
fn evaluate(&self, value: Rational) -> Q
Evaluates a PolyOverZ on a given input.
Parameters:
value: the value with which to evaluate the polynomial.
Returns the evaluation of the polynomial as a Q.
§Examples
use qfall_math::traits::*;
use qfall_math::rational::Q;
use qfall_math::integer::PolyOverZ;
use std::str::FromStr;
let poly = PolyOverZ::from_str("5 0 1 2 -3 1").unwrap();
let value = Q::from((3, 2));
let res: Q = poly.evaluate(&value);Source§impl From<&Q> for String
impl From<&Q> for String
Source§fn from(value: &Q) -> Self
fn from(value: &Q) -> Self
Converts a Q into its String representation.
Parameters:
value: specifies the rational that will be represented as aString
Returns a String of the form "x/y".
§Examples
use qfall_math::rational::Q;
use std::str::FromStr;
let rational = Q::from_str("6/7").unwrap();
let string: String = rational.into();Source§impl From<&Q> for f64
impl From<&Q> for f64
Source§fn from(value: &Q) -> Self
fn from(value: &Q) -> Self
Convert a rational Q into an f64.
The value is rounded to the closest f64 representation.
WARNING: The return is system dependent if self is
is too large or too small to fit in an f64, i.e. the value should be within
f64::MIN and f64::MAX. It the entry can’t be represented exactly, it will
be rounded towards zero.
WARNING: Please be aware that the deviation of the representation of self as a f64
will scale with the size of self, e.g. a value within the size of 2^{64}
might deviate from the original value by a distance of 1_000.
§Examples
use qfall_math::rational::Q;
let one_half = Q::from((1, 2));
let float = f64::from(&one_half);
assert_eq!(0.5, float);Source§impl<IntegerNumerator: AsInteger, IntegerDenominator: AsInteger> From<(IntegerNumerator, IntegerDenominator)> for Q
impl<IntegerNumerator: AsInteger, IntegerDenominator: AsInteger> From<(IntegerNumerator, IntegerDenominator)> for Q
Source§fn from((num, den): (IntegerNumerator, IntegerDenominator)) -> Self
fn from((num, den): (IntegerNumerator, IntegerDenominator)) -> Self
Creates a Q from two integers.
Parameters:
num: the value of the numerator.den: the value of the denominator.
Returns a Q.
§Examples
use qfall_math::rational::Q;
use qfall_math::integer::Z;
let a = Q::from((42, &2));
let b = Q::from((Z::from(84), 4));
assert_eq!(a, b);§Panics …
- if the denominator is zero.
Source§impl From<f64> for Q
impl From<f64> for Q
Source§fn from(value: f64) -> Self
fn from(value: f64) -> Self
Create a new rational number of type Q from a f64.
This function works with the bit representation of the float it received as input.
Floats like 0.1 that are not completely representable,
will not be instantiated as 1/10.
Input parameters:
value: the value the rational number will have, provided as af64
Returns a Q.
§Examples
use qfall_math::rational::Q;
let a: Q = Q::from(0.3);
let a: Q = Q::from(-123.4567);Source§impl FromStr for Q
impl FromStr for Q
Source§fn from_str(s: &str) -> Result<Self, MathError>
fn from_str(s: &str) -> Result<Self, MathError>
Creates a Q rational from a String
In the string should be two decimal numbers separated by /.
Optionally, before one or both of them can be a -.
The format of that string looks like this -12/53.
If the number is an integer, the string can be in the format of one.
The format of that string looks like this -12.
It is automatically transformed to -12/1.
Parameters:
s: the rational value
Returns a Q or an error if the provided string was not formatted
correctly, contained a Null byte, or the denominator was 0.
§Examples
use std::str::FromStr;
use qfall_math::rational::Q;
let a: Q = "100/3".parse().unwrap();
let b: Q = Q::from_str("100/3").unwrap();use std::str::FromStr;
use qfall_math::rational::Q;
let q: Q = Q::from_str("-10/3").unwrap();
let b: Q = Q::from_str("10/-3").unwrap();use std::str::FromStr;
use qfall_math::rational::Q;
let q: Q = Q::from_str("-10").unwrap();
let b: Q = Q::from_str("10").unwrap();§Errors and Failures
- Returns a
MathErrorof typeStringConversionError- if the provided string contains a
Nullbyte, or - if the provided string was not formatted correctly.
- if the provided string contains a
- Returns a
MathErrorof typeDivisionByZeroErrorif the provided string has0as the denominator.
Source§impl GetCoefficient<Q> for PolyOverQ
impl GetCoefficient<Q> for PolyOverQ
Source§unsafe fn get_coeff_unchecked(&self, index: i64) -> Q
unsafe fn get_coeff_unchecked(&self, index: i64) -> Q
Returns the coefficient of a polynomial PolyOverQ as a Q.
If an index is provided which exceeds the highest set coefficient, 0 is returned.
Parameters:
index: the index of the coefficient to get (has to be positive)
Returns the coefficient as a Q, or a MathError if the provided index
is negative and therefore invalid, or it does not fit into an i64.
§Examples
use qfall_math::rational::{Q, PolyOverQ};
use std::str::FromStr;
use qfall_math::traits::*;
let poly = PolyOverQ::from_str("4 0 1 2/3 3/2").unwrap();
let coeff_0 = poly.get_coeff(0).unwrap();
let coeff_1 = unsafe{ poly.get_coeff_unchecked(1) };
let coeff_4 = poly.get_coeff(4).unwrap();
assert_eq!(Q::ZERO, coeff_0);
assert_eq!(Q::ONE, coeff_1);
assert_eq!(Q::ZERO, coeff_4);§Safety
To use this function safely, make sure that the selected index
is greater or equal than 0.
Source§impl MatrixGetEntry<Q> for MatQ
impl MatrixGetEntry<Q> for MatQ
Source§unsafe fn get_entry_unchecked(&self, row: i64, column: i64) -> Q
unsafe fn get_entry_unchecked(&self, row: i64, column: i64) -> Q
Outputs the Q value of a specific matrix entry
without checking whether it’s part of the matrix.
Parameters:
row: specifies the row in which the entry is locatedcolumn: specifies the column in which the entry is located
Returns the Q value of the matrix at the position of the given
row and column.
§Safety
To use this function safely, make sure that the selected entry is part of the matrix. If it is not, memory leaks, unexpected panics, etc. might occur.
§Examples
use qfall_math::rational::{MatQ, Q};
use qfall_math::traits::MatrixGetEntry;
use std::str::FromStr;
let matrix = MatQ::from_str("[[1, 2, 3/4],[4, 5, 6],[7, 8, 9]]").unwrap();
assert_eq!(unsafe { matrix.get_entry_unchecked(0, 2) }, Q::from((3, 4)));
assert_eq!(unsafe { matrix.get_entry_unchecked(2, 1) }, Q::from(8));
assert_eq!(unsafe { matrix.get_entry_unchecked(2, 1) }, Q::from(8));Source§fn get_entry(
&self,
row: impl TryInto<i64> + Display,
column: impl TryInto<i64> + Display,
) -> Result<T, MathError>
fn get_entry( &self, row: impl TryInto<i64> + Display, column: impl TryInto<i64> + Display, ) -> Result<T, MathError>
Source§fn get_entries(&self) -> Vec<Vec<T>>
fn get_entries(&self) -> Vec<Vec<T>>
Vec<Vec<T>> containing all entries of the matrix s.t.
any entry in row i and column j can be accessed via entries[i][j]
if entries = matrix.get_entries. Read moreSource§fn get_entries_rowwise(&self) -> Vec<T>
fn get_entries_rowwise(&self) -> Vec<T>
Source§impl Mul<&Q> for &MatQ
impl Mul<&Q> for &MatQ
Source§fn mul(self, scalar: &Q) -> Self::Output
fn mul(self, scalar: &Q) -> Self::Output
Implements the Mul trait for a MatQ matrix with a Q rational.
Mul is implemented for any combination of owned and borrowed values.
Parameters:
scalar: specifies the scalar by which the matrix is multiplied
Returns the product of self and scalar as a MatQ.
§Examples
use qfall_math::rational::MatQ;
use qfall_math::rational::Q;
use std::str::FromStr;
let mat_1 = MatQ::from_str("[[2/3, 1],[1/2, 2]]").unwrap();
let rational = Q::from(3/7);
let mat_2 = &mat_1 * &rational;Source§impl Mul<&Q> for &MatZ
impl Mul<&Q> for &MatZ
Source§fn mul(self, scalar: &Q) -> Self::Output
fn mul(self, scalar: &Q) -> Self::Output
Implements the Mul trait for a MatZ matrix with a Q rational.
Mul is implemented for any combination of owned and borrowed values.
Parameters:
scalar: specifies the scalar by which the matrix is multiplied
Returns the product of self and scalar as a MatQ.
§Examples
use qfall_math::integer::MatZ;
use qfall_math::rational::Q;
use std::str::FromStr;
let mat_1 = MatZ::from_str("[[2, 1],[1, 2]]").unwrap();
let rational = Q::from((1,3));
let mat_2 = &mat_1 * &rational;Source§impl Mul<&Q> for &PolyOverQ
impl Mul<&Q> for &PolyOverQ
Source§fn mul(self, scalar: &Q) -> Self::Output
fn mul(self, scalar: &Q) -> Self::Output
Implements the Mul trait for a PolyOverQ with a Q rational.
Mul is implemented for any combination of owned and borrowed values.
Mul is also implemented for Q using PolyOverQ.
Parameters:
scalar: specifies the scalar by which the polynomial is multiplied
Returns the product of self and scalar as a PolyOverQ.
§Examples
use qfall_math::rational::{PolyOverQ, Q};
use std::str::FromStr;
let poly_1 = PolyOverQ::from_str("4 1/2 2 3/4 4").unwrap();
let rational = Q::from((2,3));
let poly_2 = &poly_1 * &rational;Source§impl Mul<&Q> for &PolyOverZ
impl Mul<&Q> for &PolyOverZ
Source§fn mul(self, scalar: &Q) -> PolyOverQ
fn mul(self, scalar: &Q) -> PolyOverQ
Implements the Mul trait for a PolyOverZ with a Q.
Mul is implemented for any combination of owned and borrowed values.
Mul is also implemented for Q using PolyOverZ.
Parameters:
scalar: specifies the scalar by which the polynomial is multiplied
Returns the product of self and scalar as a PolyOverQ.
§Examples
use qfall_math::integer::PolyOverZ;
use qfall_math::rational::Q;
use std::str::FromStr;
let poly_1 = PolyOverZ::from_str("4 1 2 3 4").unwrap();
let rational = Q::from((3,2));
let poly_2 = &poly_1 * &rational;Source§impl Mul<&Q> for &Z
impl Mul<&Q> for &Z
Source§fn mul(self, other: &Q) -> Self::Output
fn mul(self, other: &Q) -> Self::Output
Implements the Mul trait for Z and Q values.
Mul is implemented for any combination of owned and borrowed values.
Parameters:
other: specifies the value to multiply withself
Returns the product of both numbers as a Q.
§Examples
use qfall_math::rational::Q;
use qfall_math::integer::Z;
use std::str::FromStr;
let a: Z = Z::from(-42);
let b: Q = Q::from((42, 19));
let c: Q = &a * &b;
let d: Q = a * b;
let e: Q = &Z::from(42) * d;
let f: Q = Z::from(42) * &e;Source§impl Mul<&Z> for &Q
impl Mul<&Z> for &Q
Source§fn mul(self, other: &Z) -> Self::Output
fn mul(self, other: &Z) -> Self::Output
Implements the Mul trait for Q and Z values.
Mul is implemented for any combination of owned and borrowed values.
Parameters:
other: specifies the value to multiply withself
Returns the product of both numbers as a Q.
§Examples
use qfall_math::rational::Q;
use qfall_math::integer::Z;
use std::str::FromStr;
let a: Q = Q::from((42, 19));
let b: Z = Z::from(-42);
let c: Q = &a * &b;
let d: Q = a * b;
let e: Q = &c * Z::from(42);
let f: Q = c * &Z::from(42);Source§impl Mul for &Q
impl Mul for &Q
Source§fn mul(self, other: Self) -> Self::Output
fn mul(self, other: Self) -> Self::Output
Implements the Mul trait for two Q values.
Mul is implemented for any combination of Q and borrowed Q.
Parameters:
other: specifies the value to multiply withself
Returns the product of both numbers as a Q.
§Examples
use qfall_math::rational::Q;
use std::str::FromStr;
let a: Q = Q::from(42);
let b: Q = Q::from(24);
let c: Q = &a * &b;
let d: Q = a * b;
let e: Q = &c * d;
let f: Q = c * &e;Source§impl MulAssign<&Q> for MatQ
impl MulAssign<&Q> for MatQ
Source§fn mul_assign(&mut self, scalar: &Q)
fn mul_assign(&mut self, scalar: &Q)
Computes the scalar multiplication of self and other reusing
the memory of self.
Parameters:
other: specifies the value to multiply toself
Returns the scalar of the matrix as a MatQ.
§Examples
use qfall_math::integer::Z;
use qfall_math::rational::{MatQ, Q};
use std::str::FromStr;
let mut a = MatQ::from_str("[[2, 1],[-1, 2/7]]").unwrap();
let b = Z::from(2);
let c = Q::from((2,5));
a *= &b;
a *= b;
a *= &c;
a *= c;
a *= 2;
a *= -2;Source§impl MulAssign<&Q> for PolyOverQ
impl MulAssign<&Q> for PolyOverQ
Source§fn mul_assign(&mut self, scalar: &Q)
fn mul_assign(&mut self, scalar: &Q)
Computes the scalar multiplication of self and other reusing
the memory of self.
Parameters:
other: specifies the value to multiply toself
Returns the scalar of the polynomial as a PolyOverQ.
§Examples
use qfall_math::integer::Z;
use qfall_math::rational::{PolyOverQ, Q};
use std::str::FromStr;
let mut a = PolyOverQ::from_str("3 1 2 -3/2").unwrap();
let b = Q::from((2,5));
let c = Z::from(2);
a *= &b;
a *= b;
a *= &c;
a *= c;
a *= 2;
a *= -2;Source§impl MulAssign<&Q> for Q
impl MulAssign<&Q> for Q
Source§fn mul_assign(&mut self, other: &Self)
fn mul_assign(&mut self, other: &Self)
Computes the multiplication of self and other reusing
the memory of self.
MulAssign can be used on Q in combination with
Q, Z, f64, f32, i64, i32, i16, i8, u64, u32, u16 and u8.
Parameters:
other: specifies the value to multiply toself
Returns the product of both rationals as a Q.
§Examples
use qfall_math::{rational::Q, integer::Z};
let mut a: Q = Q::from(42);
let b: Q = Q::from((-42, 2));
let c: Z = Z::from(5);
a *= &b;
a *= b;
a *= 5;
a *= c;
a *= 5.0;Source§impl MulAssign<&Z> for Q
impl MulAssign<&Z> for Q
Source§fn mul_assign(&mut self, other: &Z)
fn mul_assign(&mut self, other: &Z)
Documentation at Q::mul_assign.
Source§impl MulAssign<Q> for MatQ
impl MulAssign<Q> for MatQ
Source§fn mul_assign(&mut self, other: Q)
fn mul_assign(&mut self, other: Q)
Documentation at MatQ::mul_assign.
Source§impl MulAssign<Q> for PolyOverQ
impl MulAssign<Q> for PolyOverQ
Source§fn mul_assign(&mut self, other: Q)
fn mul_assign(&mut self, other: Q)
Documentation at PolyOverQ::mul_assign.
Source§impl MulAssign<Z> for Q
impl MulAssign<Z> for Q
Source§fn mul_assign(&mut self, other: Z)
fn mul_assign(&mut self, other: Z)
Documentation at Q::mul_assign.
Source§impl MulAssign<f32> for Q
impl MulAssign<f32> for Q
Source§fn mul_assign(&mut self, other: f32)
fn mul_assign(&mut self, other: f32)
Documentation at Q::mul_assign.
Source§impl MulAssign<f64> for Q
impl MulAssign<f64> for Q
Source§fn mul_assign(&mut self, other: f64)
fn mul_assign(&mut self, other: f64)
Documentation at Q::mul_assign.
Source§impl MulAssign<i16> for Q
impl MulAssign<i16> for Q
Source§fn mul_assign(&mut self, other: i16)
fn mul_assign(&mut self, other: i16)
Documentation at Q::mul_assign.
Source§impl MulAssign<i32> for Q
impl MulAssign<i32> for Q
Source§fn mul_assign(&mut self, other: i32)
fn mul_assign(&mut self, other: i32)
Documentation at Q::mul_assign.
Source§impl MulAssign<i64> for Q
impl MulAssign<i64> for Q
Source§fn mul_assign(&mut self, other: i64)
fn mul_assign(&mut self, other: i64)
Documentation at Q::mul_assign.
Source§impl MulAssign<i8> for Q
impl MulAssign<i8> for Q
Source§fn mul_assign(&mut self, other: i8)
fn mul_assign(&mut self, other: i8)
Documentation at Q::mul_assign.
Source§impl MulAssign<u16> for Q
impl MulAssign<u16> for Q
Source§fn mul_assign(&mut self, other: u16)
fn mul_assign(&mut self, other: u16)
Documentation at Q::mul_assign.
Source§impl MulAssign<u32> for Q
impl MulAssign<u32> for Q
Source§fn mul_assign(&mut self, other: u32)
fn mul_assign(&mut self, other: u32)
Documentation at Q::mul_assign.
Source§impl MulAssign<u64> for Q
impl MulAssign<u64> for Q
Source§fn mul_assign(&mut self, other: u64)
fn mul_assign(&mut self, other: u64)
Documentation at Q::mul_assign.
Source§impl MulAssign<u8> for Q
impl MulAssign<u8> for Q
Source§fn mul_assign(&mut self, other: u8)
fn mul_assign(&mut self, other: u8)
Documentation at Q::mul_assign.
Source§impl MulAssign for Q
impl MulAssign for Q
Source§fn mul_assign(&mut self, other: Q)
fn mul_assign(&mut self, other: Q)
Documentation at Q::mul_assign.
Source§impl Ord for Q
Enables the usage of max, min, and clamp.
impl Ord for Q
Enables the usage of max, min, and clamp.
§Examples
use qfall_math::rational::Q;
use std::cmp::{max, min};
let a: Q = Q::from(10);
let b: Q = Q::from(42);
assert_eq!(b, max(a.clone(), b.clone()));
assert_eq!(a, min(a.clone(), b.clone()));
assert_eq!(a, Q::ZERO.clamp(a.clone(), b.clone()));Source§fn cmp(&self, other: &Self) -> Ordering
fn cmp(&self, other: &Self) -> Ordering
Compares two Q values. Used by the <, <=, >, and >= operators.
Parameters:
other: the other value that is used to compare the elements
Returns the Ordering of the elements.
§Examples
use qfall_math::rational::Q;
let a: Q = Q::from(10);
let b: Q = Q::from(42);
assert!(a < b);
assert!(a <= b);
assert!(b > a);
assert!(b >= a);1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq<Modulus> for Q
impl PartialEq<Modulus> for Q
Source§fn eq(&self, other: &Modulus) -> bool
fn eq(&self, other: &Modulus) -> bool
Checks if an integer and a rational are equal. Used by the == and != operators.
PartialEq is also implemented for Modulus using Q.
Parameters:
other: the other value that is used to compare the elements
Returns true if the elements are equal, otherwise false.
§Examples
use qfall_math::integer_mod_q::Modulus;
use qfall_math::rational::Q;
let a: Q = Q::from(42);
let b: Modulus = Modulus::from(42);
// These are all equivalent and return true.
let compared: bool = (a == b);
let compared: bool = (b == a);
let compared: bool = (&a == &b);
let compared: bool = (&b == &a);
let compared: bool = (a.eq(&b));
let compared: bool = (b.eq(&a));
let compared: bool = (Q::eq(&a, &b));
let compared: bool = (Modulus::eq(&b, &a));Source§impl PartialEq<Z> for Q
impl PartialEq<Z> for Q
Source§fn eq(&self, other: &Z) -> bool
fn eq(&self, other: &Z) -> bool
Checks if an integer and a rational are equal. Used by the == and != operators.
PartialEq is also implemented for Z using Q.
Parameters:
other: the other value that is used to compare the elements
Returns true if the elements are equal, otherwise false.
§Examples
use qfall_math::integer::Z;
use qfall_math::rational::Q;
let a: Q = Q::from(42);
let b: Z = Z::from(42);
// These are all equivalent and return true.
let compared: bool = (a == b);
let compared: bool = (b == a);
let compared: bool = (&a == &b);
let compared: bool = (&b == &a);
let compared: bool = (a.eq(&b));
let compared: bool = (b.eq(&a));
let compared: bool = (Q::eq(&a, &b));
let compared: bool = (Z::eq(&b, &a));Source§impl PartialEq for Q
impl PartialEq for Q
Source§fn eq(&self, other: &Self) -> bool
fn eq(&self, other: &Self) -> bool
Checks if two rationals are equal. Used by the == and != operators.
Parameters:
other: the other value that is used to compare the elements
Returns true if the elements are equal, otherwise false.
§Examples
use qfall_math::rational::Q;
use std::str::FromStr;
let a: Q = Q::from((42, 24));
let b: Q = Q::from((24, 42));
// These are all equivalent and return false.
let compared: bool = (a == b);
let compared: bool = (&a == &b);
let compared: bool = (a.eq(&b));
let compared: bool = (Q::eq(&a, &b));Source§impl PartialOrd for Q
impl PartialOrd for Q
Source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
Compares two Q values. Used by the <, <=, >, and >= operators.
Parameters:
other: the other value that is used to compare the elements
Returns the Ordering of the elements.
§Examples
use qfall_math::rational::Q;
let a: Q = Q::from((1, 10));
let b: Q = Q::from((2, 10));
assert!(a < b);
assert!(a <= b);
assert!(b > a);
assert!(b >= a);
assert!(&a < &b);Source§impl<Integer: Into<Z>> Pow<Integer> for Q
impl<Integer: Into<Z>> Pow<Integer> for Q
Source§fn pow(&self, exp: Integer) -> Result<Self::Output, MathError>
fn pow(&self, exp: Integer) -> Result<Self::Output, MathError>
Raises the value of self to the power of an integer exp.
Parameters:
exp: specifies the exponent to which the value is raised
Returns the value of self powered by exp as a new Q instance
or a MathError, if the exponent is negative and the base value of self is not invertible.
§Examples
use qfall_math::{rational::Q, integer::Z};
use qfall_math::traits::*;
let base = Q::from(3);
let powered_value = base.pow(-2).unwrap();
assert_eq!(Q::from((1, 9)), powered_value);§Errors and Failures
- Returns a
MathErrorof typeInvalidExponentif the provided exponent is negative and the base value ofselfis not invertible.
type Output = Q
Source§impl Sub<&Q> for &Z
impl Sub<&Q> for &Z
Source§fn sub(self, other: &Q) -> Self::Output
fn sub(self, other: &Q) -> Self::Output
Implements the Sub trait for Z and Q values.
Sub is implemented for any combination of owned and borrowed values.
Parameters:
other: specifies the value to subtract fromself
Returns the result of the subtraction as a Q.
§Examples
use qfall_math::rational::Q;
use qfall_math::integer::Z;
use std::str::FromStr;
let a: Z = Z::from(-42);
let b: Q = Q::from((42, 19));
let c: Q = &a - &b;
let d: Q = a - b;
let e: Q = &Z::from(42) - d;
let f: Q = Z::from(42) - &e;Source§impl Sub<&Z> for &Q
impl Sub<&Z> for &Q
Source§fn sub(self, other: &Z) -> Self::Output
fn sub(self, other: &Z) -> Self::Output
Implements the Sub trait for Q and Z values.
Sub is implemented for any combination of owned and borrowed values.
Parameters:
other: specifies the value to subtract fromself
Returns the result of the subtraction of both numbers as a Q.
§Examples
use qfall_math::rational::Q;
use qfall_math::integer::Z;
use std::str::FromStr;
let a: Q = Q::from((42, 19));
let b: Z = Z::from(42);
let c: Q = &a - &b;
let d: Q = a - b;
let e: Q = &c - Z::from(42);
let f: Q = c - &Z::from(42);Source§impl Sub for &Q
impl Sub for &Q
Source§fn sub(self, other: Self) -> Self::Output
fn sub(self, other: Self) -> Self::Output
Implements the Sub trait for two Q values.
Sub is implemented for any combination of Q and borrowed Q.
Parameters:
other: specifies the value to subtract fromself
Returns the result of the subtraction as a Q.
§Examples
use qfall_math::rational::Q;
use std::str::FromStr;
let a: Q = Q::from(42);
let b: Q = Q::from((-42, 2));
let c: Q = &a - &b;
let d: Q = a - b;
let e: Q = &c - d;
let f: Q = c - &e;Source§impl SubAssign<&Q> for Q
impl SubAssign<&Q> for Q
Source§fn sub_assign(&mut self, other: &Self)
fn sub_assign(&mut self, other: &Self)
Computes the subtraction of self and other reusing
the memory of self.
SubAssign can be used on Q in combination with
Q, Z, f64, f32, i64, i32, i16, i8, u64, u32, u16 and u8.
Parameters:
other: specifies the value to subtract fromself
Returns the difference of both rationals as a Q.
§Examples
use qfall_math::{rational::Q, integer::Z};
let mut a: Q = Q::from(42);
let b: Q = Q::from((-42, 2));
let c: Z = Z::from(5);
a -= &b;
a -= b;
a -= 5;
a -= c;
a -= 5.0;Source§impl SubAssign<&Z> for Q
impl SubAssign<&Z> for Q
Source§fn sub_assign(&mut self, other: &Z)
fn sub_assign(&mut self, other: &Z)
Documentation at Q::sub_assign.
Source§impl SubAssign<Z> for Q
impl SubAssign<Z> for Q
Source§fn sub_assign(&mut self, other: Z)
fn sub_assign(&mut self, other: Z)
Documentation at Q::sub_assign.
Source§impl SubAssign<f32> for Q
impl SubAssign<f32> for Q
Source§fn sub_assign(&mut self, other: f32)
fn sub_assign(&mut self, other: f32)
Documentation at Q::sub_assign.
Source§impl SubAssign<f64> for Q
impl SubAssign<f64> for Q
Source§fn sub_assign(&mut self, other: f64)
fn sub_assign(&mut self, other: f64)
Documentation at Q::sub_assign.
Source§impl SubAssign<i16> for Q
impl SubAssign<i16> for Q
Source§fn sub_assign(&mut self, other: i16)
fn sub_assign(&mut self, other: i16)
Documentation at Q::sub_assign.
Source§impl SubAssign<i32> for Q
impl SubAssign<i32> for Q
Source§fn sub_assign(&mut self, other: i32)
fn sub_assign(&mut self, other: i32)
Documentation at Q::sub_assign.
Source§impl SubAssign<i64> for Q
impl SubAssign<i64> for Q
Source§fn sub_assign(&mut self, other: i64)
fn sub_assign(&mut self, other: i64)
Documentation at Q::sub_assign.
Source§impl SubAssign<i8> for Q
impl SubAssign<i8> for Q
Source§fn sub_assign(&mut self, other: i8)
fn sub_assign(&mut self, other: i8)
Documentation at Q::sub_assign.
Source§impl SubAssign<u16> for Q
impl SubAssign<u16> for Q
Source§fn sub_assign(&mut self, other: u16)
fn sub_assign(&mut self, other: u16)
Documentation at Q::sub_assign.
Source§impl SubAssign<u32> for Q
impl SubAssign<u32> for Q
Source§fn sub_assign(&mut self, other: u32)
fn sub_assign(&mut self, other: u32)
Documentation at Q::sub_assign.
Source§impl SubAssign<u64> for Q
impl SubAssign<u64> for Q
Source§fn sub_assign(&mut self, other: u64)
fn sub_assign(&mut self, other: u64)
Documentation at Q::sub_assign.
Source§impl SubAssign<u8> for Q
impl SubAssign<u8> for Q
Source§fn sub_assign(&mut self, other: u8)
fn sub_assign(&mut self, other: u8)
Documentation at Q::sub_assign.
Source§impl SubAssign for Q
impl SubAssign for Q
Source§fn sub_assign(&mut self, other: Q)
fn sub_assign(&mut self, other: Q)
Documentation at Q::sub_assign.