use super::super::PolyOverQ;
use crate::{
integer::PolyOverZ,
macros::arithmetics::{
arithmetic_assign_trait_borrowed_to_owned, arithmetic_trait_borrowed_to_owned,
arithmetic_trait_mixed_borrowed_owned,
},
};
use flint_sys::fmpq_poly::fmpq_poly_sub;
use std::ops::{Sub, SubAssign};
impl SubAssign<&PolyOverQ> for PolyOverQ {
fn sub_assign(&mut self, other: &Self) {
unsafe { fmpq_poly_sub(&mut self.poly, &self.poly, &other.poly) };
}
}
impl SubAssign<&PolyOverZ> for PolyOverQ {
fn sub_assign(&mut self, other: &PolyOverZ) {
let other = PolyOverQ::from(other);
self.sub_assign(other);
}
}
arithmetic_assign_trait_borrowed_to_owned!(SubAssign, sub_assign, PolyOverQ, PolyOverQ);
arithmetic_assign_trait_borrowed_to_owned!(SubAssign, sub_assign, PolyOverQ, PolyOverZ);
impl Sub for &PolyOverQ {
type Output = PolyOverQ;
fn sub(self, other: Self) -> Self::Output {
let mut out = PolyOverQ::default();
unsafe {
fmpq_poly_sub(&mut out.poly, &self.poly, &other.poly);
}
out
}
}
arithmetic_trait_borrowed_to_owned!(Sub, sub, PolyOverQ, PolyOverQ, PolyOverQ);
arithmetic_trait_mixed_borrowed_owned!(Sub, sub, PolyOverQ, PolyOverQ, PolyOverQ);
impl Sub<&PolyOverZ> for &PolyOverQ {
type Output = PolyOverQ;
fn sub(self, other: &PolyOverZ) -> Self::Output {
let mut out = PolyOverQ::default();
unsafe {
fmpq_poly_sub(&mut out.poly, &self.poly, &PolyOverQ::from(other).poly);
}
out
}
}
arithmetic_trait_borrowed_to_owned!(Sub, sub, PolyOverQ, PolyOverZ, PolyOverQ);
arithmetic_trait_mixed_borrowed_owned!(Sub, sub, PolyOverQ, PolyOverZ, PolyOverQ);
#[cfg(test)]
mod test_sub_assign {
use super::PolyOverQ;
use crate::{integer::PolyOverZ, rational::Q};
use std::str::FromStr;
#[test]
fn correct_small() {
let mut a = PolyOverQ::from_str("3 1/7 2/7 -3").unwrap();
let b = PolyOverQ::from_str("3 -1 2/7 -3").unwrap();
let cmp = PolyOverQ::from_str("1 8/7").unwrap();
a -= &b;
assert_eq!(cmp, a);
}
#[test]
fn correct_large() {
let mut a: PolyOverQ = PolyOverQ::from_str(&format!(
"3 {} {}/{} {}",
u64::MAX,
i64::MIN,
u128::MAX,
i64::MAX
))
.unwrap();
let b = PolyOverQ::from_str(&format!("2 -{} -{}", u64::MAX, i64::MAX)).unwrap();
let cmp = PolyOverQ::from_str(&format!(
"3 {} {} {}",
u128::from(u64::MAX) * 2,
(Q::from_str(&format!("{}/{}", i64::MIN, u128::MAX)).unwrap() + Q::from(i64::MAX)),
i64::MAX
))
.unwrap();
a -= b;
assert_eq!(cmp, a);
}
#[test]
fn availability() {
let mut a = PolyOverQ::from_str("3 1 2 -3").unwrap();
let b = PolyOverQ::from_str("3 -1 -2 3").unwrap();
let c = PolyOverZ::from_str("4 2 -1 2 3").unwrap();
a -= &b;
a -= b;
a -= &c;
a -= c;
}
}
#[cfg(test)]
mod test_sub {
use super::PolyOverQ;
use crate::rational::Q;
use std::str::FromStr;
#[test]
fn sub() {
let a: PolyOverQ = PolyOverQ::from_str("3 1/9 2 -3/7").unwrap();
let b: PolyOverQ = PolyOverQ::from_str("5 1/8 -2/9 5/7 1 2/9").unwrap();
let c: PolyOverQ = a - b;
assert_eq!(
c,
PolyOverQ::from_str("5 -1/72 20/9 -8/7 -1 -2/9").unwrap()
);
}
#[test]
fn sub_borrow() {
let a: PolyOverQ = PolyOverQ::from_str("3 1/9 2 -3/7").unwrap();
let b: PolyOverQ = PolyOverQ::from_str("5 1/8 -2/9 5/7 1 2/9").unwrap();
let c: PolyOverQ = &a - &b;
assert_eq!(
c,
PolyOverQ::from_str("5 -1/72 20/9 -8/7 -1 -2/9").unwrap()
);
}
#[test]
fn sub_first_borrowed() {
let a: PolyOverQ = PolyOverQ::from_str("3 1/9 2 -3/7").unwrap();
let b: PolyOverQ = PolyOverQ::from_str("5 1/8 -2/9 5/7 1 2/9").unwrap();
let c: PolyOverQ = &a - b;
assert_eq!(
c,
PolyOverQ::from_str("5 -1/72 20/9 -8/7 -1 -2/9").unwrap()
);
}
#[test]
fn sub_second_borrowed() {
let a: PolyOverQ = PolyOverQ::from_str("3 1/9 2 -3/7").unwrap();
let b: PolyOverQ = PolyOverQ::from_str("5 1/8 -2/9 5/7 1 2/9").unwrap();
let c: PolyOverQ = a - &b;
assert_eq!(
c,
PolyOverQ::from_str("5 -1/72 20/9 -8/7 -1 -2/9").unwrap()
);
}
#[test]
fn sub_eliminating_coefficients() {
let a: PolyOverQ = PolyOverQ::from_str("3 1/8 2/7 -3").unwrap();
let b: PolyOverQ = PolyOverQ::from_str("3 1/8 2/7 -3").unwrap();
let c: PolyOverQ = a - b;
assert_eq!(c, PolyOverQ::default());
}
#[test]
fn sub_large_numbers() {
let a: PolyOverQ = PolyOverQ::from_str(&format!(
"3 {} {}/{} {}",
u64::MAX,
i64::MIN,
u128::MAX - 126,
i64::MAX
))
.unwrap();
let b: PolyOverQ = PolyOverQ::from_str(&format!("2 {} {}", u64::MAX, i64::MAX)).unwrap();
let c: PolyOverQ = a - b;
assert!(
c == PolyOverQ::from_str(&format!(
"3 0 {} {}",
(Q::from_str(&format!("{}/{}", i64::MIN, u128::MAX - 126)).unwrap()
- Q::from(i64::MAX)),
i64::MAX
))
.unwrap()
);
}
}
#[cfg(test)]
mod test_mul_poly_over_z {
use super::PolyOverQ;
use crate::integer::PolyOverZ;
use std::str::FromStr;
#[test]
fn borrowed_correctness() {
let poly_1 = PolyOverQ::from_str(&format!("1 1/{}", i64::MAX)).unwrap();
let poly_2 = PolyOverZ::from_str("2 1 2").unwrap();
let poly_cmp =
PolyOverQ::from_str(&format!("2 {}/{} -2", -i64::MAX as i128 + 1, i64::MAX)).unwrap();
let poly_1 = &poly_1 - &poly_2;
assert_eq!(poly_cmp, poly_1);
}
#[test]
fn availability() {
let poly = PolyOverQ::from_str("3 1/2 2 3/7").unwrap();
let z = PolyOverZ::from(2);
_ = poly.clone() - z.clone();
_ = &poly - &z;
_ = &poly - z.clone();
_ = poly.clone() - &z;
}
}