extendr_api::prelude::mat

Function from_raw_parts_mut

pub unsafe fn from_raw_parts_mut<'a, E>(
    ptr: <<E as Entity>::Group as ForType>::FaerOf<*mut <E as Entity>::Unit>,
    nrows: usize,
    ncols: usize,
    row_stride: isize,
    col_stride: isize,
) -> MatMut<'a, E>
where E: Entity,
Expand description

Creates a MatMut from pointers to the matrix data, dimensions, and strides.

The row (resp. column) stride is the offset from the memory address of a given matrix element at indices (row: i, col: j), to the memory address of the matrix element at indices (row: i + 1, col: 0) (resp. (row: 0, col: i + 1)). This offset is specified in number of elements, not in bytes.

§Safety

The behavior is undefined if any of the following conditions are violated:

  • For each matrix unit, the entire memory region addressed by the matrix must be contained within a single allocation, accessible in its entirety by the corresponding pointer in ptr.
  • For each matrix unit, the corresponding pointer must be non null and properly aligned, even for a zero-sized matrix.
  • The values accessible by the matrix must be initialized at some point before they are read, or references to them are formed.
  • No aliasing (including self aliasing) is allowed. In other words, none of the elements accessible by any matrix unit may be accessed for reads or writes by any other means for the duration of the lifetime 'a. No two elements within a single matrix unit may point to the same address (such a thing can be achieved with a zero stride, for example), and no two matrix units may point to the same address.

§Example

use faer::mat;

// row major matrix with 2 rows, 3 columns, with a column at the end that we want to skip.
// the row stride is the pointer offset from the address of 1.0 to the address of 4.0,
// which is 4.
// the column stride is the pointer offset from the address of 1.0 to the address of 2.0,
// which is 1.
let mut data = [[1.0, 2.0, 3.0, f64::NAN], [4.0, 5.0, 6.0, f64::NAN]];
let mut matrix =
    unsafe { mat::from_raw_parts_mut::<f64>(data.as_mut_ptr() as *mut f64, 2, 3, 4, 1) };

let expected = mat![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
assert_eq!(expected.as_ref(), matrix);