|
| 1 | +//! Solve Hermite/Symmetric linear problems |
| 2 | +
|
| 3 | +use ndarray::*; |
| 4 | + |
| 5 | +use super::convert::*; |
| 6 | +use super::error::*; |
| 7 | +use super::layout::*; |
| 8 | +use super::types::*; |
| 9 | + |
| 10 | +pub use lapack_traits::{Pivot, UPLO}; |
| 11 | + |
| 12 | +pub trait SolveH<A: Scalar> { |
| 13 | + fn solveh<S: Data<Elem = A>>(&self, a: &ArrayBase<S, Ix1>) -> Result<Array1<A>> { |
| 14 | + let mut a = replicate(a); |
| 15 | + self.solveh_mut(&mut a)?; |
| 16 | + Ok(a) |
| 17 | + } |
| 18 | + fn solveh_into<S: DataMut<Elem = A>>(&self, mut a: ArrayBase<S, Ix1>) -> Result<ArrayBase<S, Ix1>> { |
| 19 | + self.solveh_mut(&mut a)?; |
| 20 | + Ok(a) |
| 21 | + } |
| 22 | + fn solveh_mut<'a, S: DataMut<Elem = A>>(&self, &'a mut ArrayBase<S, Ix1>) -> Result<&'a mut ArrayBase<S, Ix1>>; |
| 23 | +} |
| 24 | + |
| 25 | +pub struct FactorizedH<S: Data> { |
| 26 | + pub a: ArrayBase<S, Ix2>, |
| 27 | + pub ipiv: Pivot, |
| 28 | +} |
| 29 | + |
| 30 | +impl<A, S> SolveH<A> for FactorizedH<S> |
| 31 | +where |
| 32 | + A: Scalar, |
| 33 | + S: Data<Elem = A>, |
| 34 | +{ |
| 35 | + fn solveh_mut<'a, Sb>(&self, rhs: &'a mut ArrayBase<Sb, Ix1>) -> Result<&'a mut ArrayBase<Sb, Ix1>> |
| 36 | + where |
| 37 | + Sb: DataMut<Elem = A>, |
| 38 | + { |
| 39 | + unsafe { |
| 40 | + A::solveh( |
| 41 | + self.a.square_layout()?, |
| 42 | + UPLO::Upper, |
| 43 | + self.a.as_allocated()?, |
| 44 | + &self.ipiv, |
| 45 | + rhs.as_slice_mut().unwrap(), |
| 46 | + )? |
| 47 | + }; |
| 48 | + Ok(rhs) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl<A, S> SolveH<A> for ArrayBase<S, Ix2> |
| 53 | +where |
| 54 | + A: Scalar, |
| 55 | + S: Data<Elem = A>, |
| 56 | +{ |
| 57 | + fn solveh_mut<'a, Sb>(&self, mut rhs: &'a mut ArrayBase<Sb, Ix1>) -> Result<&'a mut ArrayBase<Sb, Ix1>> |
| 58 | + where |
| 59 | + Sb: DataMut<Elem = A>, |
| 60 | + { |
| 61 | + let f = self.factorizeh()?; |
| 62 | + f.solveh_mut(rhs) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +impl<A, S> FactorizedH<S> |
| 68 | +where |
| 69 | + A: Scalar, |
| 70 | + S: DataMut<Elem = A>, |
| 71 | +{ |
| 72 | + pub fn into_inverseh(mut self) -> Result<ArrayBase<S, Ix2>> { |
| 73 | + unsafe { |
| 74 | + A::invh( |
| 75 | + self.a.square_layout()?, |
| 76 | + UPLO::Upper, |
| 77 | + self.a.as_allocated_mut()?, |
| 78 | + &self.ipiv, |
| 79 | + )? |
| 80 | + }; |
| 81 | + Ok(self.a) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +pub trait FactorizeH<S: Data> { |
| 86 | + fn factorizeh(&self) -> Result<FactorizedH<S>>; |
| 87 | +} |
| 88 | + |
| 89 | +pub trait FactorizeHInto<S: Data> { |
| 90 | + fn factorizeh_into(self) -> Result<FactorizedH<S>>; |
| 91 | +} |
| 92 | + |
| 93 | +impl<A, S> FactorizeHInto<S> for ArrayBase<S, Ix2> |
| 94 | +where |
| 95 | + A: Scalar, |
| 96 | + S: DataMut<Elem = A>, |
| 97 | +{ |
| 98 | + fn factorizeh_into(mut self) -> Result<FactorizedH<S>> { |
| 99 | + let ipiv = unsafe { A::bk(self.layout()?, UPLO::Upper, self.as_allocated_mut()?)? }; |
| 100 | + Ok(FactorizedH { |
| 101 | + a: self, |
| 102 | + ipiv: ipiv, |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl<A, Si> FactorizeH<OwnedRepr<A>> for ArrayBase<Si, Ix2> |
| 108 | +where |
| 109 | + A: Scalar, |
| 110 | + Si: Data<Elem = A>, |
| 111 | +{ |
| 112 | + fn factorizeh(&self) -> Result<FactorizedH<OwnedRepr<A>>> { |
| 113 | + let mut a: Array2<A> = replicate(self); |
| 114 | + let ipiv = unsafe { A::bk(a.layout()?, UPLO::Upper, a.as_allocated_mut()?)? }; |
| 115 | + Ok(FactorizedH { a: a, ipiv: ipiv }) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +pub trait InverseH { |
| 120 | + type Output; |
| 121 | + fn invh(&self) -> Result<Self::Output>; |
| 122 | +} |
| 123 | + |
| 124 | +pub trait InverseHInto { |
| 125 | + type Output; |
| 126 | + fn invh_into(self) -> Result<Self::Output>; |
| 127 | +} |
| 128 | + |
| 129 | +impl<A, S> InverseHInto for ArrayBase<S, Ix2> |
| 130 | +where |
| 131 | + A: Scalar, |
| 132 | + S: DataMut<Elem = A>, |
| 133 | +{ |
| 134 | + type Output = Self; |
| 135 | + |
| 136 | + fn invh_into(self) -> Result<Self::Output> { |
| 137 | + let f = self.factorizeh_into()?; |
| 138 | + f.into_inverseh() |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +impl<A, Si> InverseH for ArrayBase<Si, Ix2> |
| 143 | +where |
| 144 | + A: Scalar, |
| 145 | + Si: Data<Elem = A>, |
| 146 | +{ |
| 147 | + type Output = Array2<A>; |
| 148 | + |
| 149 | + fn invh(&self) -> Result<Self::Output> { |
| 150 | + let f = self.factorizeh()?; |
| 151 | + f.into_inverseh() |
| 152 | + } |
| 153 | +} |
0 commit comments