PolyOverQ

Struct PolyOverQ 

Source
pub struct PolyOverQ { /* private fields */ }
Expand description

PolyOverQ is a type of polynomial with arbitrarily many coefficients of type Q.

§Examples

use qfall_math::rational::{PolyOverQ, Q};
use qfall_math::traits::*;
use std::str::FromStr;

// instantiations
let poly_1 = PolyOverQ::from_str("4  0 1/2 2 3/4").unwrap();
let poly_2 = PolyOverQ::default();

// evaluate function
let value = Q::default();
let res = poly_1.evaluate(&value);

// comparison
assert_ne!(poly_1, poly_2);

Implementations§

Source§

impl PolyOverQ

Source

pub fn dot_product(&self, other: &Self) -> Result<Q, MathError>

Returns the dot product of two polynomials of type PolyOverQ. The dot product for polynomials is obtained by treating the coefficients of the polynomials as vectors and then applying the standard dot product operation.

Parameters:

  • other: specifies the other polynomial the dot product is calculated over

Returns the resulting dot_product as a PolyOverQ.

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

let poly_1 = PolyOverQ::from_str("4  -1/2 0 7/8 1").unwrap();
let poly_2 = PolyOverQ::from_str("1  5/42").unwrap();

let dot_prod = poly_1.dot_product(&poly_2).unwrap();
Source§

impl PolyOverQ

Source

pub fn get_degree(&self) -> i64

Returns the degree of a polynomial PolyOverQ as a i64. The zero polynomial has degree -1.

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

let poly = PolyOverQ::from_str("4  0 1/9 2 -3/4").unwrap();

let degree = poly.get_degree(); // This would only return 3
Source§

impl PolyOverQ

Source

pub fn norm_eucl_sqrd(&self) -> Q

Returns the squared Euclidean norm or squared 2-norm of the given polynomial. The squared Euclidean norm for a polynomial is obtained by treating the coefficients of the polynomial as a vector and then applying the standard squared Euclidean norm.

§Examples
use qfall_math::rational::{PolyOverQ, Q};
use std::str::FromStr;

let poly = PolyOverQ::from_str("3  1/7 2/7 3/7").unwrap();

let sqrd_2_norm = poly.norm_eucl_sqrd();

// (1*1 + 2*2 + 3*3)/49 = 14/49 = 2/7
assert_eq!(Q::from((2, 7)), sqrd_2_norm);
Source§

impl PolyOverQ

Source

pub fn norm_infty(&self) -> Q

Returns the infinity norm or the maximal absolute value of a coefficient of the given polynomial. The infinity norm for a polynomial is obtained by treating the coefficients of the polynomial as a vector and then applying the standard infinity norm.

§Examples
use qfall_math::rational::{PolyOverQ, Q};
use std::str::FromStr;

let poly = PolyOverQ::from_str("3  1/7 2/7 3/7").unwrap();

let infty_norm = poly.norm_infty();

// max coefficient is 3/7
assert_eq!(Q::from((3, 7)), infty_norm);
Source§

impl PolyOverQ

Source

pub fn is_one(&self) -> bool

Checks if a PolyOverQ is the constant polynomial with coefficient 1.

Returns true if there is only one coefficient, which is 1.

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

let value = PolyOverQ::from_str("1  1").unwrap();
assert!(value.is_one());
Source

pub fn is_zero(&self) -> bool

Checks if every entry of a PolyOverQ is 0.

Returns true if PolyOverQ has no coefficients.

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

let value = PolyOverQ::from_str("0").unwrap();
assert!(value.is_zero());
Source§

impl PolyOverQ

Source

pub fn floor(&self) -> PolyOverZ

Rounds all coefficients of the given rational polynomial PolyOverQ down to the next integer as a PolyOverZ.

§Examples
use qfall_math::rational::PolyOverQ;
use qfall_math::integer::PolyOverZ;
use std::str::FromStr;

let value = PolyOverQ::from_str("2  5/2 1").unwrap();
assert_eq!(PolyOverZ::from_str("2  2 1").unwrap(), value.floor());

let value = PolyOverQ::from_str("2  -5/2 1").unwrap();
assert_eq!(PolyOverZ::from_str("2  -3 1").unwrap(), value.floor());
Source

pub fn ceil(&self) -> PolyOverZ

Rounds all coefficients of the given rational polynomial PolyOverQ up to the next integer as a PolyOverZ.

§Examples
use qfall_math::rational::PolyOverQ;
use qfall_math::integer::PolyOverZ;
use std::str::FromStr;

let value = PolyOverQ::from_str("2  5/2 1").unwrap();
assert_eq!(PolyOverZ::from_str("2  3 1").unwrap(), value.ceil());

let value = PolyOverQ::from_str("2  -5/2 1").unwrap();
assert_eq!(PolyOverZ::from_str("2  -2 1").unwrap(), value.ceil());
Source

pub fn round(&self) -> PolyOverZ

Rounds all coefficients of the given rational polynomial PolyOverQ to the closest integer as a PolyOverZ.

§Examples
use qfall_math::rational::PolyOverQ;
use qfall_math::integer::PolyOverZ;
use std::str::FromStr;

let value = PolyOverQ::from_str("2  5/2 1").unwrap();
assert_eq!(PolyOverZ::from_str("2  3 1").unwrap(), value.round());

let value = PolyOverQ::from_str("2  -5/2 1").unwrap();
assert_eq!(PolyOverZ::from_str("2  -2 1").unwrap(), value.round());
Source

pub fn randomized_rounding( &self, r: impl Into<Q>, ) -> Result<PolyOverZ, MathError>

Performs the randomized rounding algorithm coefficient-wise 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 polynomial as a PolyOverZ or an error if r < 0.

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

let value = PolyOverQ::from_str("2  5/2 1").unwrap();
let rounded = value.randomized_rounding(3).unwrap();
§Errors and Failures

This function implements randomized rounding according to:

Source§

impl PolyOverQ

Source

pub fn sample_gauss( max_degree: impl TryInto<i64> + Display, center: impl Into<Q>, sigma: impl Into<f64>, ) -> Result<Self, MathError>

Initializes a new PolyOverQ with maximum degree max_degree and with each coefficient sampled independently according to the Gaussian distribution, using Q::sample_gauss.

Parameters:

  • max_degree: specifies the included maximal degree the created PolyOverQ should have
  • center: specifies the center for each coefficient of the polynomial
  • sigma: specifies the standard deviation

Returns a fresh PolyOverQ instance of maximum degree max_degree with coefficients chosen independently according the Gaussian distribution or a MathError if the specified parameters were not chosen appropriately (sigma > 0).

§Examples
use qfall_math::rational::PolyOverQ;

let sample = PolyOverQ::sample_gauss(2, 0, 1).unwrap();
§Errors and Failures
§Panics …
  • if max_degree is negative, or does not fit into an i64.
Source§

impl PolyOverQ

Source

pub fn to_string_decimal(&self, nr_decimal_digits: usize) -> String

Outputs a representation of PolyOverQ with the decimal representation of each coefficient with the specified number of decimal digits. If a coefficient can’t be represented exactly, it provides the closest value representable with nr_decimal_digits rounded towards zero.

WARNING: This function converts every coefficient into an f64 before outputting the decimal representation. Thus, values that can’t be represented exactly by a 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 the polynomial in form of a String. For polynomial 2 1/2 5/3 the String looks like this 2 0.50 1.66 if nr_decimal_digits = 2.

§Examples
use qfall_math::rational::PolyOverQ;
use std::str::FromStr;
let poly = PolyOverQ::from_str("4  5/2 2 -2/3 4/3").unwrap();

let decimal_repr = poly.to_string_decimal(3);
Source§

impl PolyOverQ

Source

pub unsafe fn get_fmpq_poly_struct(&mut self) -> &mut fmpq_poly_struct

Returns a mutable reference to the field poly of type fmpq_poly_struct.

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 PolyOverQ

Source

pub unsafe fn set_fmpq_poly_struct(&mut self, flint_struct: fmpq_poly_struct)

Sets the field poly of type fmpq_poly_struct 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<&PolyOverZ> for &PolyOverQ

Source§

fn add(self, other: &PolyOverZ) -> Self::Output

Implements the Add trait for PolyOverQ and PolyOverZ. Add is implemented for any combination of owned and borrowed values.

Parameters:

  • other: specifies the polynomial to add to self

Returns the addition of both polynomials as a PolyOverQ.

§Examples
use qfall_math::rational::PolyOverQ;
use qfall_math::integer::PolyOverZ;
use std::str::FromStr;

let a = PolyOverQ::from_str("4  1/2 0 3/7 1").unwrap();
let b = PolyOverZ::from_str("4  2 0 3 1").unwrap();

let c: PolyOverQ = &a + &b;
Source§

type Output = PolyOverQ

The resulting type after applying the + operator.
Source§

impl Add for &PolyOverQ

Source§

fn add(self, other: Self) -> Self::Output

Implements the Add trait for two PolyOverQ values. Add is implemented for any combination of PolyOverQ and borrowed PolyOverQ.

Parameters:

  • other: specifies the value to add to self

Returns the sum of both polynomials as a PolyOverQ.

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

let a: PolyOverQ = PolyOverQ::from_str("3  1/8 2/7 -7").unwrap();
let b: PolyOverQ = PolyOverQ::from_str("5  1/9 2/9 -3/8 0 8/9").unwrap();

let c: PolyOverQ = &a + &b;
let d: PolyOverQ = a + b;
let e: PolyOverQ = &c + d;
let f: PolyOverQ = c + &e;
Source§

type Output = PolyOverQ

The resulting type after applying the + operator.
Source§

impl AddAssign<&PolyOverQ> for PolyOverQ

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 PolyOverQ in combination with PolyOverQ and PolyOverZ.

Parameters:

  • other: specifies the polynomial to add to self

Returns the sum of both polynomials as a PolyOverQ.

§Examples
use qfall_math::{rational::PolyOverQ, integer::PolyOverZ};
use std::str::FromStr;

let mut a = PolyOverQ::from_str("3  1 2/3 -3/4").unwrap();
let b = PolyOverQ::from_str("5  1 2 -3 0 8/9").unwrap();
let c = PolyOverZ::from_str("2  -1 2").unwrap();

a += &b;
a += b;
a += &c;
a += c;
Source§

impl AddAssign<&PolyOverZ> for PolyOverQ

Source§

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

Documentation at PolyOverQ::add_assign.

Source§

impl AddAssign<PolyOverZ> for PolyOverQ

Source§

fn add_assign(&mut self, other: PolyOverZ)

Documentation at PolyOverQ::add_assign.

Source§

impl AddAssign for PolyOverQ

Source§

fn add_assign(&mut self, other: PolyOverQ)

Documentation at PolyOverQ::add_assign.

Source§

impl Clone for PolyOverQ

Source§

fn clone(&self) -> Self

Clones the given PolyOverQ element by returning a deep clone, storing two separately stored fmpz values for nominator and denominator in memory.

§Examples
use qfall_math::rational::PolyOverQ;

let a = PolyOverQ::default();
let b = a.clone();
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl CompareBase<PolyOverZ> for PolyOverQ

Source§

fn compare_base(&self, other: &T) -> bool

Compares the base elements of the objects and returns true if they match and an operation between the two provided types is possible. Read more
Source§

fn call_compare_base_error(&self, other: &T) -> Option<MathError>

Calls an error that gives small explanation how the base elements differ. This function only calls the error and does not check if the two actually differ. Read more
Source§

impl<Rational: Into<Q>> CompareBase<Rational> for PolyOverQ

Source§

fn compare_base(&self, other: &T) -> bool

Compares the base elements of the objects and returns true if they match and an operation between the two provided types is possible. Read more
Source§

fn call_compare_base_error(&self, other: &T) -> Option<MathError>

Calls an error that gives small explanation how the base elements differ. This function only calls the error and does not check if the two actually differ. Read more
Source§

impl CompareBase for PolyOverQ

Source§

fn compare_base(&self, other: &T) -> bool

Compares the base elements of the objects and returns true if they match and an operation between the two provided types is possible. Read more
Source§

fn call_compare_base_error(&self, other: &T) -> Option<MathError>

Calls an error that gives small explanation how the base elements differ. This function only calls the error and does not check if the two actually differ. Read more
Source§

impl Debug for PolyOverQ

Source§

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

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

impl Default for PolyOverQ

Source§

fn default() -> Self

Initializes a PolyOverQ as the zero polynomial.

§Examples
use qfall_math::rational::PolyOverQ;

let zero = PolyOverQ::default();
Source§

impl<'de> Deserialize<'de> for PolyOverQ

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Implements the deserialize option. This allows to create a PolyOverQ from a given Json-object.

Source§

impl Display for PolyOverQ

Source§

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

Allows to convert a polynomial of type PolyOverQ into a String.

§Examples
use qfall_math::rational::PolyOverQ;
use std::str::FromStr;
use core::fmt;

let poly = PolyOverQ::from_str("5  0 1 2/5 -3/2 1").unwrap();
println!("{poly}");
use qfall_math::rational::PolyOverQ;
use std::str::FromStr;

let poly = PolyOverQ::from_str("5  0 1 2/5 -3/2 1").unwrap();
let poly_string = poly.to_string();
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<&Z> for &PolyOverQ

Source§

fn div(self, scalar: &Z) -> Self::Output

Implements the Div trait for a PolyOverQ by a Z integer. 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 qfall_math::integer::Z;
use std::str::FromStr;

let poly_1 = PolyOverQ::from_str("4  1/2 2 3/4 4").unwrap();
let integer = Z::from(2);

&poly_1 / &integer;
&poly_1 / integer;
&poly_1 / 2;
&poly_1 / -2;
§Panics …
  • if scalar is 0.
Source§

type Output = PolyOverQ

The resulting type after applying the / operator.
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<&Z> for PolyOverQ

Source§

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

Documentation at PolyOverQ::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 PolyOverQ

Source§

fn div_assign(&mut self, other: Z)

Documentation at PolyOverQ::div_assign.

Source§

impl DivAssign<f32> for PolyOverQ

Source§

fn div_assign(&mut self, other: f32)

Documentation at PolyOverQ::div_assign.

Source§

impl DivAssign<f64> for PolyOverQ

Source§

fn div_assign(&mut self, other: f64)

Documentation at PolyOverQ::div_assign.

Source§

impl DivAssign<i16> for PolyOverQ

Source§

fn div_assign(&mut self, other: i16)

Documentation at PolyOverQ::div_assign.

Source§

impl DivAssign<i32> for PolyOverQ

Source§

fn div_assign(&mut self, other: i32)

Documentation at PolyOverQ::div_assign.

Source§

impl DivAssign<i64> for PolyOverQ

Source§

fn div_assign(&mut self, scalar: i64)

Documentation at PolyOverQ::div_assign.

Source§

impl DivAssign<i8> for PolyOverQ

Source§

fn div_assign(&mut self, other: i8)

Documentation at PolyOverQ::div_assign.

Source§

impl DivAssign<u16> for PolyOverQ

Source§

fn div_assign(&mut self, other: u16)

Documentation at PolyOverQ::div_assign.

Source§

impl DivAssign<u32> for PolyOverQ

Source§

fn div_assign(&mut self, other: u32)

Documentation at PolyOverQ::div_assign.

Source§

impl DivAssign<u64> for PolyOverQ

Source§

fn div_assign(&mut self, scalar: u64)

Documentation at PolyOverQ::div_assign.

Source§

impl DivAssign<u8> for PolyOverQ

Source§

fn div_assign(&mut self, other: u8)

Documentation at PolyOverQ::div_assign.

Source§

impl Drop for PolyOverQ

Source§

fn drop(&mut self)

Drops the given memory allocated for the underlying value.

§Examples
use qfall_math::rational::PolyOverQ;
{
    let a = PolyOverQ::default();
} // as a's scope ends here, it get's dropped
use qfall_math::rational::PolyOverQ;

let a = PolyOverQ::default();
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 From<&PolyOverQ> for PolyOverQ

Source§

fn from(value: &PolyOverQ) -> Self

Alias for PolyOverQ::clone.

Source§

impl From<&PolyOverQ> for String

Source§

fn from(value: &PolyOverQ) -> Self

Converts a PolyOverQ into its String representation.

Parameters:

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

Returns a String of the form "[#number of coefficients]⌴⌴[0th coefficient]⌴[1st coefficient]⌴...".

§Examples
use qfall_math::rational::PolyOverQ;
use std::str::FromStr;
let poly = PolyOverQ::from_str("2  6/7 1").unwrap();

let string: String = poly.into();
Source§

impl From<&PolyOverZ> for PolyOverQ

Source§

fn from(poly: &PolyOverZ) -> Self

Creates a PolyOverQ from a PolyOverZ.

Parameters:

  • poly: the polynomial from which the coefficients are copied
§Examples
use qfall_math::integer::PolyOverZ;
use qfall_math::rational::PolyOverQ;
use std::str::FromStr;

let poly = PolyOverZ::from_str("4  0 1 102 3").unwrap();

let poly_q = PolyOverQ::from(&poly);
Source§

impl From<PolyOverQ> for String

Source§

fn from(value: PolyOverQ) -> Self

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

Source§

impl From<PolyOverZ> for PolyOverQ

Source§

fn from(value: PolyOverZ) -> Self

Documentation can be found at PolyOverQ::from for &PolyOverZ.

Source§

impl<Rational: Into<Q>> From<Rational> for PolyOverQ

Source§

fn from(value: Rational) -> Self

Creates a constant PolyOverQ with a specified rational constant.

Parameters:

  • value: the constant value the polynomial will have. It has to be a rational number like Q, an integer or a tuple of integers (numerator, denominator).

Returns a new constant polynomial with the specified value.

§Examples
use qfall_math::{rational::*, traits::GetCoefficient};

let one = PolyOverQ::from(1);
let three_quarter = PolyOverQ::from(Q::from((3, 4)));
let one_half = PolyOverQ::from((1, 2));

assert_eq!(one_half.get_coeff(0).unwrap(), Q::from((1, 2)));
assert_eq!(one_half.get_degree(), 0);
§Panics …
  • if the provided value can not be converted into a Q. For example, because of a division by zero.
Source§

impl FromCoefficientEmbedding<&MatQ> for PolyOverQ

Source§

fn from_coefficient_embedding(embedding: &MatQ) -> Self

Computes a polynomial from a vector. The first i-th entry of the column vector is taken as the coefficient of the polynomial. It inverts the operation of PolyOverQ::into_coefficient_embedding.

Parameters:

  • embedding: the column vector that encodes the embedding

Returns a polynomial that corresponds to the embedding.

§Examples
use std::str::FromStr;
use qfall_math::{
    rational::{MatQ, PolyOverQ},
    traits::FromCoefficientEmbedding,
};

let vector = MatQ::from_str("[[17/3],[3/2],[-5]]").unwrap();
let poly = PolyOverQ::from_coefficient_embedding(&vector);
let cmp_poly = PolyOverQ::from_str("3  17/3 3/2 -5").unwrap();
assert_eq!(cmp_poly, poly);
§Panics …
  • if the provided embedding is not a column vector.
Source§

impl FromStr for PolyOverQ

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Creates a polynomial with arbitrarily many coefficients of type Q.

Parameters:

  • s: the polynomial of form: “[#number of coefficients]⌴⌴[0th coefficient]⌴[1st coefficient]⌴...

Note that the [#number of coefficients] and [0th coefficient] are divided by two spaces and the input string is trimmed, i.e. all whitespaces before and after are ignored.

Returns a PolyOverQ or an error if the provided string was not formatted correctly, the number of coefficients was smaller than the number provided at the start of the provided string, or the provided string contains a Null Byte.

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

let poly = PolyOverQ::from_str("5  0 1/3 2/10 -3/2 1").unwrap();
§Errors and Failures
  • Returns a MathError of type StringConversionError
    • if the provided string was not formatted correctly,
    • if the number of coefficients was smaller than the number provided at the start of the provided string,
    • if the provided value did not contain two whitespaces, or
    • if the provided string contains a Null Byte.
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 IntoCoefficientEmbedding<MatQ> for &PolyOverQ

Source§

fn into_coefficient_embedding(self, size: impl Into<i64>) -> MatQ

Computes the coefficient embedding of the polynomial in a MatQ as a column vector, where the i-th entry of the vector corresponds to the i-th coefficient. It inverts the operation of PolyOverQ::from_coefficient_embedding.

Parameters:

  • size: determines the number of rows of the embedding. It has to be larger than the degree of the polynomial.

Returns a coefficient embedding as a column vector if size is large enough.

§Examples
use std::str::FromStr;
use qfall_math::{
    rational::{MatQ, PolyOverQ},
    traits::IntoCoefficientEmbedding,
};

let poly = PolyOverQ::from_str("3  17/3 3/2 -5").unwrap();
let vector = poly.into_coefficient_embedding(4);
let cmp_vector = MatQ::from_str("[[17/3],[3/2],[-5],[0]]").unwrap();
assert_eq!(cmp_vector, vector);
§Panics …
  • if size is not larger than the degree of the polynomial, i.e. not all coefficients can be embedded.
Source§

impl Mul<&PolyOverZ> for &PolyOverQ

Source§

fn mul(self, other: &PolyOverZ) -> Self::Output

Implements the Mul trait for PolyOverQ and PolyOverZ. Mul is implemented for any combination of owned and borrowed values.

Parameters:

  • other: specifies the polynomial to multiply to self

Returns the product of both polynomials as a PolyOverQ.

§Examples
use qfall_math::rational::PolyOverQ;
use qfall_math::integer::PolyOverZ;
use std::str::FromStr;

let a = PolyOverQ::from_str("4  1/2 0 3/7 1").unwrap();
let b = PolyOverZ::from_str("4  2 0 3 1").unwrap();

let c: PolyOverQ = &a * &b;
Source§

type Output = PolyOverQ

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<&Z> for &PolyOverQ

Source§

fn mul(self, scalar: &Z) -> Self::Output

Implements the Mul trait for a PolyOverQ with a Z integer. Mul is implemented for any combination of owned and borrowed values. Mul is also implemented for Z 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 qfall_math::integer::Z;
use std::str::FromStr;

let poly_1 = PolyOverQ::from_str("4  1/2 2 3/4 4").unwrap();
let integer = Z::from(2);

let poly_2 = &poly_1 * &integer;
Source§

type Output = PolyOverQ

The resulting type after applying the * operator.
Source§

impl Mul for &PolyOverQ

Source§

fn mul(self, other: Self) -> Self::Output

Implements the Mul trait for two PolyOverQ values. Mul is implemented for any combination of PolyOverQ and borrowed PolyOverQ.

Parameters:

  • other: specifies the value to multiply with self

Returns the product of both polynomials as a PolyOverQ.

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

let a: PolyOverQ = PolyOverQ::from_str("3  1/7 2/5 -3").unwrap();
let b: PolyOverQ = PolyOverQ::from_str("5  1/9 2/5 -3/17 0 8/9").unwrap();

let c: PolyOverQ = &a * &b;
let d: PolyOverQ = a * b;
let e: PolyOverQ = &c * d;
let f: PolyOverQ = c * &e;
Source§

type Output = PolyOverQ

The resulting type after applying the * operator.
Source§

impl MulAssign<&PolyOverQ> for PolyOverQ

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 PolyOverQ in combination with PolyOverQ and PolyOverZ.

Parameters:

  • other: specifies the polynomial to multiply to self

Returns the product of both polynomials as a PolyOverQ.

§Examples
use qfall_math::{rational::PolyOverQ, integer::PolyOverZ};
use std::str::FromStr;

let mut a = PolyOverQ::from_str("3  1 2/3 -3/4").unwrap();
let b = PolyOverQ::from_str("5  1 2 -3 0 8/9").unwrap();
let c = PolyOverZ::from_str("2  -1 2").unwrap();

a *= &b;
a *= b;
a *= &c;
a *= c;
Source§

impl MulAssign<&PolyOverZ> for PolyOverQ

Source§

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

Documentation at PolyOverQ::mul_assign.

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<&Z> for PolyOverQ

Source§

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

Documentation at PolyOverQ::mul_assign.

Source§

impl MulAssign<PolyOverZ> for PolyOverQ

Source§

fn mul_assign(&mut self, other: PolyOverZ)

Documentation at PolyOverQ::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 PolyOverQ

Source§

fn mul_assign(&mut self, other: Z)

Documentation at PolyOverQ::mul_assign.

Source§

impl MulAssign<f32> for PolyOverQ

Source§

fn mul_assign(&mut self, other: f32)

Documentation at PolyOverQ::mul_assign.

Source§

impl MulAssign<f64> for PolyOverQ

Source§

fn mul_assign(&mut self, other: f64)

Documentation at PolyOverQ::mul_assign.

Source§

impl MulAssign<i16> for PolyOverQ

Source§

fn mul_assign(&mut self, other: i16)

Documentation at PolyOverQ::mul_assign.

Source§

impl MulAssign<i32> for PolyOverQ

Source§

fn mul_assign(&mut self, other: i32)

Documentation at PolyOverQ::mul_assign.

Source§

impl MulAssign<i64> for PolyOverQ

Source§

fn mul_assign(&mut self, other: i64)

Documentation at PolyOverQ::mul_assign.

Source§

impl MulAssign<i8> for PolyOverQ

Source§

fn mul_assign(&mut self, other: i8)

Documentation at PolyOverQ::mul_assign.

Source§

impl MulAssign<u16> for PolyOverQ

Source§

fn mul_assign(&mut self, other: u16)

Documentation at PolyOverQ::mul_assign.

Source§

impl MulAssign<u32> for PolyOverQ

Source§

fn mul_assign(&mut self, other: u32)

Documentation at PolyOverQ::mul_assign.

Source§

impl MulAssign<u64> for PolyOverQ

Source§

fn mul_assign(&mut self, other: u64)

Documentation at PolyOverQ::mul_assign.

Source§

impl MulAssign<u8> for PolyOverQ

Source§

fn mul_assign(&mut self, other: u8)

Documentation at PolyOverQ::mul_assign.

Source§

impl MulAssign for PolyOverQ

Source§

fn mul_assign(&mut self, other: PolyOverQ)

Documentation at PolyOverQ::mul_assign.

Source§

impl PartialEq<PolyOverZ> for PolyOverQ

Source§

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

Checks if an integer matrix and a rational matrix are equal. Used by the == and != operators. PartialEq is also implemented for PolyOverZ using PolyOverQ.

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::PolyOverZ;
use qfall_math::rational::PolyOverQ;
use std::str::FromStr;
let a: PolyOverQ = PolyOverQ::from_str("3  1 2 3").unwrap();
let b: PolyOverZ = PolyOverZ::from_str("3  1 2 3").unwrap();

// 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 = (PolyOverQ::eq(&a, &b));
let compared: bool = (PolyOverZ::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 PolyOverQ

Source§

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

Checks if two polynomials over Q 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::PolyOverQ;
use std::str::FromStr;
let a: PolyOverQ = PolyOverQ::from_str("2  42/24 1").unwrap();
let b: PolyOverQ = PolyOverQ::from_str("2  24/42 1").unwrap();

// 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 = (PolyOverQ::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 Serialize for PolyOverQ

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

Source§

impl<Rational: Into<Q>> SetCoefficient<Rational> for PolyOverQ

Source§

unsafe fn set_coeff_unchecked(&mut self, index: i64, value: Rational)

Sets the coefficient of a polynomial PolyOverQ. We advise to use small coefficients, since already 2^32 coefficients take space of roughly 34 GB. If not careful, be prepared that memory problems can occur, if the index is very high.

Parameters:

  • index: the index of the coefficient to set (has to be positive)
  • value: the new value the index should have

Returns an empty Ok if the action could be performed successfully. Otherwise, a MathError is returned if either the index is negative or it does not fit into an i64.

§Examples
use qfall_math::rational::PolyOverQ;
use qfall_math::rational::Q;
use qfall_math::traits::*;
use std::str::FromStr;

let mut poly = PolyOverQ::from_str("4  0 1 2/3 3/17").unwrap();
let value = Q::from((3, 17));

assert!(poly.set_coeff(4, &value).is_ok());
unsafe{ poly.set_coeff_unchecked(5, 3.5_f64) };
§Safety

To use this function safely, make sure that the selected index is greater or equal than 0 and that the provided value has the same base so that they have a matching base.

Source§

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

Sets coefficient of the object, e.g. polynomial, for a given input value and a index. Read more
Source§

impl Sub<&PolyOverQ> for &PolyOverZ

Source§

fn sub(self, other: &PolyOverQ) -> Self::Output

Implements the Sub trait for PolyOverZ and PolyOverQ. Sub is implemented for any combination of owned and borrowed values.

Parameters:

  • other: specifies the polynomial to subtract from self

Returns the subtraction of both polynomials as a PolyOverQ.

§Examples
use qfall_math::rational::PolyOverQ;
use qfall_math::integer::PolyOverZ;
use std::str::FromStr;

let a = PolyOverZ::from_str("4  2 0 3 1").unwrap();
let b = PolyOverQ::from_str("4  1/2 0 3/7 1").unwrap();

let c: PolyOverQ = &a - &b;
Source§

type Output = PolyOverQ

The resulting type after applying the - operator.
Source§

impl Sub<&PolyOverZ> for &PolyOverQ

Source§

fn sub(self, other: &PolyOverZ) -> Self::Output

Implements the Sub trait for PolyOverQ and PolyOverZ. Sub is implemented for any combination of owned and borrowed values.

Parameters:

  • other: specifies the polynomial to subtract from self

Returns the subtraction of both polynomials as a PolyOverQ.

§Examples
use qfall_math::rational::PolyOverQ;
use qfall_math::integer::PolyOverZ;
use std::str::FromStr;

let a = PolyOverQ::from_str("4  1/2 0 3/7 1").unwrap();
let b = PolyOverZ::from_str("4  2 0 3 1").unwrap();

let c: PolyOverQ = &a - &b;
Source§

type Output = PolyOverQ

The resulting type after applying the - operator.
Source§

impl Sub for &PolyOverQ

Source§

fn sub(self, other: Self) -> Self::Output

Implements the Sub trait for two PolyOverQ values. Sub is implemented for any combination of PolyOverQ and borrowed PolyOverQ.

Parameters:

  • other: specifies the value to subtract from self

Returns the result of the subtraction of both polynomials as a PolyOverQ.

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

let a: PolyOverQ = PolyOverQ::from_str("3  1/8 2/5 -3").unwrap();
let b: PolyOverQ = PolyOverQ::from_str("5  1/9 2/7 -3/17 0 8/9").unwrap();

let c: PolyOverQ = &a - &b;
let d: PolyOverQ = a - b;
let e: PolyOverQ = &c - d;
let f: PolyOverQ = c - &e;
Source§

type Output = PolyOverQ

The resulting type after applying the - operator.
Source§

impl SubAssign<&PolyOverQ> for PolyOverQ

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 PolyOverQ in combination with PolyOverQ and PolyOverZ.

Parameters:

  • other: specifies the polynomial to subtract to self

Returns the difference of both polynomials as a PolyOverQ.

§Examples
use qfall_math::{rational::PolyOverQ, integer::PolyOverZ};
use std::str::FromStr;

let mut a = PolyOverQ::from_str("3  1 2/3 -3/4").unwrap();
let b = PolyOverQ::from_str("5  1 2 -3 0 8/9").unwrap();
let c = PolyOverZ::from_str("2  -1 2").unwrap();

a -= &b;
a -= b;
a -= &c;
a -= c;
Source§

impl SubAssign<&PolyOverZ> for PolyOverQ

Source§

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

Documentation at PolyOverQ::sub_assign.

Source§

impl SubAssign<PolyOverZ> for PolyOverQ

Source§

fn sub_assign(&mut self, other: PolyOverZ)

Documentation at PolyOverQ::sub_assign.

Source§

impl SubAssign for PolyOverQ

Source§

fn sub_assign(&mut self, other: PolyOverQ)

Documentation at PolyOverQ::sub_assign.

Source§

impl Eq for PolyOverQ

Source§

impl Send for PolyOverQ

Source§

impl Sync for PolyOverQ

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<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>,