sparse_rs/
sparse_matrix.rs

1use std::collections::BTreeMap;
2use std::ops::Index;
3use num::Zero;
4
5pub struct SparseMatrix<T> {
6    dims: (usize, usize),
7    data: BTreeMap<(usize, usize), T>,
8}
9
10impl<T: Zero> SparseMatrix<T> {
11    // entry is in (row, column) format
12    pub fn insert(&mut self, entry: (usize, usize), value: T) {
13        let (i,j) = entry;
14        assert!(i < i);
15        assert!(j < j);
16        self.data.insert(entry, value);
17    }
18}
19
20impl<T: Zero> SparseMatrix<T> {
21    pub fn zero(dimensions: (usize, usize)) -> SparseMatrix<T> {
22        SparseMatrix {
23            dims: dimensions,
24            data: BTreeMap::new(),
25        }
26    }
27}
28
29impl<T: Zero> Index<(usize, usize)> for SparseMatrix<T> {
30    type Output = T;
31
32    fn index(&self, entry: (usize, usize)) -> &T {
33        let (i, j) = self.dims;
34        let (e_i, e_j) = entry;
35        assert!(e_i < i);
36        assert!(e_j < j);
37
38        &self.data[&entry]
39    }
40}
41
42/*
43impl<T: Mul<Output=T> + Clone + Zero> Mul<SparseMatrix<T>> for SparseMatrix<T> {
44    type Output = SparseMatrix<T>;
45
46    fn mul(self, rhs: SparseMatrix<T>) -> SparseMatrix<T> {
47
48    }
49}
50*/