Q

Struct Q 

Source
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

Source

pub fn div_safe(&self, divisor: &Q) -> Result<Q, MathError>

Implements division for two borrowed Q values.

Parameters:

  • divisor: specifies the value self is 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
Source§

impl Q

Source

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.
Source

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

Source

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
Source

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
Source§

impl Q

Source

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.
Source

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:

  • precision specifies 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
Source§

impl Q

Source

pub const ONE: Q

Returns an instantiation of Q with value 1.

§Examples
use qfall_math::rational::Q;
  
let a: Q = Q::ONE;
Source

pub const ZERO: Q

Returns an instantiation of Q with value 0.

§Examples
use qfall_math::rational::Q;
  
let a: Q = Q::ZERO;
Source

pub const MINUS_ONE: Q

Returns an instantiation of Q with value -1.

§Examples
use qfall_math::rational::Q;
  
let a: Q = Q::MINUS_ONE;
Source

pub const E: Q

Returns an instantiation of Q with value e ≈ 2.718281... with a precision of ~ 10^-36.

§Examples
use qfall_math::rational::Q;
  
let a: Q = Q::E;
Source

pub const PI: Q

Returns an instantiation of Q with value pi ≈ 3.141592... with a precision of ~ 10^-37.

§Examples
use qfall_math::rational::Q;
  
let a: Q = Q::PI;
Source

pub const MAX62: Q

Returns an instantiation of Q with value 2^62 - 1.

§Examples
use qfall_math::rational::Q;
  
let a: Q = Q::MAX62;
Source

pub const INV_MAX62: Q

Returns an instantiation of Q with value 1 / (2^62 - 1).

§Examples
use qfall_math::rational::Q;
  
let a: Q = Q::INV_MAX62;
Source

pub const MAX32: Q

Returns an instantiation of Q with value 2^32 - 1.

§Examples
use qfall_math::rational::Q;
  
let a: Q = Q::MAX32;
Source

pub const INV_MAX32: Q

Returns an instantiation of Q with value 1 / (2^32 - 1).

§Examples
use qfall_math::rational::Q;
  
let a: Q = Q::INV_MAX32;
Source

pub const MAX16: Q

Returns an instantiation of Q with value 2^16 - 1.

§Examples
use qfall_math::rational::Q;
  
let a: Q = Q::MAX16;
Source

pub const INV_MAX16: Q

Returns an instantiation of Q with value 1 / (2^16 - 1).

§Examples
use qfall_math::rational::Q;
  
let a: Q = Q::INV_MAX16;
Source

pub const MAX8: Q

Returns an instantiation of Q with value 2^8 - 1.

§Examples
use qfall_math::rational::Q;
  
let a: Q = Q::MAX8;
Source

pub const INV_MAX8: Q

Returns an instantiation of Q with value 1 / (2^8 - 1).

§Examples
use qfall_math::rational::Q;
  
let a: Q = Q::INV_MAX8;
Source§

impl Q

Source

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 a f64

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

pub fn from_f32(value: f32) -> Q

Convert f32 to Q using Q::from_f64.

Source§

impl Q

Source

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));
Source

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

Source

pub fn abs(self) -> Self

Returns the given Q instance with its absolute value.

§Examples
use qfall_math::rational::Q;
let mut value = Q::from(-1);

let value = value.abs();

assert_eq!(Q::ONE, value);
Source

pub fn inverse(&self) -> Option<Q>

Returns the inverse of self as a fresh Q instance.

As the inverse of 0 is undefined, it returns None in case self == 0.

§Examples
use qfall_math::rational::Q;
let value = Q::from(4);

let inverse = value.inverse().unwrap();

assert_eq!(Q::from((1, 4)), inverse);
Source

pub fn is_zero(&self) -> bool

Checks if a Q is 0.

Returns true if the value is 0.

§Examples
use qfall_math::rational::Q;

let value = Q::ZERO;
assert!(value.is_zero());
Source

pub fn is_one(&self) -> bool

Checks if a Q is 1.

Returns true if the value is 1.

§Examples
use qfall_math::rational::Q;

let value = Q::ONE;
assert!(value.is_one());
Source§

impl Q

Source

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());
Source

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());
Source

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());
Source

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 from self. 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));
Source

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 deviation sigma * 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

This function implements randomized rounding according to:

Source§

impl Q

Source

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 center
  • sigma: 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
Source§

impl Q

Source

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 output String

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

Source

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

Source

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

Source§

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 to self

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§

type Output = Q

The resulting type after applying the + operator.
Source§

impl Add<&Z> for &Q

Source§

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 to self

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§

type Output = Q

The resulting type after applying the + operator.
Source§

impl Add for &Q

Source§

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 to self

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§

type Output = Q

The resulting type after applying the + operator.
Source§

impl AddAssign<&Q> for Q

Source§

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 to self

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

Source§

fn add_assign(&mut self, other: &Z)

Documentation at Q::add_assign.

Source§

impl AddAssign<Z> for Q

Source§

fn add_assign(&mut self, other: Z)

Documentation at Q::add_assign.

Source§

impl AddAssign<f32> for Q

Source§

fn add_assign(&mut self, other: f32)

Documentation at Q::add_assign.

Source§

impl AddAssign<f64> for Q

Source§

fn add_assign(&mut self, other: f64)

Documentation at Q::add_assign.

Source§

impl AddAssign<i16> for Q

Source§

fn add_assign(&mut self, other: i16)

Documentation at Q::add_assign.

Source§

impl AddAssign<i32> for Q

Source§

fn add_assign(&mut self, other: i32)

Documentation at Q::add_assign.

Source§

impl AddAssign<i64> for Q

Source§

fn add_assign(&mut self, other: i64)

Documentation at Q::add_assign.

Source§

impl AddAssign<i8> for Q

Source§

fn add_assign(&mut self, other: i8)

Documentation at Q::add_assign.

Source§

impl AddAssign<u16> for Q

Source§

fn add_assign(&mut self, other: u16)

Documentation at Q::add_assign.

Source§

impl AddAssign<u32> for Q

Source§

fn add_assign(&mut self, other: u32)

Documentation at Q::add_assign.

Source§

impl AddAssign<u64> for Q

Source§

fn add_assign(&mut self, other: u64)

Documentation at Q::add_assign.

Source§

impl AddAssign<u8> for Q

Source§

fn add_assign(&mut self, other: u8)

Documentation at Q::add_assign.

Source§

impl AddAssign for Q

Source§

fn add_assign(&mut self, other: Q)

Documentation at Q::add_assign.

Source§

impl Clone for Q

Source§

fn clone(&self) -> Self

Clones the given element and returns another cloned reference to the fmpq element.

§Examples
use qfall_math::rational::Q;
use std::str::FromStr;

let a = Q::from((3, 4));
let b = a.clone();
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Q

Source§

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

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

impl Default for Q

Source§

fn default() -> Self

Returns an instantiation of Q with value 0/1.

§Examples
use std::default::Default;
use qfall_math::rational::Q;
  
let a: Q = Q::default();
Source§

impl<'de> Deserialize<'de> for Q

Source§

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

Source§

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

Source§

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 the Q values whose distance is calculated to self

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);
Source§

type Output = Q

Source§

impl Div<&Q> for &MatQ

Source§

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 scalar is 0.
Source§

type Output = MatQ

The resulting type after applying the / operator.
Source§

impl Div<&Q> for &PolyOverQ

Source§

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 scalar is 0.
Source§

type Output = PolyOverQ

The resulting type after applying the / operator.
Source§

impl Div<&Q> for &Z

Source§

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 divide self by

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§

type Output = Q

The resulting type after applying the / operator.
Source§

impl Div<&Z> for &Q

Source§

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 divide self by

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§

type Output = Q

The resulting type after applying the / operator.
Source§

impl Div for &Q

Source§

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 value self is 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 other value is 0.
Source§

type Output = Q

The resulting type after applying the / operator.
Source§

impl DivAssign<&Q> for MatQ

Source§

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 to self

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 scalar is 0.
Source§

impl DivAssign<&Q> for PolyOverQ

Source§

fn div_assign(&mut self, scalar: &Q)

Divides the polynomial coefficient-wise.

Parameters:

  • scalar: specifies the value to multiply to self

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 scalar is 0.
Source§

impl DivAssign<&Q> for Q

Source§

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 divide self by

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

Source§

fn div_assign(&mut self, other: &Z)

Documentation at Q::div_assign.

Source§

impl DivAssign<Q> for MatQ

Source§

fn div_assign(&mut self, other: Q)

Documentation at MatQ::div_assign.

Source§

impl DivAssign<Q> for PolyOverQ

Source§

fn div_assign(&mut self, other: Q)

Documentation at PolyOverQ::div_assign.

Source§

impl DivAssign<Z> for Q

Source§

fn div_assign(&mut self, other: Z)

Documentation at Q::div_assign.

Source§

impl DivAssign<f32> for Q

Source§

fn div_assign(&mut self, other: f32)

Documentation at Q::div_assign.

Source§

impl DivAssign<f64> for Q

Source§

fn div_assign(&mut self, other: f64)

Documentation at Q::div_assign.

Source§

impl DivAssign<i16> for Q

Source§

fn div_assign(&mut self, other: i16)

Documentation at Q::div_assign.

Source§

impl DivAssign<i32> for Q

Source§

fn div_assign(&mut self, other: i32)

Documentation at Q::div_assign.

Source§

impl DivAssign<i64> for Q

Source§

fn div_assign(&mut self, other: i64)

Documentation at Q::div_assign.

Source§

impl DivAssign<i8> for Q

Source§

fn div_assign(&mut self, other: i8)

Documentation at Q::div_assign.

Source§

impl DivAssign<u16> for Q

Source§

fn div_assign(&mut self, other: u16)

Documentation at Q::div_assign.

Source§

impl DivAssign<u32> for Q

Source§

fn div_assign(&mut self, other: u32)

Documentation at Q::div_assign.

Source§

impl DivAssign<u64> for Q

Source§

fn div_assign(&mut self, other: u64)

Documentation at Q::div_assign.

Source§

impl DivAssign<u8> for Q

Source§

fn div_assign(&mut self, other: u8)

Documentation at Q::div_assign.

Source§

impl DivAssign for Q

Source§

fn div_assign(&mut self, other: Q)

Documentation at Q::div_assign.

Source§

impl Drop for Q

Source§

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 dropped
use qfall_math::rational::Q;
use std::str::FromStr;

let a = Q::from((3, 4));
drop(a); // explicitly drops a's value
Source§

impl<Rational: Into<Q>> Evaluate<Rational, Q> for PolyOverQ

Source§

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

Source§

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 Q

Source§

fn from(value: &Q) -> Self

An alias for Q::clone. It makes the use of generic Into<Q> types easier.

Source§

impl From<&Q> for String

Source§

fn from(value: &Q) -> Self

Converts a Q into its String representation.

Parameters:

  • value: specifies the rational that will be represented as a String

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

Source§

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

Source§

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<Integer: Into<Z>> From<Integer> for Q

Source§

fn from(value: Integer) -> Self

Creates a Q from a value that implements Into<Z>.

Parameters:

  • value: the initial value the Q should have.

Returns a Q.

§Examples
use qfall_math::rational::Q;
use qfall_math::integer::Z;

let a: Q = Q::from(17);
let b: Q = Q::from(Z::from(17));
Source§

impl From<Q> for String

Source§

fn from(value: Q) -> Self

Documentation can be found at String::from for &Q.

Source§

impl From<f32> for Q

Source§

fn from(value: f32) -> Q

Convert f32 to Q using Q::from_f32.

Source§

impl From<f64> for Q

Source§

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 a f64

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

Source§

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
Source§

type Err = MathError

The associated error which can be returned from parsing.
Source§

impl GetCoefficient<Q> for PolyOverQ

Source§

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§

fn get_coeff(&self, index: impl TryInto<i64> + Display) -> Result<T, MathError>

Returns a coefficient of the given object, e.g. a polynomial, for a given index. Read more
Source§

impl MatrixGetEntry<Q> for MatQ

Source§

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 located
  • column: 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>

Returns the value of a specific matrix entry. Read more
Source§

fn get_entries(&self) -> Vec<Vec<T>>

Outputs a 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 more
Source§

fn get_entries_rowwise(&self) -> Vec<T>

Outputs a Vec<T> containing all entries of the matrix in a row-wise order, i.e. a matrix [[2, 3, 4],[5, 6, 7]] can be accessed via this function in this order [2, 3, 4, 5, 6, 7]. Read more
Source§

fn get_entries_columnwise(&self) -> Vec<T>

Outputs a Vec<T> containing all entries of the matrix in a column-wise order, i.e. a matrix [[2, 3, 4],[5, 6, 7]] can be accessed via this function in this order [2, 5, 3, 6, 4, 7]. Read more
Source§

impl Mul<&Q> for &MatQ

Source§

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§

type Output = MatQ

The resulting type after applying the * operator.
Source§

impl Mul<&Q> for &MatZ

Source§

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§

type Output = MatQ

The resulting type after applying the * operator.
Source§

impl Mul<&Q> for &PolyOverQ

Source§

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§

type Output = PolyOverQ

The resulting type after applying the * operator.
Source§

impl Mul<&Q> for &PolyOverZ

Source§

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§

type Output = PolyOverQ

The resulting type after applying the * operator.
Source§

impl Mul<&Q> for &Z

Source§

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 with self

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§

type Output = Q

The resulting type after applying the * operator.
Source§

impl Mul<&Z> for &Q

Source§

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 with self

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§

type Output = Q

The resulting type after applying the * operator.
Source§

impl Mul for &Q

Source§

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 with self

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§

type Output = Q

The resulting type after applying the * operator.
Source§

impl MulAssign<&Q> for MatQ

Source§

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 to self

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

Source§

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 to self

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

Source§

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 to self

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

Source§

fn mul_assign(&mut self, other: &Z)

Documentation at Q::mul_assign.

Source§

impl MulAssign<Q> for MatQ

Source§

fn mul_assign(&mut self, other: Q)

Documentation at MatQ::mul_assign.

Source§

impl MulAssign<Q> for PolyOverQ

Source§

fn mul_assign(&mut self, other: Q)

Documentation at PolyOverQ::mul_assign.

Source§

impl MulAssign<Z> for Q

Source§

fn mul_assign(&mut self, other: Z)

Documentation at Q::mul_assign.

Source§

impl MulAssign<f32> for Q

Source§

fn mul_assign(&mut self, other: f32)

Documentation at Q::mul_assign.

Source§

impl MulAssign<f64> for Q

Source§

fn mul_assign(&mut self, other: f64)

Documentation at Q::mul_assign.

Source§

impl MulAssign<i16> for Q

Source§

fn mul_assign(&mut self, other: i16)

Documentation at Q::mul_assign.

Source§

impl MulAssign<i32> for Q

Source§

fn mul_assign(&mut self, other: i32)

Documentation at Q::mul_assign.

Source§

impl MulAssign<i64> for Q

Source§

fn mul_assign(&mut self, other: i64)

Documentation at Q::mul_assign.

Source§

impl MulAssign<i8> for Q

Source§

fn mul_assign(&mut self, other: i8)

Documentation at Q::mul_assign.

Source§

impl MulAssign<u16> for Q

Source§

fn mul_assign(&mut self, other: u16)

Documentation at Q::mul_assign.

Source§

impl MulAssign<u32> for Q

Source§

fn mul_assign(&mut self, other: u32)

Documentation at Q::mul_assign.

Source§

impl MulAssign<u64> for Q

Source§

fn mul_assign(&mut self, other: u64)

Documentation at Q::mul_assign.

Source§

impl MulAssign<u8> for Q

Source§

fn mul_assign(&mut self, other: u8)

Documentation at Q::mul_assign.

Source§

impl MulAssign for Q

Source§

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.

§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

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) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

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

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

impl PartialEq<Modulus> for Q

Source§

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));
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<Z> for Q

Source§

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));
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for Q

Source§

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));
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Q

Source§

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);
1.0.0 · 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 · 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 · 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 · 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<Integer: Into<Z>> Pow<Integer> for Q

Source§

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 MathError of type InvalidExponent if the provided exponent is negative and the base value of self is not invertible.
Source§

type Output = Q

Source§

impl Serialize for Q

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Implements the serialize option. This allows to create a Json-object from a given Q.

Source§

impl Sub<&Q> for &Z

Source§

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 from self

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§

type Output = Q

The resulting type after applying the - operator.
Source§

impl Sub<&Z> for &Q

Source§

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 from self

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§

type Output = Q

The resulting type after applying the - operator.
Source§

impl Sub for &Q

Source§

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 from self

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§

type Output = Q

The resulting type after applying the - operator.
Source§

impl SubAssign<&Q> for Q

Source§

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 from self

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

Source§

fn sub_assign(&mut self, other: &Z)

Documentation at Q::sub_assign.

Source§

impl SubAssign<Z> for Q

Source§

fn sub_assign(&mut self, other: Z)

Documentation at Q::sub_assign.

Source§

impl SubAssign<f32> for Q

Source§

fn sub_assign(&mut self, other: f32)

Documentation at Q::sub_assign.

Source§

impl SubAssign<f64> for Q

Source§

fn sub_assign(&mut self, other: f64)

Documentation at Q::sub_assign.

Source§

impl SubAssign<i16> for Q

Source§

fn sub_assign(&mut self, other: i16)

Documentation at Q::sub_assign.

Source§

impl SubAssign<i32> for Q

Source§

fn sub_assign(&mut self, other: i32)

Documentation at Q::sub_assign.

Source§

impl SubAssign<i64> for Q

Source§

fn sub_assign(&mut self, other: i64)

Documentation at Q::sub_assign.

Source§

impl SubAssign<i8> for Q

Source§

fn sub_assign(&mut self, other: i8)

Documentation at Q::sub_assign.

Source§

impl SubAssign<u16> for Q

Source§

fn sub_assign(&mut self, other: u16)

Documentation at Q::sub_assign.

Source§

impl SubAssign<u32> for Q

Source§

fn sub_assign(&mut self, other: u32)

Documentation at Q::sub_assign.

Source§

impl SubAssign<u64> for Q

Source§

fn sub_assign(&mut self, other: u64)

Documentation at Q::sub_assign.

Source§

impl SubAssign<u8> for Q

Source§

fn sub_assign(&mut self, other: u8)

Documentation at Q::sub_assign.

Source§

impl SubAssign for Q

Source§

fn sub_assign(&mut self, other: Q)

Documentation at Q::sub_assign.

Source§

impl Eq for Q

Auto Trait Implementations§

§

impl Freeze for Q

§

impl RefUnwindSafe for Q

§

impl Send for Q

§

impl Sync for Q

§

impl Unpin for Q

§

impl UnwindSafe for Q

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<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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,