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):
5 7863
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.).
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__()).
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).
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: - 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 - 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
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: - 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 - 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__(): - 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[0], item[1]) for item in self])
-
-
if __name__=="__main__":
-
x = GeneralSparse({(2,1):1}, 3, 6)
-
print x
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Derek Fountain |
last post by:
I'm just starting another PHP project and can see a familiar task not far on
the horizon. I have several database record tuples that I need to
manipulate - person, department, client, job, etc. -...
|
by: Benjamin Scott |
last post by:
Hello.
I attempted to build a compound dictionary:
len(Lst)=1000
len(nuerLst)=250
len(nuestLst)=500
Dict={}
|
by: George Sakkis |
last post by:
Is there any sparse matrix package compatible with Numeric/Numarray ? Ideally, the implementation of
a matrix (dense/sparse) should be transparent to the application. However the APIs of the only...
|
by: mariaczi |
last post by:
Hi,
I code class to storage sparse matrix row compressed and i have a
problem with implements method to setVal and addVal.
I will replace later this methods overloaded operator().
Please, can You...
|
by: deLenn |
last post by:
Hi,
Does scipy have an equivalent to Matlab's 'find' function, to list the
indices of all nonzero elements in a sparse matrix?
Cheers.
|
by: mediratta |
last post by:
Hi,
I want to allocate memory for a large matrix, whose size will be
around 2.5 million x 17000. Three fourth of its rows will have all
zeroes, but it is not known which will be those rows. If I...
|
by: hvmclrhu |
last post by:
Hi I have a big problem. When we compile serial.c with gcc, I get this error
program is generating the sparse matrix
Segmentation fault
I think ı have to use malloc() but I don't know how to...
|
by: adam.kleinbaum |
last post by:
Hi there,
I'm a novice C programmer working with a series of large (30,000 x
30,000) sparse matrices on a Linux system using the GCC compiler. To
represent and store these matrices, I'd like to...
|
by: ishakteyran |
last post by:
hello to all.. i have a realy tough assignment which requires me to add, substract, multiply, and get inverse of non-sparse and sparse matrixes..
in a more clear way it wants me to to the...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |