Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. class Matrix_(object):
  2. def init(self, *args, **kwargs):
  3. # Preprocessing data
  4. # Making a dict of all non-zero key-value pairs
  5. if len(args) == 3 and callable(args[2]):
  6. rows, cols, mat = _from_callable(args)
  7. elif len(args) == 3 and isinstance(args[2], (list, tuple)):
  8. rows, cols, mat = _from_list(args)
  9. else: # Value passing only through lambda and list right now
  10. raise NotImplementedError("Data type not understood")
  11. # mat is now a dict of of all non-zero elements
  12.  
  13. # Choosing a representation for the matrix
  14. if 'repr' in kwargs:
  15. # User-specified representation
  16. if kwargs['repr'] == 'dok':
  17. mat = _dict_to_dokmatrix(rows, cols, mat)
  18. elif kwargs['repr'] == 'lil':
  19. mat = _dict_to_lilmatrix(rows, cols, mat)
  20. elif kwargs['repr'] == 'dense':
  21. mat = _dict_to_densematrix(rows, cols, mat)
  22. else:
  23. raise Exception('repr %s not recognized' % kwargs['repr'])
  24. else:
  25. # Representation chosen by sparsity check
  26. sparsity = float(len(mat)) / (rows * cols)
  27. if sparsity > 0.5:
  28. mat = _dict_to_densematrix(rows, cols, mat)
  29. else:
  30. mat = _dict_to_dokmatrix(rows, cols, mat)
  31.  
  32. # mat is now one of the low-level matrices
  33.  
  34. self.rows = rows
  35. self.cols = cols
  36. self.mat = mat
  37.  
  38. # We're done.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement