pub struct Vector<T> {
pub vec: Vec<T>,
/* private fields */
}
Fields§
§vec: Vec<T>
Implementations§
source§impl<T: Default> Vector<T>
impl<T: Default> Vector<T>
sourcepub fn resize(&mut self, new_size: usize)
pub fn resize(&mut self, new_size: usize)
Resize the vector
Examples found in repository?
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
fn main() {
println!("----- Vector algebra -----");
// Create a filled vector of specified size
let mut a = Vec64::new( 4, 0.0 );
a[ 0 ] = 1.0; a[ 1 ] = 3.0; a[ 2 ] = 1.0; a[ 3 ] = - 1.0;
println!(" * a = {}", a.clone() );
// Create a vector from a standard vec
let mut b = Vector::<f64>::create( vec![ 2.0, 1.0, 3.0, 4.0 ] );
println!(" * b = {}", b.clone() );
// Create an empty vector of unspecified size
let mut c = Vec64::empty();
println!(" * c = {}", c );
// Do some basic algebra
c = a.clone() + b.clone();
println!( " * c = a + b = {}", c );
c = b.clone() - a.clone();
println!( " * c = b - a = {}", c );
c = 3.0 * a.clone();
println!( " * c = 3 * a = {}", c );
c = a.clone() / 3.0;
println!( " * c = a / 3 = {}", c );
// Add some new elements and swap them around
a.push( 2.0 );
b.resize( 5 );
b[ 4 ] = - 3.0;
b.swap( 3, 4 );
println!(" * a = {}", a.clone() );
println!(" * b = {}", b.clone() );
// Remove elements
a.pop();
a.pop();
b.resize( 3 );
println!( " * a = {}", a.clone() );
println!( " * b = {}", b.clone() );
// Magnitude of the vectors
println!( " * |a| = {}", a.norm_2() );
println!( " * |b| = {}", b.norm_p( 2.0 ) );
// Angle between the two vectors
let cos: f64 = a.dot( b.clone() ) / ( a.norm_2() * b.norm_2() );
let rad = f64::acos( cos );
let deg = rad * 180.0 / PI;
println!( " * angle between a and b = {} degrees", deg );
// Create vectors of spaced elements
let a = Vec64::linspace( 0.0, 1.0, 5 );
let b = Vec64::powspace( 0.0, 1.0, 5, 1.5 );
println!( " * Linearly spaced elements = {}", a );
println!( " * Power law spaced elements (p=1.5) = {}", b );
println!( "--- FINISHED ---" );
}
source§impl<T: Clone + Number> Vector<T>
impl<T: Clone + Number> Vector<T>
sourcepub fn dot(&self, w: Vector<T>) -> T
pub fn dot(&self, w: Vector<T>) -> T
Return the dot product of two vectors v.dot(w)
Examples found in repository?
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
fn main() {
println!("----- Vector algebra -----");
// Create a filled vector of specified size
let mut a = Vec64::new( 4, 0.0 );
a[ 0 ] = 1.0; a[ 1 ] = 3.0; a[ 2 ] = 1.0; a[ 3 ] = - 1.0;
println!(" * a = {}", a.clone() );
// Create a vector from a standard vec
let mut b = Vector::<f64>::create( vec![ 2.0, 1.0, 3.0, 4.0 ] );
println!(" * b = {}", b.clone() );
// Create an empty vector of unspecified size
let mut c = Vec64::empty();
println!(" * c = {}", c );
// Do some basic algebra
c = a.clone() + b.clone();
println!( " * c = a + b = {}", c );
c = b.clone() - a.clone();
println!( " * c = b - a = {}", c );
c = 3.0 * a.clone();
println!( " * c = 3 * a = {}", c );
c = a.clone() / 3.0;
println!( " * c = a / 3 = {}", c );
// Add some new elements and swap them around
a.push( 2.0 );
b.resize( 5 );
b[ 4 ] = - 3.0;
b.swap( 3, 4 );
println!(" * a = {}", a.clone() );
println!(" * b = {}", b.clone() );
// Remove elements
a.pop();
a.pop();
b.resize( 3 );
println!( " * a = {}", a.clone() );
println!( " * b = {}", b.clone() );
// Magnitude of the vectors
println!( " * |a| = {}", a.norm_2() );
println!( " * |b| = {}", b.norm_p( 2.0 ) );
// Angle between the two vectors
let cos: f64 = a.dot( b.clone() ) / ( a.norm_2() * b.norm_2() );
let rad = f64::acos( cos );
let deg = rad * 180.0 / PI;
println!( " * angle between a and b = {} degrees", deg );
// Create vectors of spaced elements
let a = Vec64::linspace( 0.0, 1.0, 5 );
let b = Vec64::powspace( 0.0, 1.0, 5, 1.5 );
println!( " * Linearly spaced elements = {}", a );
println!( " * Power law spaced elements (p=1.5) = {}", b );
println!( "--- FINISHED ---" );
}
sourcepub fn sum_slice(&self, start: usize, end: usize) -> T
pub fn sum_slice(&self, start: usize, end: usize) -> T
Return the sum of the elements in the vector from index start to end
sourcepub fn product_slice(&self, start: usize, end: usize) -> T
pub fn product_slice(&self, start: usize, end: usize) -> T
Return the product of the elements in the vector from index start to end
source§impl<T: PartialEq + PartialOrd> Vector<T>
impl<T: PartialEq + PartialOrd> Vector<T>
source§impl<T> Vector<T>
impl<T> Vector<T>
sourcepub fn swap(&mut self, i: usize, j: usize)
pub fn swap(&mut self, i: usize, j: usize)
Swap elements two elements in the vector
Examples found in repository?
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
fn main() {
println!("----- Vector algebra -----");
// Create a filled vector of specified size
let mut a = Vec64::new( 4, 0.0 );
a[ 0 ] = 1.0; a[ 1 ] = 3.0; a[ 2 ] = 1.0; a[ 3 ] = - 1.0;
println!(" * a = {}", a.clone() );
// Create a vector from a standard vec
let mut b = Vector::<f64>::create( vec![ 2.0, 1.0, 3.0, 4.0 ] );
println!(" * b = {}", b.clone() );
// Create an empty vector of unspecified size
let mut c = Vec64::empty();
println!(" * c = {}", c );
// Do some basic algebra
c = a.clone() + b.clone();
println!( " * c = a + b = {}", c );
c = b.clone() - a.clone();
println!( " * c = b - a = {}", c );
c = 3.0 * a.clone();
println!( " * c = 3 * a = {}", c );
c = a.clone() / 3.0;
println!( " * c = a / 3 = {}", c );
// Add some new elements and swap them around
a.push( 2.0 );
b.resize( 5 );
b[ 4 ] = - 3.0;
b.swap( 3, 4 );
println!(" * a = {}", a.clone() );
println!(" * b = {}", b.clone() );
// Remove elements
a.pop();
a.pop();
b.resize( 3 );
println!( " * a = {}", a.clone() );
println!( " * b = {}", b.clone() );
// Magnitude of the vectors
println!( " * |a| = {}", a.norm_2() );
println!( " * |b| = {}", b.norm_p( 2.0 ) );
// Angle between the two vectors
let cos: f64 = a.dot( b.clone() ) / ( a.norm_2() * b.norm_2() );
let rad = f64::acos( cos );
let deg = rad * 180.0 / PI;
println!( " * angle between a and b = {} degrees", deg );
// Create vectors of spaced elements
let a = Vec64::linspace( 0.0, 1.0, 5 );
let b = Vec64::powspace( 0.0, 1.0, 5, 1.5 );
println!( " * Linearly spaced elements = {}", a );
println!( " * Power law spaced elements (p=1.5) = {}", b );
println!( "--- FINISHED ---" );
}
sourcepub fn push(&mut self, elem: T)
pub fn push(&mut self, elem: T)
Push a new element onto the end of the vector
Examples found in repository?
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
fn main() {
println!("----- Vector algebra -----");
// Create a filled vector of specified size
let mut a = Vec64::new( 4, 0.0 );
a[ 0 ] = 1.0; a[ 1 ] = 3.0; a[ 2 ] = 1.0; a[ 3 ] = - 1.0;
println!(" * a = {}", a.clone() );
// Create a vector from a standard vec
let mut b = Vector::<f64>::create( vec![ 2.0, 1.0, 3.0, 4.0 ] );
println!(" * b = {}", b.clone() );
// Create an empty vector of unspecified size
let mut c = Vec64::empty();
println!(" * c = {}", c );
// Do some basic algebra
c = a.clone() + b.clone();
println!( " * c = a + b = {}", c );
c = b.clone() - a.clone();
println!( " * c = b - a = {}", c );
c = 3.0 * a.clone();
println!( " * c = 3 * a = {}", c );
c = a.clone() / 3.0;
println!( " * c = a / 3 = {}", c );
// Add some new elements and swap them around
a.push( 2.0 );
b.resize( 5 );
b[ 4 ] = - 3.0;
b.swap( 3, 4 );
println!(" * a = {}", a.clone() );
println!(" * b = {}", b.clone() );
// Remove elements
a.pop();
a.pop();
b.resize( 3 );
println!( " * a = {}", a.clone() );
println!( " * b = {}", b.clone() );
// Magnitude of the vectors
println!( " * |a| = {}", a.norm_2() );
println!( " * |b| = {}", b.norm_p( 2.0 ) );
// Angle between the two vectors
let cos: f64 = a.dot( b.clone() ) / ( a.norm_2() * b.norm_2() );
let rad = f64::acos( cos );
let deg = rad * 180.0 / PI;
println!( " * angle between a and b = {} degrees", deg );
// Create vectors of spaced elements
let a = Vec64::linspace( 0.0, 1.0, 5 );
let b = Vec64::powspace( 0.0, 1.0, 5, 1.5 );
println!( " * Linearly spaced elements = {}", a );
println!( " * Power law spaced elements (p=1.5) = {}", b );
println!( "--- FINISHED ---" );
}
sourcepub fn insert(&mut self, pos: usize, new_elem: T)
pub fn insert(&mut self, pos: usize, new_elem: T)
Insert a new element into the Vector at a specified position
sourcepub fn pop(&mut self) -> T
pub fn pop(&mut self) -> T
Remove the last element from the vector and return it
Examples found in repository?
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
fn main() {
println!("----- Vector algebra -----");
// Create a filled vector of specified size
let mut a = Vec64::new( 4, 0.0 );
a[ 0 ] = 1.0; a[ 1 ] = 3.0; a[ 2 ] = 1.0; a[ 3 ] = - 1.0;
println!(" * a = {}", a.clone() );
// Create a vector from a standard vec
let mut b = Vector::<f64>::create( vec![ 2.0, 1.0, 3.0, 4.0 ] );
println!(" * b = {}", b.clone() );
// Create an empty vector of unspecified size
let mut c = Vec64::empty();
println!(" * c = {}", c );
// Do some basic algebra
c = a.clone() + b.clone();
println!( " * c = a + b = {}", c );
c = b.clone() - a.clone();
println!( " * c = b - a = {}", c );
c = 3.0 * a.clone();
println!( " * c = 3 * a = {}", c );
c = a.clone() / 3.0;
println!( " * c = a / 3 = {}", c );
// Add some new elements and swap them around
a.push( 2.0 );
b.resize( 5 );
b[ 4 ] = - 3.0;
b.swap( 3, 4 );
println!(" * a = {}", a.clone() );
println!(" * b = {}", b.clone() );
// Remove elements
a.pop();
a.pop();
b.resize( 3 );
println!( " * a = {}", a.clone() );
println!( " * b = {}", b.clone() );
// Magnitude of the vectors
println!( " * |a| = {}", a.norm_2() );
println!( " * |b| = {}", b.norm_p( 2.0 ) );
// Angle between the two vectors
let cos: f64 = a.dot( b.clone() ) / ( a.norm_2() * b.norm_2() );
let rad = f64::acos( cos );
let deg = rad * 180.0 / PI;
println!( " * angle between a and b = {} degrees", deg );
// Create vectors of spaced elements
let a = Vec64::linspace( 0.0, 1.0, 5 );
let b = Vec64::powspace( 0.0, 1.0, 5, 1.5 );
println!( " * Linearly spaced elements = {}", a );
println!( " * Power law spaced elements (p=1.5) = {}", b );
println!( "--- FINISHED ---" );
}
source§impl Vector<f64>
impl Vector<f64>
sourcepub fn linspace(a: f64, b: f64, size: usize) -> Self
pub fn linspace(a: f64, b: f64, size: usize) -> Self
Create a linearly spaced vector of f64 with n elements with lower limit a and upper limit b
Examples found in repository?
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
fn main() {
println!("--------- Basic integration ---------");
println!( " * Create a 1D mesh with 2 variables and set " );
println!( " * one equal 2x and the other to x^2. " );
let nodes = Vec64::linspace( 0.0, 1.0, 101 );
let mut mesh = Mesh1D::<f64, f64>::new( nodes.clone(), 2 );
for i in 0..nodes.size() {
let x = nodes[i].clone();
mesh[i][0] = 2.0 * x;
mesh[i][1] = x * x;
}
println!( " * number of nodes = {}", mesh.nnodes() );
println!( " * number of variables = {}", mesh.nvars() );
println!( " * Interpolate the value of each of the ");
println!( " * variables at x = 0.314 ");
let vars = mesh.get_interpolated_vars( 0.314 );
println!( " * vars = {}", vars );
println!( " * Numerically integrate the variables over " );
println!( " * the domain (from 0 to 1) " );
println!( " * Integral 2x = {}", mesh.trapezium( 0 ) );
println!( " * Integral x^2 = {}", mesh.trapezium( 1 ) );
// The mesh may be printed to a file using
// mesh.output( "./output.txt", 5 );
// here 5 is the precision of the output values.
// Similarly a pre-existing file can be read into a mesh using
//mesh.read( "./output.txt" );
println!( "--- FINISHED ---" );
}
More examples
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
fn main() {
println!("----- Vector algebra -----");
// Create a filled vector of specified size
let mut a = Vec64::new( 4, 0.0 );
a[ 0 ] = 1.0; a[ 1 ] = 3.0; a[ 2 ] = 1.0; a[ 3 ] = - 1.0;
println!(" * a = {}", a.clone() );
// Create a vector from a standard vec
let mut b = Vector::<f64>::create( vec![ 2.0, 1.0, 3.0, 4.0 ] );
println!(" * b = {}", b.clone() );
// Create an empty vector of unspecified size
let mut c = Vec64::empty();
println!(" * c = {}", c );
// Do some basic algebra
c = a.clone() + b.clone();
println!( " * c = a + b = {}", c );
c = b.clone() - a.clone();
println!( " * c = b - a = {}", c );
c = 3.0 * a.clone();
println!( " * c = 3 * a = {}", c );
c = a.clone() / 3.0;
println!( " * c = a / 3 = {}", c );
// Add some new elements and swap them around
a.push( 2.0 );
b.resize( 5 );
b[ 4 ] = - 3.0;
b.swap( 3, 4 );
println!(" * a = {}", a.clone() );
println!(" * b = {}", b.clone() );
// Remove elements
a.pop();
a.pop();
b.resize( 3 );
println!( " * a = {}", a.clone() );
println!( " * b = {}", b.clone() );
// Magnitude of the vectors
println!( " * |a| = {}", a.norm_2() );
println!( " * |b| = {}", b.norm_p( 2.0 ) );
// Angle between the two vectors
let cos: f64 = a.dot( b.clone() ) / ( a.norm_2() * b.norm_2() );
let rad = f64::acos( cos );
let deg = rad * 180.0 / PI;
println!( " * angle between a and b = {} degrees", deg );
// Create vectors of spaced elements
let a = Vec64::linspace( 0.0, 1.0, 5 );
let b = Vec64::powspace( 0.0, 1.0, 5, 1.5 );
println!( " * Linearly spaced elements = {}", a );
println!( " * Power law spaced elements (p=1.5) = {}", b );
println!( "--- FINISHED ---" );
}
sourcepub fn powspace(a: f64, b: f64, size: usize, p: f64) -> Self
pub fn powspace(a: f64, b: f64, size: usize, p: f64) -> Self
Create a nonuniform vector of f64 with n elements with lower limit a, upper limit b and exponent p (p=1 -> linear)
Examples found in repository?
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
fn main() {
println!("----- Vector algebra -----");
// Create a filled vector of specified size
let mut a = Vec64::new( 4, 0.0 );
a[ 0 ] = 1.0; a[ 1 ] = 3.0; a[ 2 ] = 1.0; a[ 3 ] = - 1.0;
println!(" * a = {}", a.clone() );
// Create a vector from a standard vec
let mut b = Vector::<f64>::create( vec![ 2.0, 1.0, 3.0, 4.0 ] );
println!(" * b = {}", b.clone() );
// Create an empty vector of unspecified size
let mut c = Vec64::empty();
println!(" * c = {}", c );
// Do some basic algebra
c = a.clone() + b.clone();
println!( " * c = a + b = {}", c );
c = b.clone() - a.clone();
println!( " * c = b - a = {}", c );
c = 3.0 * a.clone();
println!( " * c = 3 * a = {}", c );
c = a.clone() / 3.0;
println!( " * c = a / 3 = {}", c );
// Add some new elements and swap them around
a.push( 2.0 );
b.resize( 5 );
b[ 4 ] = - 3.0;
b.swap( 3, 4 );
println!(" * a = {}", a.clone() );
println!(" * b = {}", b.clone() );
// Remove elements
a.pop();
a.pop();
b.resize( 3 );
println!( " * a = {}", a.clone() );
println!( " * b = {}", b.clone() );
// Magnitude of the vectors
println!( " * |a| = {}", a.norm_2() );
println!( " * |b| = {}", b.norm_p( 2.0 ) );
// Angle between the two vectors
let cos: f64 = a.dot( b.clone() ) / ( a.norm_2() * b.norm_2() );
let rad = f64::acos( cos );
let deg = rad * 180.0 / PI;
println!( " * angle between a and b = {} degrees", deg );
// Create vectors of spaced elements
let a = Vec64::linspace( 0.0, 1.0, 5 );
let b = Vec64::powspace( 0.0, 1.0, 5, 1.5 );
println!( " * Linearly spaced elements = {}", a );
println!( " * Power law spaced elements (p=1.5) = {}", b );
println!( "--- FINISHED ---" );
}
sourcepub fn norm_2(&self) -> f64
pub fn norm_2(&self) -> f64
Return the L2 norm: square root of the sum of the squares
Examples found in repository?
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
fn main() {
println!("----- Vector algebra -----");
// Create a filled vector of specified size
let mut a = Vec64::new( 4, 0.0 );
a[ 0 ] = 1.0; a[ 1 ] = 3.0; a[ 2 ] = 1.0; a[ 3 ] = - 1.0;
println!(" * a = {}", a.clone() );
// Create a vector from a standard vec
let mut b = Vector::<f64>::create( vec![ 2.0, 1.0, 3.0, 4.0 ] );
println!(" * b = {}", b.clone() );
// Create an empty vector of unspecified size
let mut c = Vec64::empty();
println!(" * c = {}", c );
// Do some basic algebra
c = a.clone() + b.clone();
println!( " * c = a + b = {}", c );
c = b.clone() - a.clone();
println!( " * c = b - a = {}", c );
c = 3.0 * a.clone();
println!( " * c = 3 * a = {}", c );
c = a.clone() / 3.0;
println!( " * c = a / 3 = {}", c );
// Add some new elements and swap them around
a.push( 2.0 );
b.resize( 5 );
b[ 4 ] = - 3.0;
b.swap( 3, 4 );
println!(" * a = {}", a.clone() );
println!(" * b = {}", b.clone() );
// Remove elements
a.pop();
a.pop();
b.resize( 3 );
println!( " * a = {}", a.clone() );
println!( " * b = {}", b.clone() );
// Magnitude of the vectors
println!( " * |a| = {}", a.norm_2() );
println!( " * |b| = {}", b.norm_p( 2.0 ) );
// Angle between the two vectors
let cos: f64 = a.dot( b.clone() ) / ( a.norm_2() * b.norm_2() );
let rad = f64::acos( cos );
let deg = rad * 180.0 / PI;
println!( " * angle between a and b = {} degrees", deg );
// Create vectors of spaced elements
let a = Vec64::linspace( 0.0, 1.0, 5 );
let b = Vec64::powspace( 0.0, 1.0, 5, 1.5 );
println!( " * Linearly spaced elements = {}", a );
println!( " * Power law spaced elements (p=1.5) = {}", b );
println!( "--- FINISHED ---" );
}
More examples
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
fn main() {
println!( "------------------ Laplace's equation ------------------" );
println!( " * Solve Laplace's equation on the unit square subject");
println!( " * to the given boundary conditions. The exact solution");
println!( " * is u(x,y) = y/[(1+x)^2 + y^2].");
let start = Instant::now();
let n: usize = 64; // Number of intervals
let dx: f64 = 1.0 / ( n as f64 ); // Step size
let size: usize = ( n + 1 ) * ( n + 1 ); // Size of the linear system
let mut rhs = Vec64::zeros( size ); // Right hand side vector
let mut triplets = Vec::new();
let mut row: usize = 0;
// x = 0 boundary ( u = y / ( 1 + y^2 ) )
let i = 0;
for j in 0..n+1 {
let y = (j as f64) * dx;
triplets.push( ( row, i * ( n + 1 ) + j, 1.0 ) );
rhs[ row ] = y / ( 1.0 + y * y );
row += 1;
}
for i in 1..n {
let x = (i as f64) * dx;
// y = 0 boundary ( u = 0 )
let j = 0;
triplets.push( ( row, i * ( n + 1 ) + j, 1.0 ) );
rhs[ row ] = 0.0;
row += 1;
// Interior nodes
for j in 1..n {
triplets.push( ( row, ( i - 1 ) * ( n + 1 ) + j, 1.0 ) );
triplets.push( ( row, ( i + 1 ) * ( n + 1 ) + j, 1.0 ) );
triplets.push( ( row, i * ( n + 1 ) + j - 1, 1.0 ) );
triplets.push( ( row, i * ( n + 1 ) + j + 1, 1.0 ) );
triplets.push( ( row, i * ( n + 1 ) + j, - 4.0 ) );
rhs[ row ] = 0.0;
row += 1;
}
// y = 1 boundary ( u = 1 / ( ( 1 + x )^2 + 1 ) )
let j = n;
triplets.push( ( row, i * ( n + 1 ) + j, 1.0 ) );
rhs[ row ] = 1. / ( ( 1. + x ) * ( 1. + x ) + 1. );
row += 1;
}
// x = 1 boundary ( u = y / ( 4 + y^2 ) )
let i = n;
for j in 0..n+1 {
let y = (j as f64) * dx;
triplets.push( ( row, i * ( n + 1 ) + j, 1.0 ) );
rhs[ row ] = y / ( 4. + y * y );
row += 1;
}
// Exact solution u = y / ( ( 1 + x )^2 + y^2 )
let mut u_exact = Vec64::zeros( size );
row = 0;
for i in 0..n+1 {
let x = (i as f64) * dx;
for j in 0..n+1 {
let y = (j as f64) * dx;
u_exact[ row ] = y / ( ( 1. + x ) * ( 1. + x ) + y * y );
row += 1;
}
}
// Create the sparse matrix from the triplets
let mut sparse = Sparse::from_triplets( triplets );
let duration = start.elapsed();
println!(" * Time to create the matrix: {:?}", duration);
// Solve the sparse system
let u = sparse.solve( rhs ).unwrap();
// Output time and error
let duration = start.elapsed();
println!(" * Time to solve the system: {:?}", duration);
let u_diff = u - u_exact;
println!(" * Solution error = {}", u_diff.norm_2() );
println!( "-------------------- FINISHED ---------------------" );
}
sourcepub fn norm_p(&self, p: f64) -> f64
pub fn norm_p(&self, p: f64) -> f64
Return the Lp norm: p-th root of the sum of the absolute values raised to the power p
Examples found in repository?
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
fn main() {
println!("----- Vector algebra -----");
// Create a filled vector of specified size
let mut a = Vec64::new( 4, 0.0 );
a[ 0 ] = 1.0; a[ 1 ] = 3.0; a[ 2 ] = 1.0; a[ 3 ] = - 1.0;
println!(" * a = {}", a.clone() );
// Create a vector from a standard vec
let mut b = Vector::<f64>::create( vec![ 2.0, 1.0, 3.0, 4.0 ] );
println!(" * b = {}", b.clone() );
// Create an empty vector of unspecified size
let mut c = Vec64::empty();
println!(" * c = {}", c );
// Do some basic algebra
c = a.clone() + b.clone();
println!( " * c = a + b = {}", c );
c = b.clone() - a.clone();
println!( " * c = b - a = {}", c );
c = 3.0 * a.clone();
println!( " * c = 3 * a = {}", c );
c = a.clone() / 3.0;
println!( " * c = a / 3 = {}", c );
// Add some new elements and swap them around
a.push( 2.0 );
b.resize( 5 );
b[ 4 ] = - 3.0;
b.swap( 3, 4 );
println!(" * a = {}", a.clone() );
println!(" * b = {}", b.clone() );
// Remove elements
a.pop();
a.pop();
b.resize( 3 );
println!( " * a = {}", a.clone() );
println!( " * b = {}", b.clone() );
// Magnitude of the vectors
println!( " * |a| = {}", a.norm_2() );
println!( " * |b| = {}", b.norm_p( 2.0 ) );
// Angle between the two vectors
let cos: f64 = a.dot( b.clone() ) / ( a.norm_2() * b.norm_2() );
let rad = f64::acos( cos );
let deg = rad * 180.0 / PI;
println!( " * angle between a and b = {} degrees", deg );
// Create vectors of spaced elements
let a = Vec64::linspace( 0.0, 1.0, 5 );
let b = Vec64::powspace( 0.0, 1.0, 5, 1.5 );
println!( " * Linearly spaced elements = {}", a );
println!( " * Power law spaced elements (p=1.5) = {}", b );
println!( "--- FINISHED ---" );
}
sourcepub fn norm_inf(&self) -> f64
pub fn norm_inf(&self) -> f64
Return the Inf norm: largest absolute value element (p -> infinity)
Examples found in repository?
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
fn main() {
println!( "------------------ Sparse linear system ------------------" );
println!( " * Solving the linear system Ax = b, for x, where" );
let n: usize = 4;
let mut a = Mat64::new( n, n, 0.0 );
let mut triplets = Vec::new();
for i in 0..n {
a[i][i] = 2.0;
triplets.push( ( i, i, 2.0 ) );
if i > 0 {
a[ i - 1 ][ i ] = 1.0;
triplets.push( ( i - 1, i, 1.0 ) );
}
if i < n - 1 {
a[ i + 1 ][ i ] = 1.0;
triplets.push( ( i + 1, i, 1.0 ) );
}
}
println!( " * A ={}", a );
let b = Vec64::random( n );
println!( " * b^T ={}", b );
println!( " * as both dense and sparse linear systems.");
let dense_x = a.clone().solve_basic( b.clone() );
println!( " * The dense system gives the solution vector");
println!( " * x^T ={}", dense_x );
let mut sparse = Sparse::from_triplets( triplets );
let sparse_x = sparse.solve( b ).unwrap();
println!( " * The sparse system gives the solution vector");
println!( " * x^T ={}", sparse_x );
let diff = dense_x - sparse_x;
println!( " * The maximum difference between the two is {:+.2e}", diff.norm_inf() );
println!( "-------------------- FINISHED ---------------------" );
}
sourcepub fn random(size: usize) -> Self
pub fn random(size: usize) -> Self
Examples found in repository?
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
fn main() {
println!( "------------------ Sparse linear system ------------------" );
println!( " * Solving the linear system Ax = b, for x, where" );
let n: usize = 4;
let mut a = Mat64::new( n, n, 0.0 );
let mut triplets = Vec::new();
for i in 0..n {
a[i][i] = 2.0;
triplets.push( ( i, i, 2.0 ) );
if i > 0 {
a[ i - 1 ][ i ] = 1.0;
triplets.push( ( i - 1, i, 1.0 ) );
}
if i < n - 1 {
a[ i + 1 ][ i ] = 1.0;
triplets.push( ( i + 1, i, 1.0 ) );
}
}
println!( " * A ={}", a );
let b = Vec64::random( n );
println!( " * b^T ={}", b );
println!( " * as both dense and sparse linear systems.");
let dense_x = a.clone().solve_basic( b.clone() );
println!( " * The dense system gives the solution vector");
println!( " * x^T ={}", dense_x );
let mut sparse = Sparse::from_triplets( triplets );
let sparse_x = sparse.solve( b ).unwrap();
println!( " * The sparse system gives the solution vector");
println!( " * x^T ={}", sparse_x );
let diff = dense_x - sparse_x;
println!( " * The maximum difference between the two is {:+.2e}", diff.norm_inf() );
println!( "-------------------- FINISHED ---------------------" );
}
source§impl<T> Vector<T>
impl<T> Vector<T>
sourcepub const fn empty() -> Self
pub const fn empty() -> Self
Create a new vector of unspecified size
Examples found in repository?
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
fn main() {
println!("----- Vector algebra -----");
// Create a filled vector of specified size
let mut a = Vec64::new( 4, 0.0 );
a[ 0 ] = 1.0; a[ 1 ] = 3.0; a[ 2 ] = 1.0; a[ 3 ] = - 1.0;
println!(" * a = {}", a.clone() );
// Create a vector from a standard vec
let mut b = Vector::<f64>::create( vec![ 2.0, 1.0, 3.0, 4.0 ] );
println!(" * b = {}", b.clone() );
// Create an empty vector of unspecified size
let mut c = Vec64::empty();
println!(" * c = {}", c );
// Do some basic algebra
c = a.clone() + b.clone();
println!( " * c = a + b = {}", c );
c = b.clone() - a.clone();
println!( " * c = b - a = {}", c );
c = 3.0 * a.clone();
println!( " * c = 3 * a = {}", c );
c = a.clone() / 3.0;
println!( " * c = a / 3 = {}", c );
// Add some new elements and swap them around
a.push( 2.0 );
b.resize( 5 );
b[ 4 ] = - 3.0;
b.swap( 3, 4 );
println!(" * a = {}", a.clone() );
println!(" * b = {}", b.clone() );
// Remove elements
a.pop();
a.pop();
b.resize( 3 );
println!( " * a = {}", a.clone() );
println!( " * b = {}", b.clone() );
// Magnitude of the vectors
println!( " * |a| = {}", a.norm_2() );
println!( " * |b| = {}", b.norm_p( 2.0 ) );
// Angle between the two vectors
let cos: f64 = a.dot( b.clone() ) / ( a.norm_2() * b.norm_2() );
let rad = f64::acos( cos );
let deg = rad * 180.0 / PI;
println!( " * angle between a and b = {} degrees", deg );
// Create vectors of spaced elements
let a = Vec64::linspace( 0.0, 1.0, 5 );
let b = Vec64::powspace( 0.0, 1.0, 5, 1.5 );
println!( " * Linearly spaced elements = {}", a );
println!( " * Power law spaced elements (p=1.5) = {}", b );
println!( "--- FINISHED ---" );
}
sourcepub fn create(vec: Vec<T>) -> Self
pub fn create(vec: Vec<T>) -> Self
Create a vector from an std::vec::Vec
Examples found in repository?
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
fn main() {
println!( "------------------ Root finding ------------------" );
println!( " * Let us solve the equation cos(x) - x = 0 ");
println!( " * with initial guess x_0 = 1.");
fn function(x: f64) -> f64 {
f64::cos( x ) - x
}
let newton = Newton::<f64>::new( 1.0 );
let solution = newton.solve( &function );
println!( " * Our solution is x = {:.6}", solution.unwrap() );
println!( " * to six decimal places.");
println!( "---------------------------------------------------" );
println!( " * Now we shall solve the set of equations ");
println!( " * x^3 + y - 1 = 0, " );
println!( " * y^3 - x + 1 = 0, " );
println!( " * using Newton's method and the initial guess " );
let guess = Vec64::create( vec![ 0.5, 0.25 ] );
println!( " * ( x_0, y_0 ) = {}.", guess.clone() );
fn vector_function( x: Vec64 ) -> Vec64 {
let mut f = Vec64::new( 2, 0.0 );
f[0] = f64::powf( x[0], 3.0 ) + x[1] -1.0;
f[1] = f64::powf( x[1], 3.0 ) - x[0] + 1.0;
f
}
let newton = Newton::<Vec64>::new( guess );
let solution = newton.solve( &vector_function ).unwrap();
println!( " * Our solution is x = {:.2} and", solution[0] );
println!( " * y = {:.2} to two decimal places.", solution[1] );
println!( "-------------------- FINISHED ---------------------" );
}
More examples
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
fn main() {
println!("----- Vector algebra -----");
// Create a filled vector of specified size
let mut a = Vec64::new( 4, 0.0 );
a[ 0 ] = 1.0; a[ 1 ] = 3.0; a[ 2 ] = 1.0; a[ 3 ] = - 1.0;
println!(" * a = {}", a.clone() );
// Create a vector from a standard vec
let mut b = Vector::<f64>::create( vec![ 2.0, 1.0, 3.0, 4.0 ] );
println!(" * b = {}", b.clone() );
// Create an empty vector of unspecified size
let mut c = Vec64::empty();
println!(" * c = {}", c );
// Do some basic algebra
c = a.clone() + b.clone();
println!( " * c = a + b = {}", c );
c = b.clone() - a.clone();
println!( " * c = b - a = {}", c );
c = 3.0 * a.clone();
println!( " * c = 3 * a = {}", c );
c = a.clone() / 3.0;
println!( " * c = a / 3 = {}", c );
// Add some new elements and swap them around
a.push( 2.0 );
b.resize( 5 );
b[ 4 ] = - 3.0;
b.swap( 3, 4 );
println!(" * a = {}", a.clone() );
println!(" * b = {}", b.clone() );
// Remove elements
a.pop();
a.pop();
b.resize( 3 );
println!( " * a = {}", a.clone() );
println!( " * b = {}", b.clone() );
// Magnitude of the vectors
println!( " * |a| = {}", a.norm_2() );
println!( " * |b| = {}", b.norm_p( 2.0 ) );
// Angle between the two vectors
let cos: f64 = a.dot( b.clone() ) / ( a.norm_2() * b.norm_2() );
let rad = f64::acos( cos );
let deg = rad * 180.0 / PI;
println!( " * angle between a and b = {} degrees", deg );
// Create vectors of spaced elements
let a = Vec64::linspace( 0.0, 1.0, 5 );
let b = Vec64::powspace( 0.0, 1.0, 5, 1.5 );
println!( " * Linearly spaced elements = {}", a );
println!( " * Power law spaced elements (p=1.5) = {}", b );
println!( "--- FINISHED ---" );
}
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
fn main() {
println!( "------------------ Linear system ------------------" );
println!( " * Solving the linear system Ax = b, for x, where" );
let mut a = Mat64::new( 3, 3, 0.0 );
a[0][0] = 1.0; a[0][1] = 1.0; a[0][2] = 1.0;
a[1][0] = 0.0; a[1][1] = 2.0; a[1][2] = 5.0;
a[2][0] = 2.0; a[2][1] = 5.0; a[2][2] = - 1.0;
println!( " * A ={}", a );
let b = Vec64::create( vec![ 6.0, -4.0, 27.0 ] );
println!( " * b^T ={}", b );
let x = a.clone().solve_basic( b );
println!( " * gives the solution vector");
println!( " * x^T ={}", x );
println!( "---------------------------------------------------" );
println!( " * Lets solve the complex linear system Cy = d where" );
let mut c = Matrix::<Cmplx>::new( 2, 2, Cmplx::new( 1.0, 1.0 ) );
c[0][1] = Cmplx::new( -1.0, 0.0 );
c[1][0] = Cmplx::new( 1.0, -1.0 );
println!( " * C ={}", c );
let mut d = Vector::<Cmplx>::new( 2, Cmplx::new( 0.0, 1.0 ) );
d[1] = Cmplx::new( 1.0, 0.0 );
println!( " * d^T ={}", d );
println!( " * Using LU decomposition we find that ");
let y = c.clone().solve_lu( d );
println!( " * y^T ={}", y );
println!( "---------------------------------------------------" );
println!( " * We may also find the determinant of a matrix" );
println!( " * |A| = {}", a.clone().determinant() );
println!( " * |C| = {}", c.clone().determinant() );
println!( " * or the inverse of a matrix" );
let inverse = a.clone().inverse();
println!( " * A^-1 =\n{}", inverse );
println!( " * C^-1 =\n{}", c.clone().inverse() );
println!( " * We can check this by multiplication" );
println!( " * I = A * A^-1 =\n{}", a * inverse );
println!( " * which is sufficiently accurate for our purposes.");
println!( "-------------------- FINISHED ---------------------" );
}
sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Return the size of the vector
Examples found in repository?
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
fn main() {
println!("--------- Basic integration ---------");
println!( " * Create a 1D mesh with 2 variables and set " );
println!( " * one equal 2x and the other to x^2. " );
let nodes = Vec64::linspace( 0.0, 1.0, 101 );
let mut mesh = Mesh1D::<f64, f64>::new( nodes.clone(), 2 );
for i in 0..nodes.size() {
let x = nodes[i].clone();
mesh[i][0] = 2.0 * x;
mesh[i][1] = x * x;
}
println!( " * number of nodes = {}", mesh.nnodes() );
println!( " * number of variables = {}", mesh.nvars() );
println!( " * Interpolate the value of each of the ");
println!( " * variables at x = 0.314 ");
let vars = mesh.get_interpolated_vars( 0.314 );
println!( " * vars = {}", vars );
println!( " * Numerically integrate the variables over " );
println!( " * the domain (from 0 to 1) " );
println!( " * Integral 2x = {}", mesh.trapezium( 0 ) );
println!( " * Integral x^2 = {}", mesh.trapezium( 1 ) );
// The mesh may be printed to a file using
// mesh.output( "./output.txt", 5 );
// here 5 is the precision of the output values.
// Similarly a pre-existing file can be read into a mesh using
//mesh.read( "./output.txt" );
println!( "--- FINISHED ---" );
}
More examples
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
fn main() {
println!("----- Polynomials -----");
// Create a new polynomial
let p = Polynomial::<f64>::new( vec![ 1.0, 2.0, 3.0 ] );
println!( " * p(x) = {}", p );
println!( " * p[0] = {}", p[0] );
println!( " * p[1] = {}", p[1] );
println!( " * p[2] = {}", p[2] );
println!( " * p is of degree {}", p.degree().unwrap() );
// Evaluate the polynomial at a given point
println!( " * p(0.5) = {}", p.eval( 0.5 ) );
println!( " * p(1.0) = {}", p.eval( 1.0 ) );
// Determine the roots of the polynomial
let roots = p.roots( true );
println!( " * p(x) = {} = 0 has {} roots", p, roots.size() );
println!( " * x0 = {:.6} {:.6} i", roots[0].real, roots[0].imag );
println!( " * x1 = {:.6} +{:.6} i", roots[1].real, roots[1].imag );
// Arithmetic
let q = Polynomial::cubic( 3.0, 5.0, 4.0, 0.0 );
println!( " * q(x) = {}", q );
println!( " * p(x) + q(x) = {}", p.clone() + q.clone() );
println!( " * p(x) - q(x) = {}", p.clone() - q.clone() );
println!( " * -p(x) = {}", -p.clone() );
println!( " * p(x) * q(x) = {}", p.clone() * q.clone() );
println!( " * p(x) * 3 = {}", p.clone() * 3.0 );
let result = q.polydiv( &p );
match result {
Ok( ( q, r ) ) => println!( " * q(x) / p(x) = {} remainder {}", q, r ),
Err( e ) => println!( " * q(x) / p(x) = {}", e )
}
// Derivatives
println!( " * p'(x) = {}", p.derivative() );
println!( " * p''(x) = {}", p.derivative_n( 2 ) );
println!( " * p'(0.5) = {}", p.derivative_at( 0.5, 1 ) );
println!( " * p''(0.5) = {}", p.derivative_at( 0.5, 2 ) );
println!( "--- FINISHED ---" );
}
source§impl<T: Clone> Vector<T>
impl<T: Clone> Vector<T>
sourcepub fn new(size: usize, elem: T) -> Self
pub fn new(size: usize, elem: T) -> Self
Create a new vector of specified size
Examples found in repository?
More examples
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
fn main() {
println!("----- Vector algebra -----");
// Create a filled vector of specified size
let mut a = Vec64::new( 4, 0.0 );
a[ 0 ] = 1.0; a[ 1 ] = 3.0; a[ 2 ] = 1.0; a[ 3 ] = - 1.0;
println!(" * a = {}", a.clone() );
// Create a vector from a standard vec
let mut b = Vector::<f64>::create( vec![ 2.0, 1.0, 3.0, 4.0 ] );
println!(" * b = {}", b.clone() );
// Create an empty vector of unspecified size
let mut c = Vec64::empty();
println!(" * c = {}", c );
// Do some basic algebra
c = a.clone() + b.clone();
println!( " * c = a + b = {}", c );
c = b.clone() - a.clone();
println!( " * c = b - a = {}", c );
c = 3.0 * a.clone();
println!( " * c = 3 * a = {}", c );
c = a.clone() / 3.0;
println!( " * c = a / 3 = {}", c );
// Add some new elements and swap them around
a.push( 2.0 );
b.resize( 5 );
b[ 4 ] = - 3.0;
b.swap( 3, 4 );
println!(" * a = {}", a.clone() );
println!(" * b = {}", b.clone() );
// Remove elements
a.pop();
a.pop();
b.resize( 3 );
println!( " * a = {}", a.clone() );
println!( " * b = {}", b.clone() );
// Magnitude of the vectors
println!( " * |a| = {}", a.norm_2() );
println!( " * |b| = {}", b.norm_p( 2.0 ) );
// Angle between the two vectors
let cos: f64 = a.dot( b.clone() ) / ( a.norm_2() * b.norm_2() );
let rad = f64::acos( cos );
let deg = rad * 180.0 / PI;
println!( " * angle between a and b = {} degrees", deg );
// Create vectors of spaced elements
let a = Vec64::linspace( 0.0, 1.0, 5 );
let b = Vec64::powspace( 0.0, 1.0, 5, 1.5 );
println!( " * Linearly spaced elements = {}", a );
println!( " * Power law spaced elements (p=1.5) = {}", b );
println!( "--- FINISHED ---" );
}
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
fn main() {
println!( "------------------ Linear system ------------------" );
println!( " * Solving the linear system Ax = b, for x, where" );
let mut a = Mat64::new( 3, 3, 0.0 );
a[0][0] = 1.0; a[0][1] = 1.0; a[0][2] = 1.0;
a[1][0] = 0.0; a[1][1] = 2.0; a[1][2] = 5.0;
a[2][0] = 2.0; a[2][1] = 5.0; a[2][2] = - 1.0;
println!( " * A ={}", a );
let b = Vec64::create( vec![ 6.0, -4.0, 27.0 ] );
println!( " * b^T ={}", b );
let x = a.clone().solve_basic( b );
println!( " * gives the solution vector");
println!( " * x^T ={}", x );
println!( "---------------------------------------------------" );
println!( " * Lets solve the complex linear system Cy = d where" );
let mut c = Matrix::<Cmplx>::new( 2, 2, Cmplx::new( 1.0, 1.0 ) );
c[0][1] = Cmplx::new( -1.0, 0.0 );
c[1][0] = Cmplx::new( 1.0, -1.0 );
println!( " * C ={}", c );
let mut d = Vector::<Cmplx>::new( 2, Cmplx::new( 0.0, 1.0 ) );
d[1] = Cmplx::new( 1.0, 0.0 );
println!( " * d^T ={}", d );
println!( " * Using LU decomposition we find that ");
let y = c.clone().solve_lu( d );
println!( " * y^T ={}", y );
println!( "---------------------------------------------------" );
println!( " * We may also find the determinant of a matrix" );
println!( " * |A| = {}", a.clone().determinant() );
println!( " * |C| = {}", c.clone().determinant() );
println!( " * or the inverse of a matrix" );
let inverse = a.clone().inverse();
println!( " * A^-1 =\n{}", inverse );
println!( " * C^-1 =\n{}", c.clone().inverse() );
println!( " * We can check this by multiplication" );
println!( " * I = A * A^-1 =\n{}", a * inverse );
println!( " * which is sufficiently accurate for our purposes.");
println!( "-------------------- FINISHED ---------------------" );
}
source§impl<T: Clone + Number> Vector<T>
impl<T: Clone + Number> Vector<T>
sourcepub fn zeros(size: usize) -> Self
pub fn zeros(size: usize) -> Self
Create a vector of zeros
Examples found in repository?
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
fn main() {
println!( "------------------ Laplace's equation ------------------" );
println!( " * Solve Laplace's equation on the unit square subject");
println!( " * to the given boundary conditions. The exact solution");
println!( " * is u(x,y) = y/[(1+x)^2 + y^2].");
let start = Instant::now();
let n: usize = 64; // Number of intervals
let dx: f64 = 1.0 / ( n as f64 ); // Step size
let size: usize = ( n + 1 ) * ( n + 1 ); // Size of the linear system
let mut rhs = Vec64::zeros( size ); // Right hand side vector
let mut triplets = Vec::new();
let mut row: usize = 0;
// x = 0 boundary ( u = y / ( 1 + y^2 ) )
let i = 0;
for j in 0..n+1 {
let y = (j as f64) * dx;
triplets.push( ( row, i * ( n + 1 ) + j, 1.0 ) );
rhs[ row ] = y / ( 1.0 + y * y );
row += 1;
}
for i in 1..n {
let x = (i as f64) * dx;
// y = 0 boundary ( u = 0 )
let j = 0;
triplets.push( ( row, i * ( n + 1 ) + j, 1.0 ) );
rhs[ row ] = 0.0;
row += 1;
// Interior nodes
for j in 1..n {
triplets.push( ( row, ( i - 1 ) * ( n + 1 ) + j, 1.0 ) );
triplets.push( ( row, ( i + 1 ) * ( n + 1 ) + j, 1.0 ) );
triplets.push( ( row, i * ( n + 1 ) + j - 1, 1.0 ) );
triplets.push( ( row, i * ( n + 1 ) + j + 1, 1.0 ) );
triplets.push( ( row, i * ( n + 1 ) + j, - 4.0 ) );
rhs[ row ] = 0.0;
row += 1;
}
// y = 1 boundary ( u = 1 / ( ( 1 + x )^2 + 1 ) )
let j = n;
triplets.push( ( row, i * ( n + 1 ) + j, 1.0 ) );
rhs[ row ] = 1. / ( ( 1. + x ) * ( 1. + x ) + 1. );
row += 1;
}
// x = 1 boundary ( u = y / ( 4 + y^2 ) )
let i = n;
for j in 0..n+1 {
let y = (j as f64) * dx;
triplets.push( ( row, i * ( n + 1 ) + j, 1.0 ) );
rhs[ row ] = y / ( 4. + y * y );
row += 1;
}
// Exact solution u = y / ( ( 1 + x )^2 + y^2 )
let mut u_exact = Vec64::zeros( size );
row = 0;
for i in 0..n+1 {
let x = (i as f64) * dx;
for j in 0..n+1 {
let y = (j as f64) * dx;
u_exact[ row ] = y / ( ( 1. + x ) * ( 1. + x ) + y * y );
row += 1;
}
}
// Create the sparse matrix from the triplets
let mut sparse = Sparse::from_triplets( triplets );
let duration = start.elapsed();
println!(" * Time to create the matrix: {:?}", duration);
// Solve the sparse system
let u = sparse.solve( rhs ).unwrap();
// Output time and error
let duration = start.elapsed();
println!(" * Time to solve the system: {:?}", duration);
let u_diff = u - u_exact;
println!(" * Solution error = {}", u_diff.norm_2() );
println!( "-------------------- FINISHED ---------------------" );
}
Trait Implementations§
source§impl<T: Clone + Number> AddAssign<T> for Vector<T>
impl<T: Clone + Number> AddAssign<T> for Vector<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 vector
source§impl<T: Clone + Number> AddAssign for Vector<T>
impl<T: Clone + Number> AddAssign for Vector<T>
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Add a vector to a mutable vector and assign the result ( += )
source§impl<T: Clone + Number> DivAssign<T> for Vector<T>
impl<T: Clone + Number> DivAssign<T> for Vector<T>
source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
Divide every element in a mutable vector by a scalar
source§impl<T: Clone + Number> MulAssign<T> for Vector<T>
impl<T: Clone + Number> MulAssign<T> for Vector<T>
source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
Multiply every element in a mutable vector by a scalar