Matrix

Struct Matrix 

Source
pub struct Matrix {
    pub rows: usize,
    pub cols: usize,
    pub data: Vec<f32>,
}
Expand description

Represents a 2D matrix with dimensions and data.

This is the core data structure for all linear algebra operations in the library. It can represent both regular matrices and vectors (as 1D matrices with either one row or one column).

§Examples

Creating a new matrix:

use metal_matrix::Matrix;

// Create a 3x3 zero matrix
let mut matrix = Matrix::new(3, 3);

// Set some values
matrix.set(0, 0, 1.0);
matrix.set(1, 1, 2.0);
matrix.set(2, 2, 3.0);

Creating a vector:

use metal_matrix::Matrix;

// Create a column vector
let vector = Matrix::vector(vec![1.0, 2.0, 3.0]);
assert_eq!(vector.rows, 3);
assert_eq!(vector.cols, 1);

Fields§

§rows: usize

Number of rows in the matrix

§cols: usize

Number of columns in the matrix

§data: Vec<f32>

Matrix data in row-major order

Implementations§

Source§

impl Matrix

Source

pub fn new(rows: usize, cols: usize) -> Self

Create a new matrix with given dimensions, initialized with zeros.

§Arguments
  • rows - Number of rows
  • cols - Number of columns
§Returns

A new matrix of the specified dimensions, filled with zeros.

Source

pub fn with_data(rows: usize, cols: usize, data: Vec<f32>) -> Result<Self>

Create a new matrix with given dimensions and data.

§Arguments
  • rows - Number of rows
  • cols - Number of columns
  • data - Vector of data in row-major order
§Returns

A Result containing the new matrix or an error if the data length doesn’t match dimensions.

§Errors

Returns an error if data.len() != rows * cols.

Source

pub fn vector(data: Vec<f32>) -> Self

Create a column vector (1D matrix) with given data.

§Arguments
  • data - Vector of data
§Returns

A new matrix with dimensions (data.len(), 1).

Source

pub fn identity(n: usize) -> Self

Create an identity matrix of size n×n.

§Arguments
  • n - Size of the square matrix
§Returns

A new n×n identity matrix (ones on the diagonal, zeros elsewhere).

Source

pub fn get(&self, row: usize, col: usize) -> f32

Get element at position (row, col).

§Arguments
  • row - Row index (0-based)
  • col - Column index (0-based)
§Returns

The value at the specified position.

§Panics

Panics if the indices are out of bounds.

Source

pub fn set(&mut self, row: usize, col: usize, value: f32)

Set element at position (row, col).

§Arguments
  • row - Row index (0-based)
  • col - Column index (0-based)
  • value - Value to set
§Panics

Panics if the indices are out of bounds.

Source

pub fn is_vector(&self) -> bool

Check if this matrix is a vector (1D matrix).

§Returns

true if the matrix has either one row or one column, false otherwise.

Source

pub fn vector_size(&self) -> usize

Get the size of the matrix if it’s a vector.

§Returns

The number of elements if the matrix is a vector, or 0 if it’s not a vector.

Source

pub fn vector_get(&self, index: usize) -> Result<f32>

Get vector element at index (for 1D matrices).

§Arguments
  • index - Element index (0-based)
§Returns

A Result containing the value at the specified index or an error if the matrix is not a vector.

§Errors

Returns an error if the matrix is not a vector.

Source

pub fn row(&self, row: usize) -> Self

Extract a row as a new Matrix.

§Arguments
  • row - Row index (0-based)
§Returns

A new 1×n matrix containing the specified row.

§Panics

Panics if the row index is out of bounds.

Source

pub fn column(&self, col: usize) -> Self

Extract a column as a new Matrix.

§Arguments
  • col - Column index (0-based)
§Returns

A new m×1 matrix containing the specified column.

§Panics

Panics if the column index is out of bounds.

Trait Implementations§

Source§

impl Clone for Matrix

Source§

fn clone(&self) -> Matrix

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Matrix

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Matrix

§

impl RefUnwindSafe for Matrix

§

impl Send for Matrix

§

impl Sync for Matrix

§

impl Unpin for Matrix

§

impl UnwindSafe for Matrix

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.