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: usizeNumber of rows in the matrix
cols: usizeNumber of columns in the matrix
data: Vec<f32>Matrix data in row-major order
Implementations§
Source§impl Matrix
impl Matrix
Sourcepub fn with_data(rows: usize, cols: usize, data: Vec<f32>) -> Result<Self>
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 rowscols- Number of columnsdata- 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.
Sourcepub fn is_vector(&self) -> bool
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.
Sourcepub fn vector_size(&self) -> usize
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.