Skip to content

Commit 1894874

Browse files
committed
Introduce MFloat and HMFloat
1 parent 4c44542 commit 1894874

File tree

4 files changed

+18
-37
lines changed

4 files changed

+18
-37
lines changed

src/hermite.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
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 HMFloat for f32 {}
14+
impl HMFloat for f64 {}
15+
1916
/// Methods for Hermite matrix
2017
pub trait HermiteMatrix: SquareMatrix + Matrix {
2118
/// eigenvalue decomposition
@@ -28,9 +25,7 @@ pub trait HermiteMatrix: SquareMatrix + Matrix {
2825
fn deth(self) -> Result<Self::Scalar, LinalgError>;
2926
}
3027

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

src/matrix.rs

Lines changed: 5 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,10 @@ use svd::ImplSVD;
1210
use norm::ImplNorm;
1311
use solve::ImplSolve;
1412

13+
pub trait MFloat: ImplQR + ImplSVD + ImplNorm + ImplSolve + NdFloat {}
14+
impl MFloat for f32 {}
15+
impl MFloat for f64 {}
16+
1517
/// Methods for general matrices
1618
pub trait Matrix: Sized {
1719
type Scalar;
@@ -42,9 +44,7 @@ pub trait Matrix: Sized {
4244
}
4345
}
4446

45-
impl<A> Matrix for Array<A, Ix2>
46-
where A: ImplQR + ImplSVD + ImplNorm + ImplSolve + LinalgScalar + Debug
47-
{
47+
impl<A: MFloat> Matrix for Array<A, Ix2> {
4848
type Scalar = A;
4949
type Vector = Array<A, Ix1>;
5050
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)