Trait custos_math::BaseOps

source ·
pub trait BaseOps<T, S: Shape = (), D: Device = Self>: Device {
    // Required methods
    fn add(
        &self,
        lhs: &Matrix<'_, T, D, S>,
        rhs: &Matrix<'_, T, D, S>
    ) -> Matrix<'_, T, Self, S>;
    fn sub(
        &self,
        lhs: &Matrix<'_, T, D, S>,
        rhs: &Matrix<'_, T, D, S>
    ) -> Matrix<'_, T, Self, S>;
    fn mul(
        &self,
        lhs: &Matrix<'_, T, D, S>,
        rhs: &Matrix<'_, T, D, S>
    ) -> Matrix<'_, T, Self, S>;
    fn div(
        &self,
        lhs: &Matrix<'_, T, D, S>,
        rhs: &Matrix<'_, T, D, S>
    ) -> Matrix<'_, T, Self, S>;
}
Expand description

Element-wise +, -, *, / operations for matrices.

Examples

use custos::CPU;
use custos_math::Matrix;

let device = CPU::new();
let a = Matrix::from((&device, (2, 3), [2, 4, 6, 8, 10, 12]));
let b = Matrix::from((&device, (2, 3), [12, 4, 3, 1, -5, -3]));

let c = &a + &b;
assert_eq!(c.read(), vec![14, 8, 9, 9, 5, 9]);

use custos_math::BaseOps;
let sub = device.sub(&a, &b);
assert_eq!(sub.read(), vec![-10, 0, 3, 7, 15, 15]);

Required Methods§

source

fn add( &self, lhs: &Matrix<'_, T, D, S>, rhs: &Matrix<'_, T, D, S> ) -> Matrix<'_, T, Self, S>

Element-wise addition

Example
use custos::CPU;
use custos_math::Matrix;

let device = CPU::new();
let a = Matrix::from((&device, 2, 3, [2, 4, 6, 8, 10, 12]));
let b = Matrix::from((&device, 2, 3, [12, 4, 3, 1, -5, -3]));

let c = a + b;
assert_eq!(c.read(), vec![14, 8, 9, 9, 5, 9]);
source

fn sub( &self, lhs: &Matrix<'_, T, D, S>, rhs: &Matrix<'_, T, D, S> ) -> Matrix<'_, T, Self, S>

Element-wise subtraction

Example
use custos::CPU;
use custos_math::{Matrix, BaseOps};

let device = CPU::new();
let a = Matrix::from((&device, 2, 3, [2, 4, 6, 8, 10, 12]));
let b = Matrix::from((&device, 2, 3, [12, 4, 3, 1, -5, -3]));

let sub = device.sub(&a, &b);
assert_eq!(sub.read(), vec![-10, 0, 3, 7, 15, 15]);
source

fn mul( &self, lhs: &Matrix<'_, T, D, S>, rhs: &Matrix<'_, T, D, S> ) -> Matrix<'_, T, Self, S>

Element-wise multiplication

Example
use custos::CPU;
use custos_math::{Matrix, BaseOps};

let device = CPU::new();
let a = Matrix::from((&device, 2, 3, [2, 4, 6, 8, 10, 12]));
let b = Matrix::from((&device, 2, 3, [12, 4, 3, 1, -5, -3]));

let mul = a * b;
assert_eq!(mul.read(), vec![24, 16, 18, 8, -50, -36]);
source

fn div( &self, lhs: &Matrix<'_, T, D, S>, rhs: &Matrix<'_, T, D, S> ) -> Matrix<'_, T, Self, S>

Element-wise division

Example
use custos::CPU;
use custos_math::{Matrix, BaseOps};

let device = CPU::new();
let a = Matrix::from((&device, 2, 3, [2, 4, 6, 8, 10, 12]));
let b = Matrix::from((&device, 2, 3, [12, 4, 3, 1, -5, -3]));

let div = device.div(&a, &b);
assert_eq!(div.read(), vec![0, 1, 2, 8, -2, -4]);

Implementors§

source§

impl<T, S, D> BaseOps<T, S, D> for CPUwhere T: Number, S: Shape, D: MainMemory,

source§

impl<T: CDatatype> BaseOps<T, (), CUDA> for CUDA

source§

impl<T: CDatatype> BaseOps<T, (), OpenCL> for OpenCL