Skip to content
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

Remove Any bounds #183

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 6 additions & 5 deletions benches/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ extern crate test;
extern crate rand;

pub mod linalg {
mod cholesky;
mod iter;
mod matrix;
mod svd;
mod lu;
mod cholesky;
mod qr;
mod matrix;
mod norm;
mod triangular;
mod permutation;
mod qr;
mod svd;
mod transpose;
mod triangular;
pub mod util;
}
47 changes: 47 additions & 0 deletions benches/linalg/transpose.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use rulinalg::matrix::Matrix;
use rulinalg::matrix::BaseMatrix;
use test::Bencher;
use test::black_box;

macro_rules! bench_transpose {
($name:ident, $rows:expr, $cols:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
let a = Matrix::new($rows, $cols, vec![2.0; $rows * $cols]);

b.iter(|| {
let _ = black_box(a.transpose());
});
}
}
}

macro_rules! bench_in_place_transpose {
($name:ident, $rows:expr, $cols:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
let mut a = Matrix::new($rows, $cols, vec![2.0; $rows * $cols]);

b.iter(|| {
a.in_place_transpose();
let _ = black_box(&a);
});
}
}
}

bench_transpose!(bench_transpose_10_50, 10, 50);
bench_transpose!(bench_transpose_50_10, 50, 10);
bench_transpose!(bench_transpose_10_500, 10, 500);
bench_transpose!(bench_transpose_500_10, 500, 10);
bench_transpose!(bench_transpose_500_500, 500, 500);
bench_transpose!(bench_transpose_100_5000, 100, 5000);
bench_transpose!(bench_transpose_5000_100, 5000, 100);

bench_in_place_transpose!(bench_in_place_transpose_10_50, 10, 50);
bench_in_place_transpose!(bench_in_place_transpose_50_10, 50, 10);
bench_in_place_transpose!(bench_in_place_transpose_10_500, 10, 500);
bench_in_place_transpose!(bench_in_place_transpose_500_10, 500, 10);
bench_in_place_transpose!(bench_in_place_transpose_500_500, 500, 500);
bench_in_place_transpose!(bench_in_place_transpose_100_5000, 100, 5000);
bench_in_place_transpose!(bench_in_place_transpose_5000_100, 5000, 100);
7 changes: 3 additions & 4 deletions src/matrix/base/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use utils;
use libnum::{Zero, Float};
use error::Error;

use std::any::Any;
use std::cmp::min;
use std::marker::PhantomData;
use std::mem;
Expand Down Expand Up @@ -87,7 +86,7 @@ pub trait BaseMatrix<T>: Sized {
/// ```
/// # #[macro_use] extern crate rulinalg; fn main() {
/// use rulinalg::matrix::{Matrix, BaseMatrix};
///
///
/// let mat = matrix![0, 1;
/// 3, 4;
/// 6, 7];
Expand Down Expand Up @@ -1013,7 +1012,7 @@ pub trait BaseMatrix<T>: Sized {
/// - There is no valid solution to the system (matrix is singular).
/// - The matrix is empty.
fn solve_u_triangular(&self, y: Vector<T>) -> Result<Vector<T>, Error>
where T: Any + Float
where T: Float
{
assert!(self.cols() == y.size(),
format!("Vector size {0} != {1} Matrix column count.",
Expand Down Expand Up @@ -1056,7 +1055,7 @@ pub trait BaseMatrix<T>: Sized {
/// - There is no valid solution to the system (matrix is singular).
/// - The matrix is empty.
fn solve_l_triangular(&self, y: Vector<T>) -> Result<Vector<T>, Error>
where T: Any + Float
where T: Float
{
assert!(self.cols() == y.size(),
format!("Vector size {0} != {1} Matrix column count.",
Expand Down
3 changes: 1 addition & 2 deletions src/matrix/decomposition/bidiagonal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ use matrix::{Matrix, BaseMatrix, BaseMatrixMut, MatrixSlice, MatrixSliceMut};
use error::{Error, ErrorKind};

use std;
use std::any::Any;

use libnum::Float;

impl<T> Matrix<T>
where T: Any + Float
where T: Float + 'static
{
/// Converts matrix to bidiagonal form
///
Expand Down
4 changes: 1 addition & 3 deletions src/matrix/decomposition/cholesky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use matrix::forward_substitution;
use vector::Vector;
use utils::dot;

use std::any::Any;

use libnum::{Zero, Float};

/// Cholesky decomposition.
Expand Down Expand Up @@ -247,7 +245,7 @@ impl<T: Zero> Decomposition for Cholesky<T> {


impl<T> Matrix<T>
where T: Any + Float
where T: Float
{
/// Cholesky decomposition
///
Expand Down
3 changes: 1 addition & 2 deletions src/matrix/decomposition/eigen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ use norm::Euclidean;
use error::{Error, ErrorKind};

use std::cmp;
use std::any::Any;

use libnum::{Float, Signed};
use libnum::{cast, abs};

impl<T: Any + Float + Signed> Matrix<T> {
impl<T: Float + Signed + 'static> Matrix<T> {
fn balance_matrix(&mut self) {
let n = self.rows();
let radix = T::one() + T::one();
Expand Down
4 changes: 1 addition & 3 deletions src/matrix/decomposition/hessenberg.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use matrix::{Matrix, BaseMatrix, BaseMatrixMut, MatrixSlice, MatrixSliceMut};
use error::{Error, ErrorKind};

use std::any::Any;

use libnum::{Float};

impl<T: Any + Float> Matrix<T> {
impl<T: Float + 'static> Matrix<T> {
/// Returns H, where H is the upper hessenberg form.
///
/// If the transformation matrix is also required, you should
Expand Down
13 changes: 4 additions & 9 deletions src/matrix/decomposition/lu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use matrix::PermutationMatrix;
use vector::Vector;
use error::{Error, ErrorKind};

use std::any::Any;
use std::cmp;

use libnum::{Float, Zero, One};
Expand Down Expand Up @@ -194,9 +193,7 @@ impl<T: 'static + Float> PartialPivLu<T> {
}
}

// TODO: Remove Any bound (cannot for the time being, since
// back substitution uses Any bound)
impl<T> PartialPivLu<T> where T: Any + Float {
impl<T> PartialPivLu<T> where T: Float {
/// Solves the linear system `Ax = b`.
///
/// Here, `A` is the decomposed matrix satisfying
Expand Down Expand Up @@ -430,9 +427,7 @@ impl<T: 'static + Float> FullPivLu<T> {
}
}

// TODO: Remove Any bound (cannot for the time being, since
// back substitution uses Any bound)
impl<T> FullPivLu<T> where T: Any + Float {
impl<T> FullPivLu<T> where T: Float {

/// Solves the linear system `Ax = b`.
///
Expand Down Expand Up @@ -663,7 +658,7 @@ fn unit_lower_triangular_part<T, M>(matrix: &M) -> Matrix<T>
}


impl<T> Matrix<T> where T: Any + Float
impl<T> Matrix<T> where T: Float
{
/// Computes L, U, and P for LUP decomposition.
///
Expand Down Expand Up @@ -938,7 +933,7 @@ mod tests {
-12.0, 5.0, 17.0;
15.0, 0.0, -18.0;
-6.0, 0.0, 20.0];

FullPivLu::decompose(x.clone()).unwrap();
}

Expand Down
4 changes: 1 addition & 3 deletions src/matrix/decomposition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ mod lu;
mod eigen;
mod householder;

use std::any::Any;

use matrix::{Matrix, BaseMatrix};
use norm::Euclidean;
use vector::Vector;
Expand Down Expand Up @@ -166,7 +164,7 @@ pub trait Decomposition {
}

impl<T> Matrix<T>
where T: Any + Float
where T: Float + 'static
{
/// Compute the cos and sin values for the givens rotation.
///
Expand Down
4 changes: 1 addition & 3 deletions src/matrix/decomposition/qr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use matrix::decomposition::{
};
use matrix::decomposition::householder;

use std::any::Any;

use libnum::Float;

/// The result of unpacking a QR decomposition.
Expand Down Expand Up @@ -315,7 +313,7 @@ fn extract_r1<T: Float>(qr: &Matrix<T>) -> Matrix<T> {
}

impl<T> Matrix<T>
where T: Any + Float
where T: Float + 'static
{
/// Compute the QR decomposition of the matrix.
///
Expand Down
7 changes: 3 additions & 4 deletions src/matrix/decomposition/svd.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use matrix::{Matrix, BaseMatrix, BaseMatrixMut, MatrixSlice, MatrixSliceMut};
use error::{Error, ErrorKind};

use std::any::Any;
use std::cmp;

use libnum::{Float, Signed};
Expand All @@ -16,7 +15,7 @@ fn correct_svd_signs<T>(mut b: Matrix<T>,
mut u: Matrix<T>,
mut v: Matrix<T>)
-> (Matrix<T>, Matrix<T>, Matrix<T>)
where T: Any + Float + Signed
where T: Float + Signed
{

// When correcting the signs of the singular vectors, we can choose
Expand Down Expand Up @@ -47,7 +46,7 @@ fn sort_svd<T>(mut b: Matrix<T>,
mut u: Matrix<T>,
mut v: Matrix<T>)
-> (Matrix<T>, Matrix<T>, Matrix<T>)
where T: Any + Float + Signed
where T: Float + Signed
{

assert!(u.cols() == b.cols() && b.cols() == v.cols());
Expand Down Expand Up @@ -89,7 +88,7 @@ fn sort_svd<T>(mut b: Matrix<T>,
(b, u, v)
}

impl<T: Any + Float + Signed> Matrix<T> {
impl<T: Float + Signed + 'static> Matrix<T> {
/// Singular Value Decomposition
///
/// Computes the SVD using the Golub-Reinsch algorithm.
Expand Down
Loading