Struct custos_math::Matrix
source · pub struct Matrix<'a, T = f32, D: Device = CPU, S: Shape = ()> {
pub data: Buffer<'a, T, D, S>,
pub dims: (usize, usize),
}Expand description
A matrix using Buffer described with rows and columns
Example
The following example creates a zeroed (or values set to default) Matrix with the given dimensions.
use custos::CPU;
use custos_math::Matrix;
let device = CPU::new();
let m = Matrix::<i32>::new(&device, (5, 8));
assert_eq!(m.rows(), 5);
assert_eq!(m.cols(), 8);
assert_eq!(m.size(), 5*8);
assert_eq!(m.read(), vec![0; 5*8])Fields§
§data: Buffer<'a, T, D, S>§dims: (usize, usize)Implementations§
source§impl<'a, T, D: Device, S: Shape> Matrix<'a, T, D, S>
impl<'a, T, D: Device, S: Shape> Matrix<'a, T, D, S>
sourcepub fn new(device: &'a D, dims: (usize, usize)) -> Matrix<'a, T, D, S>where
D: Alloc<'a, T, S>,
pub fn new(device: &'a D, dims: (usize, usize)) -> Matrix<'a, T, D, S>where D: Alloc<'a, T, S>,
Returns an empty matrix with the specified dimensions (rows, cols).
Example
use custos::CPU;
use custos_math::Matrix;
let device = CPU::new();
let m = Matrix::<f64>::new(&device, (20, 10));
assert_eq!(m.size(), 20*10);
assert_eq!(m.read(), vec![0.0; 20*10])pub fn device(&self) -> &'a D
sourcepub fn as_buf(&self) -> &Buffer<'a, T, D, S>
pub fn as_buf(&self) -> &Buffer<'a, T, D, S>
Returns a reference to the underlying buffer.
Example
use custos::{CPU, Read};
use custos_math::Matrix;
let device = CPU::new();
let a = Matrix::from((&device, (2, 3), [1., 2., 3., 3., 2., 1.,]));
let read = a.read();
assert_eq!(vec![1., 2., 3., 3., 2., 1.,], read);pub fn to_buf(self) -> Buffer<'a, T, D, S>
sourcepub fn as_buf_mut(&mut self) -> &mut Buffer<'a, T, D, S>
pub fn as_buf_mut(&mut self) -> &mut Buffer<'a, T, D, S>
Returns a mutable reference to the underlying buffer.
pub fn dims(&self) -> (usize, usize)
pub fn reshape(&mut self, dims: (usize, usize))
sourcepub fn rows(&self) -> usize
pub fn rows(&self) -> usize
Returns the row count of the matrix.
Example
use custos::CPU;
use custos_math::Matrix;
let device = CPU::new();
let matrix = Matrix::<i32>::new(&device, (2, 5));
assert_eq!(matrix.rows(), 2)sourcepub fn cols(&self) -> usize
pub fn cols(&self) -> usize
Returns the column count of the matrix.
Example
use custos::CPU;
use custos_math::Matrix;
let device = CPU::new();
let matrix = Matrix::<i32>::new(&device, (2, 5));
assert_eq!(matrix.cols(), 5)sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the number of elements in the matrix: rows * cols
Example
use custos::CPU;
use custos_math::Matrix;
let device = CPU::new();
let matrix = Matrix::<u16>::new(&device, (4, 12));
assert_eq!(matrix.size(), 48)pub fn as_slice(&self) -> &[T] ⓘwhere D: MainMemory,
pub fn as_mut_slice(&mut self) -> &mut [T] ⓘwhere D: MainMemory,
sourcepub fn read(&'a self) -> D::Read<'a>where
T: Default + Copy,
D: Read<T, D, S>,
pub fn read(&'a self) -> D::Read<'a>where T: Default + Copy, D: Read<T, D, S>,
Uses VecRead and current global device to read Matrix
Example
use custos::CPU;
use custos_math::Matrix;
let device = CPU::new();
let a = Matrix::from((&device, (2, 2), [5, 7, 2, 10,]));
assert_eq!(a.read(), vec![5, 7, 2, 10])sourcepub fn read_to_vec(&self) -> Vec<T>where
T: Default + Copy,
D: Read<T, D, S>,
pub fn read_to_vec(&self) -> Vec<T>where T: Default + Copy, D: Read<T, D, S>,
Uses VecRead and current global device to read Matrix
Example
use custos::CPU;
use custos_math::Matrix;
let device = CPU::new();
let a = Matrix::from((&device, (2, 2), [5, 7, 2, 10,]));
assert_eq!(a.read(), vec![5, 7, 2, 10])sourcepub fn shallow(&self) -> Matrix<'a, T, D, S>where
D::Ptr<T, S>: ShallowCopy,
pub fn shallow(&self) -> Matrix<'a, T, D, S>where D::Ptr<T, S>: ShallowCopy,
Creates a shallow copy of &self.
sourcepub fn shallow_or_clone(&self) -> Matrix<'a, T, D, S>where
T: Clone,
D::Ptr<T, S>: ShallowCopy,
D: CloneBuf<'a, T, S>,
pub fn shallow_or_clone(&self) -> Matrix<'a, T, D, S>where T: Clone, D::Ptr<T, S>: ShallowCopy, D: CloneBuf<'a, T, S>,
Creates a shallow copy or a deep copy of &self, depening on whether the realloc feature is activated.
source§impl<T, D: IsShapeIndep, S: Shape> Matrix<'_, T, D, S>
impl<T, D: IsShapeIndep, S: Shape> Matrix<'_, T, D, S>
source§impl<'a, T, D: ActivationOps<T, S>, S: Shape> Matrix<'a, T, D, S>
impl<'a, T, D: ActivationOps<T, S>, S: Shape> Matrix<'a, T, D, S>
pub fn tanh(&self) -> Matrix<'a, T, D, S>
pub fn tanh_grad(&self) -> Matrix<'a, T, D, S>
pub fn relu(&self) -> Matrix<'a, T, D, S>
pub fn relu_mut(&mut self)
pub fn relu_grad(&self) -> Matrix<'a, T, D, S>
pub fn relu_grad_mut(&mut self)
pub fn sigmoid(&self) -> Matrix<'a, T, D, S>
sourcepub fn sigmoid_grad(&self) -> Matrix<'a, T, D, S>
pub fn sigmoid_grad(&self) -> Matrix<'a, T, D, S>
uses pre-computed sigmoid activation
source§impl<'a, T, D: SoftmaxOps<T>> Matrix<'a, T, D>
impl<'a, T, D: SoftmaxOps<T>> Matrix<'a, T, D>
source§impl<'a, T, D: DiagflatOp<T>> Matrix<'a, T, D>
impl<'a, T, D: DiagflatOp<T>> Matrix<'a, T, D>
source§impl<'a, T, D: Device, LS: Shape> Matrix<'a, T, D, LS>
impl<'a, T, D: Device, LS: Shape> Matrix<'a, T, D, LS>
sourcepub fn gemm<RS: Shape, OS: Shape>(
&self,
rhs: &Matrix<'a, T, D, RS>
) -> Matrix<'a, T, D, OS>where
D: Gemm<T, LS, RS, OS, D>,
pub fn gemm<RS: Shape, OS: Shape>( &self, rhs: &Matrix<'a, T, D, RS> ) -> Matrix<'a, T, D, OS>where D: Gemm<T, LS, RS, OS, D>,
Matrix multiplication. Uses current global device.
Example
use custos::CPU;
use custos_math::Matrix;
let device = CPU::new();
let a = Matrix::from((&device, (2, 3), [1., 2., 3., 4., 5., 6.,]));
let b = Matrix::from((&device, (3, 2), [6., 5., 4., 3., 2., 1.,]));
let c = a.gemm(&b);
println!("c: {c:?}");
assert_eq!(c.read(), vec![20., 14., 56., 41.,]);source§impl<'a, T, D, S> Matrix<'a, T, D, S>where
D: AdditionalOps<T, S>,
S: Shape,
impl<'a, T, D, S> Matrix<'a, T, D, S>where D: AdditionalOps<T, S>, S: Shape,
source§impl<'a, T, D: Device, IS: Shape> Matrix<'a, T, D, IS>
impl<'a, T, D: Device, IS: Shape> Matrix<'a, T, D, IS>
pub fn sum_rows<OS: Shape>(&self) -> Matrix<'a, T, D, OS>where D: SumOverOps<T, IS, OS>,
pub fn sum_cols<OS: Shape>(&self) -> Matrix<'a, T, D, OS>where D: SumOverOps<T, IS, OS>,
Methods from Deref<Target = Buffer<'a, T, D, S>>§
pub fn device(&self) -> &'a D
pub fn read(&'a self) -> <D as Read<T, D, S>>::Read<'a>where T: Clone + Default, D: Read<T, D, S>,
sourcepub fn read_to_vec(&self) -> Vec<T, Global>where
D: Read<T, D, S>,
T: Default + Clone,
pub fn read_to_vec(&self) -> Vec<T, Global>where D: Read<T, D, S>, T: Default + Clone,
Reads the contents of the buffer and writes them into a vector.
If it is certain whether a CPU, or an unified CPU + OpenCL Buffer, is used, calling .as_slice() (or deref/mut to &/mut [&T]) is probably preferred.
Example
use custos::{CPU, Buffer};
let device = CPU::new();
let buf = Buffer::from((&device, [1, 2, 3, 4]));
assert_eq!(buf.read_to_vec(), vec![1, 2, 3, 4]);sourcepub fn write(&mut self, data: &[T])where
T: Clone,
D: WriteBuf<T, D, S>,
pub fn write(&mut self, data: &[T])where T: Clone, D: WriteBuf<T, D, S>,
Writes a slice to the Buffer. With a CPU buffer, the slice is just copied to the slice of the buffer.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements contained in Buffer.
Example
use custos::{CPU, Buffer};
let device = CPU::new();
let a = Buffer::<i32, _>::new(&device, 10);
assert_eq!(a.len(), 10)sourcepub unsafe fn shallow(&self) -> Buffer<'a, T, D, S>where
<D as Device>::Ptr<T, S>: ShallowCopy,
pub unsafe fn shallow(&self) -> Buffer<'a, T, D, S>where <D as Device>::Ptr<T, S>: ShallowCopy,
Creates a shallow copy of &self.
Safety
Itself, this function does not need to be unsafe.
However, declaring this function as unsafe highlights the violation of creating two or more owners for one resource.
Furthermore, the resulting Buffer can outlive self.
sourcepub unsafe fn shallow_or_clone(&self) -> Buffer<'a, T, D, S>where
<D as Device>::Ptr<T, S>: ShallowCopy,
T: Clone,
D: CloneBuf<'a, T, S>,
pub unsafe fn shallow_or_clone(&self) -> Buffer<'a, T, D, S>where <D as Device>::Ptr<T, S>: ShallowCopy, T: Clone, D: CloneBuf<'a, T, S>,
Returns a shallow copy of &self, if the realloc feature is deactivated.
If the realloc feature is activated, it returns a deep copy / clone.
Safety
Itself, this function does not need to be unsafe.
However, declaring this function as unsafe highlights the violation of possibly creating two or more owners for one resource.
Furthermore, the resulting Buffer can outlive self.
pub fn id(&self) -> Ident
pub fn as_dims<'b, O>(&self) -> &Buffer<'b, T, D, O>where O: Shape,
pub fn as_dims_mut<'b, O>(&mut self) -> &mut Buffer<'b, T, D, O>where O: Shape,
Trait Implementations§
source§impl<'a, T, D, S: Shape> Add<&Matrix<'a, T, D, S>> for &Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
impl<'a, T, D, S: Shape> Add<&Matrix<'a, T, D, S>> for &Matrix<'a, T, D, S>where D: BaseOps<T, S>,
source§impl<'a, T, D, S: Shape> Add<&Matrix<'a, T, D, S>> for Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
impl<'a, T, D, S: Shape> Add<&Matrix<'a, T, D, S>> for Matrix<'a, T, D, S>where D: BaseOps<T, S>,
source§impl<'a, T, D, S: Shape> Add<Matrix<'a, T, D, S>> for &Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
impl<'a, T, D, S: Shape> Add<Matrix<'a, T, D, S>> for &Matrix<'a, T, D, S>where D: BaseOps<T, S>,
source§impl<'a, T, D, S: Shape> Add<Matrix<'a, T, D, S>> for Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
impl<'a, T, D, S: Shape> Add<Matrix<'a, T, D, S>> for Matrix<'a, T, D, S>where D: BaseOps<T, S>,
source§impl<'a, T, D> Add<T> for Matrix<'a, T, D>where
D: AdditionalOps<T>,
impl<'a, T, D> Add<T> for Matrix<'a, T, D>where D: AdditionalOps<T>,
source§impl<T, D, S: Shape> AddAssign<&Matrix<'_, T, D, S>> for Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
impl<T, D, S: Shape> AddAssign<&Matrix<'_, T, D, S>> for Matrix<'_, T, D, S>where D: AssignOps<T, S, D>,
source§fn add_assign(&mut self, rhs: &Self)
fn add_assign(&mut self, rhs: &Self)
+= operation. Read moresource§impl<T, D, S: Shape> AddAssign<Matrix<'_, T, D, S>> for Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
impl<T, D, S: Shape> AddAssign<Matrix<'_, T, D, S>> for Matrix<'_, T, D, S>where D: AssignOps<T, S, D>,
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moresource§impl<'a, T, S, D> AddAssign<T> for Matrix<'a, T, D, S>where
S: Shape,
D: ScalarAssign<T, S>,
impl<'a, T, S, D> AddAssign<T> for Matrix<'a, T, D, S>where S: Shape, D: ScalarAssign<T, S>,
source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
+= operation. Read moresource§impl<'a, T> AsClCvoidPtr for &Matrix<'a, T, OpenCL>
impl<'a, T> AsClCvoidPtr for &Matrix<'a, T, OpenCL>
source§impl<'a, T> AsClCvoidPtr for Matrix<'a, T, OpenCL>
impl<'a, T> AsClCvoidPtr for Matrix<'a, T, OpenCL>
source§impl<'a, T, S, D> Clone for Matrix<'a, T, D, S>where
T: Clone,
S: Shape,
D: CloneBuf<'a, T, S>,
impl<'a, T, S, D> Clone for Matrix<'a, T, D, S>where T: Clone, S: Shape, D: CloneBuf<'a, T, S>,
source§impl<T, S, D> DivAssign<T> for Matrix<'_, T, D, S>where
S: Shape,
D: ScalarAssign<T, S>,
impl<T, S, D> DivAssign<T> for Matrix<'_, T, D, S>where S: Shape, D: ScalarAssign<T, S>,
source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
/= operation. Read moresource§impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, (usize, usize), &[T])> for Matrix<'a, T, D>
impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, (usize, usize), &[T])> for Matrix<'a, T, D>
source§impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, (usize, usize), &Vec<T, Global>)> for Matrix<'a, T, D>
impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, (usize, usize), &Vec<T, Global>)> for Matrix<'a, T, D>
source§impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep, const N: usize> From<(&'a D, (usize, usize), [T; N])> for Matrix<'a, T, D>
impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep, const N: usize> From<(&'a D, (usize, usize), [T; N])> for Matrix<'a, T, D>
source§impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, (usize, usize), Vec<T, Global>)> for Matrix<'a, T, D>
impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, (usize, usize), Vec<T, Global>)> for Matrix<'a, T, D>
source§impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, usize, usize, &[T])> for Matrix<'a, T, D>
impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, usize, usize, &[T])> for Matrix<'a, T, D>
source§impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep, const N: usize> From<(&'a D, usize, usize, [T; N])> for Matrix<'a, T, D>
impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep, const N: usize> From<(&'a D, usize, usize, [T; N])> for Matrix<'a, T, D>
source§impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, usize, usize, Vec<T, Global>)> for Matrix<'a, T, D>
impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, usize, usize, Vec<T, Global>)> for Matrix<'a, T, D>
source§impl<'a, T, D: Device, S: Shape> From<(Buffer<'a, T, D, S>, (usize, usize))> for Matrix<'a, T, D, S>
impl<'a, T, D: Device, S: Shape> From<(Buffer<'a, T, D, S>, (usize, usize))> for Matrix<'a, T, D, S>
source§impl<'a, T, D: Device, S: Shape> From<(Buffer<'a, T, D, S>, usize, usize)> for Matrix<'a, T, D, S>
impl<'a, T, D: Device, S: Shape> From<(Buffer<'a, T, D, S>, usize, usize)> for Matrix<'a, T, D, S>
source§impl<'a, T, D, S: Shape> Mul<&Matrix<'a, T, D, S>> for &Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
impl<'a, T, D, S: Shape> Mul<&Matrix<'a, T, D, S>> for &Matrix<'a, T, D, S>where D: BaseOps<T, S>,
source§impl<'a, T, D, S: Shape> Mul<&Matrix<'a, T, D, S>> for Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
impl<'a, T, D, S: Shape> Mul<&Matrix<'a, T, D, S>> for Matrix<'a, T, D, S>where D: BaseOps<T, S>,
source§impl<'a, T, D, S: Shape> Mul<Matrix<'a, T, D, S>> for Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
impl<'a, T, D, S: Shape> Mul<Matrix<'a, T, D, S>> for Matrix<'a, T, D, S>where D: BaseOps<T, S>,
source§impl<T, D, S: Shape> MulAssign<&Matrix<'_, T, D, S>> for Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
impl<T, D, S: Shape> MulAssign<&Matrix<'_, T, D, S>> for Matrix<'_, T, D, S>where D: AssignOps<T, S, D>,
source§fn mul_assign(&mut self, rhs: &Self)
fn mul_assign(&mut self, rhs: &Self)
*= operation. Read moresource§impl<T, D, S: Shape> MulAssign<Matrix<'_, T, D, S>> for Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
impl<T, D, S: Shape> MulAssign<Matrix<'_, T, D, S>> for Matrix<'_, T, D, S>where D: AssignOps<T, S, D>,
source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moresource§impl<T, S, D> MulAssign<T> for Matrix<'_, T, D, S>where
S: Shape,
D: ScalarAssign<T, S>,
impl<T, S, D> MulAssign<T> for Matrix<'_, T, D, S>where S: Shape, D: ScalarAssign<T, S>,
source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
*= operation. Read moresource§impl<'a, T, D, S> Sub<&Matrix<'a, T, D, S>> for &Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
S: Shape,
impl<'a, T, D, S> Sub<&Matrix<'a, T, D, S>> for &Matrix<'a, T, D, S>where D: BaseOps<T, S>, S: Shape,
source§impl<'a, T, D, S> Sub<&Matrix<'a, T, D, S>> for Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
S: Shape,
impl<'a, T, D, S> Sub<&Matrix<'a, T, D, S>> for Matrix<'a, T, D, S>where D: BaseOps<T, S>, S: Shape,
source§impl<'a, T, D, S> Sub<Matrix<'a, T, D, S>> for &Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
S: Shape,
impl<'a, T, D, S> Sub<Matrix<'a, T, D, S>> for &Matrix<'a, T, D, S>where D: BaseOps<T, S>, S: Shape,
source§impl<'a, T, D, S> Sub<Matrix<'a, T, D, S>> for Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
S: Shape,
impl<'a, T, D, S> Sub<Matrix<'a, T, D, S>> for Matrix<'a, T, D, S>where D: BaseOps<T, S>, S: Shape,
source§impl<T, D, S: Shape> SubAssign<&Matrix<'_, T, D, S>> for &mut Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
impl<T, D, S: Shape> SubAssign<&Matrix<'_, T, D, S>> for &mut Matrix<'_, T, D, S>where D: AssignOps<T, S, D>,
source§fn sub_assign(&mut self, rhs: &Matrix<'_, T, D, S>)
fn sub_assign(&mut self, rhs: &Matrix<'_, T, D, S>)
-= operation. Read moresource§impl<T, D, S: Shape> SubAssign<&Matrix<'_, T, D, S>> for Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
impl<T, D, S: Shape> SubAssign<&Matrix<'_, T, D, S>> for Matrix<'_, T, D, S>where D: AssignOps<T, S, D>,
source§fn sub_assign(&mut self, rhs: &Self)
fn sub_assign(&mut self, rhs: &Self)
-= operation. Read moresource§impl<T, D, S: Shape> SubAssign<Matrix<'_, T, D, S>> for &mut Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
impl<T, D, S: Shape> SubAssign<Matrix<'_, T, D, S>> for &mut Matrix<'_, T, D, S>where D: AssignOps<T, S, D>,
source§fn sub_assign(&mut self, rhs: Matrix<'_, T, D, S>)
fn sub_assign(&mut self, rhs: Matrix<'_, T, D, S>)
-= operation. Read more