Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sparse_functions
- class GetAttr(type):
- def __getitem__(cls, x):
- return getattr(cls, x)
- class Sparse:
- __metaclass__ = GetAttr
- """ Class for sparse matrices"""
- # This is the constructor when dimensions and sparsity are specified
- def __init__(self, *args):
- # The name of the helper function is sparse_gen
- if len(args) == 3:
- num_row = args[0]
- num_col = args[1]
- spar = args[2]
- if ((type(args[0]) is not int) or (type(args[1]) is not int)):
- raise TypeError('The first two arguments should be integers!')
- elif not ((args[0] > 0) and (args[1] > 0)):
- raise ValueError('The first two agruments should be positive!')
- elif not ((args[2] < 1) and (args[2] > 0)):
- raise ValueError('Sparsity must be between 0 and 1!')
- self.sparse_rep = sparse_functions.sparse_gen(num_row, num_col, spar)
- elif len(args) == 1:
- if (type(args[0] is not list)):
- raise TypeError('The only argument supplied should be a list!')
- # The list of lists matrix is provided convert it to sparse
- self.sparse_rep = sparse_functions.mat2sparse(arg[0])
- else:
- raise AttributeError('Invalid number of arguments. There should be either one argument or three arguments!')
- # Overload the addition operation
- def __add__(a,b):
- # Here we can make use of the already defined functions
- return sparse_functions.sparse_add(a,b)
- # Overload the subtraction operator
- def __sub__(a,b):
- return sparse_functions.sparse_add(a,-b)
- # Overload the print function
- def __str__(self):
- sparse_functions.sparse_print(self)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement