Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

From_str for PolyRingZq #411

Merged
merged 11 commits into from
Sep 27, 2024
20 changes: 18 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use thiserror::Error;
/// - [`InvalidInterval`](MathError::InvalidInterval) is thrown if an invalid
/// interval, e.g. of negative size, is provided.
/// - [`InvalidModulus`](MathError::InvalidModulus) is thrown if an integer is
/// provided, which is not greater than `1`.
/// provided, which is smaller than `2`.
/// - [`NulError`](MathError::NulError) is thrown if a [`NulError`] is thrown,
/// which currently only happens if an invalid string is given to construct
/// a [`CString`](std::ffi::CString).
Expand Down Expand Up @@ -165,6 +165,10 @@ pub enum MathError {
/// is thrown if an invalid string is given
/// to construct a [`PolyOverZq`](crate::integer_mod_q::PolyOverZq), i.e. it is
/// not formatted correctly.
/// - [`InvalidStringToPolyRingZqInput`](StringConversionError::InvalidStringToPolyRingZqInput)
/// is thrown if an invalid string is given
/// to construct a [`PolynomialRingZq`](crate::integer_mod_q::PolynomialRingZq), i.e. it is
/// not formatted correctly.
/// - [`InvalidStringToQInput`](StringConversionError::InvalidStringToQInput)
/// is thrown if an invalid string is given to construct a [`Q`](crate::rational::Q).
/// - [`InvalidStringToZInput`](StringConversionError::InvalidStringToZInput)
Expand Down Expand Up @@ -216,11 +220,23 @@ pub enum StringConversionError {
The format must \
be '[#number of coefficients] [0th coefficient] [1st coefficient] ... \
mod [modulus]'.
Note that the after the number of coefficients, there are two \
Note that after the number of coefficients, there are two \
whitespaces."
)]
InvalidStringToPolyModulusInput(String),

/// Parse string to [`PolynomialRingZq`](crate::integer_mod_q::PolynomialRingZq) error.
#[error(
"Invalid string input to parse to polynomial / polynomial mod q {0}.
The format must \
be `[#number of coefficients of element]⌴⌴[0th coefficient]⌴ \
[1st coefficient]⌴...⌴/⌴[#number of coefficients of polynomial modulus] \
⌴⌴[0th coefficient]⌴[1st coefficient]⌴...⌴mod⌴[q]`.
Note that after the number of coefficients, there are two \
whitespaces."
)]
InvalidStringToPolyRingZqInput(String),

/// Parse string to [`Q`](crate::rational::Q) error
#[error("Invalid string input to parse to Q {0}")]
InvalidStringToQInput(String),
Expand Down
3 changes: 2 additions & 1 deletion src/integer/mat_poly_over_z/arithmetic/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ impl MatPolyOverZ {
/// Parameters:
/// - `other`: specifies the value to multiply with `self`
///
/// Returns the product of `self` and `other` as a [`MatPolyOverZ`].
/// Returns the product of `self` and `other` as a [`MatPolyOverZ`]
/// or an error if the dimensions of `self` and `other` do not match for multiplication.
///
/// # Examples
/// ```
Expand Down
2 changes: 1 addition & 1 deletion src/integer/mat_poly_over_z/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Concatenate for &MatPolyOverZ {
/// Parameters:
/// - `other`: the other matrix to concatenate with `self`
///
/// Returns a vertical concatenation of the two matrices or a
/// Returns a vertical concatenation of the two matrices or
/// an error, if the matrices can not be concatenated vertically.
///
/// # Examples
Expand Down
22 changes: 12 additions & 10 deletions src/integer/mat_poly_over_z/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ impl FromStr for MatPolyOverZ {
type Err = MathError;

/// Creates a [`MatPolyOverZ`] matrix from a [`String`].
/// The format of that string looks like <br>
/// `[[poly_1, poly_2, poly_3],[poly_4, poly_5, poly_6]]` for a 2x3 matrix
/// where thirst three polynomials are in the first row and the second three are
/// in the second row.
///
/// **Warning**: Each entry is parsed as a [`PolyOverZ`] object.
/// If an entry string starts with a correctly formatted [`PolyOverZ`] object,
/// the rest of this entry string is ignored. This means that the entry input
/// string `"4 0 1 2 3"` is the same as `"4 0 1 2 3 4 5 6 7"`.
///
/// Parameters:
/// - `string`: the matrix as a string
/// - `string`: the matrix of form: `"[[poly_1, poly_2, poly_3],[poly_4, poly_5, poly_6]]"`
/// for a 2x3 matrix where first three polynomials are in the first row
/// and the second three are in the second row.
///
/// Returns a [`MatPolyOverZ`] or an error, if the matrix is not formatted in a suitable way,
/// Returns a [`MatPolyOverZ`] or an error if the matrix is not formatted in a suitable way,
/// the number of rows or columns is too large (must fit into [`i64`]),
/// the number of entries in rows is unequal or if the regular expression
/// inside of the function could not be processed.
/// the number of entries in rows is unequal, or if an entry is not formatted correctly.
///
/// # Examples
/// ```
Expand Down Expand Up @@ -88,10 +90,10 @@ impl FromStr for MatPolyOverZ {
}

impl From<&MatZ> for MatPolyOverZ {
/// Initialize a [`MatPolyOverZ`] with constant polynomials defined by a [`MatZ`].
/// Creates a [`MatPolyOverZ`] with constant polynomials defined by a [`MatZ`].
///
/// # Parameters
/// - `matrix`: A matrix with constant integers.
/// - `matrix`: a matrix with constant integers.
///
/// Returns a matrix of polynomial that all have the first coefficient
/// set to the value in the matrix.
Expand Down
17 changes: 9 additions & 8 deletions src/integer/mat_poly_over_z/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl GetEntry<PolyOverZ> for MatPolyOverZ {
/// the last element.
///
/// Returns the [`PolyOverZ`] value of the matrix at the position of the given
/// row and column or an error, if the number of rows or columns is
/// row and column or an error if the number of rows or columns is
/// greater than the matrix.
///
/// # Examples
Expand Down Expand Up @@ -111,7 +111,7 @@ impl MatPolyOverZ {
/// - `row`: specifies the row of the matrix
///
/// Returns a row vector of the matrix at the position of the given
/// `row` or an error, if the number of rows is
/// `row` or an error if the number of rows is
/// greater than the matrix or negative.
///
/// # Examples
Expand Down Expand Up @@ -148,7 +148,7 @@ impl MatPolyOverZ {
/// * `column`: specifies the column of the matrix
///
/// Returns a column vector of the matrix at the position of the given
/// `column` or an error, if the number of columns is
/// `column` or an error if the number of columns is
/// greater than the matrix or negative.
///
/// # Examples
Expand Down Expand Up @@ -185,15 +185,16 @@ impl MatPolyOverZ {
/// Otherwise the function will panic.
///
/// Parameters:
/// `row_1`: The starting row of the submatrix
/// `row_2`: The ending row of the submatrix
/// `col_1`: The starting column of the submatrix
/// `col_2`: The ending column of the submatrix
/// `row_1`: the starting row of the submatrix
/// `row_2`: the ending row of the submatrix
/// `col_1`: the starting column of the submatrix
/// `col_2`: the ending column of the submatrix
///
/// Negative indices can be used to index from the back, e.g., `-1` for
/// the last element.
///
/// Returns the submatrix from `(row_1, col_1)` to `(row_2, col_2)`(inclusively).
/// Returns the submatrix from `(row_1, col_1)` to `(row_2, col_2)`(inclusively)
/// or an error if any provided row or column is greater than the matrix.
///
/// # Examples
/// ```
Expand Down
3 changes: 2 additions & 1 deletion src/integer/mat_poly_over_z/sample/discrete_gauss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ impl MatPolyOverZ {
/// to the standard deviation `sigma * sqrt(2 * pi) = s`
///
/// Returns a vector of polynomials sampled according to the
/// discrete Gaussian distribution.
/// discrete Gaussian distribution or an error if the basis is not a row vector,
/// the `n <= 1` or `s <= 0`, or the number of rows of the `basis` and `center` differ.
///
/// # Example
/// ```
Expand Down
2 changes: 1 addition & 1 deletion src/integer/mat_poly_over_z/vector/dot_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl MatPolyOverZ {
/// Parameters:
/// - `other`: specifies the other vector the dot product is calculated over
///
/// Returns the resulting `dot_product` as a [`PolyOverZ`] or an error,
/// Returns the resulting `dot_product` as a [`PolyOverZ`] or an error
/// if the given [`MatPolyOverZ`] instances aren't vectors or have different
/// numbers of entries.
///
Expand Down
9 changes: 3 additions & 6 deletions src/integer/mat_z/arithmetic/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ impl MatZ {
/// Parameters:
/// - `divisor`: specifies the divisor by which the matrix is divided
///
/// Returns the quotient of `self` divided by `divisor` as a [`MatZ`]
/// or panics if the divisor is `0`.
/// Returns the quotient of `self` divided by `divisor` as a [`MatZ`].
///
/// # Safety
/// The divisor MUST exactly divide each element in the matrix.
Expand Down Expand Up @@ -61,8 +60,7 @@ impl MatZ {
/// Parameters:
/// - `divisor`: specifies the divisor by which the matrix is divided
///
/// Returns the quotient of `self` divided by `divisor` as a [`MatZ`]
/// or panics if the divisor is `0`.
/// Returns the quotient of `self` divided by `divisor` as a [`MatZ`].
///
/// # Safety
/// The divisor MUST exactly divide each element in the matrix.
Expand Down Expand Up @@ -102,8 +100,7 @@ impl Div<&Z> for &MatZ {
/// Parameters:
/// - `divisor`: specifies the divisor by which the matrix is divided
///
/// Returns the quotient of `self` divided by `divisor` as a [`MatQ`]
/// or panics if the divisor is `0`.
/// Returns the quotient of `self` divided by `divisor` as a [`MatQ`].
///
/// # Examples
/// ```
Expand Down
3 changes: 2 additions & 1 deletion src/integer/mat_z/arithmetic/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ impl MatZ {
/// Parameters:
/// - `other`: specifies the value to multiply with `self`
///
/// Returns the product of `self` and `other` as a [`MatZ`].
/// Returns the product of `self` and `other` as a [`MatZ`] or
/// an error, if the dimensions of `self` and `other` do not match for multiplication.
///
/// # Examples
/// ```
Expand Down
3 changes: 2 additions & 1 deletion src/integer/mat_z/determinant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use crate::{
use flint_sys::fmpz_mat::fmpz_mat_det;

impl MatZ {
/// Returns the determinant of the matrix.
/// Returns the determinant of the matrix or an error if
/// the number of rows and columns is not equal.
///
/// # Examples
/// ```
Expand Down
11 changes: 5 additions & 6 deletions src/integer/mat_z/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ impl FromStr for MatZ {
type Err = MathError;

/// Creates a [`MatZ`] matrix with entries in [`Z`] from a [`String`].
/// The format of that string looks like this `[[1, 2, 3],[4, 5, 6]]` for a 2x3 matrix
/// with entries 1, 2, 3 in the first row and 4, 5, 6 in the second row.
///
/// Parameters:
/// - `string`: the matrix as a string
/// - `string`: the matrix of form: `"[[1, 2, 3],[4, 5, 6]]"`
/// for a 2x3 matrix with entries 1, 2, 3 in the first row and 4, 5, 6
/// in the second row.
///
/// Returns a [`MatZ`] or an error, if the matrix is not formatted in a suitable way,
/// Returns a [`MatZ`] or an error if the matrix is not formatted in a suitable way,
/// the number of rows or columns is too large (must fit into [`i64`]),
/// the number of entries in rows is unequal or if the regular expression
/// inside of the function could not be processed.
/// the number of entries in rows is unequal or if an entry is not formatted correctly.
///
/// # Examples
/// ```
Expand Down
17 changes: 9 additions & 8 deletions src/integer/mat_z/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl GetEntry<Z> for MatZ {
/// the last element.
///
/// Returns the [`Z`] value of the matrix at the position of the given
/// row and column or an error, if the number of rows or columns is
/// row and column or an error if the number of rows or columns is
/// greater than the matrix.
///
/// # Examples
Expand Down Expand Up @@ -109,7 +109,7 @@ impl MatZ {
/// - `row`: specifies the row of the matrix
///
/// Returns a row vector of the matrix at the position of the given
/// `row` or an error, if the number of rows is
/// `row` or an error if the number of rows is
/// greater than the matrix or negative.
///
/// # Examples
Expand Down Expand Up @@ -145,7 +145,7 @@ impl MatZ {
/// * `column`: specifies the column of the matrix
///
/// Returns a column vector of the matrix at the position of the given
/// `column` or an error, if the number of columns is
/// `column` or an error if the number of columns is
/// greater than the matrix or negative.
///
/// # Examples
Expand Down Expand Up @@ -183,15 +183,16 @@ impl MatZ {
/// Otherwise the function will panic.
///
/// Parameters:
/// `row_1`: The starting row of the submatrix
/// `row_2`: The ending row of the submatrix
/// `col_1`: The starting column of the submatrix
/// `col_2`: The ending column of the submatrix
/// `row_1`: the starting row of the submatrix
/// `row_2`: the ending row of the submatrix
/// `col_1`: the starting column of the submatrix
/// `col_2`: the ending column of the submatrix
///
/// Negative indices can be used to index from the back, e.g., `-1` for
/// the last element.
///
/// Returns the submatrix from `(row_1, col_1)` to `(row_2, col_2)`(inclusively).
/// Returns the submatrix from `(row_1, col_1)` to `(row_2, col_2)`(inclusively)
/// or an error if any provided row or column is greater than the matrix.
///
/// # Examples
/// ```
Expand Down
10 changes: 7 additions & 3 deletions src/integer/mat_z/sample/discrete_gauss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl MatZ {
/// to the standard deviation `sigma * sqrt(2 * pi) = s`
///
/// Returns a matrix with each entry sampled independently from the
/// specified discrete Gaussian distribution.
/// specified discrete Gaussian distribution or an error if the `n <= 1` or `s <= 0`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// specified discrete Gaussian distribution or an error if the `n <= 1` or `s <= 0`.
/// specified discrete Gaussian distribution or an error if `n <= 1` or `s <= 0`.

///
/// # Examples
/// ```
Expand Down Expand Up @@ -81,7 +81,9 @@ impl MatZ {
/// - `s`: specifies the Gaussian parameter, which is proportional
/// to the standard deviation `sigma * sqrt(2 * pi) = s`
///
/// Returns a lattice vector sampled according to the discrete Gaussian distribution.
/// Returns a lattice vector sampled according to the discrete Gaussian distribution
/// or an error if the `n <= 1` or `s <= 0`, the number of rows of the `basis` and `center` differ,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// or an error if the `n <= 1` or `s <= 0`, the number of rows of the `basis` and `center` differ,
/// or an error if `n <= 1` or `s <= 0`, the number of rows of the `basis` and `center` differ,

see the others as well -> this occurs more often

/// or if `center` is not a column vector.
///
/// # Examples
/// ```
Expand Down Expand Up @@ -158,7 +160,9 @@ impl MatZ {
/// - `s`: specifies the Gaussian parameter, which is proportional
/// to the standard deviation `sigma * sqrt(2 * pi) = s`
///
/// Returns a lattice vector sampled according to the discrete Gaussian distribution.
/// Returns a lattice vector sampled according to the discrete Gaussian distribution
/// or an error if the `n <= 1` or `s <= 0`, the number of rows of the `basis` and `center` differ,
/// or if `center` is not a column vector.
///
/// # Examples
/// ```
Expand Down
2 changes: 1 addition & 1 deletion src/integer/mat_z/vector/dot_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl MatZ {
/// Parameters:
/// - `other`: specifies the other vector the dot product is calculated over
///
/// Returns the resulting `dot_product` as a [`Z`] or an error,
/// Returns the resulting `dot_product` as a [`Z`] or an error
/// if the given [`MatZ`] instances aren't vectors or have different
/// numbers of entries.
///
Expand Down
6 changes: 4 additions & 2 deletions src/integer/mat_z/vector/norm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use crate::{
use flint_sys::fmpz::fmpz_addmul;

impl MatZ {
/// Returns the squared Euclidean norm or 2-norm of the given (row or column) vector.
/// Returns the squared Euclidean norm or 2-norm of the given (row or column) vector
/// or an error if the given [`MatZ`] instance is not a (row or column) vector.
///
/// # Examples
/// ```
Expand Down Expand Up @@ -57,7 +58,8 @@ impl MatZ {
Ok(result)
}

/// Returns the infinity norm or ∞-norm of the given (row or column) vector.
/// Returns the infinity norm or ∞-norm of the given (row or column) vector
/// or an error if the given [`MatZ`] instance is not a (row or column) vector.
///
/// # Examples
/// ```
Expand Down
Loading
Loading