use std::ops::{Index, IndexMut};
use num_traits::Zero;
use tenferro_tensor::{Rank, TensorScalar, TypedTensor};
#[derive(Clone, Debug, PartialEq)]
pub struct Mat<T> {
data: Vec<T>,
shape: (usize, usize),
}
impl<T> Mat<T> {
pub fn from_vec_col_major(shape: [usize; 2], data: Vec<T>) -> Self {
assert_eq!(
data.len(),
shape[0] * shape[1],
"Mat::from_vec_col_major: buffer length does not match shape"
);
Self {
data,
shape: (shape[0], shape[1]),
}
}
pub fn from_fn<F: FnMut(&[usize]) -> T>(shape: [usize; 2], mut f: F) -> Self {
let (m, n) = (shape[0], shape[1]);
let mut data = Vec::with_capacity(m * n);
for j in 0..n {
for i in 0..m {
data.push(f(&[i, j]));
}
}
Self {
data,
shape: (m, n),
}
}
#[inline]
pub fn shape(&self) -> &(usize, usize) {
&self.shape
}
#[inline]
pub fn dims(&self) -> [usize; 2] {
[self.shape.0, self.shape.1]
}
#[inline]
pub fn dim(&self, axis: usize) -> usize {
match axis {
0 => self.shape.0,
1 => self.shape.1,
_ => panic!("Mat::dim: axis {axis} out of range for a matrix"),
}
}
#[inline]
pub fn nrows(&self) -> usize {
self.shape.0
}
#[inline]
pub fn ncols(&self) -> usize {
self.shape.1
}
#[inline]
pub fn len(&self) -> usize {
self.data.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
#[inline]
pub fn as_slice(&self) -> &[T] {
&self.data
}
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [T] {
&mut self.data
}
pub fn into_vec(self) -> Vec<T> {
self.data
}
#[inline]
pub fn col(&self, j: usize) -> &[T] {
let m = self.shape.0;
&self.data[j * m..(j + 1) * m]
}
#[inline]
pub fn col_mut(&mut self, j: usize) -> &mut [T] {
let m = self.shape.0;
&mut self.data[j * m..(j + 1) * m]
}
pub fn iter(&self) -> std::slice::Iter<'_, T> {
self.data.iter()
}
pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, T> {
self.data.iter_mut()
}
pub fn map<U, F: FnMut(&T) -> U>(&self, f: F) -> Mat<U> {
Mat {
data: self.data.iter().map(f).collect(),
shape: self.shape,
}
}
}
impl<T: Clone> Mat<T> {
pub fn from_elem(shape: [usize; 2], elem: T) -> Self {
Self {
data: vec![elem; shape[0] * shape[1]],
shape: (shape[0], shape[1]),
}
}
pub fn transpose(&self) -> Self {
let (m, n) = self.shape;
Self::from_fn([n, m], |idx| self.data[idx[1] + m * idx[0]].clone())
}
pub fn leading_cols(&self, ncols: usize) -> Self {
assert!(ncols <= self.shape.1, "Mat::leading_cols: too many columns");
Self {
data: self.data[..self.shape.0 * ncols].to_vec(),
shape: (self.shape.0, ncols),
}
}
}
impl<T: Clone> Mat<T> {
pub fn from_rows(rows: Vec<Vec<T>>) -> Self {
let m = rows.len();
let n = rows.first().map_or(0, Vec::len);
assert!(
rows.iter().all(|r| r.len() == n),
"Mat::from_rows: ragged rows"
);
Self::from_fn([m, n], |idx| rows[idx[0]][idx[1]].clone())
}
}
#[allow(unused_macros)]
#[doc(hidden)]
#[macro_export]
macro_rules! mat {
($([$($x:expr),* $(,)?]),+ $(,)?) => {
$crate::matrix::Mat::from_rows(vec![$(vec![$($x),*]),+])
};
}
#[allow(unused_imports)]
#[doc(hidden)]
pub use crate::mat;
impl<T: Clone + Zero> Mat<T> {
pub fn zeros(shape: [usize; 2]) -> Self {
Self::from_elem(shape, T::zero())
}
}
impl<T: TensorScalar> Mat<T> {
pub fn into_typed(self) -> TypedTensor<T, Rank<2>> {
let (m, n) = self.shape;
TypedTensor::from_vec_col_major([m, n], self.data)
.expect("Mat invariant: buffer length equals nrows * ncols")
}
pub fn from_typed(t: &TypedTensor<T, Rank<2>>) -> Result<Self, tenferro_tensor::Error> {
let view = t.host_col_major_view()?;
let shape = *view.shape();
Ok(Self {
data: view.as_slice().to_vec(),
shape: (shape[0], shape[1]),
})
}
}
impl<T> Index<[usize; 2]> for Mat<T> {
type Output = T;
#[inline(always)]
fn index(&self, idx: [usize; 2]) -> &T {
debug_assert!(idx[0] < self.shape.0 && idx[1] < self.shape.1);
&self.data[idx[0] + self.shape.0 * idx[1]]
}
}
impl<T> IndexMut<[usize; 2]> for Mat<T> {
#[inline(always)]
fn index_mut(&mut self, idx: [usize; 2]) -> &mut T {
debug_assert!(idx[0] < self.shape.0 && idx[1] < self.shape.1);
&mut self.data[idx[0] + self.shape.0 * idx[1]]
}
}
impl<T> Index<&[usize]> for Mat<T> {
type Output = T;
#[inline(always)]
fn index(&self, idx: &[usize]) -> &T {
&self[[idx[0], idx[1]]]
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Mat3<T> {
data: Vec<T>,
shape: (usize, usize, usize),
}
impl<T> Mat3<T> {
#[inline]
pub fn shape(&self) -> &(usize, usize, usize) {
&self.shape
}
#[inline]
pub fn as_slice(&self) -> &[T] {
&self.data
}
pub fn into_vec(self) -> Vec<T> {
self.data
}
pub fn from_vec_col_major(shape: [usize; 3], data: Vec<T>) -> Self {
assert_eq!(
data.len(),
shape[0] * shape[1] * shape[2],
"Mat3::from_vec_col_major: buffer length does not match shape"
);
Self {
data,
shape: (shape[0], shape[1], shape[2]),
}
}
}
impl<T: Clone> Mat3<T> {
pub fn from_elem(shape: [usize; 3], elem: T) -> Self {
Self {
data: vec![elem; shape[0] * shape[1] * shape[2]],
shape: (shape[0], shape[1], shape[2]),
}
}
}
impl<T: Clone + Zero> Mat3<T> {
pub fn zeros(shape: [usize; 3]) -> Self {
Self::from_elem(shape, T::zero())
}
}
impl<T> Index<[usize; 3]> for Mat3<T> {
type Output = T;
#[inline(always)]
fn index(&self, idx: [usize; 3]) -> &T {
let (a, b, _) = self.shape;
&self.data[idx[0] + a * (idx[1] + b * idx[2])]
}
}
impl<T> IndexMut<[usize; 3]> for Mat3<T> {
#[inline(always)]
fn index_mut(&mut self, idx: [usize; 3]) -> &mut T {
let (a, b, _) = self.shape;
&mut self.data[idx[0] + a * (idx[1] + b * idx[2])]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn col_major_layout_and_transpose() {
let a = Mat::from_fn([2, 3], |idx| (10 * idx[0] + idx[1]) as f64);
assert_eq!(a.as_slice(), &[0.0, 10.0, 1.0, 11.0, 2.0, 12.0]);
assert_eq!(a[[1, 2]], 12.0);
assert_eq!(a.col(1), &[1.0, 11.0]);
let t = a.transpose();
assert_eq!(*t.shape(), (3, 2));
assert_eq!(t[[2, 1]], 12.0);
}
#[test]
fn typed_roundtrip_is_zero_copy_layout() {
let a = Mat::from_fn([2, 2], |idx| (idx[0] + 2 * idx[1]) as f64);
let t = a.clone().into_typed();
assert_eq!(t.shape(), &[2, 2]);
assert_eq!(*t.get2(1, 0).unwrap(), 1.0);
assert_eq!(Mat::from_typed(&t).unwrap(), a);
}
#[test]
fn rank3_indexing() {
let mut a = Mat3::<f64>::zeros([2, 3, 4]);
a[[1, 2, 3]] = 5.0;
assert_eq!(a.as_slice()[1 + 2 * (2 + 3 * 3)], 5.0);
}
}