Struct russell_lab::Matrix[][src]

pub struct Matrix { /* fields omitted */ }

Implementations

Holds matrix components

Note

Data is stored in col-major format

Example of col-major data:

      _      _
     |  0  3  |
 A = |  1  4  |            ⇒     a = [0, 1, 2, 3, 4, 5]
     |_ 2  5 _|(m x n)

 a[i+j*m] = A[i][j]

Creates new (nrow x ncol) Matrix filled with zeros

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

Creates new matrix from given data

Examples

use russell_lab::*;
let a = Matrix::from(&[
    &[1.0, 2.0, 3.0],
    &[4.0, 5.0, 6.0],
    &[7.0, 8.0, 9.0],
]);
let correct = "┌       ┐\n\
               │ 1 2 3 │\n\
               │ 4 5 6 │\n\
               │ 7 8 9 │\n\
               └       ┘";
assert_eq!(format!("{}", a), correct);

Panics

This function panics if there are rows with different number of columns

Returns the number of rows

Examples

use russell_lab::*;
let a = Matrix::new(4, 3);
assert_eq!(a.nrow(), 4);

Returns the number of columns

Examples

use russell_lab::*;
let a = Matrix::new(4, 3);
assert_eq!(a.ncol(), 3);

Returns the dimensions (nrow, ncol) of this matrix

Examples

use russell_lab::*;
let a = Matrix::new(4, 3);
assert_eq!(a.dims(), (4, 3));

Scales this matrix

a := alpha * a

Examples

use russell_lab::*;
let mut a = Matrix::from(&[
    &[1.0, 2.0, 3.0],
    &[4.0, 5.0, 6.0],
]);
a.scale(0.5);
let correct = "┌             ┐\n\
               │ 0.5   1 1.5 │\n\
               │   2 2.5   3 │\n\
               └             ┘";
assert_eq!(format!("{}", a), correct);

Trait Implementations

Implements the Display trait

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.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

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.