Skip to main content

RhaiMatrix

Struct RhaiMatrix 

Source
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

Source

pub fn from_array(arr: Array) -> Self

Construct a RhaiMatrix from a rhai::Array.

Source

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());
Source

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());
Source

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());
Source

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());
Source

pub fn to_array(self) -> Array

Convert the matrix back into a rhai::Array.

Source

pub fn to_dmatrix(&self) -> Result<DMatrix<FLOAT>, Box<EvalAltResult>>

Convert the matrix into a nalgebra::DMatrix.

§Errors

Returns an error if any element is non-numeric or rows have differing lengths.

§Panics

Panics if an integer value cannot be represented as FLOAT.

Source

pub fn from_dmatrix(mat: &DMatrix<FLOAT>) -> Self

Create a RhaiMatrix from a nalgebra::DMatrix.

Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> RhaiMatrix

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for RhaiMatrix

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V