| 1 |
use num::{One, Zero}; |
| 2 |
use simba::scalar::{ClosedAddAssign, ClosedMulAssign}; |
| 3 |
use std::ops::{Add, Mul}; |
| 4 |
|
| 5 |
use crate::allocator::Allocator; |
| 6 |
use crate::constraint::{AreMultipliable, DimEq, ShapeConstraint}; |
| 7 |
use crate::sparse::{CsMatrix, CsStorage, CsStorageMut, CsVector}; |
| 8 |
use crate::storage::StorageMut; |
| 9 |
use crate::{Const, DefaultAllocator, Dim, Matrix, OVector, Scalar, Vector}; |
| 10 |
|
| 11 |
impl<T: Scalar, R: Dim, C: Dim, S: CsStorage<T, R, C>> CsMatrix<T, R, C, S> { |
| 12 |
fn scatter<R2: Dim, C2: Dim>( |
| 13 |
&self, |
| 14 |
j: usize, |
| 15 |
beta: T, |
| 16 |
timestamps: &mut [usize], |
| 17 |
timestamp: usize, |
| 18 |
workspace: &mut [T], |
| 19 |
mut nz: usize, |
| 20 |
res: &mut CsMatrix<T, R2, C2>, |
| 21 |
) -> usize |
| 22 |
where |
| 23 |
T: ClosedAddAssign + ClosedMulAssign, |
| 24 |
DefaultAllocator: Allocator<C2>, |
| 25 |
{ |
| 26 |
for (i, val) in self.data.column_entries(j) { |
| 27 |
if timestamps[i] < timestamp { |
| 28 |
timestamps[i] = timestamp; |
| 29 |
res.data.i[nz] = i; |
| 30 |
nz += 1; |
| 31 |
workspace[i] = val * beta.clone(); |
| 32 |
} else { |
| 33 |
workspace[i] += val * beta.clone(); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
nz |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/* |
| 42 |
impl<T: Scalar, R, S> CsVector<T, R, S> { |
| 43 |
pub fn axpy(&mut self, alpha: T, x: CsVector<T, R, S>, beta: T) { |
| 44 |
// First, compute the number of non-zero entries. |
| 45 |
let mut nnzero = 0; |
| 46 |
|
| 47 |
// Allocate a size large enough. |
| 48 |
self.data.set_column_len(0, nnzero); |
| 49 |
|
| 50 |
// Fill with the axpy. |
| 51 |
let mut i = self.len(); |
| 52 |
let mut j = x.len(); |
| 53 |
let mut k = nnzero - 1; |
| 54 |
let mut rid1 = self.data.row_index(0, i - 1); |
| 55 |
let mut rid2 = x.data.row_index(0, j - 1); |
| 56 |
|
| 57 |
while k > 0 { |
| 58 |
if rid1 == rid2 { |
| 59 |
self.data.set_row_index(0, k, rid1); |
| 60 |
self[k] = alpha * x[j] + beta * self[k]; |
| 61 |
i -= 1; |
| 62 |
j -= 1; |
| 63 |
} else if rid1 < rid2 { |
| 64 |
self.data.set_row_index(0, k, rid1); |
| 65 |
self[k] = beta * self[i]; |
| 66 |
i -= 1; |
| 67 |
} else { |
| 68 |
self.data.set_row_index(0, k, rid2); |
| 69 |
self[k] = alpha * x[j]; |
| 70 |
j -= 1; |
| 71 |
} |
| 72 |
|
| 73 |
k -= 1; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
*/ |
| 78 |
|
| 79 |
impl<T: Scalar + Zero + ClosedAddAssign + ClosedMulAssign, D: Dim, S: StorageMut<T, D>> |
| 80 |
Vector<T, D, S> |
| 81 |
{ |
| 82 |
/// Perform a sparse axpy operation: `self = alpha * x + beta * self` operation. |
| 83 |
pub fn axpy_cs<D2: Dim, S2>(&mut self, alpha: T, x: &CsVector<T, D2, S2>, beta: T) |
| 84 |
where |
| 85 |
S2: CsStorage<T, D2>, |
| 86 |
ShapeConstraint: DimEq<D, D2>, |
| 87 |
{ |
| 88 |
if beta.is_zero() { |
| 89 |
for i in 0..x.len() { |
| 90 |
unsafe { |
| 91 |
let k = x.data.row_index_unchecked(i); |
| 92 |
let y = self.vget_unchecked_mut(k); |
| 93 |
*y = alpha.clone() * x.data.get_value_unchecked(i).clone(); |
| 94 |
} |
| 95 |
} |
| 96 |
} else { |
| 97 |
// Needed to be sure even components not present on `x` are multiplied. |
| 98 |
*self *= beta.clone(); |
| 99 |
|
| 100 |
for i in 0..x.len() { |
| 101 |
unsafe { |
| 102 |
let k = x.data.row_index_unchecked(i); |
| 103 |
let y = self.vget_unchecked_mut(k); |
| 104 |
*y += alpha.clone() * x.data.get_value_unchecked(i).clone(); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/* |
| 111 |
pub fn gemv_sparse<R2: Dim, C2: Dim, S2>(&mut self, alpha: T, a: &CsMatrix<T, R2, C2, S2>, x: &DVector<T>, beta: T) |
| 112 |
where |
| 113 |
S2: CsStorage<T, R2, C2> { |
| 114 |
let col2 = a.column(0); |
| 115 |
let val = unsafe { *x.vget_unchecked(0) }; |
| 116 |
self.axpy_sparse(alpha * val, &col2, beta); |
| 117 |
|
| 118 |
for j in 1..ncols2 { |
| 119 |
let col2 = a.column(j); |
| 120 |
let val = unsafe { *x.vget_unchecked(j) }; |
| 121 |
|
| 122 |
self.axpy_sparse(alpha * val, &col2, T::one()); |
| 123 |
} |
| 124 |
} |
| 125 |
*/ |
| 126 |
} |
| 127 |
|
| 128 |
impl<'b, T, R1, R2, C1, C2, S1, S2> Mul<&'b CsMatrix<T, R2, C2, S2>> for &'_ CsMatrix<T, R1, C1, S1> |
| 129 |
where |
| 130 |
T: Scalar + ClosedAddAssign + ClosedMulAssign + Zero, |
| 131 |
R1: Dim, |
| 132 |
C1: Dim, |
| 133 |
R2: Dim, |
| 134 |
C2: Dim, |
| 135 |
S1: CsStorage<T, R1, C1>, |
| 136 |
S2: CsStorage<T, R2, C2>, |
| 137 |
ShapeConstraint: AreMultipliable<R1, C1, R2, C2>, |
| 138 |
DefaultAllocator: Allocator<C2> + Allocator<R1> + Allocator<R1>, |
| 139 |
{ |
| 140 |
type Output = CsMatrix<T, R1, C2>; |
| 141 |
|
| 142 |
fn mul(self, rhs: &'b CsMatrix<T, R2, C2, S2>) -> Self::Output { |
| 143 |
let (nrows1, ncols1) = self.data.shape(); |
| 144 |
let (nrows2, ncols2) = rhs.data.shape(); |
| 145 |
assert_eq!( |
| 146 |
ncols1.value(), |
| 147 |
nrows2.value(), |
| 148 |
"Mismatched dimensions for matrix multiplication." |
| 149 |
); |
| 150 |
|
| 151 |
let mut res = CsMatrix::new_uninitialized_generic(nrows1, ncols2, self.len() + rhs.len()); |
| 152 |
let mut workspace = OVector::<T, R1>::zeros_generic(nrows1, Const::<1>); |
| 153 |
let mut nz = 0; |
| 154 |
|
| 155 |
for j in 0..ncols2.value() { |
| 156 |
res.data.p[j] = nz; |
| 157 |
let new_size_bound = nz + nrows1.value(); |
| 158 |
res.data.i.resize(new_size_bound, 0); |
| 159 |
res.data.vals.resize(new_size_bound, T::zero()); |
| 160 |
|
| 161 |
for (i, beta) in rhs.data.column_entries(j) { |
| 162 |
for (k, val) in self.data.column_entries(i) { |
| 163 |
workspace[k] += val.clone() * beta.clone(); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
for (i, val) in workspace.as_mut_slice().iter_mut().enumerate() { |
| 168 |
if !val.is_zero() { |
| 169 |
res.data.i[nz] = i; |
| 170 |
res.data.vals[nz] = val.clone(); |
| 171 |
*val = T::zero(); |
| 172 |
nz += 1; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
// NOTE: the following has a lower complexity, but is slower in many cases, likely because |
| 178 |
// of branching inside of the inner loop. |
| 179 |
// |
| 180 |
// let mut res = CsMatrix::new_uninitialized_generic(nrows1, ncols2, self.len() + rhs.len()); |
| 181 |
// let mut timestamps = OVector::zeros_generic(nrows1, Const::<)>; |
| 182 |
// let mut workspace = unsafe { OVector::new_uninitialized_generic(nrows1, Const::<)> }; |
| 183 |
// let mut nz = 0; |
| 184 |
// |
| 185 |
// for j in 0..ncols2.value() { |
| 186 |
// res.data.p[j] = nz; |
| 187 |
// let new_size_bound = nz + nrows1.value(); |
| 188 |
// res.data.i.resize(new_size_bound, 0); |
| 189 |
// res.data.vals.resize(new_size_bound, T::zero()); |
| 190 |
// |
| 191 |
// for (i, val) in rhs.data.column_entries(j) { |
| 192 |
// nz = self.scatter( |
| 193 |
// i, |
| 194 |
// val, |
| 195 |
// timestamps.as_mut_slice(), |
| 196 |
// j + 1, |
| 197 |
// workspace.as_mut_slice(), |
| 198 |
// nz, |
| 199 |
// &mut res, |
| 200 |
// ); |
| 201 |
// } |
| 202 |
// |
| 203 |
// // Keep the output sorted. |
| 204 |
// let range = res.data.p[j]..nz; |
| 205 |
// res.data.i[range.clone()].sort(); |
| 206 |
// |
| 207 |
// for p in range { |
| 208 |
// res.data.vals[p] = workspace[res.data.i[p]] |
| 209 |
// } |
| 210 |
// } |
| 211 |
|
| 212 |
res.data.i.truncate(nz); |
| 213 |
res.data.i.shrink_to_fit(); |
| 214 |
res.data.vals.truncate(nz); |
| 215 |
res.data.vals.shrink_to_fit(); |
| 216 |
res |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
impl<'b, T, R1, R2, C1, C2, S1, S2> Add<&'b CsMatrix<T, R2, C2, S2>> for &'_ CsMatrix<T, R1, C1, S1> |
| 221 |
where |
| 222 |
T: Scalar + ClosedAddAssign + ClosedMulAssign + Zero + One, |
| 223 |
R1: Dim, |
| 224 |
C1: Dim, |
| 225 |
R2: Dim, |
| 226 |
C2: Dim, |
| 227 |
S1: CsStorage<T, R1, C1>, |
| 228 |
S2: CsStorage<T, R2, C2>, |
| 229 |
ShapeConstraint: DimEq<R1, R2> + DimEq<C1, C2>, |
| 230 |
DefaultAllocator: Allocator<C2> + Allocator<R1> + Allocator<R1>, |
| 231 |
{ |
| 232 |
type Output = CsMatrix<T, R1, C2>; |
| 233 |
|
| 234 |
fn add(self, rhs: &'b CsMatrix<T, R2, C2, S2>) -> Self::Output { |
| 235 |
let (nrows1, ncols1) = self.data.shape(); |
| 236 |
let (nrows2, ncols2) = rhs.data.shape(); |
| 237 |
assert_eq!( |
| 238 |
(nrows1.value(), ncols1.value()), |
| 239 |
(nrows2.value(), ncols2.value()), |
| 240 |
"Mismatched dimensions for matrix sum." |
| 241 |
); |
| 242 |
|
| 243 |
let mut res = CsMatrix::new_uninitialized_generic(nrows1, ncols2, self.len() + rhs.len()); |
| 244 |
let mut timestamps = OVector::zeros_generic(nrows1, Const::<1>); |
| 245 |
let mut workspace = Matrix::zeros_generic(nrows1, Const::<1>); |
| 246 |
let mut nz = 0; |
| 247 |
|
| 248 |
for j in 0..ncols2.value() { |
| 249 |
res.data.p[j] = nz; |
| 250 |
|
| 251 |
nz = self.scatter( |
| 252 |
j, |
| 253 |
T::one(), |
| 254 |
timestamps.as_mut_slice(), |
| 255 |
j + 1, |
| 256 |
workspace.as_mut_slice(), |
| 257 |
nz, |
| 258 |
&mut res, |
| 259 |
); |
| 260 |
|
| 261 |
nz = rhs.scatter( |
| 262 |
j, |
| 263 |
T::one(), |
| 264 |
timestamps.as_mut_slice(), |
| 265 |
j + 1, |
| 266 |
workspace.as_mut_slice(), |
| 267 |
nz, |
| 268 |
&mut res, |
| 269 |
); |
| 270 |
|
| 271 |
// Keep the output sorted. |
| 272 |
let range = res.data.p[j]..nz; |
| 273 |
res.data.i[range.clone()].sort_unstable(); |
| 274 |
|
| 275 |
for p in range { |
| 276 |
res.data.vals[p] = workspace[res.data.i[p]].clone() |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
res.data.i.truncate(nz); |
| 281 |
res.data.i.shrink_to_fit(); |
| 282 |
res.data.vals.truncate(nz); |
| 283 |
res.data.vals.shrink_to_fit(); |
| 284 |
res |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
impl<T, R, C, S> Mul<T> for CsMatrix<T, R, C, S> |
| 289 |
where |
| 290 |
T: Scalar + ClosedAddAssign + ClosedMulAssign + Zero, |
| 291 |
R: Dim, |
| 292 |
C: Dim, |
| 293 |
S: CsStorageMut<T, R, C>, |
| 294 |
{ |
| 295 |
type Output = Self; |
| 296 |
|
| 297 |
fn mul(mut self, rhs: T) -> Self::Output { |
| 298 |
for e in self.values_mut() { |
| 299 |
*e *= rhs.clone() |
| 300 |
} |
| 301 |
|
| 302 |
self |
| 303 |
} |
| 304 |
} |