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
impl PolyOverQ
Sourcepub fn dot_product(&self, other: &Self) -> Result<Q, MathError>
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
impl PolyOverQ
Sourcepub fn norm_eucl_sqrd(&self) -> Q
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
impl PolyOverQ
Sourcepub fn norm_infty(&self) -> Q
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
impl PolyOverQ
Sourcepub fn floor(&self) -> PolyOverZ
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());Sourcepub fn ceil(&self) -> PolyOverZ
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());Sourcepub fn round(&self) -> PolyOverZ
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());Sourcepub fn randomized_rounding(
&self,
r: impl Into<Q>,
) -> Result<PolyOverZ, MathError>
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 deviationsigma * 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
- Returns a
MathErrorof typeInvalidIntegerInputifr < 0.
This function implements randomized rounding according to:
- [1] Peikert, C. (2010, August). An efficient and parallel Gaussian sampler for lattices. In: Annual Cryptology Conference (pp. 80-97). https://link.springer.com/chapter/10.1007/978-3-642-14623-7_5
Source§impl PolyOverQ
impl PolyOverQ
Sourcepub fn sample_gauss(
max_degree: impl TryInto<i64> + Display,
center: impl Into<Q>,
sigma: impl Into<f64>,
) -> Result<Self, MathError>
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 createdPolyOverQshould havecenter: specifies the center for each coefficient of the polynomialsigma: 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
- Returns a
MathErrorof typeNonPositiveifsigma <= 0.
§Panics …
- if
max_degreeis negative, or does not fit into ani64.
Source§impl PolyOverQ
impl PolyOverQ
Sourcepub fn to_string_decimal(&self, nr_decimal_digits: usize) -> String
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 outputString
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
impl PolyOverQ
Sourcepub unsafe fn get_fmpq_poly_struct(&mut self) -> &mut fmpq_poly_struct
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
impl PolyOverQ
Sourcepub unsafe fn set_fmpq_poly_struct(&mut self, flint_struct: fmpq_poly_struct)
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
impl Add<&PolyOverZ> for &PolyOverQ
Source§fn add(self, other: &PolyOverZ) -> Self::Output
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 toself
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§impl Add for &PolyOverQ
impl Add for &PolyOverQ
Source§fn add(self, other: Self) -> Self::Output
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 toself
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§impl AddAssign<&PolyOverQ> for PolyOverQ
impl AddAssign<&PolyOverQ> for PolyOverQ
Source§fn add_assign(&mut self, other: &Self)
fn add_assign(&mut self, other: &Self)
Computes the addition of self and other reusing
the memory of self.
AddAssign can be used on PolyOverQ in combination with
PolyOverQ and PolyOverZ.
Parameters:
other: specifies the polynomial to add toself
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
impl AddAssign<&PolyOverZ> for PolyOverQ
Source§fn add_assign(&mut self, other: &PolyOverZ)
fn add_assign(&mut self, other: &PolyOverZ)
Documentation at PolyOverQ::add_assign.
Source§impl AddAssign<PolyOverZ> for PolyOverQ
impl AddAssign<PolyOverZ> for PolyOverQ
Source§fn add_assign(&mut self, other: PolyOverZ)
fn add_assign(&mut self, other: PolyOverZ)
Documentation at PolyOverQ::add_assign.
Source§impl AddAssign for PolyOverQ
impl AddAssign for PolyOverQ
Source§fn add_assign(&mut self, other: PolyOverQ)
fn add_assign(&mut self, other: PolyOverQ)
Documentation at PolyOverQ::add_assign.
Source§impl Clone for PolyOverQ
impl Clone for PolyOverQ
Source§impl CompareBase<PolyOverZ> for PolyOverQ
impl CompareBase<PolyOverZ> for PolyOverQ
Source§impl<Rational: Into<Q>> CompareBase<Rational> for PolyOverQ
impl<Rational: Into<Q>> CompareBase<Rational> for PolyOverQ
Source§impl CompareBase for PolyOverQ
impl CompareBase for PolyOverQ
Source§impl<'de> Deserialize<'de> for PolyOverQ
impl<'de> Deserialize<'de> for PolyOverQ
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Implements the deserialize option. This allows to create a PolyOverQ from a given Json-object.
Source§impl Display for PolyOverQ
impl Display for PolyOverQ
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
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
impl Div<&Q> for &PolyOverQ
Source§fn div(self, scalar: &Q) -> Self::Output
fn div(self, scalar: &Q) -> Self::Output
Implements the Div trait for a PolyOverQ by a Q rational.
Div is implemented for any combination of owned and borrowed values.
Parameters:
scalar: specifies the scalar by which the polynomial is divided
Returns the division of self by scalar as a PolyOverQ.
§Examples
use qfall_math::rational::{PolyOverQ, Q};
use std::str::FromStr;
let poly_1 = PolyOverQ::from_str("4 1/2 2 3/4 4").unwrap();
let rational = Q::from((2,3));
&poly_1 / &rational;
&poly_1 / rational;
&poly_1 / 2.0_f32;
&poly_1 / -2.0_f64;§Panics …
- if
scalaris0.
Source§impl Div<&Z> for &PolyOverQ
impl Div<&Z> for &PolyOverQ
Source§fn div(self, scalar: &Z) -> Self::Output
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
scalaris0.
Source§impl DivAssign<&Q> for PolyOverQ
impl DivAssign<&Q> for PolyOverQ
Source§fn div_assign(&mut self, scalar: &Q)
fn div_assign(&mut self, scalar: &Q)
Divides the polynomial coefficient-wise.
Parameters:
scalar: specifies the value to multiply toself
Divides self coefficient-wise by scalar returning a PolyOverQ.
§Examples
use qfall_math::rational::{Q, PolyOverQ};
use std::str::FromStr;
let mut polyq = PolyOverQ::from_str(&format!("3 1/3 -5 99/2")).unwrap();
let q = Q::from((3, 4));
polyq /= &q;
polyq /= q;
polyq /= 2_f32;
polyq /= -2_f64;§Panics …
- if the
scalaris0.
Source§impl DivAssign<&Z> for PolyOverQ
impl DivAssign<&Z> for PolyOverQ
Source§fn div_assign(&mut self, scalar: &Z)
fn div_assign(&mut self, scalar: &Z)
Documentation at PolyOverQ::div_assign.
Source§impl DivAssign<Q> for PolyOverQ
impl DivAssign<Q> for PolyOverQ
Source§fn div_assign(&mut self, other: Q)
fn div_assign(&mut self, other: Q)
Documentation at PolyOverQ::div_assign.
Source§impl DivAssign<Z> for PolyOverQ
impl DivAssign<Z> for PolyOverQ
Source§fn div_assign(&mut self, other: Z)
fn div_assign(&mut self, other: Z)
Documentation at PolyOverQ::div_assign.
Source§impl DivAssign<f32> for PolyOverQ
impl DivAssign<f32> for PolyOverQ
Source§fn div_assign(&mut self, other: f32)
fn div_assign(&mut self, other: f32)
Documentation at PolyOverQ::div_assign.
Source§impl DivAssign<f64> for PolyOverQ
impl DivAssign<f64> for PolyOverQ
Source§fn div_assign(&mut self, other: f64)
fn div_assign(&mut self, other: f64)
Documentation at PolyOverQ::div_assign.
Source§impl DivAssign<i16> for PolyOverQ
impl DivAssign<i16> for PolyOverQ
Source§fn div_assign(&mut self, other: i16)
fn div_assign(&mut self, other: i16)
Documentation at PolyOverQ::div_assign.
Source§impl DivAssign<i32> for PolyOverQ
impl DivAssign<i32> for PolyOverQ
Source§fn div_assign(&mut self, other: i32)
fn div_assign(&mut self, other: i32)
Documentation at PolyOverQ::div_assign.
Source§impl DivAssign<i64> for PolyOverQ
impl DivAssign<i64> for PolyOverQ
Source§fn div_assign(&mut self, scalar: i64)
fn div_assign(&mut self, scalar: i64)
Documentation at PolyOverQ::div_assign.
Source§impl DivAssign<i8> for PolyOverQ
impl DivAssign<i8> for PolyOverQ
Source§fn div_assign(&mut self, other: i8)
fn div_assign(&mut self, other: i8)
Documentation at PolyOverQ::div_assign.
Source§impl DivAssign<u16> for PolyOverQ
impl DivAssign<u16> for PolyOverQ
Source§fn div_assign(&mut self, other: u16)
fn div_assign(&mut self, other: u16)
Documentation at PolyOverQ::div_assign.
Source§impl DivAssign<u32> for PolyOverQ
impl DivAssign<u32> for PolyOverQ
Source§fn div_assign(&mut self, other: u32)
fn div_assign(&mut self, other: u32)
Documentation at PolyOverQ::div_assign.
Source§impl DivAssign<u64> for PolyOverQ
impl DivAssign<u64> for PolyOverQ
Source§fn div_assign(&mut self, scalar: u64)
fn div_assign(&mut self, scalar: u64)
Documentation at PolyOverQ::div_assign.
Source§impl DivAssign<u8> for PolyOverQ
impl DivAssign<u8> for PolyOverQ
Source§fn div_assign(&mut self, other: u8)
fn div_assign(&mut self, other: u8)
Documentation at PolyOverQ::div_assign.
Source§impl Drop for PolyOverQ
impl Drop for PolyOverQ
Source§fn drop(&mut self)
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 droppeduse qfall_math::rational::PolyOverQ;
let a = PolyOverQ::default();
drop(a); // explicitly drops a's valueSource§impl<Rational: Into<Q>> Evaluate<Rational, Q> for PolyOverQ
impl<Rational: Into<Q>> Evaluate<Rational, Q> for PolyOverQ
Source§fn evaluate(&self, value: Rational) -> Q
fn evaluate(&self, value: Rational) -> Q
Evaluates a PolyOverQ on a given input.
Parameters:
value: the value with which to evaluate the polynomial.
Returns the evaluation of the polynomial as a Q.
§Examples
use qfall_math::traits::*;
use qfall_math::rational::Q;
use qfall_math::rational::PolyOverQ;
use std::str::FromStr;
let poly = PolyOverQ::from_str("5 0 1 2/3 -3/2 1").unwrap();
let value = Q::from((3, 2));
let res = poly.evaluate(&value);Source§impl From<&PolyOverQ> for PolyOverQ
impl From<&PolyOverQ> for PolyOverQ
Source§fn from(value: &PolyOverQ) -> Self
fn from(value: &PolyOverQ) -> Self
Alias for PolyOverQ::clone.
Source§impl From<&PolyOverQ> for String
impl From<&PolyOverQ> for String
Source§fn from(value: &PolyOverQ) -> Self
fn from(value: &PolyOverQ) -> Self
Converts a PolyOverQ into its String representation.
Parameters:
value: specifies the polynomial that will be represented as aString
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
impl From<&PolyOverZ> for PolyOverQ
Source§fn from(poly: &PolyOverZ) -> Self
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<Rational: Into<Q>> From<Rational> for PolyOverQ
impl<Rational: Into<Q>> From<Rational> for PolyOverQ
Source§fn from(value: Rational) -> Self
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 likeQ, 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
impl FromCoefficientEmbedding<&MatQ> for PolyOverQ
Source§fn from_coefficient_embedding(embedding: &MatQ) -> Self
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
impl FromStr for PolyOverQ
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
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
MathErrorof typeStringConversionError- 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
NullByte.
Source§impl GetCoefficient<Q> for PolyOverQ
impl GetCoefficient<Q> for PolyOverQ
Source§unsafe fn get_coeff_unchecked(&self, index: i64) -> Q
unsafe fn get_coeff_unchecked(&self, index: i64) -> Q
Returns the coefficient of a polynomial PolyOverQ as a Q.
If an index is provided which exceeds the highest set coefficient, 0 is returned.
Parameters:
index: the index of the coefficient to get (has to be positive)
Returns the coefficient as a Q, or a MathError if the provided index
is negative and therefore invalid, or it does not fit into an i64.
§Examples
use qfall_math::rational::{Q, PolyOverQ};
use std::str::FromStr;
use qfall_math::traits::*;
let poly = PolyOverQ::from_str("4 0 1 2/3 3/2").unwrap();
let coeff_0 = poly.get_coeff(0).unwrap();
let coeff_1 = unsafe{ poly.get_coeff_unchecked(1) };
let coeff_4 = poly.get_coeff(4).unwrap();
assert_eq!(Q::ZERO, coeff_0);
assert_eq!(Q::ONE, coeff_1);
assert_eq!(Q::ZERO, coeff_4);§Safety
To use this function safely, make sure that the selected index
is greater or equal than 0.
Source§impl IntoCoefficientEmbedding<MatQ> for &PolyOverQ
impl IntoCoefficientEmbedding<MatQ> for &PolyOverQ
Source§fn into_coefficient_embedding(self, size: impl Into<i64>) -> MatQ
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
sizeis not larger than the degree of the polynomial, i.e. not all coefficients can be embedded.
Source§impl Mul<&PolyOverZ> for &PolyOverQ
impl Mul<&PolyOverZ> for &PolyOverQ
Source§fn mul(self, other: &PolyOverZ) -> Self::Output
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 toself
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§impl Mul<&Q> for &PolyOverQ
impl Mul<&Q> for &PolyOverQ
Source§fn mul(self, scalar: &Q) -> Self::Output
fn mul(self, scalar: &Q) -> Self::Output
Implements the Mul trait for a PolyOverQ with a Q rational.
Mul is implemented for any combination of owned and borrowed values.
Mul is also implemented for Q using PolyOverQ.
Parameters:
scalar: specifies the scalar by which the polynomial is multiplied
Returns the product of self and scalar as a PolyOverQ.
§Examples
use qfall_math::rational::{PolyOverQ, Q};
use std::str::FromStr;
let poly_1 = PolyOverQ::from_str("4 1/2 2 3/4 4").unwrap();
let rational = Q::from((2,3));
let poly_2 = &poly_1 * &rational;Source§impl Mul<&Z> for &PolyOverQ
impl Mul<&Z> for &PolyOverQ
Source§fn mul(self, scalar: &Z) -> Self::Output
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§impl Mul for &PolyOverQ
impl Mul for &PolyOverQ
Source§fn mul(self, other: Self) -> Self::Output
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 withself
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§impl MulAssign<&PolyOverQ> for PolyOverQ
impl MulAssign<&PolyOverQ> for PolyOverQ
Source§fn mul_assign(&mut self, other: &Self)
fn mul_assign(&mut self, other: &Self)
Computes the multiplication of self and other reusing
the memory of self.
MulAssign can be used on PolyOverQ in combination with
PolyOverQ and PolyOverZ.
Parameters:
other: specifies the polynomial to multiply toself
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
impl MulAssign<&PolyOverZ> for PolyOverQ
Source§fn mul_assign(&mut self, other: &PolyOverZ)
fn mul_assign(&mut self, other: &PolyOverZ)
Documentation at PolyOverQ::mul_assign.
Source§impl MulAssign<&Q> for PolyOverQ
impl MulAssign<&Q> for PolyOverQ
Source§fn mul_assign(&mut self, scalar: &Q)
fn mul_assign(&mut self, scalar: &Q)
Computes the scalar multiplication of self and other reusing
the memory of self.
Parameters:
other: specifies the value to multiply toself
Returns the scalar of the polynomial as a PolyOverQ.
§Examples
use qfall_math::integer::Z;
use qfall_math::rational::{PolyOverQ, Q};
use std::str::FromStr;
let mut a = PolyOverQ::from_str("3 1 2 -3/2").unwrap();
let b = Q::from((2,5));
let c = Z::from(2);
a *= &b;
a *= b;
a *= &c;
a *= c;
a *= 2;
a *= -2;Source§impl MulAssign<&Z> for PolyOverQ
impl MulAssign<&Z> for PolyOverQ
Source§fn mul_assign(&mut self, other: &Z)
fn mul_assign(&mut self, other: &Z)
Documentation at PolyOverQ::mul_assign.
Source§impl MulAssign<PolyOverZ> for PolyOverQ
impl MulAssign<PolyOverZ> for PolyOverQ
Source§fn mul_assign(&mut self, other: PolyOverZ)
fn mul_assign(&mut self, other: PolyOverZ)
Documentation at PolyOverQ::mul_assign.
Source§impl MulAssign<Q> for PolyOverQ
impl MulAssign<Q> for PolyOverQ
Source§fn mul_assign(&mut self, other: Q)
fn mul_assign(&mut self, other: Q)
Documentation at PolyOverQ::mul_assign.
Source§impl MulAssign<Z> for PolyOverQ
impl MulAssign<Z> for PolyOverQ
Source§fn mul_assign(&mut self, other: Z)
fn mul_assign(&mut self, other: Z)
Documentation at PolyOverQ::mul_assign.
Source§impl MulAssign<f32> for PolyOverQ
impl MulAssign<f32> for PolyOverQ
Source§fn mul_assign(&mut self, other: f32)
fn mul_assign(&mut self, other: f32)
Documentation at PolyOverQ::mul_assign.
Source§impl MulAssign<f64> for PolyOverQ
impl MulAssign<f64> for PolyOverQ
Source§fn mul_assign(&mut self, other: f64)
fn mul_assign(&mut self, other: f64)
Documentation at PolyOverQ::mul_assign.
Source§impl MulAssign<i16> for PolyOverQ
impl MulAssign<i16> for PolyOverQ
Source§fn mul_assign(&mut self, other: i16)
fn mul_assign(&mut self, other: i16)
Documentation at PolyOverQ::mul_assign.
Source§impl MulAssign<i32> for PolyOverQ
impl MulAssign<i32> for PolyOverQ
Source§fn mul_assign(&mut self, other: i32)
fn mul_assign(&mut self, other: i32)
Documentation at PolyOverQ::mul_assign.
Source§impl MulAssign<i64> for PolyOverQ
impl MulAssign<i64> for PolyOverQ
Source§fn mul_assign(&mut self, other: i64)
fn mul_assign(&mut self, other: i64)
Documentation at PolyOverQ::mul_assign.
Source§impl MulAssign<i8> for PolyOverQ
impl MulAssign<i8> for PolyOverQ
Source§fn mul_assign(&mut self, other: i8)
fn mul_assign(&mut self, other: i8)
Documentation at PolyOverQ::mul_assign.
Source§impl MulAssign<u16> for PolyOverQ
impl MulAssign<u16> for PolyOverQ
Source§fn mul_assign(&mut self, other: u16)
fn mul_assign(&mut self, other: u16)
Documentation at PolyOverQ::mul_assign.
Source§impl MulAssign<u32> for PolyOverQ
impl MulAssign<u32> for PolyOverQ
Source§fn mul_assign(&mut self, other: u32)
fn mul_assign(&mut self, other: u32)
Documentation at PolyOverQ::mul_assign.
Source§impl MulAssign<u64> for PolyOverQ
impl MulAssign<u64> for PolyOverQ
Source§fn mul_assign(&mut self, other: u64)
fn mul_assign(&mut self, other: u64)
Documentation at PolyOverQ::mul_assign.
Source§impl MulAssign<u8> for PolyOverQ
impl MulAssign<u8> for PolyOverQ
Source§fn mul_assign(&mut self, other: u8)
fn mul_assign(&mut self, other: u8)
Documentation at PolyOverQ::mul_assign.
Source§impl MulAssign for PolyOverQ
impl MulAssign for PolyOverQ
Source§fn mul_assign(&mut self, other: PolyOverQ)
fn mul_assign(&mut self, other: PolyOverQ)
Documentation at PolyOverQ::mul_assign.
Source§impl PartialEq<PolyOverZ> for PolyOverQ
impl PartialEq<PolyOverZ> for PolyOverQ
Source§fn eq(&self, other: &PolyOverZ) -> bool
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));Source§impl PartialEq for PolyOverQ
impl PartialEq for PolyOverQ
Source§fn eq(&self, other: &Self) -> bool
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));Source§impl<Rational: Into<Q>> SetCoefficient<Rational> for PolyOverQ
impl<Rational: Into<Q>> SetCoefficient<Rational> for PolyOverQ
Source§unsafe fn set_coeff_unchecked(&mut self, index: i64, value: Rational)
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§impl Sub<&PolyOverQ> for &PolyOverZ
impl Sub<&PolyOverQ> for &PolyOverZ
Source§fn sub(self, other: &PolyOverQ) -> Self::Output
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 fromself
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§impl Sub<&PolyOverZ> for &PolyOverQ
impl Sub<&PolyOverZ> for &PolyOverQ
Source§fn sub(self, other: &PolyOverZ) -> Self::Output
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 fromself
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§impl Sub for &PolyOverQ
impl Sub for &PolyOverQ
Source§fn sub(self, other: Self) -> Self::Output
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 fromself
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§impl SubAssign<&PolyOverQ> for PolyOverQ
impl SubAssign<&PolyOverQ> for PolyOverQ
Source§fn sub_assign(&mut self, other: &Self)
fn sub_assign(&mut self, other: &Self)
Computes the subtraction of self and other reusing
the memory of self.
SubAssign can be used on PolyOverQ in combination with
PolyOverQ and PolyOverZ.
Parameters:
other: specifies the polynomial to subtract toself
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
impl SubAssign<&PolyOverZ> for PolyOverQ
Source§fn sub_assign(&mut self, other: &PolyOverZ)
fn sub_assign(&mut self, other: &PolyOverZ)
Documentation at PolyOverQ::sub_assign.
Source§impl SubAssign<PolyOverZ> for PolyOverQ
impl SubAssign<PolyOverZ> for PolyOverQ
Source§fn sub_assign(&mut self, other: PolyOverZ)
fn sub_assign(&mut self, other: PolyOverZ)
Documentation at PolyOverQ::sub_assign.
Source§impl SubAssign for PolyOverQ
impl SubAssign for PolyOverQ
Source§fn sub_assign(&mut self, other: PolyOverQ)
fn sub_assign(&mut self, other: PolyOverQ)
Documentation at PolyOverQ::sub_assign.