Struct russell_lab::Vector[][src]

pub struct Vector { /* fields omitted */ }
Expand description

Holds vector components and associated functions

Implementations

Creates a new (zeroed) vector

Example

use russell_lab::*;
let u = Vector::new(3);
let correct = "┌   ┐\n\
               │ 0 │\n\
               │ 0 │\n\
               │ 0 │\n\
               └   ┘";
assert_eq!(format!("{}", u), correct);

Creates new vector completely filled with the same value

Example

use russell_lab::*;
let u = Vector::filled(3, 4.0);
let correct = "┌   ┐\n\
               │ 4 │\n\
               │ 4 │\n\
               │ 4 │\n\
               └   ┘";
assert_eq!(format!("{}", u), correct);

Creates a vector from data

Example

use russell_lab::*;
let u = Vector::from(&[1.0, 2.0, 3.0]);
let correct = "┌   ┐\n\
               │ 1 │\n\
               │ 2 │\n\
               │ 3 │\n\
               └   ┘";
assert_eq!(format!("{}", u), correct);

Returns evenly spaced numbers over a specified closed interval

Example

use russell_lab::*;
let x = Vector::linspace(2.0, 3.0, 5);
let correct = "┌      ┐\n\
               │    2 │\n\
               │ 2.25 │\n\
               │  2.5 │\n\
               │ 2.75 │\n\
               │    3 │\n\
               └      ┘";
assert_eq!(format!("{}", x), correct);

Returns the dimension (size) of this vector

Example

use russell_lab::*;
let u = Vector::from(&[1.0, 2.0, 3.0]);
assert_eq!(u.dim(), 3);

Scales this vector

u := alpha * u

Example

use russell_lab::*;
let mut u = Vector::from(&[1.0, 2.0, 3.0]);
u.scale(0.5);
let correct = "┌     ┐\n\
               │ 0.5 │\n\
               │   1 │\n\
               │ 1.5 │\n\
               └     ┘";
assert_eq!(format!("{}", u), correct);

Fills this vector with a given value

u[i] := value

Example

use russell_lab::*;
let mut u = Vector::new(3);
u.fill(8.8);
let correct = "┌     ┐\n\
               │ 8.8 │\n\
               │ 8.8 │\n\
               │ 8.8 │\n\
               └     ┘";
assert_eq!(format!("{}", u), correct);

Returns an access to the underlying data

Example

use russell_lab::*;
let u = Vector::from(&[1.0, 2.0, 3.0]);
assert_eq!(u.as_data(), &[1.0, 2.0, 3.0]);

Returns a mutable access to the underlying data

Example

use russell_lab::*;
let mut u = Vector::from(&[1.0, 2.0, 3.0]);
let data = u.as_mut_data();
data[1] = 2.2;
assert_eq!(data, &[1.0, 2.2, 3.0]);

Returns the i-th component

Example

use russell_lab::*;
let u = Vector::from(&[1.0, 2.0]);
assert_eq!(u.get(1), 2.0);

Change the i-th component

Example

use russell_lab::*;
let mut u = Vector::from(&[1.0, 2.0]);
u.set(1, -2.0);
let correct = "┌    ┐\n\
               │  1 │\n\
               │ -2 │\n\
               └    ┘";
assert_eq!(format!("{}", u), correct);

Executes the += operation on the i-th component

u_i += value

Example

use russell_lab::*;
let mut u = Vector::from(&[1.0, 2.0]);
u.plus_equal(1, 0.22);
let correct = "┌      ┐\n\
               │ 1.00 │\n\
               │ 2.22 │\n\
               └      ┘";
assert_eq!(format!("{:.2}", u), correct);

Applies a function over all components of this vector

u := apply(function(ui))

Example

use russell_lab::*;
let mut u = Vector::from(&[1.0, 2.0, 3.0]);
u.apply(|x| x * x);
let correct = "┌   ┐\n\
               │ 1 │\n\
               │ 4 │\n\
               │ 9 │\n\
               └   ┘";
assert_eq!(format!("{}", u), correct);

Applies a function (with index) over all components of this vector

u := apply(function(i, ui))

Example

use russell_lab::*;
let mut u = Vector::from(&[1.0, 2.0, 3.0]);
u.apply_with_index(|i, x| x * x + (i as f64));
let correct = "┌    ┐\n\
               │  1 │\n\
               │  5 │\n\
               │ 11 │\n\
               └    ┘";
assert_eq!(format!("{}", u), correct);

Returns a copy of this vector

Example

use russell_lab::*;
let mut u = Vector::from(&[1.0, 2.0, 3.0]);
let u_copy = u.get_copy();
u.set(1, 5.0);
let u_correct = "┌   ┐\n\
                 │ 1 │\n\
                 │ 5 │\n\
                 │ 3 │\n\
                 └   ┘";
let u_copy_correct = "┌   ┐\n\
                      │ 1 │\n\
                      │ 2 │\n\
                      │ 3 │\n\
                      └   ┘";
assert_eq!(format!("{}", u), u_correct);
assert_eq!(format!("{}", u_copy), u_copy_correct);

Returns the vector norm

Computes one of:

One:  1-norm (taxicab or sum of abs values)

      ‖u‖_1 := sum_i |uᵢ|

Euc:  Euclidean-norm

      ‖u‖_2 = sqrt(Σ_i uᵢ⋅uᵢ)

Max:  max-norm (inf-norm)

      ‖u‖_max = max_i ( |uᵢ| ) == ‖u‖_∞

Example

use russell_lab::*;
let u = Vector::from(&[2.0, -2.0, 2.0, -2.0, -3.0]);
assert_eq!(u.norm(EnumVectorNorm::One), 11.0);
assert_eq!(u.norm(EnumVectorNorm::Euc), 5.0);
assert_eq!(u.norm(EnumVectorNorm::Max), 3.0);

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.