471,354 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

How To Create A Dictionary Based - Sparse Matrix

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
5 7601
bartonc
6,596 Expert 4TB
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
cLoque
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
bartonc
6,596 Expert 4TB
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
cLoque
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
  1. class GeneralSparse:
  2.  
  3.     def __init__(self, mtx={}, m=0, n=0):
  4.         self.matrix = {}
  5.         self.matrix.update(mtx)
  6.         self.row = m
  7.         self.col = n
  8.  
  9.     def __getitem__(self, ij):
  10.         return self.matrix.get(ij,0)   
  11.  
  12.     def __setitem__(self, ij, val):
  13.         self.matrix[i,j] = val
  14.  
  15.     def __str__(self):
  16.         s = "["
  17.         if self.row > 0:
  18.             for i in range(self.row):
  19.                 s += str(self[i])
  20.                 if i < self.row:
  21.                      s += ", "
  22.         for j in range(self.col):
  23.                     s += str(self[j]) 
  24.             s += "]"
  25.         return s
  26.  
  27.  
  28. if __name__=="__main__":
  29.     x = GeneralSparse({(2,1):1}, 2, 3)
  30.     print x
this what i get each time i run this

Expand|Select|Wrap|Line Numbers
  1. Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32
  2. Type "copyright", "credits" or "license" for more information.
  3. IDLE 0.8 -- press F1 for help
  4. >>> 
  5. [0, 0000, 000]
  6. [0, 000]
  7. [0, 0000, 000]
  8. [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
bvdet
2,851 Expert Mod 2GB
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
  1. class GeneralSparse:
  2.  
  3.     def __init__(self, mtx={}, m=0, n=0):
  4.         self.matrix = {}
  5.         self.matrix.update(mtx)
  6.         self.row = m
  7.         self.col = n
  8.  
  9.     def __getitem__(self, ij):
  10.         return self.matrix.get(ij,0)   
  11.  
  12.     def __setitem__(self, ij, val):
  13.         self.matrix[i,j] = val
  14.  
  15.     def __str__(self):
  16.         s = "["
  17.         if self.row > 0:
  18.             for i in range(self.row):
  19.                 s += str(self[i])
  20.                 if i < self.row:
  21.                      s += ", "
  22.         for j in range(self.col):
  23.                     s += str(self[j]) 
  24.             s += "]"
  25.         return s
  26.  
  27.  
  28. if __name__=="__main__":
  29.     x = GeneralSparse({(2,1):1}, 2, 3)
  30.     print x
this what i get each time i run this

Expand|Select|Wrap|Line Numbers
  1. Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32
  2. Type "copyright", "credits" or "license" for more information.
  3. IDLE 0.8 -- press F1 for help
  4. >>> 
  5. [0, 0000, 000]
  6. [0, 000]
  7. [0, 0000, 000]
  8. [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
  1. class GeneralSparse(object):
  2.  
  3.     def __init__(self, mtx={}, row=4, col=4):
  4.         self.row = row
  5.         self.col = col
  6.         self.matrix = dict(zip([(i,j) for i in range(row) for j in range(col)], \
  7.                                [0.0 for i in range(row) for j in range(col)]))
  8.         self.matrix.update(mtx)
  9.  
  10.     def __getitem__(self, ij):
  11.         return self.matrix[ij]   
  12.  
  13.     def __setitem__(self, ij, val):
  14.         self.matrix[ij] = val
  15.  
  16.     def __iter__(self):
  17.         keys = self.matrix.keys()
  18.         keys.sort()
  19.         for key in keys:
  20.             yield key, self.matrix[key]
  21.  
  22.     def __str__(self):
  23.         return '{%s}' % ', '.join(['%s: %s' % (item[0], item[1]) for item in self])
  24.  
  25. if __name__=="__main__":
  26.     x = GeneralSparse({(2,1):1}, 3, 6)
  27.     print x
Nov 7 '07 #6

Post your reply

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

Similar topics

4 posts views Thread by Derek Fountain | last post: by
8 posts views Thread by Benjamin Scott | last post: by
reply views Thread by George Sakkis | last post: by
7 posts views Thread by mariaczi | last post: by
4 posts views Thread by deLenn | last post: by
3 posts views Thread by mediratta | last post: by
5 posts views Thread by adam.kleinbaum | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.