use super::super::MatQ;
use crate::error::MathError;
use crate::integer::MatZ;
use crate::macros::arithmetics::{
arithmetic_assign_trait_borrowed_to_owned, arithmetic_trait_borrowed_to_owned,
arithmetic_trait_mixed_borrowed_owned,
};
use crate::traits::MatrixDimensions;
use flint_sys::fmpq_mat::fmpq_mat_sub;
use std::ops::{Sub, SubAssign};
impl SubAssign<&MatQ> for MatQ {
fn sub_assign(&mut self, other: &Self) {
if self.get_num_rows() != other.get_num_rows()
|| self.get_num_columns() != other.get_num_columns()
{
panic!(
"Tried to subtract a '{}x{}' matrix and a '{}x{}' matrix.",
self.get_num_rows(),
self.get_num_columns(),
other.get_num_rows(),
other.get_num_columns()
);
}
unsafe { fmpq_mat_sub(&mut self.matrix, &self.matrix, &other.matrix) };
}
}
impl SubAssign<&MatZ> for MatQ {
fn sub_assign(&mut self, other: &MatZ) {
if self.get_num_rows() != other.get_num_rows()
|| self.get_num_columns() != other.get_num_columns()
{
panic!(
"Tried to subtract a '{}x{}' matrix and a '{}x{}' matrix.",
self.get_num_rows(),
self.get_num_columns(),
other.get_num_rows(),
other.get_num_columns()
);
}
let other = MatQ::from(other);
unsafe {
fmpq_mat_sub(&mut self.matrix, &self.matrix, &other.matrix);
}
}
}
arithmetic_assign_trait_borrowed_to_owned!(SubAssign, sub_assign, MatQ, MatQ);
arithmetic_assign_trait_borrowed_to_owned!(SubAssign, sub_assign, MatQ, MatZ);
impl Sub for &MatQ {
type Output = MatQ;
fn sub(self, other: Self) -> Self::Output {
self.sub_safe(other).unwrap()
}
}
impl Sub<&MatZ> for &MatQ {
type Output = MatQ;
fn sub(self, other: &MatZ) -> Self::Output {
let other = MatQ::from(other);
self.sub_safe(&other).unwrap()
}
}
arithmetic_trait_borrowed_to_owned!(Sub, sub, MatQ, MatZ, MatQ);
arithmetic_trait_mixed_borrowed_owned!(Sub, sub, MatQ, MatZ, MatQ);
impl MatQ {
pub fn sub_safe(&self, other: &Self) -> Result<MatQ, MathError> {
if self.get_num_rows() != other.get_num_rows()
|| self.get_num_columns() != other.get_num_columns()
{
return Err(MathError::MismatchingMatrixDimension(format!(
"Tried to subtract a '{}x{}' matrix and a '{}x{}' matrix.",
other.get_num_rows(),
other.get_num_columns(),
self.get_num_rows(),
self.get_num_columns()
)));
}
let mut out = MatQ::new(self.get_num_rows(), self.get_num_columns());
unsafe {
fmpq_mat_sub(&mut out.matrix, &self.matrix, &other.matrix);
}
Ok(out)
}
}
arithmetic_trait_borrowed_to_owned!(Sub, sub, MatQ, MatQ, MatQ);
arithmetic_trait_mixed_borrowed_owned!(Sub, sub, MatQ, MatQ, MatQ);
#[cfg(test)]
mod test_sub_assign {
use crate::{integer::MatZ, rational::MatQ};
use std::str::FromStr;
#[test]
fn correct_small() {
let mut a = MatQ::identity(2, 2);
let b = MatQ::from_str("[[-4/5, -5],[6, 1]]").unwrap();
let mut c = a.clone();
let d = MatZ::from_str("[[-4, -5],[6, 1]]").unwrap();
let cmp_0 = MatQ::from_str("[[9/5, 5],[-6, 0]]").unwrap();
let cmp_1 = MatQ::from_str("[[5, 5],[-6, 0]]").unwrap();
a -= b;
c -= d;
assert_eq!(cmp_0, a);
assert_eq!(cmp_1, c);
}
#[test]
fn correct_large() {
let mut a = MatQ::from_str(&format!("[[{}/1, 5/2],[{}, -1]]", i64::MAX, i64::MIN)).unwrap();
let b = MatQ::from_str(&format!("[[-{}, 6/2],[-6, 1]]", i64::MAX)).unwrap();
let cmp = MatQ::from_str(&format!(
"[[{}, -1/2],[{}, -2]]",
2 * (i64::MAX as u64),
i64::MIN + 6
))
.unwrap();
a -= b;
assert_eq!(cmp, a);
}
#[test]
fn matrix_dimensions() {
let dimensions = [(3, 3), (5, 1), (1, 4)];
for (nr_rows, nr_cols) in dimensions {
let mut a = MatQ::identity(nr_rows, nr_cols);
let b = MatQ::new(nr_rows, nr_cols);
a -= b;
assert_eq!(MatQ::identity(nr_rows, nr_cols), a);
}
}
#[test]
fn availability() {
let mut a = MatQ::new(2, 2);
let b = MatQ::new(2, 2);
let c = MatZ::new(2, 2);
a -= &b;
a -= b;
a -= &c;
a -= c;
}
}
#[cfg(test)]
mod test_sub {
use super::MatQ;
use crate::{integer::MatZ, rational::Q};
use std::str::FromStr;
#[test]
fn sub() {
let a: MatQ = MatQ::from_str("[[1/2, 1, 2],[3/7, 4/7, -5]]").unwrap();
let b: MatQ = MatQ::from_str("[[1/2, 2, 3/4],[3/7, -4/9, 5]]").unwrap();
let c: MatQ = a - b;
assert_eq!(c, MatQ::from_str("[[0, -1, 5/4],[0, 64/63, -10]]").unwrap());
}
#[test]
fn sub_borrow() {
let a: MatQ = MatQ::from_str("[[1/2, 1, 2],[3/7, 4/7, -5]]").unwrap();
let b: MatQ = MatQ::from_str("[[1/2, 2, 3/4],[3/7, -4/9, 5]]").unwrap();
let c: MatQ = &a - &b;
assert_eq!(c, MatQ::from_str("[[0, -1, 5/4],[0, 64/63, -10]]").unwrap());
}
#[test]
fn sub_first_borrowed() {
let a: MatQ = MatQ::from_str("[[1/2, 1, 2],[3/7, 4/7, -5]]").unwrap();
let b: MatQ = MatQ::from_str("[[1/2, 2, 3/4],[3/7, -4/9, 5]]").unwrap();
let c: MatQ = &a - b;
assert_eq!(c, MatQ::from_str("[[0, -1, 5/4],[0, 64/63, -10]]").unwrap());
}
#[test]
fn sub_second_borrowed() {
let a: MatQ = MatQ::from_str("[[1/2, 1, 2],[3/7, 4/7, -5]]").unwrap();
let b: MatQ = MatQ::from_str("[[1/2, 2, 3/4],[3/7, -4/9, 5]]").unwrap();
let c: MatQ = a - &b;
assert_eq!(c, MatQ::from_str("[[0, -1, 5/4],[0, 64/63, -10]]").unwrap());
}
#[test]
fn sub_large_numbers() {
let a: MatQ =
MatQ::from_str(&format!("[[1, 2, {}],[3, -4, {}]]", i64::MIN, u64::MAX)).unwrap();
let b: MatQ =
MatQ::from_str(&format!("[[1, 1, {}],[3, 9, 1/{}]]", i64::MAX, i64::MAX)).unwrap();
let c: MatQ = a - &b;
assert_eq!(
c,
MatQ::from_str(&format!(
"[[0, 1, -{}],[0, -13, {}]]",
u64::MAX,
Q::from(u64::MAX) - Q::from((1, i64::MAX))
))
.unwrap()
);
}
#[test]
fn sub_safe() {
let a: MatQ = MatQ::from_str("[[1/2, 1, 2],[3/7, 4/7, -5]]").unwrap();
let b: MatQ = MatQ::from_str("[[1/2, 2, 3/4],[3/7, -4/9, 5]]").unwrap();
let c: MatQ = a.sub_safe(&b).unwrap();
assert_eq!(c, MatQ::from_str("[[0, -1, 5/4],[0, 64/63, -10]]").unwrap());
}
#[test]
fn sub_safe_is_err() {
let a: MatQ = MatQ::from_str("[[1, 2],[3, 4]]").unwrap();
let b: MatQ = MatQ::from_str("[[1, 2, 3],[3, -4, 5]]").unwrap();
let c: MatQ = MatQ::from_str("[[1, 2, 3]]").unwrap();
assert!(a.sub_safe(&b).is_err());
assert!(c.sub_safe(&b).is_err());
}
#[test]
fn availability() {
let a = MatQ::new(2, 2);
let b = MatZ::new(2, 2);
let c = MatQ::new(2, 2);
let _ = &a - &b;
let _ = &a - b.clone();
let _ = a.clone() - &b;
let _ = a.clone() - b.clone();
let _ = &b - &a;
let _ = &b - a.clone();
let _ = b.clone() - &a;
let _ = b.clone() - a.clone();
let _ = &a - &c;
let _ = &a - c.clone();
let _ = a.clone() - &c;
let _ = a.clone() - c.clone();
let _ = &c - &a;
let _ = &c - a.clone();
let _ = c.clone() - &a;
let _ = c.clone() - a.clone();
}
}
#[cfg(test)]
mod test_sub_matz {
use super::MatZ;
use crate::rational::{MatQ, Q};
use std::str::FromStr;
#[test]
fn small_numbers() {
let a = MatZ::from_str("[[1, 2],[3, 4]]").unwrap();
let b = MatQ::from_str("[[5, 1/2],[2, 10]]").unwrap();
let cmp = MatQ::from_str("[[4, -3/2],[-1, 6]]").unwrap();
let res = &b - &a;
assert_eq!(res, cmp);
}
#[test]
fn large_numbers() {
let a: MatZ = MatZ::from_str(&format!("[[1, 1, {}],[3, 9, 4]]", i64::MIN)).unwrap();
let b = MatQ::from_str(&format!("[[1, 2, 1/{}],[3, -4, 5]]", i64::MAX)).unwrap();
let c = &b - a;
assert_eq!(
c,
MatQ::from_str(&format!(
"[[0, 1, {}],[0, -13, 1]]",
Q::from((1, i64::MAX)) - Q::from((i64::MIN, 1))
))
.unwrap()
);
}
#[test]
fn available() {
let a = MatZ::new(2, 2);
let b = MatQ::new(2, 2);
let _ = &b - &a;
let _ = &b - a.clone();
let _ = b.clone() - &a;
let _ = b.clone() - a.clone();
}
#[test]
#[should_panic]
fn mismatching_rows() {
let a = MatZ::new(3, 2);
let b = MatQ::new(2, 2);
let _ = &b - &a;
}
#[test]
#[should_panic]
fn mismatching_column() {
let a = MatZ::new(2, 3);
let b = MatQ::new(2, 2);
let _ = &b - &a;
}
}