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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
use num_traits::One;
use num_traits::Zero;
pub mod det;
pub mod submatrix;
pub mod inv;
pub mod transpose;
pub mod add;
pub mod sub;
pub mod trace;
pub mod adj;
pub mod minor;
pub mod cofactor;
pub mod kronecker;
pub mod mul;
pub mod conj;
pub mod herm;
pub mod eig;
pub mod diag;
pub mod qr_householder;
pub mod div;
pub use self::det::*;
pub use self::submatrix::*;
pub use self::inv::*;
pub use self::transpose::*;
pub use self::add::*;
pub use self::sub::*;
pub use self::trace::*;
pub use self::adj::*;
pub use self::minor::*;
pub use self::cofactor::*;
pub use self::kronecker::*;
pub use self::mul::*;
pub use self::conj::*;
pub use self::herm::*;
pub use self::eig::*;
pub use self::diag::*;
pub use self::qr_householder::*;
pub use self::div::*;
pub trait Matrix: Sized
{
/// Returns the height of the given matrix
///
/// # Examples
///
/// ```rust
/// # #![feature(generic_const_exprs)]
/// # #![allow(unused)]
/// # extern crate matrix;
/// # extern crate num_traits;
/// # extern crate array_init;
/// #
/// let a = [
/// [1.0, 2.0, 3.0],
/// [4.0, 5.0, 6.0]
/// ];
/// assert_eq!(a.height(), 2);
/// ```
fn height(&self) -> usize;
/// Returns the length of the given matrix
///
/// # Examples
///
/// ```rust
/// # #![feature(generic_const_exprs)]
/// # #![allow(unused)]
/// # extern crate matrix;
/// # extern crate num_traits;
/// # extern crate array_init;
/// #
/// let a = [
/// [1.0, 2.0, 3.0],
/// [4.0, 5.0, 6.0]
/// ];
/// assert_eq!(a.length(), 3);
/// ```
fn length(&self) -> usize;
/// Returns an empty matrix with the given dimensions
///
/// # Examples
///
/// ```rust
/// # #![feature(generic_const_exprs)]
/// # #![allow(unused)]
/// # extern crate matrix;
/// # extern crate num_traits;
/// # extern crate array_init;
/// #
/// let a = [
/// [0.0, 0.0],
/// [0.0, 0.0]
/// ]
/// assert_eq!(a, Matrix::empty())
/// ```
fn empty() -> Self;
}
/// Initialize a matrix given an initializer expression.
/// The initializer is given the row- and collumn-indices of the cell.
/// It is allowed to mutate external state; we will always initialize the cells in order.
///
/// # Examples
///
/// ```rust
/// # #![feature(generic_const_exprs)]
/// # #![allow(unused)]
/// # extern crate matrix;
/// # extern crate num_traits;
/// # extern crate array_init;
/// #
/// let a: [[f64; 3]; 3] = matrix_init(|r, c| (r * c) as f64);
///
/// assert!(arr.iter().enumerate().all(|(r, ar)| ar.iter().enumerate().all(|(c, &arc)| arc == (r * c) as f64)));
/// ```
pub fn matrix_init<F, T, const L: usize, const H: usize>(mut initializer: F) -> [[T; L]; H]
where
F: FnMut(usize, usize) -> T,
{
use array_init::array_init;
array_init(|r| array_init(|c| initializer(r, c)))
}
impl<F: Zero, const L: usize, const H: usize> Matrix for [[F; L]; H]
where [[F; L - 1 as usize]; H - 1]:
{
fn height(&self) -> usize
{
H
}
fn length(&self) -> usize
{
L
}
fn empty() -> Self
{
matrix_init(|_, _| F::zero())
}
}
pub trait SquareMatrix: Matrix
{
/// Returns the identity matrix with the given dimensions
///
/// # Examples
///
/// ```rust
/// # #![feature(generic_const_exprs)]
/// # #![allow(unused)]
/// # extern crate matrix;
/// # extern crate num_traits;
/// # extern crate array_init;
/// let a = [
/// [1.0, 0.0],
/// [0.0, 1.0]
/// ]
/// assert_eq!(a, SquareMatrix::identity())
/// ```
fn identity() -> Self;
}
impl<F: One + Zero, const N: usize> SquareMatrix for [[F; N]; N]
where
Self: Matrix,
[[F; N - 1 as usize]; N - 1]:
{
fn identity() -> Self
{
matrix_init(|r, c| if r == c {F::one()} else {F::zero()})
}
}