Skip to content

Instantly share code, notes, and snippets.

@ubnt-intrepid
Last active July 23, 2016 06:29
Show Gist options
  • Select an option

  • Save ubnt-intrepid/8f13c9cdd2dba145ae67f5941ddca5a0 to your computer and use it in GitHub Desktop.

Select an option

Save ubnt-intrepid/8f13c9cdd2dba145ae67f5941ddca5a0 to your computer and use it in GitHub Desktop.
#[derive(Clone)]
struct Tensor<T, Index> {
body: Vec<T>,
size: Index,
}
impl<T, Index> Tensor<T, Index> {
// size :: (usize,usize,...)
// f :: (usize,usize,...) -> T
fn new<F>(size: Index, f:F) -> Tensor<T, Index> where F: Fn(Index) -> T {
Tensor { body: Vec::new(), size: size }
}
fn get(&self, idx: Index) -> Option<&T> { None }
fn get_mut(&mut self, idx: Index) -> Option<&mut T> { None }
}
use std::ops::Add;
impl<T, Index> Add for Tensor<T, Index> {
type Output = Tensor<T, Index>;
fn add(mut self, rhs: Tensor<T, Index>) -> Tensor<T, Index> { self }
}
type Vector<T> = Tensor<T, (usize)>;
type Matrix<T> = Tensor<T, (usize, usize)>;
type Cubic<T> = Tensor<T, (usize, usize, usize)>;
fn main() {
let n = 10;
let v = Vector::new(n, |i| i as f64);
let m = Matrix::new((n, n), |(i,j)| (i*j) as f64);
let c = Cubic::new((n,n,n), |(i,j,k)| Some(i*j*k) as Option<f64>);
let v2 = v.clone();
let v3 = v + v2;
let v3 = v + m;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment