Type Alias RMatrix

Source
pub type RMatrix<T> = RArray<T, 2>;

Aliased Type§

struct RMatrix<T> {
    robj: Robj,
    _data: PhantomData<T>,
}

Fields§

§robj: Robj

Owning Robj (probably should be a Pin).

§_data: PhantomData<T>

Data type of the n-array

Implementations§

Source§

impl<T> RMatrix<T>
where T: ToVectorValue, Robj: for<'a> AsTypedSlice<'a, T>,

Source

pub fn new(nrow: usize, ncol: usize) -> Self

Returns an RMatrix with dimensions according to nrow and ncol, with arbitrary entries. To initialize a matrix containing only NA values, use RMatrix::new_with_na.

Source§

impl<T> RMatrix<T>
where T: ToVectorValue + CanBeNA, Robj: for<'a> AsTypedSlice<'a, T>,

Source

pub fn new_with_na(nrow: usize, ncol: usize) -> Self

Returns an RMatrix with dimensions according to nrow and ncol, with all entries set to NA.

Note that since Raw does not have an NA representation in R, this method is not implemented for [Rbyte].

Source§

impl<T> RMatrix<T>

Source§

impl<T> RMatrix<T>
where T: ToVectorValue, Robj: for<'a> AsTypedSlice<'a, T>,

Source

pub fn new_matrix<F: Clone + FnMut(usize, usize) -> T>( nrows: usize, ncols: usize, f: F, ) -> Self

Create a new matrix wrapper.

§Arguments
  • nrows - the number of rows the returned matrix will have
  • ncols - the number of columns the returned matrix will have
  • f - a function that will be called for each entry of the matrix in order to populate it with values. It must return a scalar value that can be converted to an R scalar, such as i32, u32, or f64, i.e. see ToVectorValue. It accepts two arguments:
    • r - the current row of the entry we are creating
    • c - the current column of the entry we are creating
Source

pub fn nrows(&self) -> usize

Get the number of rows.

Source

pub fn ncols(&self) -> usize

Get the number of columns.

Source§

impl<T, const NDIM: usize> RArray<T, NDIM>
where Robj: for<'a> AsTypedSlice<'a, T>,

Source

pub fn from_parts(robj: Robj) -> Self

Source

pub fn data(&self) -> &[T]

Returns a flat representation of the array in col-major.

Source

pub fn data_mut(&mut self) -> &mut [T]

Returns a flat, mutable representation of the array in col-major.

Source

pub fn ndim(&self) -> usize

Returns the number of dimensions.

Source

pub fn dim(&self) -> Vec<usize>

Returns the dimensions of the array.

Source§

impl<T, const NDIM: usize> RArray<T, NDIM>

Source

pub fn get_dimnames(&self) -> List

Source

pub fn get_dim(&self) -> Vec<usize>

Get the dimension vector of the array.

Equivalent to dim() in R

Source

pub fn set_names(&mut self, names: Strings)

Set the names of the elements of an array.

Equivalent to names<- in R

Source

pub fn set_dimnames(&mut self, dimnames: List)

Set the dimension names of an array.

For RMatrix a list of length 2 is required, as that would entail column-names and row-names. If you only wish to set one, but not the other, then the unset element must be R NULL

Equivalent to dimnames<- in R

Source

pub fn set_dim(&mut self, dim: Robj)

Set the dimensions of an array.

Equivalent to dim<-

Source§

impl<T, const NDIM: usize> RArray<T, NDIM>
where T: ToVectorValue, Robj: for<'a> AsTypedSlice<'a, T>,

Source

pub fn new_array(dim: [usize; NDIM]) -> Self

Trait Implementations§

Source§

impl From<Mat<f64>> for RMatrix<Rfloat>

Source§

fn from(value: Mat<f64>) -> Self

Converts to this type from the input type.
Source§

impl From<Mat<f64>> for RMatrix<f64>

Convert a faer::Mat<f64> into an RMatrix<f64> which is not NA aware.

Source§

fn from(value: Mat<f64>) -> Self

Converts to this type from the input type.
Source§

impl From<MatRef<'_, f64>> for RMatrix<Rfloat>

Source§

fn from(value: MatRef<'_, f64>) -> Self

Converts to this type from the input type.
Source§

impl From<MatRef<'_, f64>> for RMatrix<f64>

Convert a faer::Mat<f64> into an RMatrix<f64> which is not NA aware.

Source§

fn from(value: MatRef<'_, f64>) -> Self

Convert a faer MatRef into Robj.

Source§

impl<T> TryFrom<&Robj> for RMatrix<T>
where Robj: for<'a> AsTypedSlice<'a, T>,

Source§

type Error = Error

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

fn try_from(robj: &Robj) -> Result<Self>

Performs the conversion.
Source§

impl<T> TryFrom<Robj> for RMatrix<T>
where Robj: for<'a> AsTypedSlice<'a, T>,

Source§

type Error = Error

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

fn try_from(robj: Robj) -> Result<Self>

Performs the conversion.
Source§

impl<T: Debug, const NDIM: usize> Debug for RArray<T, NDIM>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T, const NDIM: usize> Deref for RArray<T, NDIM>

Source§

type Target = Robj

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T, const NDIM: usize> DerefMut for RArray<T, NDIM>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T, const NDIM: usize> Index<[usize; NDIM]> for RArray<T, NDIM>
where Robj: for<'a> AsTypedSlice<'a, T>,

Source§

fn index(&self, index: [usize; NDIM]) -> &Self::Output

Zero-based indexing for DIM-dimensional arrays.

Panics if out of bounds. Zero-based indexing in row, column order.

Panics if out of bounds.

use extendr_api::prelude::*;
test! {
    let matrix = RArray::new_matrix(3, 2, |r, c| [
        [1., 2., 3.],
        [4., 5., 6.]][c][r]);
    assert_eq!(matrix[[0, 0]], 1.);
    assert_eq!(matrix[[1, 0]], 2.);
    assert_eq!(matrix[[2, 1]], 6.);

    let matrix = RArray::new_matrix3d(3, 2, 2, |r, c, d| (r + c + d) as f64);
    assert_eq!(matrix[[0, 0, 0]], 0.);
    assert_eq!(matrix[[1, 0, 1]], 2.);
    assert_eq!(matrix[[2, 1, 1]], 4.);
}
Source§

type Output = T

The returned type after indexing.
Source§

impl<T, const NDIM: usize> IndexMut<[usize; NDIM]> for RArray<T, NDIM>
where Robj: for<'a> AsTypedSlice<'a, T>,

Source§

fn index_mut(&mut self, index: [usize; NDIM]) -> &mut Self::Output

Zero-based mutable indexing for DIM-dimensional arrays.

Panics if out of bounds.

use extendr_api::prelude::*;
test! {
    let mut matrix = RMatrix::new_matrix(3, 2, |_, _| 0.);
    matrix[[0, 0]] = 1.;
    matrix[[1, 0]] = 2.;
    matrix[[2, 0]] = 3.;
    matrix[[0, 1]] = 4.;
    assert_eq!(matrix.as_real_slice().unwrap(), &[1., 2., 3., 4., 0., 0.]);

   let mut matrix = RMatrix3D::new_matrix3d(3, 2, 2, |_, _, _| 0.);
   matrix[[0, 0, 0]] = 1.;
   matrix[[1, 0, 0]] = 2.;
   matrix[[2, 0, 0]] = 3.;
   matrix[[0, 1, 0]] = 4.;
   assert_eq!(matrix.as_real_slice().unwrap(),
       &[1., 2., 3., 4., 0., 0., 0., 0., 0., 0., 0., 0.]);
}
Source§

impl<T, const NDIM: usize> Offset<[usize; NDIM]> for RArray<T, NDIM>

Source§

fn offset(&self, index: [usize; NDIM]) -> usize

Get the offset into the array for a given index.

Source§

impl<T: PartialEq, const NDIM: usize> PartialEq for RArray<T, NDIM>

Source§

fn eq(&self, other: &RArray<T, NDIM>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, const NDIM: usize> StructuralPartialEq for RArray<T, NDIM>