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
use ndarray::*;
use super::convert::*;
use super::error::*;
use super::lapack_traits::*;
use super::layout::*;
use super::types::*;
pub use lapack_traits::{Pivot, Transpose};
pub struct Factorized<S: Data> {
pub a: ArrayBase<S, Ix2>,
pub ipiv: Pivot,
}
impl<A, S> Factorized<S>
where
A: LapackScalar,
S: Data<Elem = A>,
{
pub fn solve<Sb>(&self, t: Transpose, mut rhs: ArrayBase<Sb, Ix1>) -> Result<ArrayBase<Sb, Ix1>>
where
Sb: DataMut<Elem = A>,
{
A::solve(
self.a.square_layout()?,
t,
self.a.as_allocated()?,
&self.ipiv,
rhs.as_slice_mut().unwrap(),
)?;
Ok(rhs)
}
}
impl<A, S> Factorized<S>
where
A: LapackScalar,
S: DataMut<Elem = A>,
{
pub fn into_inverse(mut self) -> Result<ArrayBase<S, Ix2>> {
A::inv(
self.a.square_layout()?,
self.a.as_allocated_mut()?,
&self.ipiv,
)?;
Ok(self.a)
}
}
pub trait Factorize<S: Data> {
fn factorize(&self) -> Result<Factorized<S>>;
}
pub trait FactorizeInto<S: Data> {
fn factorize_into(self) -> Result<Factorized<S>>;
}
impl<A, S> FactorizeInto<S> for ArrayBase<S, Ix2>
where
A: Scalar,
S: DataMut<Elem = A>,
{
fn factorize_into(mut self) -> Result<Factorized<S>> {
let ipiv = A::lu(self.layout()?, self.as_allocated_mut()?)?;
Ok(Factorized {
a: self,
ipiv: ipiv,
})
}
}
impl<A, Si, So> Factorize<So> for ArrayBase<Si, Ix2>
where
A: Scalar,
Si: Data<Elem = A>,
So: DataOwned<Elem = A> + DataMut,
{
fn factorize(&self) -> Result<Factorized<So>> {
let mut a: ArrayBase<So, Ix2> = replicate(self);
let ipiv = A::lu(a.layout()?, a.as_allocated_mut()?)?;
Ok(Factorized { a: a, ipiv: ipiv })
}
}
pub trait Inverse {
type Output;
fn inv(&self) -> Result<Self::Output>;
}
pub trait InverseInto {
type Output;
fn inv_into(self) -> Result<Self::Output>;
}
impl<A, S> InverseInto for ArrayBase<S, Ix2>
where
A: Scalar,
S: DataMut<Elem = A>,
{
type Output = Self;
fn inv_into(self) -> Result<Self::Output> {
let f = self.factorize_into()?;
f.into_inverse()
}
}
impl<A, Si> Inverse for ArrayBase<Si, Ix2>
where
A: Scalar,
Si: Data<Elem = A>,
{
type Output = Array2<A>;
fn inv(&self) -> Result<Self::Output> {
let f = self.factorize()?;
f.into_inverse()
}
}