Struct ohsl::vector::Vector

source ·
pub struct Vector<T> {
    pub vec: Vec<T>,
    /* private fields */
}

Fields§

§vec: Vec<T>

Implementations§

source§

impl<T: PartialEq> Vector<T>

source

pub fn find(&self, value: T) -> usize

Return the index of the first element in the Vector equal to a given value

source§

impl<T: Clone> Vector<T>

source

pub fn assign(&mut self, elem: T)

Assign a value to every element in the vector

source§

impl<T: Default> Vector<T>

source

pub fn resize(&mut self, new_size: usize)

Resize the vector

Examples found in repository?
examples/vector_algebra.rs (line 37)
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>

source

pub fn dot(&self, w: Vector<T>) -> T

Return the dot product of two vectors v.dot(w)

Examples found in repository?
examples/vector_algebra.rs (line 55)
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

pub fn sum(&self) -> T

Return the sum of all the elements in the vector

source

pub fn sum_slice(&self, start: usize, end: usize) -> T

Return the sum of the elements in the vector from index start to end

source

pub fn product(&self) -> T

Return the product of all the elements in the vector

source

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: Clone + Signed> Vector<T>

source

pub fn abs(&self) -> Self

Return a vector containing the absolute values of the elements

source

pub fn norm_1(&self) -> T

Return the L1 norm: sum of absolute values

source§

impl<T: Ord + PartialEq + PartialOrd> Vector<T>

source

pub fn sort(&mut self)

Sort the Vector of elements

source§

impl<T: PartialEq + PartialOrd> Vector<T>

source

pub fn sort_by<F>(&mut self, compare: F)where F: FnMut(&T, &T) -> Ordering,

Sort the Vector of elements using a comparison function

source§

impl<T> Vector<T>

source

pub fn clear(&mut self)

Remove all of the elements from the vector

source

pub fn swap(&mut self, i: usize, j: usize)

Swap elements two elements in the vector

Examples found in repository?
examples/vector_algebra.rs (line 39)
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

pub fn push(&mut self, elem: T)

Push a new element onto the end of the vector

Examples found in repository?
examples/vector_algebra.rs (line 36)
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

pub fn insert(&mut self, pos: usize, new_elem: T)

Insert a new element into the Vector at a specified position

source

pub fn pop(&mut self) -> T

Remove the last element from the vector and return it

Examples found in repository?
examples/vector_algebra.rs (line 44)
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>

source

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?
examples/basic_integration.rs (line 15)
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
Hide additional examples
examples/vector_algebra.rs (line 61)
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

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?
examples/vector_algebra.rs (line 62)
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

pub fn norm_2(&self) -> f64

Return the L2 norm: square root of the sum of the squares

Examples found in repository?
examples/vector_algebra.rs (line 51)
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
Hide additional examples
examples/sparse_laplace.rs (line 88)
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 ---------------------" );
}
source

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?
examples/vector_algebra.rs (line 52)
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

pub fn norm_inf(&self) -> f64

Return the Inf norm: largest absolute value element (p -> infinity)

Examples found in repository?
examples/sparse_linear_system.rs (line 39)
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

pub fn random(size: usize) -> Self

Examples found in repository?
examples/sparse_linear_system.rs (line 28)
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

pub fn dotf64(&self, w: &Vector<f64>) -> f64

Return the dot product of two vectors v.dot(w)

source§

impl<T: Clone + Signed> Vector<Complex<T>>

source

pub fn conj(&self) -> Self

Return a vector containing the complex conjugate of the elements

source§

impl<T: Clone + Number> Vector<Complex<T>>

source

pub fn real(&self) -> Vector<T>

Return a vector containing the real parts of a vector of complex numbers

source§

impl<T> Vector<T>

source

pub const fn empty() -> Self

Create a new vector of unspecified size

Examples found in repository?
examples/vector_algebra.rs (line 22)
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

pub fn create(vec: Vec<T>) -> Self

Create a vector from an std::vec::Vec

Examples found in repository?
examples/root_finding.rs (line 25)
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
Hide additional examples
examples/vector_algebra.rs (line 18)
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 ---" );
}
examples/linear_systems.rs (line 19)
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

pub fn size(&self) -> usize

Return the size of the vector

Examples found in repository?
examples/basic_integration.rs (line 17)
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
Hide additional examples
examples/polynomial.rs (line 20)
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>

source

pub fn new(size: usize, elem: T) -> Self

Create a new vector of specified size

Examples found in repository?
examples/root_finding.rs (line 29)
28
29
30
31
32
33
    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
    }
More examples
Hide additional examples
examples/vector_algebra.rs (line 13)
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 ---" );
}
examples/linear_systems.rs (line 30)
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>

source

pub fn zeros(size: usize) -> Self

Create a vector of zeros

Examples found in repository?
examples/sparse_laplace.rs (line 20)
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 ---------------------" );
}
source

pub fn ones(size: usize) -> Self

Create a vector of ones

source§

impl<T: Display> Vector<T>

source

pub fn output(&self, filename: &str)

Print the vector to a file

Trait Implementations§

source§

impl<T: Clone + Number> Add for Vector<T>

source§

fn add(self, plus: Self) -> Self::Output

Add the elements of two vectors together ( binary + )

§

type Output = Vector<T>

The resulting type after applying the + operator.
source§

impl<T: Clone + Number> AddAssign<T> for Vector<T>

source§

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>

source§

fn add_assign(&mut self, rhs: Self)

Add a vector to a mutable vector and assign the result ( += )

source§

impl<T: Clone> Clone for Vector<T>

source§

fn clone(&self) -> Self

Clone the vector

1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T> Debug for Vector<T>where T: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Format the output [ v_0, v_1, … ]

source§

impl<T> Display for Vector<T>where T: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Format the output [ v_0, v_1, … ]

source§

impl<T: Clone + Number> Div<T> for Vector<T>

source§

fn div(self, scalar: T) -> Self::Output

Divide a vector by a scalar (vector / scalar)

§

type Output = Vector<T>

The resulting type after applying the / operator.
source§

impl<T: Clone + Number> DivAssign<T> for Vector<T>

source§

fn div_assign(&mut self, rhs: T)

Divide every element in a mutable vector by a scalar

source§

impl<T> Index<usize> for Vector<T>

source§

fn index<'a>(&'a self, index: usize) -> &'a T

Indexing operator [] (read only)

§

type Output = T

The returned type after indexing.
source§

impl<T> IndexMut<usize> for Vector<T>

source§

fn index_mut(&mut self, index: usize) -> &mut T

Indexing operator [] (read/write)

source§

impl<T: Clone + Number> Mul<T> for Vector<T>

source§

fn mul(self, scalar: T) -> Self::Output

Multiply a vector by a scalar (vector * scalar)

§

type Output = Vector<T>

The resulting type after applying the * operator.
source§

impl<T: Clone + Number> Mul<Vector<T>> for Matrix<T>

source§

fn mul(self, vec: Vector<T>) -> Vector<T>

Multiply a matrix with a (column) vector ( matrix * vector )

§

type Output = Vector<T>

The resulting type after applying the * operator.
source§

impl Mul<Vector<f64>> for f64

source§

fn mul(self, vector: Vector<f64>) -> Self::Output

Allow multiplication on the left by f64 (f64 * vector)

§

type Output = Vector<f64>

The resulting type after applying the * operator.
source§

impl<T: Clone + Number> MulAssign<T> for Vector<T>

source§

fn mul_assign(&mut self, rhs: T)

Multiply every element in a mutable vector by a scalar

source§

impl<T: Clone + Neg<Output = T>> Neg for Vector<T>

source§

fn neg(self) -> Self::Output

Return the unary negation ( unary - ) of each element

§

type Output = Vector<T>

The resulting type after applying the - operator.
source§

impl<T: Clone + Number> Sub for Vector<T>

source§

fn sub(self, minus: Self) -> Self::Output

Subtract the elements of one vector from another ( binary - )

§

type Output = Vector<T>

The resulting type after applying the - operator.
source§

impl<T: Clone + Number> SubAssign<T> for Vector<T>

source§

fn sub_assign(&mut self, rhs: T)

Subtract the same value from every element in a mutable vector

source§

impl<T: Clone + Number> SubAssign for Vector<T>

source§

fn sub_assign(&mut self, rhs: Self)

Substract a vector from a mutable vector and assign the result ( -= )

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Vector<T>where T: RefUnwindSafe,

§

impl<T> Send for Vector<T>where T: Send,

§

impl<T> Sync for Vector<T>where T: Sync,

§

impl<T> Unpin for Vector<T>where T: Unpin,

§

impl<T> UnwindSafe for Vector<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T, Rhs> AssignOperations<Rhs> for Twhere T: AddAssign<Rhs> + SubAssign<Rhs> + MulAssign<Rhs> + DivAssign<Rhs>,