By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 424,842 Members | 2,198 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,842 IT Pros & Developers. It's quick & easy.

# How To Create A Dictionary Based - Sparse Matrix

 P: 10 Hi, I would like to know how to create a dictionary based matrix. Where column and row are considered in placing my desired value. and will pass value as dictionary just like this def __init__(self, mtx={}, m=0, n=0): Nov 5 '07 #1
Share this Question
5 Replies

 Expert 5K+ P: 6,596 The author of this thread had some wild ideas about using a dictionary to store a matrix. Your Idea looks much simpler, but we may need a little more info to go on (like an example of usage, etc.). Nov 5 '07 #2

 P: 10 hi this is what i need to figure out. it confuses me because most of the example in creating a sparse vector are in list, mine uses dictionary. i am just a beginner in python, so i really need all the help that i can get in doing this project. hope you can help me guys # def __init__(self, mtx={}, m=0, n=0): The constructor takes as arguments the dictionary representation of a sparse matrix (mtx), along with the row and column dimensions (m and n, respectively), and initializes the corresponding instance variables. The instance dictionary must be a true copy of the dictionary passed in the argument, not just a pointer. We explained in lecture how a true copy is made by the constructor in class SparseVector; the technique is similar here. Note there is potential for error using this constructor. Specifically, m and/or n could be less than (or equal to) the largest row/column index appearing in the dictionary. This cannot happen. If the matrix dimension is m×n, no row index can be ≥ m and no column index can be ≥ n. Your constructor must detect this error, and if it occurs an exception must be raised. Exceptions are discussed below. >>>for this one what i understand is i have to initialize or create an empty matrix that will hold the my matrix. # def __getitem__(self, ij): [] is overloaded for GeneralSparse matrix element retrieval. Similar to __getitem__() in class SparseVector. There is no need to check for out-of-bounds indices. __getitem__() is used only by methods __add__() and __mul__() of class GeneralSparse, and these methods are protected from out-of-bounds index access (see below). # def __setitem__(self, ij, val): [] is overloaded for GeneralSparse matrix element assignment. Similar to __setitem__() in class SparseVector. There is no need to check for out-of-bounds indices (as with __getitem__()). Nov 5 '07 #3

 Expert 5K+ P: 6,596 As this is coursework, you'll really need to post work that you have done and seek guidance finding errors in YOUR OWN work. This site has strict policies against doing you coursework for you. Instructions for using CODE tags are on the right hand side of the page and all policies are enumerated in our Posting Guidelines (also linked to from the announcement at the top of the page). Nov 5 '07 #4

 P: 10 hi, i want to hava matrix output by my code doesn't return what i want, hope you can help me with my __str__() here it is: Expand|Select|Wrap|Line Numbers class GeneralSparse:       def __init__(self, mtx={}, m=0, n=0):         self.matrix = {}         self.matrix.update(mtx)         self.row = m         self.col = n       def __getitem__(self, ij):         return self.matrix.get(ij,0)          def __setitem__(self, ij, val):         self.matrix[i,j] = val       def __str__(self):         s = "["         if self.row > 0:             for i in range(self.row):                 s += str(self[i])                 if i < self.row:                      s += ", "         for j in range(self.col):                     s += str(self[j])              s += "]"         return s     if __name__=="__main__":     x = GeneralSparse({(2,1):1}, 2, 3)     print x this what i get each time i run this Expand|Select|Wrap|Line Numbers Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32 Type "copyright", "credits" or "license" for more information. IDLE 0.8 -- press F1 for help >>>  [0, 0000, 000] [0, 000] [0, 0000, 000] [0, 0000, 000] instead of having a matrix i only get rows and doesn't prints my the desired value just 0 hope you can help me Nov 7 '07 #5

 Expert Mod 2.5K+ P: 2,851 hi, i want to hava matrix output by my code doesn't return what i want, hope you can help me with my __str__() here it is: Expand|Select|Wrap|Line Numbers class GeneralSparse:       def __init__(self, mtx={}, m=0, n=0):         self.matrix = {}         self.matrix.update(mtx)         self.row = m         self.col = n       def __getitem__(self, ij):         return self.matrix.get(ij,0)          def __setitem__(self, ij, val):         self.matrix[i,j] = val       def __str__(self):         s = "["         if self.row > 0:             for i in range(self.row):                 s += str(self[i])                 if i < self.row:                      s += ", "         for j in range(self.col):                     s += str(self[j])              s += "]"         return s     if __name__=="__main__":     x = GeneralSparse({(2,1):1}, 2, 3)     print x this what i get each time i run this Expand|Select|Wrap|Line Numbers Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32 Type "copyright", "credits" or "license" for more information. IDLE 0.8 -- press F1 for help >>>  [0, 0000, 000] [0, 000] [0, 0000, 000] [0, 0000, 000] instead of having a matrix i only get rows and doesn't prints my the desired value just 0 hope you can help me I initialized your self.matrix with zeros, added an __iter__() method, and modified __str__(): Expand|Select|Wrap|Line Numbers class GeneralSparse(object):       def __init__(self, mtx={}, row=4, col=4):         self.row = row         self.col = col         self.matrix = dict(zip([(i,j) for i in range(row) for j in range(col)], \                                [0.0 for i in range(row) for j in range(col)]))         self.matrix.update(mtx)       def __getitem__(self, ij):         return self.matrix[ij]          def __setitem__(self, ij, val):         self.matrix[ij] = val       def __iter__(self):         keys = self.matrix.keys()         keys.sort()         for key in keys:             yield key, self.matrix[key]       def __str__(self):         return '{%s}' % ', '.join(['%s: %s' % (item, item) for item in self])   if __name__=="__main__":     x = GeneralSparse({(2,1):1}, 3, 6)     print x Nov 7 '07 #6

### Post your reply

Sign in to post your reply or Sign up for a free account. 