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
use ndarray::*;
use super::convert::*;
use super::error::*;
use super::layout::*;
use super::types::*;
pub use lapack_traits::{Pivot, UPLO};
pub trait SolveH<A: Scalar> {
fn solveh<S: Data<Elem = A>>(&self, a: &ArrayBase<S, Ix1>) -> Result<Array1<A>> {
let mut a = replicate(a);
self.solveh_mut(&mut a)?;
Ok(a)
}
fn solveh_into<S: DataMut<Elem = A>>(&self, mut a: ArrayBase<S, Ix1>) -> Result<ArrayBase<S, Ix1>> {
self.solveh_mut(&mut a)?;
Ok(a)
}
fn solveh_mut<'a, S: DataMut<Elem = A>>(&self, &'a mut ArrayBase<S, Ix1>) -> Result<&'a mut ArrayBase<S, Ix1>>;
}
pub struct FactorizedH<S: Data> {
pub a: ArrayBase<S, Ix2>,
pub ipiv: Pivot,
}
impl<A, S> SolveH<A> for FactorizedH<S>
where
A: Scalar,
S: Data<Elem = A>,
{
fn solveh_mut<'a, Sb>(&self, rhs: &'a mut ArrayBase<Sb, Ix1>) -> Result<&'a mut ArrayBase<Sb, Ix1>>
where
Sb: DataMut<Elem = A>,
{
unsafe {
A::solveh(
self.a.square_layout()?,
UPLO::Upper,
self.a.as_allocated()?,
&self.ipiv,
rhs.as_slice_mut().unwrap(),
)?
};
Ok(rhs)
}
}
impl<A, S> SolveH<A> for ArrayBase<S, Ix2>
where
A: Scalar,
S: Data<Elem = A>,
{
fn solveh_mut<'a, Sb>(&self, mut rhs: &'a mut ArrayBase<Sb, Ix1>) -> Result<&'a mut ArrayBase<Sb, Ix1>>
where
Sb: DataMut<Elem = A>,
{
let f = self.factorizeh()?;
f.solveh_mut(rhs)
}
}
impl<A, S> FactorizedH<S>
where
A: Scalar,
S: DataMut<Elem = A>,
{
pub fn into_inverseh(mut self) -> Result<ArrayBase<S, Ix2>> {
unsafe {
A::invh(
self.a.square_layout()?,
UPLO::Upper,
self.a.as_allocated_mut()?,
&self.ipiv,
)?
};
Ok(self.a)
}
}
pub trait FactorizeH<S: Data> {
fn factorizeh(&self) -> Result<FactorizedH<S>>;
}
pub trait FactorizeHInto<S: Data> {
fn factorizeh_into(self) -> Result<FactorizedH<S>>;
}
impl<A, S> FactorizeHInto<S> for ArrayBase<S, Ix2>
where
A: Scalar,
S: DataMut<Elem = A>,
{
fn factorizeh_into(mut self) -> Result<FactorizedH<S>> {
let ipiv = unsafe { A::bk(self.layout()?, UPLO::Upper, self.as_allocated_mut()?)? };
Ok(FactorizedH {
a: self,
ipiv: ipiv,
})
}
}
impl<A, Si> FactorizeH<OwnedRepr<A>> for ArrayBase<Si, Ix2>
where
A: Scalar,
Si: Data<Elem = A>,
{
fn factorizeh(&self) -> Result<FactorizedH<OwnedRepr<A>>> {
let mut a: Array2<A> = replicate(self);
let ipiv = unsafe { A::bk(a.layout()?, UPLO::Upper, a.as_allocated_mut()?)? };
Ok(FactorizedH { a: a, ipiv: ipiv })
}
}
pub trait InverseH {
type Output;
fn invh(&self) -> Result<Self::Output>;
}
pub trait InverseHInto {
type Output;
fn invh_into(self) -> Result<Self::Output>;
}
impl<A, S> InverseHInto for ArrayBase<S, Ix2>
where
A: Scalar,
S: DataMut<Elem = A>,
{
type Output = Self;
fn invh_into(self) -> Result<Self::Output> {
let f = self.factorizeh_into()?;
f.into_inverseh()
}
}
impl<A, Si> InverseH for ArrayBase<Si, Ix2>
where
A: Scalar,
Si: Data<Elem = A>,
{
type Output = Array2<A>;
fn invh(&self) -> Result<Self::Output> {
let f = self.factorizeh()?;
f.into_inverseh()
}
}