Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Matrix_(object):
- def init(self, *args, **kwargs):
- # Preprocessing data
- # Making a dict of all non-zero key-value pairs
- if len(args) == 3 and callable(args[2]):
- rows, cols, mat = _from_callable(args)
- elif len(args) == 3 and isinstance(args[2], (list, tuple)):
- rows, cols, mat = _from_list(args)
- else: # Value passing only through lambda and list right now
- raise NotImplementedError("Data type not understood")
- # mat is now a dict of of all non-zero elements
- # Choosing a representation for the matrix
- if 'repr' in kwargs:
- # User-specified representation
- if kwargs['repr'] == 'dok':
- mat = _dict_to_dokmatrix(rows, cols, mat)
- elif kwargs['repr'] == 'lil':
- mat = _dict_to_lilmatrix(rows, cols, mat)
- elif kwargs['repr'] == 'dense':
- mat = _dict_to_densematrix(rows, cols, mat)
- else:
- raise Exception('repr %s not recognized' % kwargs['repr'])
- else:
- # Representation chosen by sparsity check
- sparsity = float(len(mat)) / (rows * cols)
- if sparsity > 0.5:
- mat = _dict_to_densematrix(rows, cols, mat)
- else:
- mat = _dict_to_dokmatrix(rows, cols, mat)
- # mat is now one of the low-level matrices
- self.rows = rows
- self.cols = cols
- self.mat = mat
- # We're done.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement