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
//! Define methods for triangular matrices

use ndarray::*;
use super::matrix::{Matrix, MFloat};
use super::square::SquareMatrix;
use super::error::LinalgError;
use super::util::hstack;
use super::impls::solve::ImplSolve;

pub trait SolveTriangular<Rhs>: Matrix + SquareMatrix {
    type Output;
    /// solve a triangular system with upper triangular matrix
    fn solve_upper(&self, Rhs) -> Result<Self::Output, LinalgError>;
    /// solve a triangular system with lower triangular matrix
    fn solve_lower(&self, Rhs) -> Result<Self::Output, LinalgError>;
}

impl<A, S1, S2> SolveTriangular<ArrayBase<S2, Ix1>> for ArrayBase<S1, Ix2>
    where A: MFloat,
          S1: Data<Elem = A>,
          S2: DataMut<Elem = A>,
          ArrayBase<S1, Ix2>: Matrix + SquareMatrix
{
    type Output = ArrayBase<S2, Ix1>;
    fn solve_upper(&self, mut b: ArrayBase<S2, Ix1>) -> Result<Self::Output, LinalgError> {
        let n = self.square_size()?;
        let layout = self.layout()?;
        let a = self.as_slice_memory_order().unwrap();
        ImplSolve::solve_triangle(layout,
                                  'U' as u8,
                                  n,
                                  a,
                                  b.as_slice_memory_order_mut().unwrap(),
                                  1)?;
        Ok(b)
    }
    fn solve_lower(&self, mut b: ArrayBase<S2, Ix1>) -> Result<Self::Output, LinalgError> {
        let n = self.square_size()?;
        let layout = self.layout()?;
        let a = self.as_slice_memory_order().unwrap();
        ImplSolve::solve_triangle(layout,
                                  'L' as u8,
                                  n,
                                  a,
                                  b.as_slice_memory_order_mut().unwrap(),
                                  1)?;
        Ok(b)
    }
}

impl<'a, S1, S2, A> SolveTriangular<&'a ArrayBase<S2, Ix2>> for ArrayBase<S1, Ix2>
    where A: MFloat,
          S1: Data<Elem = A>,
          S2: Data<Elem = A>,
          ArrayBase<S1, Ix2>: Matrix + SquareMatrix
{
    type Output = Array<A, Ix2>;
    fn solve_upper(&self, bs: &ArrayBase<S2, Ix2>) -> Result<Self::Output, LinalgError> {
        let mut xs = Vec::new();
        for b in bs.axis_iter(Axis(1)) {
            let x = self.solve_upper(b.to_owned())?;
            xs.push(x);
        }
        hstack(&xs).map_err(|e| e.into())
    }
    fn solve_lower(&self, bs: &ArrayBase<S2, Ix2>) -> Result<Self::Output, LinalgError> {
        let mut xs = Vec::new();
        for b in bs.axis_iter(Axis(1)) {
            let x = self.solve_lower(b.to_owned())?;
            xs.push(x);
        }
        hstack(&xs).map_err(|e| e.into())
    }
}

impl<A: MFloat> SolveTriangular<RcArray<A, Ix2>> for RcArray<A, Ix2> {
    type Output = RcArray<A, Ix2>;
    fn solve_upper(&self, b: RcArray<A, Ix2>) -> Result<Self::Output, LinalgError> {
        // XXX unnecessary clone
        let x = self.to_owned().solve_upper(&b)?;
        Ok(x.into_shared())
    }
    fn solve_lower(&self, b: RcArray<A, Ix2>) -> Result<Self::Output, LinalgError> {
        // XXX unnecessary clone
        let x = self.to_owned().solve_lower(&b)?;
        Ok(x.into_shared())
    }
}

pub fn drop_upper<A: NdFloat, S>(mut a: ArrayBase<S, Ix2>) -> ArrayBase<S, Ix2>
    where S: DataMut<Elem = A>
{
    for ((i, j), val) in a.indexed_iter_mut() {
        if i < j {
            *val = A::zero();
        }
    }
    a
}

pub fn drop_lower<A: NdFloat, S>(mut a: ArrayBase<S, Ix2>) -> ArrayBase<S, Ix2>
    where S: DataMut<Elem = A>
{
    for ((i, j), val) in a.indexed_iter_mut() {
        if i > j {
            *val = A::zero();
        }
    }
    a
}