Trait custos_math::BaseOps
source · [−]pub trait BaseOps<T> {
fn add(&self, lhs: &Matrix<'_, T>, rhs: &Matrix<'_, T>) -> Matrix<'_, T>;
fn sub(&self, lhs: &Matrix<'_, T>, rhs: &Matrix<'_, T>) -> Matrix<'_, T>;
fn mul(&self, lhs: &Matrix<'_, T>, rhs: &Matrix<'_, T>) -> Matrix<'_, T>;
fn div(&self, lhs: &Matrix<'_, T>, rhs: &Matrix<'_, T>) -> Matrix<'_, T>;
}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
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]);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]);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]);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]);