Skip to content

Triangular matrix #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jun 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@ pub fn conjugate<A, Si, So>(a: &ArrayBase<Si, Ix2>) -> ArrayBase<So, Ix2>
a
}

/// Random vector
pub fn random_vector<A, S>(n: usize) -> ArrayBase<S, Ix1>
where A: RandNormal,
S: DataOwned<Elem = A>
{
let mut rng = thread_rng();
let v: Vec<A> = (0..n).map(|_| A::randn(&mut rng)).collect();
ArrayBase::from_vec(v)
}

/// Random matrix
pub fn random<A, S>(n: usize, m: usize) -> ArrayBase<S, Ix2>
pub fn random_matrix<A, S>(n: usize, m: usize) -> ArrayBase<S, Ix2>
where A: RandNormal,
S: DataOwned<Elem = A>
{
Expand All @@ -34,7 +44,7 @@ pub fn random_square<A, S>(n: usize) -> ArrayBase<S, Ix2>
where A: RandNormal,
S: DataOwned<Elem = A>
{
random(n, n)
random_matrix(n, n)
}

/// Random Hermite matrix
Expand Down
13 changes: 12 additions & 1 deletion src/impl2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ pub mod svd;
pub mod solve;
pub mod cholesky;
pub mod eigh;
pub mod triangular;

pub use self::opnorm::*;
pub use self::qr::*;
pub use self::svd::*;
pub use self::solve::*;
pub use self::cholesky::*;
pub use self::eigh::*;
pub use self::triangular::*;

use super::error::*;

Expand All @@ -20,7 +22,8 @@ trait_alias!(LapackScalar: OperatorNorm_,
SVD_,
Solve_,
Cholesky_,
Eigh_);
Eigh_,
Triangular_);

pub fn into_result<T>(info: i32, val: T) -> Result<T> {
if info == 0 {
Expand All @@ -36,3 +39,11 @@ pub enum UPLO {
Upper = b'U',
Lower = b'L',
}

#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum Transpose {
No = b'N',
Transpose = b'T',
Hermite = b'C',
}
10 changes: 1 addition & 9 deletions src/impl2/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@ use types::*;
use error::*;
use layout::Layout;

use super::into_result;
use super::{Transpose, into_result};

pub type Pivot = Vec<i32>;

#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum Transpose {
No = b'N',
Transpose = b'T',
Hermite = b'C',
}

pub trait Solve_: Sized {
fn lu(Layout, a: &mut [Self]) -> Result<Pivot>;
fn inv(Layout, a: &mut [Self], &Pivot) -> Result<()>;
Expand Down
54 changes: 54 additions & 0 deletions src/impl2/triangular.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! Implement linear solver and inverse matrix

use lapack::c;

use error::*;
use types::*;
use layout::Layout;
use super::{UPLO, Transpose, into_result};

#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum Diag {
Unit = b'U',
NonUnit = b'N',
}

pub trait Triangular_: Sized {
fn inv_triangular(l: Layout, UPLO, Diag, a: &mut [Self]) -> Result<()>;
fn solve_triangular(al: Layout, bl: Layout, UPLO, Diag, a: &[Self], b: &mut [Self]) -> Result<()>;
}

macro_rules! impl_triangular {
($scalar:ty, $trtri:path, $trtrs:path) => {

impl Triangular_ for $scalar {
fn inv_triangular(l: Layout, uplo: UPLO, diag: Diag, a: &mut [Self]) -> Result<()> {
let (n, _) = l.size();
let lda = l.lda();
let info = $trtri(l.lapacke_layout(), uplo as u8, diag as u8, n, a, lda);
into_result(info, ())
}

fn solve_triangular(al: Layout, bl: Layout, uplo: UPLO, diag: Diag, a: &[Self], mut b: &mut [Self]) -> Result<()> {
let (n, _) = al.size();
let lda = al.lda();
let (_, nrhs) = bl.size();
let ldb = bl.lda();
println!("al = {:?}", al);
println!("bl = {:?}", bl);
println!("n = {}", n);
println!("lda = {}", lda);
println!("nrhs = {}", nrhs);
println!("ldb = {}", ldb);
let info = $trtrs(al.lapacke_layout(), uplo as u8, Transpose::No as u8, diag as u8, n, nrhs, a, lda, &mut b, ldb);
into_result(info, ())
}
}

}} // impl_triangular!

impl_triangular!(f64, c::dtrtri, c::dtrtrs);
impl_triangular!(f32, c::strtri, c::strtrs);
impl_triangular!(c64, c::ztrtri, c::ztrtrs);
impl_triangular!(c32, c::ctrtri, c::ctrtrs);
2 changes: 0 additions & 2 deletions src/impls/mod.rs

This file was deleted.

87 changes: 0 additions & 87 deletions src/impls/solve.rs

This file was deleted.

Loading