Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/integer/mat_poly_over_z/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ mod add;
mod modulo;
mod mul;
mod mul_scalar;
mod neg;
mod sub;
90 changes: 90 additions & 0 deletions src/integer/mat_poly_over_z/arithmetic/neg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2026 Jan Niklas Siemer
//
// This file is part of qFALL-math
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.

//! Implementation of the [`Neg`] trait for [`MatPolyOverZ`] values.

use super::super::MatPolyOverZ;
use crate::traits::MatrixDimensions;
use flint3_sys::fmpz_poly_mat_neg;
use std::ops::Neg;

impl Neg for MatPolyOverZ {
type Output = MatPolyOverZ;

/// Implements the [`Neg`] trait for [`MatPolyOverZ`] values.
/// [`Neg`] is implemented for both borrowed and owned [`MatPolyOverZ`].
///
/// When called on owned [`MatPolyOverZ`] it will reuse the memory of `self`.
///
/// Returns the additive inverse of `self` as a [`MatPolyOverZ`].
///
/// # Examples
/// ```
/// use qfall_math::integer::MatPolyOverZ;
/// use std::str::FromStr;
///
/// let a: MatPolyOverZ = MatPolyOverZ::from_str("[[2 1 -2, 0],[1 42, 3 1 2 3]]").unwrap();
///
/// let b: MatPolyOverZ = -&a;
/// let c: MatPolyOverZ = -a;
/// ```
fn neg(mut self) -> Self::Output {
unsafe { fmpz_poly_mat_neg(&mut self.matrix, &self.matrix) };
self
}
}

impl Neg for &MatPolyOverZ {
type Output = MatPolyOverZ;

/// Documentation at [`MatPolyOverZ::neg`].
fn neg(self) -> Self::Output {
let mut out = MatPolyOverZ::new(self.get_num_rows(), self.get_num_columns());
unsafe { fmpz_poly_mat_neg(&mut out.matrix, &self.matrix) };
out
}
}

#[cfg(test)]
mod test_neg {
use super::MatPolyOverZ;
use std::str::FromStr;

/// Ensure that `neg` works for small coefficients.
#[test]
fn correct_small() {
let a = MatPolyOverZ::from_str("[[2 1 -2, 0, 1 5],[1 -3, 3 1 0 4, 0]]").unwrap();
let b = MatPolyOverZ::from_str("[[2 -1 2, 0, 1 -5],[1 3, 3 -1 0 -4, 0]]").unwrap();
let c = MatPolyOverZ::new(2, 3);

assert_eq!(a, -(-&a));
assert_eq!(a, -&b);
assert_eq!(b, -&a);
assert_eq!(a, -b.clone());
assert_eq!(b, -a);
assert_eq!(c, -&c);
assert_eq!(c.clone(), -c);
}

/// Ensure that `neg` works for large coefficients.
#[test]
fn correct_large() {
let a =
MatPolyOverZ::from_str(&format!("[[2 {} {}],[1 1]]", u64::MAX, i64::MIN)).unwrap();
let b = MatPolyOverZ::from_str(&format!(
"[[2 -{} {}],[1 -1]]",
u64::MAX,
i64::MAX as u64 + 1
))
.unwrap();

assert_eq!(a, -(-&a));
assert_eq!(b, -&a);
assert_eq!(b, -a);
}
}
1 change: 1 addition & 0 deletions src/integer/mat_z/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ mod div;
mod modulo;
mod mul;
mod mul_scalar;
mod neg;
mod sub;
89 changes: 89 additions & 0 deletions src/integer/mat_z/arithmetic/neg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2026 Jan Niklas Siemer
//
// This file is part of qFALL-math
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.

//! Implementation of the [`Neg`] trait for [`MatZ`] values.

use super::super::MatZ;
use crate::traits::MatrixDimensions;
use flint3_sys::fmpz_mat_neg;
use std::ops::Neg;

impl Neg for MatZ {
type Output = MatZ;

/// Implements the [`Neg`] trait for [`MatZ`] values.
/// [`Neg`] is implemented for both borrowed and owned [`MatZ`].
///
/// When called on owned [`MatZ`] it will reuse the memory of `self`.
///
/// Returns the additive inverse of `self` as a [`MatZ`].
///
/// # Examples
/// ```
/// use qfall_math::integer::MatZ;
/// use std::str::FromStr;
///
/// let a: MatZ = MatZ::from_str("[[1, -2],[3, 42]]").unwrap();
///
/// let b: MatZ = -&a;
/// let c: MatZ = -a;
/// ```
fn neg(mut self) -> Self::Output {
unsafe { fmpz_mat_neg(&mut self.matrix, &self.matrix) };
self
}
}

impl Neg for &MatZ {
type Output = MatZ;

/// Documentation at [`MatZ::neg`].
fn neg(self) -> Self::Output {
let mut out = MatZ::new(self.get_num_rows(), self.get_num_columns());
unsafe { fmpz_mat_neg(&mut out.matrix, &self.matrix) };
out
}
}

#[cfg(test)]
mod test_neg {
use super::MatZ;
use std::str::FromStr;

/// Ensure that `neg` works for small entries.
#[test]
fn correct_small() {
let a = MatZ::from_str("[[1, -2, 0],[3, 4, -5]]").unwrap();
let b = MatZ::from_str("[[-1, 2, 0],[-3, -4, 5]]").unwrap();
let c = MatZ::new(2, 3);

assert_eq!(a, -(-&a));
assert_eq!(a, -&b);
assert_eq!(b, -&a);
assert_eq!(a, -b.clone());
assert_eq!(b, -a);
assert_eq!(c, -&c);
assert_eq!(c.clone(), -c);
}

/// Ensure that `neg` works for large entries.
#[test]
fn correct_large() {
let a = MatZ::from_str(&format!("[[{}, {}],[1, 2]]", u64::MAX, i64::MIN)).unwrap();
let b = MatZ::from_str(&format!(
"[[-{}, {}],[-1, -2]]",
u64::MAX,
i64::MAX as u64 + 1
))
.unwrap();

assert_eq!(a, -(-&a));
assert_eq!(b, -&a);
assert_eq!(b, -a);
}
}
1 change: 1 addition & 0 deletions src/integer/poly_over_z/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ mod add;
mod modulo;
mod mul;
mod mul_scalar;
mod neg;
mod sub;
84 changes: 84 additions & 0 deletions src/integer/poly_over_z/arithmetic/neg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2026 Jan Niklas Siemer
//
// This file is part of qFALL-math
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.

//! Implementation of the [`Neg`] trait for [`PolyOverZ`] values.

use super::super::PolyOverZ;
use flint3_sys::fmpz_poly_neg;
use std::ops::Neg;

impl Neg for PolyOverZ {
type Output = PolyOverZ;

/// Implements the [`Neg`] trait for [`PolyOverZ`] values.
/// [`Neg`] is implemented for both borrowed and owned [`PolyOverZ`].
///
/// When called on owned [`PolyOverZ`] it will reuse the memory of `self`.
///
/// Returns the additive inverse of `self` as a [`PolyOverZ`].
///
/// # Examples
/// ```
/// use qfall_math::integer::PolyOverZ;
/// use std::str::FromStr;
///
/// let a: PolyOverZ = PolyOverZ::from_str("3 1 -2 42").unwrap();
///
/// let b: PolyOverZ = -&a;
/// let c: PolyOverZ = -a;
/// ```
fn neg(mut self) -> Self::Output {
unsafe { fmpz_poly_neg(&mut self.poly, &self.poly) };
self
}
}

impl Neg for &PolyOverZ {
type Output = PolyOverZ;

/// Documentation at [`PolyOverZ::neg`].
fn neg(self) -> Self::Output {
let mut out = PolyOverZ::default();
unsafe { fmpz_poly_neg(&mut out.poly, &self.poly) };
out
}
}

#[cfg(test)]
mod test_neg {
use super::PolyOverZ;
use std::str::FromStr;

/// Ensure that `neg` works for small coefficients.
#[test]
fn correct_small() {
let a = PolyOverZ::from_str("4 1 -2 0 3").unwrap();
let b = PolyOverZ::from_str("4 -1 2 0 -3").unwrap();
let c = PolyOverZ::default();

assert_eq!(a, -(-&a));
assert_eq!(a, -&b);
assert_eq!(b, -&a);
assert_eq!(a, -b.clone());
assert_eq!(b, -a);
assert_eq!(c, -&c);
assert_eq!(c.clone(), -c);
}

/// Ensure that `neg` works for large coefficients.
#[test]
fn correct_large() {
let a = PolyOverZ::from_str(&format!("3 {} 0 {}", u64::MAX, i64::MIN)).unwrap();
let b =
PolyOverZ::from_str(&format!("3 -{} 0 {}", u64::MAX, i64::MAX as u64 + 1)).unwrap();

assert_eq!(a, -(-&a));
assert_eq!(b, -&a);
assert_eq!(b, -a);
}
}
1 change: 1 addition & 0 deletions src/integer_mod_q/mat_polynomial_ring_zq/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
mod add;
mod mul;
mod mul_scalar;
mod neg;
mod sub;
Loading
Loading