1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//! Shared elementwise arithmetic implementations.
//!
//! Binary packed operations require equal dimensions. The operator forms panic
//! on a mismatch, while [`crate::PackedLower::component_mul`] and corresponding
//! family methods report an error.
//!
//! # Examples
//!
//! ```
//! use matrixpacked::PackedSymmetric;
//!
//! let a = PackedSymmetric::from_vec(2, vec![1.0_f64, 2.0, 3.0])?;
//! let b = PackedSymmetric::from_vec(2, vec![4.0_f64, 5.0, 6.0])?;
//! assert_eq!((&a + &b).as_slice(), &[5.0, 7.0, 9.0]);
//! assert_eq!(a.component_mul(&b)?.as_slice(), &[4.0, 10.0, 18.0]);
//! # Ok::<(), matrixpacked::PackedMatrixError>(())
//! ```
macro_rules! impl_packed_ring_ops {
($name:ident) => {
impl<T, L, R> std::ops::Add<&$name<T, R>> for &$name<T, L>
where
T: crate::LapackScalar,
L: crate::storage::PackedStorage<T>,
R: crate::storage::PackedStorage<T>,
{
type Output = $name<T>;
fn add(self, rhs: &$name<T, R>) -> Self::Output {
assert_eq!(
self.dimension(),
rhs.dimension(),
"matrix dimensions must match"
);
$name::from_vec(
self.dimension(),
self.as_slice()
.iter()
.zip(rhs.as_slice())
.map(|(&a, &b)| a + b)
.collect(),
)
.expect("validated packed length")
}
}
impl<T, S, R> std::ops::AddAssign<&$name<T, R>> for $name<T, S>
where
T: crate::LapackScalar,
S: crate::storage::PackedStorageMut<T>,
R: crate::storage::PackedStorage<T>,
{
fn add_assign(&mut self, rhs: &$name<T, R>) {
assert_eq!(
self.dimension(),
rhs.dimension(),
"matrix dimensions must match"
);
for (a, &b) in self.as_mut_slice().iter_mut().zip(rhs.as_slice()) {
*a += b;
}
}
}
impl<T, L, R> std::ops::Sub<&$name<T, R>> for &$name<T, L>
where
T: crate::LapackScalar,
L: crate::storage::PackedStorage<T>,
R: crate::storage::PackedStorage<T>,
{
type Output = $name<T>;
fn sub(self, rhs: &$name<T, R>) -> Self::Output {
assert_eq!(
self.dimension(),
rhs.dimension(),
"matrix dimensions must match"
);
$name::from_vec(
self.dimension(),
self.as_slice()
.iter()
.zip(rhs.as_slice())
.map(|(&a, &b)| a - b)
.collect(),
)
.expect("validated packed length")
}
}
impl<T, S, R> std::ops::SubAssign<&$name<T, R>> for $name<T, S>
where
T: crate::LapackScalar,
S: crate::storage::PackedStorageMut<T>,
R: crate::storage::PackedStorage<T>,
{
fn sub_assign(&mut self, rhs: &$name<T, R>) {
assert_eq!(
self.dimension(),
rhs.dimension(),
"matrix dimensions must match"
);
for (a, &b) in self.as_mut_slice().iter_mut().zip(rhs.as_slice()) {
*a -= b;
}
}
}
impl<T, S> std::ops::Neg for &$name<T, S>
where
T: crate::LapackScalar,
S: crate::storage::PackedStorage<T>,
{
type Output = $name<T>;
fn neg(self) -> Self::Output {
$name::from_vec(
self.dimension(),
self.as_slice().iter().map(|&x| -x).collect(),
)
.expect("validated packed length")
}
}
impl<T, S> std::ops::Mul<T> for &$name<T, S>
where
T: crate::LapackScalar,
S: crate::storage::PackedStorage<T>,
{
type Output = $name<T>;
fn mul(self, rhs: T) -> Self::Output {
$name::from_vec(
self.dimension(),
self.as_slice().iter().map(|&x| x * rhs).collect(),
)
.expect("validated packed length")
}
}
impl<T, S> std::ops::Div<T> for &$name<T, S>
where
T: crate::LapackScalar,
S: crate::storage::PackedStorage<T>,
{
type Output = $name<T>;
fn div(self, rhs: T) -> Self::Output {
$name::from_vec(
self.dimension(),
self.as_slice().iter().map(|&x| x / rhs).collect(),
)
.expect("validated packed length")
}
}
impl<T, S> std::ops::MulAssign<T> for $name<T, S>
where
T: crate::LapackScalar,
S: crate::storage::PackedStorageMut<T>,
{
fn mul_assign(&mut self, rhs: T) {
for x in self.as_mut_slice() {
*x *= rhs;
}
}
}
impl<T, S> std::ops::DivAssign<T> for $name<T, S>
where
T: crate::LapackScalar,
S: crate::storage::PackedStorageMut<T>,
{
fn div_assign(&mut self, rhs: T) {
for x in self.as_mut_slice() {
*x /= rhs;
}
}
}
impl<T, S> $name<T, S>
where
T: crate::LapackScalar,
S: crate::storage::PackedStorage<T>,
{
/// Multiplies corresponding stored packed elements.
///
/// # Errors
///
/// Returns [`crate::PackedMatrixError::DimensionMismatch`] when the
/// square dimensions differ.
pub fn component_mul<R: crate::storage::PackedStorage<T>>(
&self,
rhs: &$name<T, R>,
) -> Result<$name<T>, crate::PackedMatrixError> {
if self.dimension() != rhs.dimension() {
return Err(crate::PackedMatrixError::DimensionMismatch {
left: self.dimension(),
right: rhs.dimension(),
});
}
$name::from_vec(
self.dimension(),
self.as_slice()
.iter()
.zip(rhs.as_slice())
.map(|(&a, &b)| a * b)
.collect(),
)
}
/// Divides corresponding stored packed elements.
///
/// Scalar division semantics apply to zero denominators.
///
/// # Errors
///
/// Returns [`crate::PackedMatrixError::DimensionMismatch`] when the
/// square dimensions differ.
pub fn component_div<R: crate::storage::PackedStorage<T>>(
&self,
rhs: &$name<T, R>,
) -> Result<$name<T>, crate::PackedMatrixError> {
if self.dimension() != rhs.dimension() {
return Err(crate::PackedMatrixError::DimensionMismatch {
left: self.dimension(),
right: rhs.dimension(),
});
}
$name::from_vec(
self.dimension(),
self.as_slice()
.iter()
.zip(rhs.as_slice())
.map(|(&a, &b)| a / b)
.collect(),
)
}
/// Returns the sum of squared magnitudes of physically stored elements.
///
/// This is a storage diagnostic, not generally the squared Frobenius
/// norm of the logical matrix because mirrored off-diagonal entries
/// are counted only once.
pub fn stored_norm_squared(&self) -> T::Real {
self.as_slice()
.iter()
.fold(<T::Real as num_traits::Zero>::zero(), |acc, &x| {
acc + x.abs_squared()
})
}
}
};
}
pub(crate) use impl_packed_ring_ops;