Skip to content

Commit 53cb90f

Browse files
authored
Merge pull request #22 from termoshtt/traits
Introduce MFloat and HMFloat
2 parents 4c44542 + 0f927ba commit 53cb90f

File tree

4 files changed

+16
-37
lines changed

4 files changed

+16
-37
lines changed

src/hermite.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
//! Define trait for Hermite matrices
22
3-
use ndarray::{Ix2, Array, LinalgScalar};
4-
use std::fmt::Debug;
5-
use num_traits::float::Float;
6-
use num_traits::One;
3+
use ndarray::{Ix2, Array};
74
use lapack::c::Layout;
85

9-
use matrix::Matrix;
6+
use matrix::{Matrix, MFloat};
107
use square::SquareMatrix;
118
use error::LinalgError;
129
use eigh::ImplEigh;
13-
use qr::ImplQR;
14-
use svd::ImplSVD;
15-
use norm::ImplNorm;
16-
use solve::ImplSolve;
1710
use cholesky::ImplCholesky;
1811

12+
pub trait HMFloat: ImplEigh + ImplCholesky + MFloat {}
13+
impl<A: ImplEigh + ImplCholesky + MFloat> HMFloat for A {}
14+
1915
/// Methods for Hermite matrix
2016
pub trait HermiteMatrix: SquareMatrix + Matrix {
2117
/// eigenvalue decomposition
@@ -28,9 +24,7 @@ pub trait HermiteMatrix: SquareMatrix + Matrix {
2824
fn deth(self) -> Result<Self::Scalar, LinalgError>;
2925
}
3026

31-
impl<A> HermiteMatrix for Array<A, Ix2>
32-
where A: ImplQR + ImplSVD + ImplNorm + ImplSolve + ImplEigh + ImplCholesky + LinalgScalar + Float + Debug + One
33-
{
27+
impl<A: HMFloat> HermiteMatrix for Array<A, Ix2> {
3428
fn eigh(self) -> Result<(Self::Vector, Self), LinalgError> {
3529
self.check_square()?;
3630
let layout = self.layout()?;

src/matrix.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
//! Define trait for general matrix
22
33
use std::cmp::min;
4-
use std::fmt::Debug;
54
use ndarray::prelude::*;
6-
use ndarray::LinalgScalar;
75
use lapack::c::Layout;
86

97
use error::{LinalgError, StrideError};
@@ -12,6 +10,9 @@ use svd::ImplSVD;
1210
use norm::ImplNorm;
1311
use solve::ImplSolve;
1412

13+
pub trait MFloat: ImplQR + ImplSVD + ImplNorm + ImplSolve + NdFloat {}
14+
impl<A: ImplQR + ImplSVD + ImplNorm + ImplSolve + NdFloat> MFloat for A {}
15+
1516
/// Methods for general matrices
1617
pub trait Matrix: Sized {
1718
type Scalar;
@@ -42,9 +43,7 @@ pub trait Matrix: Sized {
4243
}
4344
}
4445

45-
impl<A> Matrix for Array<A, Ix2>
46-
where A: ImplQR + ImplSVD + ImplNorm + ImplSolve + LinalgScalar + Debug
47-
{
46+
impl<A: MFloat> Matrix for Array<A, Ix2> {
4847
type Scalar = A;
4948
type Vector = Array<A, Ix1>;
5049
type Permutator = Vec<i32>;

src/square.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
//! Define trait for Hermite matrices
22
3-
use ndarray::{Ix2, Array, LinalgScalar};
4-
use std::fmt::Debug;
5-
use num_traits::float::Float;
3+
use ndarray::{Ix2, Array};
64
use lapack::c::Layout;
75

8-
use matrix::Matrix;
6+
use matrix::{Matrix, MFloat};
97
use error::{LinalgError, NotSquareError};
10-
use qr::ImplQR;
11-
use svd::ImplSVD;
12-
use norm::ImplNorm;
138
use solve::ImplSolve;
149

1510
/// Methods for square matrices
@@ -37,9 +32,7 @@ pub trait SquareMatrix: Matrix {
3732
}
3833
}
3934

40-
impl<A> SquareMatrix for Array<A, Ix2>
41-
where A: ImplQR + ImplNorm + ImplSVD + ImplSolve + LinalgScalar + Float + Debug
42-
{
35+
impl<A: MFloat> SquareMatrix for Array<A, Ix2> {
4336
fn inv(self) -> Result<Self, LinalgError> {
4437
self.check_square()?;
4538
let (n, _) = self.size();

src/triangular.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11

2-
use ndarray::{Ix2, Array, LinalgScalar};
3-
use std::fmt::Debug;
4-
use num_traits::float::Float;
2+
use ndarray::{Ix2, Array};
53

6-
use matrix::Matrix;
4+
use matrix::{Matrix, MFloat};
75
use square::SquareMatrix;
86
use error::LinalgError;
9-
use qr::ImplQR;
10-
use svd::ImplSVD;
11-
use norm::ImplNorm;
127
use solve::ImplSolve;
138

149
pub trait TriangularMatrix: Matrix + SquareMatrix {
@@ -18,9 +13,7 @@ pub trait TriangularMatrix: Matrix + SquareMatrix {
1813
fn solve_lower(&self, Self::Vector) -> Result<Self::Vector, LinalgError>;
1914
}
2015

21-
impl<A> TriangularMatrix for Array<A, Ix2>
22-
where A: ImplQR + ImplNorm + ImplSVD + ImplSolve + LinalgScalar + Float + Debug
23-
{
16+
impl<A: MFloat> TriangularMatrix for Array<A, Ix2> {
2417
fn solve_upper(&self, b: Self::Vector) -> Result<Self::Vector, LinalgError> {
2518
self.check_square()?;
2619
let (n, _) = self.size();

0 commit comments

Comments
 (0)