pub struct RhaiMatrix(/* private fields */);Expand description
Wrapper around rhai::Array representing a matrix.
This type provides conversions between Rhai arrays and
nalgebra::DMatrix.
§Examples
use rhai::{Array, Dynamic};
use rhai_sci::matrix::RhaiMatrix;
let raw: Array = vec![
Dynamic::from_array(vec![Dynamic::from_float(1.0), Dynamic::from_float(2.0)]),
Dynamic::from_array(vec![Dynamic::from_float(3.0), Dynamic::from_float(4.0)]),
];
let matrix = RhaiMatrix::from_array(raw.clone());
assert_eq!(matrix.to_array().len(), raw.len());Implementations§
Source§impl RhaiMatrix
impl RhaiMatrix
Sourcepub fn from_array(arr: Array) -> Self
pub fn from_array(arr: Array) -> Self
Construct a RhaiMatrix from a rhai::Array.
Sourcepub fn row_vector(data: Array) -> Self
pub fn row_vector(data: Array) -> Self
Construct a RhaiMatrix representing a row vector (1×N).
§Examples
use rhai::{Array, Dynamic};
use rhai_sci::matrix::RhaiMatrix;
let data: Array = vec![Dynamic::from_int(1), Dynamic::from_int(2)];
let row = RhaiMatrix::row_vector(data.clone());
assert!(row.as_row().is_some());Sourcepub fn column_vector(data: Array) -> Self
pub fn column_vector(data: Array) -> Self
Construct a RhaiMatrix representing a column vector (N×1).
§Examples
use rhai::{Array, Dynamic};
use rhai_sci::matrix::RhaiMatrix;
let data: Array = vec![Dynamic::from_int(1), Dynamic::from_int(2)];
let column = RhaiMatrix::column_vector(data.clone());
assert!(column.as_column().is_some());Sourcepub fn as_row(&self) -> Option<Self>
pub fn as_row(&self) -> Option<Self>
Return the matrix as a row vector (1×N), reshaping a column vector if necessary.
Returns None if the matrix is not 1×N or N×1.
§Examples
use rhai::{Array, Dynamic};
use rhai_sci::matrix::RhaiMatrix;
let data: Array = vec![Dynamic::from_int(1), Dynamic::from_int(2)];
let column = RhaiMatrix::column_vector(data.clone());
let row = column.as_row().unwrap();
assert!(row.as_row().is_some());Sourcepub fn as_column(&self) -> Option<Self>
pub fn as_column(&self) -> Option<Self>
Return the matrix as a column vector (N×1), reshaping a row vector if necessary.
Returns None if the matrix is not 1×N or N×1.
§Examples
use rhai::{Array, Dynamic};
use rhai_sci::matrix::RhaiMatrix;
let data: Array = vec![Dynamic::from_int(1), Dynamic::from_int(2)];
let row = RhaiMatrix::row_vector(data.clone());
let column = row.as_column().unwrap();
assert!(column.as_column().is_some());Sourcepub fn to_array(self) -> Array
pub fn to_array(self) -> Array
Convert the matrix back into a rhai::Array.
Sourcepub fn to_dmatrix(&self) -> Result<DMatrix<FLOAT>, Box<EvalAltResult>>
pub fn to_dmatrix(&self) -> Result<DMatrix<FLOAT>, Box<EvalAltResult>>
Sourcepub fn from_dmatrix(mat: &DMatrix<FLOAT>) -> Self
pub fn from_dmatrix(mat: &DMatrix<FLOAT>) -> Self
Create a RhaiMatrix from a nalgebra::DMatrix.
Sourcepub fn transpose(&self) -> Result<Self, Box<EvalAltResult>>
pub fn transpose(&self) -> Result<Self, Box<EvalAltResult>>
Transpose the matrix.
§Errors
Returns an error if the matrix contains non-numeric values or rows of unequal length.
Sourcepub fn concat_h(&self, other: &Self) -> Result<Self, Box<EvalAltResult>>
pub fn concat_h(&self, other: &Self) -> Result<Self, Box<EvalAltResult>>
Horizontally concatenate two matrices.
§Examples
use rhai::{Array, Dynamic};
use rhai_sci::{matrix::RhaiMatrix, validation_functions::is_row_vector};
let left = RhaiMatrix::row_vector(vec![Dynamic::from_int(1), Dynamic::from_int(2)]);
let right = RhaiMatrix::row_vector(vec![Dynamic::from_int(3), Dynamic::from_int(4)]);
let combined = left.concat_h(&right).unwrap();
let mut arr = combined.to_array();
assert!(is_row_vector(&mut arr));§Errors
Returns an error if the matrices have differing row counts or contain non-numeric values.
Sourcepub fn concat_v(&self, other: &Self) -> Result<Self, Box<EvalAltResult>>
pub fn concat_v(&self, other: &Self) -> Result<Self, Box<EvalAltResult>>
Vertically concatenate two matrices.
§Examples
use rhai::{Array, Dynamic};
use rhai_sci::{matrix::RhaiMatrix, validation_functions::is_column_vector};
let top = RhaiMatrix::column_vector(vec![Dynamic::from_int(1), Dynamic::from_int(2)]);
let bottom = RhaiMatrix::column_vector(vec![Dynamic::from_int(3), Dynamic::from_int(4)]);
let combined = top.concat_v(&bottom).unwrap();
let mut arr = combined.to_array();
assert!(is_column_vector(&mut arr));§Errors
Returns an error if the matrices have differing column counts or contain non-numeric values.
Trait Implementations§
Source§impl Clone for RhaiMatrix
impl Clone for RhaiMatrix
Source§fn clone(&self) -> RhaiMatrix
fn clone(&self) -> RhaiMatrix
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for RhaiMatrix
impl !Send for RhaiMatrix
impl !Sync for RhaiMatrix
impl !UnwindSafe for RhaiMatrix
impl Freeze for RhaiMatrix
impl Unpin for RhaiMatrix
impl UnsafeUnpin for RhaiMatrix
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.