From a8f5a23786c4cf6cbff80f0ffbc22c53f14d5b41 Mon Sep 17 00:00:00 2001
From: supinie <86788874+supinie@users.noreply.github.com>
Date: Wed, 5 Aug 2026 18:14:14 +0100
Subject: [PATCH 1/2] Add Neg trait for Z
---
src/integer/z/arithmetic.rs | 1 +
src/integer/z/arithmetic/neg.rs | 80 +++++++++++++++++++++++++++++++++
2 files changed, 81 insertions(+)
create mode 100644 src/integer/z/arithmetic/neg.rs
diff --git a/src/integer/z/arithmetic.rs b/src/integer/z/arithmetic.rs
index 4800b616..99a17edf 100644
--- a/src/integer/z/arithmetic.rs
+++ b/src/integer/z/arithmetic.rs
@@ -15,6 +15,7 @@ mod exp;
mod logarithm;
mod modulo;
mod mul;
+mod neg;
mod pow;
mod root;
mod sub;
diff --git a/src/integer/z/arithmetic/neg.rs b/src/integer/z/arithmetic/neg.rs
new file mode 100644
index 00000000..8bda59fa
--- /dev/null
+++ b/src/integer/z/arithmetic/neg.rs
@@ -0,0 +1,80 @@
+// Copyright 2026 Joshua Limbrey
+//
+// 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 .
+
+//! Implementation of the [`Neg`] trait for [`Z`] values.
+
+use super::super::Z;
+use flint3_sys::fmpz_neg;
+use std::ops::Neg;
+
+impl Neg for Z {
+ type Output = Z;
+
+ /// Implements the [`Neg`] trait for [`Z`] values.
+ /// [`Neg`] is implemented for both borrowed and owned [`Z`].
+ ///
+ /// When called on owned [`Z`] it will reuse the memory of `self`.
+ ///
+ /// Returns the additive inverse of `self` as a [`Z`].
+ ///
+ /// # Examples
+ /// ```
+ /// use qfall_math::integer::Z;
+ ///
+ /// let a: Z = Z::from(42);
+ ///
+ /// let b: Z = -&a;
+ /// let c: Z = -a;
+ /// ```
+ fn neg(mut self) -> Self::Output {
+ unsafe { fmpz_neg(&mut self.value, &self.value) };
+ self
+ }
+}
+
+impl Neg for &Z {
+ type Output = Z;
+
+ /// Documentation at [`Z::neg`].
+ fn neg(self) -> Self::Output {
+ let mut out = Z::default();
+ unsafe { fmpz_neg(&mut out.value, &self.value) };
+ out
+ }
+}
+
+#[cfg(test)]
+mod test_neg {
+ use super::Z;
+
+ /// Ensure that `neg` works for small numbers.
+ #[test]
+ fn correct_small() {
+ let a: Z = Z::ONE;
+ let b: Z = Z::MINUS_ONE;
+ let c: Z = Z::ZERO;
+
+ 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 numbers.
+ #[test]
+ fn correct_large() {
+ let a: Z = Z::from(u64::MAX);
+ let b: Z = Z::from(i64::MAX);
+
+ assert_eq!(a, -(-&a));
+ assert_eq!(b, -(-&b));
+ }
+}
From c6c2e2408ff3d1a0678dd2fa739e7bce20adf2a2 Mon Sep 17 00:00:00 2001
From: supinie <86788874+supinie@users.noreply.github.com>
Date: Thu, 6 Aug 2026 12:03:48 +0100
Subject: [PATCH 2/2] Add Neg trait for Q
---
src/rational/q/arithmetic.rs | 1 +
src/rational/q/arithmetic/neg.rs | 84 ++++++++++++++++++++++++++++++++
2 files changed, 85 insertions(+)
create mode 100644 src/rational/q/arithmetic/neg.rs
diff --git a/src/rational/q/arithmetic.rs b/src/rational/q/arithmetic.rs
index 0463437b..43edf024 100644
--- a/src/rational/q/arithmetic.rs
+++ b/src/rational/q/arithmetic.rs
@@ -14,6 +14,7 @@ mod div;
mod exp;
mod logarithm;
mod mul;
+mod neg;
mod pow;
mod root;
mod sub;
diff --git a/src/rational/q/arithmetic/neg.rs b/src/rational/q/arithmetic/neg.rs
new file mode 100644
index 00000000..58ddf60c
--- /dev/null
+++ b/src/rational/q/arithmetic/neg.rs
@@ -0,0 +1,84 @@
+// Copyright 2026 Joshua Limbrey
+//
+// 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 .
+
+//! Implementation of the [`Neg`] trait for [`Q`] values.
+
+use super::super::Q;
+use flint3_sys::fmpq_neg;
+use std::ops::Neg;
+
+impl Neg for Q {
+ type Output = Q;
+
+ /// Implements the [`Neg`] trait for [`Q`] values.
+ /// [`Neg`] is implements for both borrowed and owned [`Q`].
+ ///
+ /// When called on owned [`Q`] it will reuse the memory of `self`.
+ ///
+ /// Returns the additive inverse of `self` as a [`Q`].
+ ///
+ /// # Examples
+ /// ```
+ /// use qfall_math::rational::Q;
+ ///
+ /// let a: Q = Q::from((1, 42));
+ ///
+ /// let b: Q = -&a;
+ /// let c: Q = -a;
+ /// ```
+ fn neg(mut self) -> Self::Output {
+ unsafe { fmpq_neg(&mut self.value, &self.value) };
+ self
+ }
+}
+
+impl Neg for &Q {
+ type Output = Q;
+
+ /// Documentation at [`Q::neg`].
+ fn neg(self) -> Self::Output {
+ let mut out = Q::default();
+ unsafe { fmpq_neg(&mut out.value, &self.value) };
+ out
+ }
+}
+
+#[cfg(test)]
+mod test_neg {
+ use super::Q;
+
+ /// Ensure that `neg` works for small numbers.
+ #[test]
+ fn correct_small() {
+ let a: Q = Q::ONE;
+ let b: Q = Q::MINUS_ONE;
+ let c: Q = Q::ZERO;
+
+ let d: Q = Q::from((1, 42));
+
+ 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);
+ assert_eq!(d, -(-&d));
+ assert_eq!(d.clone(), -(-d));
+ }
+
+ /// Ensure that `neg` works for large numbers.
+ #[test]
+ fn correct_large() {
+ let a: Q = Q::from(u64::MAX);
+ let b: Q = Q::from(i64::MAX);
+
+ assert_eq!(a, -(-&a));
+ assert_eq!(b, -(-&b));
+ }
+}