1use crate::{Allocator, ArgminAdd, SameShapeAllocator};
9
10use crate::ClosedAdd;
11use nalgebra::{
12 base::{
13 constraint::{SameNumberOfColumns, SameNumberOfRows, ShapeConstraint},
14 dimension::Dim,
15 storage::Storage,
16 Scalar,
17 },
18 DefaultAllocator, Matrix, MatrixSum, OMatrix,
19};
20
21impl<N, R, C, S> ArgminAdd<N, OMatrix<N, R, C>> for Matrix<N, R, C, S>
22where
23 N: Scalar + ClosedAdd + Copy,
24 R: Dim,
25 C: Dim,
26 S: Storage<N, R, C>,
27 DefaultAllocator: Allocator<N, R, C>,
28{
29 #[inline]
30 fn add(&self, other: &N) -> OMatrix<N, R, C> {
31 self.add_scalar(*other)
32 }
33}
34
35impl<N, R, C, S> ArgminAdd<Matrix<N, R, C, S>, OMatrix<N, R, C>> for N
36where
37 N: Scalar + ClosedAdd + Copy,
38 R: Dim,
39 C: Dim,
40 S: Storage<N, R, C>,
41 DefaultAllocator: Allocator<N, R, C>,
42{
43 #[inline]
44 fn add(&self, other: &Matrix<N, R, C, S>) -> OMatrix<N, R, C> {
45 other.add_scalar(*self)
46 }
47}
48
49impl<N, R1, C1, R2, C2, SA, SB> ArgminAdd<Matrix<N, R2, C2, SB>, MatrixSum<N, R1, C1, R2, C2>>
50 for Matrix<N, R1, C1, SA>
51where
52 N: Scalar + ClosedAdd,
53 R1: Dim,
54 C1: Dim,
55 R2: 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 add(&self, other: &Matrix<N, R2, C2, SB>) -> MatrixSum<N, R1, C1, R2, C2> {
64 self + 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_add_vec_scalar_ $t>]() {
80 let a = Vector3::new(1 as $t, 4 as $t, 8 as $t);
81 let b = 34 as $t;
82 let target = Vector3::new(35 as $t, 38 as $t, 42 as $t);
83 let res = <Vector3<$t> as ArgminAdd<$t, Vector3<$t>>>::add(&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_add_scalar_vec_ $t>]() {
93 let a = Vector3::new(1 as $t, 4 as $t, 8 as $t);
94 let b = 34 as $t;
95 let target = Vector3::new(35 as $t, 38 as $t, 42 as $t);
96 let res = <$t as ArgminAdd<Vector3<$t>, Vector3<$t>>>::add(&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_add_vec_vec_ $t>]() {
106 let a = Vector3::new(1 as $t, 4 as $t, 8 as $t);
107 let b = Vector3::new(41 as $t, 38 as $t, 34 as $t);
108 let target = Vector3::new(42 as $t, 42 as $t, 42 as $t);
109 let res = <Vector3<$t> as ArgminAdd<Vector3<$t>, Vector3<$t>>>::add(&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_add_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 ArgminAdd<DVector<$t>, DVector<$t>>>::add(&a, &b);
123 }
124 }
125
126 item! {
127 #[test]
128 #[should_panic]
129 fn [<test_add_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 ArgminAdd<DVector<$t>, DVector<$t>>>::add(&a, &b);
133 }
134 }
135
136 item! {
137 #[test]
138 #[should_panic]
139 fn [<test_add_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 ArgminAdd<DVector<$t>, DVector<$t>>>::add(&a, &b);
143 }
144 }
145
146 item! {
147 #[test]
148 fn [<test_add_mat_mat_ $t>]() {
149 let a = Matrix2x3::new(
150 1 as $t, 4 as $t, 8 as $t,
151 2 as $t, 5 as $t, 9 as $t
152 );
153 let b = Matrix2x3::new(
154 41 as $t, 38 as $t, 34 as $t,
155 40 as $t, 37 as $t, 33 as $t
156 );
157 let target = Matrix2x3::new(
158 42 as $t, 42 as $t, 42 as $t,
159 42 as $t, 42 as $t, 42 as $t
160 );
161 let res = <Matrix2x3<$t> as ArgminAdd<Matrix2x3<$t>, Matrix2x3<$t>>>::add(&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 fn [<test_add_mat_scalar_ $t>]() {
173 let a = Matrix2x3::new(
174 1 as $t, 4 as $t, 8 as $t,
175 2 as $t, 5 as $t, 9 as $t
176 );
177 let b = 2 as $t;
178 let target = Matrix2x3::new(
179 3 as $t, 6 as $t, 10 as $t,
180 4 as $t, 7 as $t, 11 as $t
181 );
182 let res = <Matrix2x3<$t> as ArgminAdd<$t, Matrix2x3<$t>>>::add(&a, &b);
183 for i in 0..3 {
184 for j in 0..2 {
185 assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = f64::EPSILON);
186 }
187 }
188 }
189 }
190
191 item! {
192 #[test]
193 #[should_panic]
194 fn [<test_add_mat_mat_panic_2_ $t>]() {
195 let a = DMatrix::from_vec(2, 3, vec![
196 1 as $t, 4 as $t, 8 as $t,
197 2 as $t, 5 as $t, 9 as $t
198 ]);
199 let b = DMatrix::from_vec(1, 2, vec![
200 41 as $t, 38 as $t,
201 ]);
202 <DMatrix<$t> as ArgminAdd<DMatrix<$t>, DMatrix<$t>>>::add(&a, &b);
203 }
204 }
205
206 item! {
207 #[test]
208 #[should_panic]
209 fn [<test_add_mat_mat_panic_3_ $t>]() {
210 let a = DMatrix::from_vec(2, 3, vec![
211 1 as $t, 4 as $t, 8 as $t,
212 2 as $t, 5 as $t, 9 as $t
213 ]);
214 let b = DMatrix::from_vec(0, 0, vec![]);
215 <DMatrix<$t> as ArgminAdd<DMatrix<$t>, DMatrix<$t>>>::add(&a, &b);
216 }
217 }
218 };
219 }
220
221 make_test!(i8);
222 make_test!(u8);
223 make_test!(i16);
224 make_test!(u16);
225 make_test!(i32);
226 make_test!(u32);
227 make_test!(i64);
228 make_test!(u64);
229 make_test!(f32);
230 make_test!(f64);
231}