1use crate::{Allocator, ArgminDiv, SameShapeAllocator};
9
10use crate::ClosedDiv;
11use nalgebra::{
12 base::{
13 constraint::{SameNumberOfColumns, SameNumberOfRows, ShapeConstraint},
14 dimension::Dim,
15 storage::Storage,
16 MatrixSum, Scalar,
17 },
18 DefaultAllocator, Matrix, OMatrix,
19};
20
21impl<N, R, C, S> ArgminDiv<N, OMatrix<N, R, C>> for Matrix<N, R, C, S>
22where
23 N: Scalar + Copy + ClosedDiv,
24 R: Dim,
25 C: Dim,
26 S: Storage<N, R, C>,
27 DefaultAllocator: Allocator<N, R, C>,
28{
29 #[inline]
30 fn div(&self, other: &N) -> OMatrix<N, R, C> {
31 self / *other
32 }
33}
34
35impl<N, R, C, S> ArgminDiv<Matrix<N, R, C, S>, OMatrix<N, R, C>> for N
36where
37 N: Scalar + Copy + ClosedDiv,
38 R: Dim,
39 C: Dim,
40 S: Storage<N, R, C>,
41 DefaultAllocator: Allocator<N, R, C>,
42{
43 #[inline]
44 fn div(&self, other: &Matrix<N, R, C, S>) -> OMatrix<N, R, C> {
45 other.map(|entry| *self / entry)
46 }
47}
48
49impl<N, R1, R2, C1, C2, SA, SB> ArgminDiv<Matrix<N, R2, C2, SB>, MatrixSum<N, R1, C1, R2, C2>>
50 for Matrix<N, R1, C1, SA>
51where
52 N: Scalar + ClosedDiv,
53 R1: Dim,
54 R2: Dim,
55 C1: Dim,
56 C2: Dim,
57 SA: Storage<N, R1, C1>,
58 SB: Storage<N, R2, C2>,
59 DefaultAllocator: SameShapeAllocator<N, R1, C1, R2, C2>,
60 ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2>,
61{
62 #[inline]
63 fn div(&self, other: &Matrix<N, R2, C2, SB>) -> MatrixSum<N, R1, C1, R2, C2> {
64 self.component_div(other)
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71 use approx::assert_relative_eq;
72 use nalgebra::{DMatrix, DVector, Matrix2x3, Vector3};
73 use paste::item;
74
75 macro_rules! make_test {
76 ($t:ty) => {
77 item! {
78 #[test]
79 fn [<test_div_vec_scalar_ $t>]() {
80 let a = Vector3::new(4 as $t, 16 as $t, 8 as $t);
81 let b = 2 as $t;
82 let target = Vector3::new(2 as $t, 8 as $t, 4 as $t);
83 let res = <Vector3<$t> as ArgminDiv<$t, Vector3<$t>>>::div(&a, &b);
84 for i in 0..3 {
85 assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = f64::EPSILON);
86 }
87 }
88 }
89
90 item! {
91 #[test]
92 fn [<test_div_scalar_vec_ $t>]() {
93 let a = Vector3::new(2 as $t, 4 as $t, 8 as $t);
94 let b = 32 as $t;
95 let target = Vector3::new(16 as $t, 8 as $t, 4 as $t);
96 let res = <$t as ArgminDiv<Vector3<$t>, Vector3<$t>>>::div(&b, &a);
97 for i in 0..3 {
98 assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = f64::EPSILON);
99 }
100 }
101 }
102
103 item! {
104 #[test]
105 fn [<test_div_vec_vec_ $t>]() {
106 let a = Vector3::new(4 as $t, 9 as $t, 8 as $t);
107 let b = Vector3::new(2 as $t, 3 as $t, 4 as $t);
108 let target = Vector3::new(2 as $t, 3 as $t, 2 as $t);
109 let res = <Vector3<$t> as ArgminDiv<Vector3<$t>, Vector3<$t>>>::div(&a, &b);
110 for i in 0..3 {
111 assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = f64::EPSILON);
112 }
113 }
114 }
115
116 item! {
117 #[test]
118 #[should_panic]
119 fn [<test_div_vec_vec_panic_ $t>]() {
120 let a = DVector::from_vec(vec![1 as $t, 4 as $t]);
121 let b = DVector::from_vec(vec![41 as $t, 38 as $t, 34 as $t]);
122 <DVector<$t> as ArgminDiv<DVector<$t>, DVector<$t>>>::div(&a, &b);
123 }
124 }
125
126 item! {
127 #[test]
128 #[should_panic]
129 fn [<test_div_vec_vec_panic_2_ $t>]() {
130 let a = DVector::from_vec(vec![]);
131 let b = DVector::from_vec(vec![41 as $t, 38 as $t, 34 as $t]);
132 <DVector<$t> as ArgminDiv<DVector<$t>, DVector<$t>>>::div(&a, &b);
133 }
134 }
135
136 item! {
137 #[test]
138 #[should_panic]
139 fn [<test_div_vec_vec_panic_3_ $t>]() {
140 let a = DVector::from_vec(vec![41 as $t, 38 as $t, 34 as $t]);
141 let b = DVector::from_vec(vec![]);
142 <DVector<$t> as ArgminDiv<DVector<$t>, DVector<$t>>>::div(&a, &b);
143 }
144 }
145
146 item! {
147 #[test]
148 fn [<test_div_mat_mat_ $t>]() {
149 let a = Matrix2x3::new(
150 4 as $t, 12 as $t, 8 as $t,
151 9 as $t, 20 as $t, 45 as $t
152 );
153 let b = Matrix2x3::new(
154 2 as $t, 3 as $t, 4 as $t,
155 3 as $t, 4 as $t, 5 as $t
156 );
157 let target = Matrix2x3::new(
158 2 as $t, 4 as $t, 2 as $t,
159 3 as $t, 5 as $t, 9 as $t
160 );
161 let res = <Matrix2x3<$t> as ArgminDiv<Matrix2x3<$t>, Matrix2x3<$t>>>::div(&a, &b);
162 for i in 0..3 {
163 for j in 0..2 {
164 assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = f64::EPSILON);
165 }
166 }
167 }
168 }
169
170 item! {
171 #[test]
172 #[should_panic]
173 fn [<test_div_mat_mat_panic_2_ $t>]() {
174 let a = DMatrix::from_vec(2, 3, vec![
175 1 as $t, 4 as $t, 8 as $t,
176 2 as $t, 5 as $t, 9 as $t
177 ]);
178 let b = DMatrix::from_vec(1, 2, vec![
179 41 as $t, 38 as $t,
180 ]);
181 <DMatrix<$t> as ArgminDiv<DMatrix<$t>, DMatrix<$t>>>::div(&a, &b);
182 }
183 }
184
185 item! {
186 #[test]
187 #[should_panic]
188 fn [<test_div_mat_mat_panic_3_ $t>]() {
189 let a = DMatrix::from_vec(2, 3, vec![
190 1 as $t, 4 as $t, 8 as $t,
191 2 as $t, 5 as $t, 9 as $t
192 ]);
193 let b = DMatrix::from_vec(0, 0, vec![]);
194 <DMatrix<$t> as ArgminDiv<DMatrix<$t>, DMatrix<$t>>>::div(&a, &b);
195 }
196 }
197 };
198 }
199
200 make_test!(i8);
201 make_test!(u8);
202 make_test!(i16);
203 make_test!(u16);
204 make_test!(i32);
205 make_test!(u32);
206 make_test!(i64);
207 make_test!(u64);
208 make_test!(f32);
209 make_test!(f64);
210}