Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. impl<T: Add<Output=T> + Mul<Output=T> + Clone + Zero> SparseVec<T> {
  2.  
  3. pub fn zeros(length: usize) -> SparseVec<T> {
  4. SparseVec {
  5. length: length,
  6. data: BTreeMap::new()
  7. }
  8. }
  9.  
  10. pub fn set(&mut self, index: usize, value: T) {
  11. self.data.insert(index, value);
  12. }
  13.  
  14. pub fn dot(self, rhs: &SparseVec<T>) -> T {
  15. self.data.into_iter().map(|(k, v)| {
  16. match rhs.data.get(&k) {
  17. Some(r) => r.clone() * v,
  18. None => Zero::zero()
  19. }
  20. }).sum()
  21. }
  22.  
  23. pub fn norm(self) {
  24.  
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement