pub struct Matrix<T> { /* private fields */ }
Implementations§
Source§impl<T: Clone + Copy + Number> Matrix<T>
impl<T: Clone + Copy + Number> Matrix<T>
Sourcepub fn set_col(&mut self, col: usize, vec: Vector<T>)
pub fn set_col(&mut self, col: usize, vec: Vector<T>)
Set a column of the matrix using a vector
Sourcepub fn delete_row(&mut self, row: usize)
pub fn delete_row(&mut self, row: usize)
Delete a row from the matrix
Sourcepub fn multiply(&self, vec: &Vector<T>) -> Vector<T>
pub fn multiply(&self, vec: &Vector<T>) -> Vector<T>
Multiply the matrix by a (column) vector and return a vector
Sourcepub fn resize(&mut self, n_rows: usize, n_cols: usize)
pub fn resize(&mut self, n_rows: usize, n_cols: usize)
Resize the matrix (empty entries are appended if necessary)
Sourcepub fn transpose_in_place(&mut self)
pub fn transpose_in_place(&mut self)
Transpose the matrix in place
Sourcepub fn swap_elem(
&mut self,
row_1: usize,
col_1: usize,
row_2: usize,
col_2: usize,
)
pub fn swap_elem( &mut self, row_1: usize, col_1: usize, row_2: usize, col_2: usize, )
Swap two elements of the matrix
Sourcepub fn fill_diag(&mut self, elem: T)
pub fn fill_diag(&mut self, elem: T)
Fill the leading diagonal of the matrix with specified elements
Sourcepub fn fill_band(&mut self, offset: isize, elem: T)
pub fn fill_band(&mut self, offset: isize, elem: T)
Fill a diagonal band of the matrix with specified elements ( offset above main diagonal +, below main diagonal - )
Sourcepub fn fill_tridiag(&mut self, lower: T, diag: T, upper: T)
pub fn fill_tridiag(&mut self, lower: T, diag: T, upper: T)
Fill the main three diagonals of the matrix with specified elements
Source§impl<T: Clone + Copy + Number + Signed + PartialOrd> Matrix<T>
impl<T: Clone + Copy + Number + Signed + PartialOrd> Matrix<T>
Sourcepub fn solve_basic(&mut self, b: &Vector<T>) -> Vector<T>
pub fn solve_basic(&mut self, b: &Vector<T>) -> Vector<T>
Solve the system of equations Ax=b where b is a specified vector using Gaussian elimination (the matrix A is modified in the process)
Examples found in repository?
9fn main() {
10 println!( "------------------ Sparse linear system ------------------" );
11 println!( " * Solving the linear system Ax = b, for x, where" );
12 let n: usize = 4;
13 let mut a = Mat64::new( n, n, 0.0 );
14 let mut triplets = Vec::new();
15 for i in 0..n {
16 a[(i,i)] = 2.0;
17 triplets.push( ( i, i, 2.0 ) );
18 if i > 0 {
19 a[( i - 1 , i )] = 1.0;
20 triplets.push( ( i - 1, i, 1.0 ) );
21 }
22 if i < n - 1 {
23 a[( i + 1 , i )] = 1.0;
24 triplets.push( ( i + 1, i, 1.0 ) );
25 }
26 }
27 println!( " * A ={}", a );
28 let b = Vec64::random( n );
29 println!( " * b^T ={}", b );
30 println!( " * as both dense and sparse linear systems.");
31 let dense_x = a.solve_basic( &b );
32 println!( " * The dense system gives the solution vector");
33 println!( " * x^T ={}", dense_x );
34 let sparse = Sparse::<f64>::from_triplets( n, n, &mut triplets );
35 //let sparse_x = sparse.solve( b ).unwrap();
36 let mut sparse_x = Vector::<f64>::new( n, 0.0 );
37 let max_iter = 1000;
38 let tol = 1e-8;
39 let result = sparse.solve_bicgstab( &b, &mut sparse_x, max_iter, tol );
40 println!( " * The sparse system gives the solution vector");
41 println!( " * x^T ={}", sparse_x );
42 match result {
43 Ok( iter ) => println!( " * The sparse system converged in {} iterations.", iter ),
44 Err( error ) => println!( " * The sparse system failed to converge, error = {}", error )
45 }
46 let diff = dense_x - sparse_x;
47 println!( " * The maximum difference between the two is {:+.2e}", diff.norm_inf() );
48 println!( "-------------------- FINISHED ---------------------" );
49}
More examples
11fn main() {
12 println!( "------------------ Linear system ------------------" );
13 println!( " * Solving the linear system Ax = b, for x, where" );
14 let mut a = Mat64::new( 3, 3, 0.0 );
15 a[(0,0)] = 1.0; a[(0,1)] = 1.0; a[(0,2)] = 1.0;
16 a[(1,0)] = 0.0; a[(1,1)] = 2.0; a[(1,2)] = 5.0;
17 a[(2,0)] = 2.0; a[(2,1)] = 5.0; a[(2,2)] = - 1.0;
18 println!( " * A ={}", a );
19 let b = Vec64::create( vec![ 6.0, -4.0, 27.0 ] );
20 println!( " * b^T ={}", b );
21 let x = a.clone().solve_basic( &b );
22 println!( " * gives the solution vector");
23 println!( " * x^T ={}", x );
24 println!( "---------------------------------------------------" );
25 println!( " * Lets solve the complex linear system Cy = d where" );
26 let mut c = Matrix::<Cmplx>::new( 2, 2, Cmplx::new( 1.0, 1.0 ) );
27 c[(0,1)] = Cmplx::new( -1.0, 0.0 );
28 c[(1,0)] = Cmplx::new( 1.0, -1.0 );
29 println!( " * C ={}", c );
30 let mut d = Vector::<Cmplx>::new( 2, Cmplx::new( 0.0, 1.0 ) );
31 d[1] = Cmplx::new( 1.0, 0.0 );
32 println!( " * d^T ={}", d );
33 println!( " * Using LU decomposition we find that ");
34 let y = c.clone().solve_lu( &d );
35 println!( " * y^T ={}", y );
36 println!( "---------------------------------------------------" );
37 println!( " * We may also find the determinant of a matrix" );
38 println!( " * |A| = {}", a.determinant() );
39 println!( " * |C| = {}", c.determinant() );
40 println!( " * or the inverse of a matrix" );
41 let inverse = a.inverse();
42 println!( " * A^-1 =\n{}", inverse );
43 println!( " * C^-1 =\n{}", c.inverse() );
44 println!( " * We can check this by multiplication" );
45 println!( " * I = A * A^-1 =\n{}", a * inverse );
46 println!( " * which is sufficiently accurate for our purposes.");
47 println!( "-------------------- FINISHED ---------------------" );
48}
Sourcepub fn lu_decomp_in_place(&mut self) -> (usize, Matrix<T>)
pub fn lu_decomp_in_place(&mut self) -> (usize, Matrix<T>)
Replace the matrix with its LU decomposition and return the number of pivots and a permutation matrix
Sourcepub fn solve_lu(&mut self, b: &Vector<T>) -> Vector<T>
pub fn solve_lu(&mut self, b: &Vector<T>) -> Vector<T>
Solve the system of equations Ax=b where b is a specified vector using LU decomposition (the matrix A is modified in the process)
Examples found in repository?
11fn main() {
12 println!( "------------------ Linear system ------------------" );
13 println!( " * Solving the linear system Ax = b, for x, where" );
14 let mut a = Mat64::new( 3, 3, 0.0 );
15 a[(0,0)] = 1.0; a[(0,1)] = 1.0; a[(0,2)] = 1.0;
16 a[(1,0)] = 0.0; a[(1,1)] = 2.0; a[(1,2)] = 5.0;
17 a[(2,0)] = 2.0; a[(2,1)] = 5.0; a[(2,2)] = - 1.0;
18 println!( " * A ={}", a );
19 let b = Vec64::create( vec![ 6.0, -4.0, 27.0 ] );
20 println!( " * b^T ={}", b );
21 let x = a.clone().solve_basic( &b );
22 println!( " * gives the solution vector");
23 println!( " * x^T ={}", x );
24 println!( "---------------------------------------------------" );
25 println!( " * Lets solve the complex linear system Cy = d where" );
26 let mut c = Matrix::<Cmplx>::new( 2, 2, Cmplx::new( 1.0, 1.0 ) );
27 c[(0,1)] = Cmplx::new( -1.0, 0.0 );
28 c[(1,0)] = Cmplx::new( 1.0, -1.0 );
29 println!( " * C ={}", c );
30 let mut d = Vector::<Cmplx>::new( 2, Cmplx::new( 0.0, 1.0 ) );
31 d[1] = Cmplx::new( 1.0, 0.0 );
32 println!( " * d^T ={}", d );
33 println!( " * Using LU decomposition we find that ");
34 let y = c.clone().solve_lu( &d );
35 println!( " * y^T ={}", y );
36 println!( "---------------------------------------------------" );
37 println!( " * We may also find the determinant of a matrix" );
38 println!( " * |A| = {}", a.determinant() );
39 println!( " * |C| = {}", c.determinant() );
40 println!( " * or the inverse of a matrix" );
41 let inverse = a.inverse();
42 println!( " * A^-1 =\n{}", inverse );
43 println!( " * C^-1 =\n{}", c.inverse() );
44 println!( " * We can check this by multiplication" );
45 println!( " * I = A * A^-1 =\n{}", a * inverse );
46 println!( " * which is sufficiently accurate for our purposes.");
47 println!( "-------------------- FINISHED ---------------------" );
48}
Sourcepub fn determinant(&self) -> T
pub fn determinant(&self) -> T
Calculate the determinant of the matrix ( via LU decomposition )
Examples found in repository?
11fn main() {
12 println!( "------------------ Linear system ------------------" );
13 println!( " * Solving the linear system Ax = b, for x, where" );
14 let mut a = Mat64::new( 3, 3, 0.0 );
15 a[(0,0)] = 1.0; a[(0,1)] = 1.0; a[(0,2)] = 1.0;
16 a[(1,0)] = 0.0; a[(1,1)] = 2.0; a[(1,2)] = 5.0;
17 a[(2,0)] = 2.0; a[(2,1)] = 5.0; a[(2,2)] = - 1.0;
18 println!( " * A ={}", a );
19 let b = Vec64::create( vec![ 6.0, -4.0, 27.0 ] );
20 println!( " * b^T ={}", b );
21 let x = a.clone().solve_basic( &b );
22 println!( " * gives the solution vector");
23 println!( " * x^T ={}", x );
24 println!( "---------------------------------------------------" );
25 println!( " * Lets solve the complex linear system Cy = d where" );
26 let mut c = Matrix::<Cmplx>::new( 2, 2, Cmplx::new( 1.0, 1.0 ) );
27 c[(0,1)] = Cmplx::new( -1.0, 0.0 );
28 c[(1,0)] = Cmplx::new( 1.0, -1.0 );
29 println!( " * C ={}", c );
30 let mut d = Vector::<Cmplx>::new( 2, Cmplx::new( 0.0, 1.0 ) );
31 d[1] = Cmplx::new( 1.0, 0.0 );
32 println!( " * d^T ={}", d );
33 println!( " * Using LU decomposition we find that ");
34 let y = c.clone().solve_lu( &d );
35 println!( " * y^T ={}", y );
36 println!( "---------------------------------------------------" );
37 println!( " * We may also find the determinant of a matrix" );
38 println!( " * |A| = {}", a.determinant() );
39 println!( " * |C| = {}", c.determinant() );
40 println!( " * or the inverse of a matrix" );
41 let inverse = a.inverse();
42 println!( " * A^-1 =\n{}", inverse );
43 println!( " * C^-1 =\n{}", c.inverse() );
44 println!( " * We can check this by multiplication" );
45 println!( " * I = A * A^-1 =\n{}", a * inverse );
46 println!( " * which is sufficiently accurate for our purposes.");
47 println!( "-------------------- FINISHED ---------------------" );
48}
Sourcepub fn inverse(&self) -> Matrix<T>
pub fn inverse(&self) -> Matrix<T>
Return the inverse of the matrix ( via LU decomposition )
Examples found in repository?
11fn main() {
12 println!( "------------------ Linear system ------------------" );
13 println!( " * Solving the linear system Ax = b, for x, where" );
14 let mut a = Mat64::new( 3, 3, 0.0 );
15 a[(0,0)] = 1.0; a[(0,1)] = 1.0; a[(0,2)] = 1.0;
16 a[(1,0)] = 0.0; a[(1,1)] = 2.0; a[(1,2)] = 5.0;
17 a[(2,0)] = 2.0; a[(2,1)] = 5.0; a[(2,2)] = - 1.0;
18 println!( " * A ={}", a );
19 let b = Vec64::create( vec![ 6.0, -4.0, 27.0 ] );
20 println!( " * b^T ={}", b );
21 let x = a.clone().solve_basic( &b );
22 println!( " * gives the solution vector");
23 println!( " * x^T ={}", x );
24 println!( "---------------------------------------------------" );
25 println!( " * Lets solve the complex linear system Cy = d where" );
26 let mut c = Matrix::<Cmplx>::new( 2, 2, Cmplx::new( 1.0, 1.0 ) );
27 c[(0,1)] = Cmplx::new( -1.0, 0.0 );
28 c[(1,0)] = Cmplx::new( 1.0, -1.0 );
29 println!( " * C ={}", c );
30 let mut d = Vector::<Cmplx>::new( 2, Cmplx::new( 0.0, 1.0 ) );
31 d[1] = Cmplx::new( 1.0, 0.0 );
32 println!( " * d^T ={}", d );
33 println!( " * Using LU decomposition we find that ");
34 let y = c.clone().solve_lu( &d );
35 println!( " * y^T ={}", y );
36 println!( "---------------------------------------------------" );
37 println!( " * We may also find the determinant of a matrix" );
38 println!( " * |A| = {}", a.determinant() );
39 println!( " * |C| = {}", c.determinant() );
40 println!( " * or the inverse of a matrix" );
41 let inverse = a.inverse();
42 println!( " * A^-1 =\n{}", inverse );
43 println!( " * C^-1 =\n{}", c.inverse() );
44 println!( " * We can check this by multiplication" );
45 println!( " * I = A * A^-1 =\n{}", a * inverse );
46 println!( " * which is sufficiently accurate for our purposes.");
47 println!( "-------------------- FINISHED ---------------------" );
48}
Source§impl Matrix<f64>
impl Matrix<f64>
Source§impl<T: Clone + Number> Matrix<T>
impl<T: Clone + Number> Matrix<T>
Sourcepub fn new(rows: usize, cols: usize, elem: T) -> Self
pub fn new(rows: usize, cols: usize, elem: T) -> Self
Create a new matrix of specified size
Examples found in repository?
9fn main() {
10 println!( "------------------ Sparse linear system ------------------" );
11 println!( " * Solving the linear system Ax = b, for x, where" );
12 let n: usize = 4;
13 let mut a = Mat64::new( n, n, 0.0 );
14 let mut triplets = Vec::new();
15 for i in 0..n {
16 a[(i,i)] = 2.0;
17 triplets.push( ( i, i, 2.0 ) );
18 if i > 0 {
19 a[( i - 1 , i )] = 1.0;
20 triplets.push( ( i - 1, i, 1.0 ) );
21 }
22 if i < n - 1 {
23 a[( i + 1 , i )] = 1.0;
24 triplets.push( ( i + 1, i, 1.0 ) );
25 }
26 }
27 println!( " * A ={}", a );
28 let b = Vec64::random( n );
29 println!( " * b^T ={}", b );
30 println!( " * as both dense and sparse linear systems.");
31 let dense_x = a.solve_basic( &b );
32 println!( " * The dense system gives the solution vector");
33 println!( " * x^T ={}", dense_x );
34 let sparse = Sparse::<f64>::from_triplets( n, n, &mut triplets );
35 //let sparse_x = sparse.solve( b ).unwrap();
36 let mut sparse_x = Vector::<f64>::new( n, 0.0 );
37 let max_iter = 1000;
38 let tol = 1e-8;
39 let result = sparse.solve_bicgstab( &b, &mut sparse_x, max_iter, tol );
40 println!( " * The sparse system gives the solution vector");
41 println!( " * x^T ={}", sparse_x );
42 match result {
43 Ok( iter ) => println!( " * The sparse system converged in {} iterations.", iter ),
44 Err( error ) => println!( " * The sparse system failed to converge, error = {}", error )
45 }
46 let diff = dense_x - sparse_x;
47 println!( " * The maximum difference between the two is {:+.2e}", diff.norm_inf() );
48 println!( "-------------------- FINISHED ---------------------" );
49}
More examples
11fn main() {
12 println!( "------------------ Linear system ------------------" );
13 println!( " * Solving the linear system Ax = b, for x, where" );
14 let mut a = Mat64::new( 3, 3, 0.0 );
15 a[(0,0)] = 1.0; a[(0,1)] = 1.0; a[(0,2)] = 1.0;
16 a[(1,0)] = 0.0; a[(1,1)] = 2.0; a[(1,2)] = 5.0;
17 a[(2,0)] = 2.0; a[(2,1)] = 5.0; a[(2,2)] = - 1.0;
18 println!( " * A ={}", a );
19 let b = Vec64::create( vec![ 6.0, -4.0, 27.0 ] );
20 println!( " * b^T ={}", b );
21 let x = a.clone().solve_basic( &b );
22 println!( " * gives the solution vector");
23 println!( " * x^T ={}", x );
24 println!( "---------------------------------------------------" );
25 println!( " * Lets solve the complex linear system Cy = d where" );
26 let mut c = Matrix::<Cmplx>::new( 2, 2, Cmplx::new( 1.0, 1.0 ) );
27 c[(0,1)] = Cmplx::new( -1.0, 0.0 );
28 c[(1,0)] = Cmplx::new( 1.0, -1.0 );
29 println!( " * C ={}", c );
30 let mut d = Vector::<Cmplx>::new( 2, Cmplx::new( 0.0, 1.0 ) );
31 d[1] = Cmplx::new( 1.0, 0.0 );
32 println!( " * d^T ={}", d );
33 println!( " * Using LU decomposition we find that ");
34 let y = c.clone().solve_lu( &d );
35 println!( " * y^T ={}", y );
36 println!( "---------------------------------------------------" );
37 println!( " * We may also find the determinant of a matrix" );
38 println!( " * |A| = {}", a.determinant() );
39 println!( " * |C| = {}", c.determinant() );
40 println!( " * or the inverse of a matrix" );
41 let inverse = a.inverse();
42 println!( " * A^-1 =\n{}", inverse );
43 println!( " * C^-1 =\n{}", c.inverse() );
44 println!( " * We can check this by multiplication" );
45 println!( " * I = A * A^-1 =\n{}", a * inverse );
46 println!( " * which is sufficiently accurate for our purposes.");
47 println!( "-------------------- FINISHED ---------------------" );
48}
Trait Implementations§
Source§impl<T: Copy + Number> AddAssign<&Matrix<T>> for Matrix<T>
impl<T: Copy + Number> AddAssign<&Matrix<T>> for Matrix<T>
Source§fn add_assign(&mut self, rhs: &Self)
fn add_assign(&mut self, rhs: &Self)
Add a matrix to a mutable matrix and assign the result ( += )
Source§impl<T: Copy + Number> AddAssign<T> for Matrix<T>
impl<T: Copy + Number> AddAssign<T> for Matrix<T>
Source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
Add the same value to every element in a mutable matrix
Source§impl<T: Copy + Number> AddAssign for Matrix<T>
impl<T: Copy + Number> AddAssign for Matrix<T>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Add a matrix to a mutable matrix and assign the result ( += )
Source§impl<T: Copy + Number> DivAssign<T> for Matrix<T>
impl<T: Copy + Number> DivAssign<T> for Matrix<T>
Source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
Divide a mutable matrix by a scalar (matrix /= scalar)
Source§impl<T: Copy + Number> MulAssign<T> for Matrix<T>
impl<T: Copy + Number> MulAssign<T> for Matrix<T>
Source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
Multiply a mutable matrix by a scalar (matrix *= scalar)
Source§impl<T: Copy + Number> SubAssign<&Matrix<T>> for Matrix<T>
impl<T: Copy + Number> SubAssign<&Matrix<T>> for Matrix<T>
Source§fn sub_assign(&mut self, rhs: &Self)
fn sub_assign(&mut self, rhs: &Self)
Subtract a matrix from a mutable matrix and assign the result ( -= )
Source§impl<T: Copy + Number> SubAssign<T> for Matrix<T>
impl<T: Copy + Number> SubAssign<T> for Matrix<T>
Source§fn sub_assign(&mut self, rhs: T)
fn sub_assign(&mut self, rhs: T)
Subtract the same value from every element in a mutable matrix
Source§impl<T: Copy + Number> SubAssign for Matrix<T>
impl<T: Copy + Number> SubAssign for Matrix<T>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Subtract a matrix from a mutable matrix and assign the result ( -= )