| 1 |
use crate::allocator::Allocator; |
| 2 |
use crate::constraint::{SameNumberOfRows, ShapeConstraint}; |
| 3 |
use crate::sparse::{CsMatrix, CsStorage, CsVector}; |
| 4 |
use crate::storage::{Storage, StorageMut}; |
| 5 |
use crate::{Const, DefaultAllocator, Dim, Matrix, OMatrix, OVector, RealField}; |
| 6 |
|
| 7 |
impl<T: RealField, D: Dim, S: CsStorage<T, D, D>> CsMatrix<T, D, D, S> { |
| 8 |
/// Solve a lower-triangular system with a dense right-hand-side. |
| 9 |
#[must_use = "Did you mean to use solve_lower_triangular_mut()?"] |
| 10 |
pub fn solve_lower_triangular<R2: Dim, C2: Dim, S2>( |
| 11 |
&self, |
| 12 |
b: &Matrix<T, R2, C2, S2>, |
| 13 |
) -> Option<OMatrix<T, R2, C2>> |
| 14 |
where |
| 15 |
S2: Storage<T, R2, C2>, |
| 16 |
DefaultAllocator: Allocator<R2, C2>, |
| 17 |
ShapeConstraint: SameNumberOfRows<D, R2>, |
| 18 |
{ |
| 19 |
let mut b = b.clone_owned(); |
| 20 |
if self.solve_lower_triangular_mut(&mut b) { |
| 21 |
Some(b) |
| 22 |
} else { |
| 23 |
None |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
/// Solve a lower-triangular system with `self` transposed and a dense right-hand-side. |
| 28 |
#[must_use = "Did you mean to use tr_solve_lower_triangular_mut()?"] |
| 29 |
pub fn tr_solve_lower_triangular<R2: Dim, C2: Dim, S2>( |
| 30 |
&self, |
| 31 |
b: &Matrix<T, R2, C2, S2>, |
| 32 |
) -> Option<OMatrix<T, R2, C2>> |
| 33 |
where |
| 34 |
S2: Storage<T, R2, C2>, |
| 35 |
DefaultAllocator: Allocator<R2, C2>, |
| 36 |
ShapeConstraint: SameNumberOfRows<D, R2>, |
| 37 |
{ |
| 38 |
let mut b = b.clone_owned(); |
| 39 |
if self.tr_solve_lower_triangular_mut(&mut b) { |
| 40 |
Some(b) |
| 41 |
} else { |
| 42 |
None |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/// Solve in-place a lower-triangular system with a dense right-hand-side. |
| 47 |
pub fn solve_lower_triangular_mut<R2: Dim, C2: Dim, S2>( |
| 48 |
&self, |
| 49 |
b: &mut Matrix<T, R2, C2, S2>, |
| 50 |
) -> bool |
| 51 |
where |
| 52 |
S2: StorageMut<T, R2, C2>, |
| 53 |
ShapeConstraint: SameNumberOfRows<D, R2>, |
| 54 |
{ |
| 55 |
let (nrows, ncols) = self.data.shape(); |
| 56 |
assert_eq!(nrows.value(), ncols.value(), "The matrix must be square."); |
| 57 |
assert_eq!(nrows.value(), b.len(), "Mismatched matrix dimensions."); |
| 58 |
|
| 59 |
for j2 in 0..b.ncols() { |
| 60 |
let mut b = b.column_mut(j2); |
| 61 |
|
| 62 |
for j in 0..ncols.value() { |
| 63 |
let mut column = self.data.column_entries(j); |
| 64 |
let mut diag_found = false; |
| 65 |
|
| 66 |
for (i, val) in &mut column { |
| 67 |
if i == j { |
| 68 |
if val.is_zero() { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
b[j] /= val; |
| 73 |
diag_found = true; |
| 74 |
break; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if !diag_found { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
for (i, val) in column { |
| 83 |
let bj = b[j].clone(); |
| 84 |
b[i] -= bj * val; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
true |
| 90 |
} |
| 91 |
|
| 92 |
/// Solve a lower-triangular system with `self` transposed and a dense right-hand-side. |
| 93 |
pub fn tr_solve_lower_triangular_mut<R2: Dim, C2: Dim, S2>( |
| 94 |
&self, |
| 95 |
b: &mut Matrix<T, R2, C2, S2>, |
| 96 |
) -> bool |
| 97 |
where |
| 98 |
S2: StorageMut<T, R2, C2>, |
| 99 |
ShapeConstraint: SameNumberOfRows<D, R2>, |
| 100 |
{ |
| 101 |
let (nrows, ncols) = self.data.shape(); |
| 102 |
assert_eq!(nrows.value(), ncols.value(), "The matrix must be square."); |
| 103 |
assert_eq!(nrows.value(), b.len(), "Mismatched matrix dimensions."); |
| 104 |
|
| 105 |
for j2 in 0..b.ncols() { |
| 106 |
let mut b = b.column_mut(j2); |
| 107 |
|
| 108 |
for j in (0..ncols.value()).rev() { |
| 109 |
let mut column = self.data.column_entries(j); |
| 110 |
let mut diag = None; |
| 111 |
|
| 112 |
for (i, val) in &mut column { |
| 113 |
if i == j { |
| 114 |
if val.is_zero() { |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
diag = Some(val); |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
if let Some(diag) = diag { |
| 124 |
for (i, val) in column { |
| 125 |
let bi = b[i].clone(); |
| 126 |
b[j] -= val * bi; |
| 127 |
} |
| 128 |
|
| 129 |
b[j] /= diag; |
| 130 |
} else { |
| 131 |
return false; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
true |
| 137 |
} |
| 138 |
|
| 139 |
/// Solve a lower-triangular system with a sparse right-hand-side. |
| 140 |
#[must_use] |
| 141 |
pub fn solve_lower_triangular_cs<D2: Dim, S2>( |
| 142 |
&self, |
| 143 |
b: &CsVector<T, D2, S2>, |
| 144 |
) -> Option<CsVector<T, D2>> |
| 145 |
where |
| 146 |
S2: CsStorage<T, D2>, |
| 147 |
DefaultAllocator: Allocator<D> + Allocator<D2>, |
| 148 |
ShapeConstraint: SameNumberOfRows<D, D2>, |
| 149 |
{ |
| 150 |
let mut reach = Vec::new(); |
| 151 |
// We don't compute a postordered reach here because it will be sorted after anyway. |
| 152 |
self.lower_triangular_reach(b, &mut reach); |
| 153 |
// We sort the reach so the result matrix has sorted indices. |
| 154 |
reach.sort_unstable(); |
| 155 |
let mut workspace = Matrix::zeros_generic(b.data.shape().0, Const::<1>); |
| 156 |
|
| 157 |
for i in reach.iter().cloned() { |
| 158 |
workspace[i] = T::zero(); |
| 159 |
} |
| 160 |
|
| 161 |
for (i, val) in b.data.column_entries(0) { |
| 162 |
workspace[i] = val; |
| 163 |
} |
| 164 |
|
| 165 |
for j in reach.iter().cloned() { |
| 166 |
let mut column = self.data.column_entries(j); |
| 167 |
let mut diag_found = false; |
| 168 |
|
| 169 |
for (i, val) in &mut column { |
| 170 |
if i == j { |
| 171 |
if val.is_zero() { |
| 172 |
break; |
| 173 |
} |
| 174 |
|
| 175 |
workspace[j] /= val; |
| 176 |
diag_found = true; |
| 177 |
break; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
if !diag_found { |
| 182 |
return None; |
| 183 |
} |
| 184 |
|
| 185 |
for (i, val) in column { |
| 186 |
let wj = workspace[j].clone(); |
| 187 |
workspace[i] -= wj * val; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// Copy the result into a sparse vector. |
| 192 |
let mut result = |
| 193 |
CsVector::new_uninitialized_generic(b.data.shape().0, Const::<1>, reach.len()); |
| 194 |
|
| 195 |
for (i, val) in reach.iter().zip(result.data.vals.iter_mut()) { |
| 196 |
*val = workspace[*i].clone(); |
| 197 |
} |
| 198 |
|
| 199 |
result.data.i = reach; |
| 200 |
Some(result) |
| 201 |
} |
| 202 |
|
| 203 |
/* |
| 204 |
// Computes the reachable, post-ordered, nodes from `b`. |
| 205 |
fn lower_triangular_reach_postordered<D2: Dim, S2>( |
| 206 |
&self, |
| 207 |
b: &CsVector<T, D2, S2>, |
| 208 |
xi: &mut Vec<usize>, |
| 209 |
) where |
| 210 |
S2: CsStorage<T, D2>, |
| 211 |
DefaultAllocator: Allocator<D>, |
| 212 |
{ |
| 213 |
let mut visited = OVector::repeat_generic(self.data.shape().1, U1, false); |
| 214 |
let mut stack = Vec::new(); |
| 215 |
|
| 216 |
for i in b.data.column_range(0) { |
| 217 |
let row_index = b.data.row_index(i); |
| 218 |
|
| 219 |
if !visited[row_index] { |
| 220 |
let rng = self.data.column_range(row_index); |
| 221 |
stack.push((row_index, rng)); |
| 222 |
self.lower_triangular_dfs(visited.as_mut_slice(), &mut stack, xi); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
fn lower_triangular_dfs( |
| 228 |
&self, |
| 229 |
visited: &mut [bool], |
| 230 |
stack: &mut Vec<(usize, Range<usize>)>, |
| 231 |
xi: &mut Vec<usize>, |
| 232 |
) |
| 233 |
{ |
| 234 |
'recursion: while let Some((j, rng)) = stack.pop() { |
| 235 |
visited[j] = true; |
| 236 |
|
| 237 |
for i in rng.clone() { |
| 238 |
let row_id = self.data.row_index(i); |
| 239 |
if row_id > j && !visited[row_id] { |
| 240 |
stack.push((j, (i + 1)..rng.end)); |
| 241 |
stack.push((row_id, self.data.column_range(row_id))); |
| 242 |
continue 'recursion; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
xi.push(j) |
| 247 |
} |
| 248 |
} |
| 249 |
*/ |
| 250 |
|
| 251 |
// Computes the nodes reachable from `b` in an arbitrary order. |
| 252 |
fn lower_triangular_reach<D2: Dim, S2>(&self, b: &CsVector<T, D2, S2>, xi: &mut Vec<usize>) |
| 253 |
where |
| 254 |
S2: CsStorage<T, D2>, |
| 255 |
DefaultAllocator: Allocator<D>, |
| 256 |
{ |
| 257 |
let mut visited = OVector::repeat_generic(self.data.shape().1, Const::<1>, false); |
| 258 |
let mut stack = Vec::new(); |
| 259 |
|
| 260 |
for irow in b.data.column_row_indices(0) { |
| 261 |
self.lower_triangular_bfs(irow, visited.as_mut_slice(), &mut stack, xi); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
fn lower_triangular_bfs( |
| 266 |
&self, |
| 267 |
start: usize, |
| 268 |
visited: &mut [bool], |
| 269 |
stack: &mut Vec<usize>, |
| 270 |
xi: &mut Vec<usize>, |
| 271 |
) { |
| 272 |
if !visited[start] { |
| 273 |
stack.clear(); |
| 274 |
stack.push(start); |
| 275 |
xi.push(start); |
| 276 |
visited[start] = true; |
| 277 |
|
| 278 |
while let Some(j) = stack.pop() { |
| 279 |
for irow in self.data.column_row_indices(j) { |
| 280 |
if irow > j && !visited[irow] { |
| 281 |
stack.push(irow); |
| 282 |
xi.push(irow); |
| 283 |
visited[irow] = true; |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
} |