Hello,
For a given matrix M (m by n) and vector V of size n, my goal is to do the following:
- Take the $\text{i}^{\text{th}}$ column of M and convert them as keywords for a dictionary and associate the keywords to the $\text{i}^{\text{th}}$ value of the vector V.
- I do (1) in MatVec(M,V) in the code below by creating a dictionary for each keyword and value. Finally I try to merge all the dictionaries.
Here is my code:
def DictionaryMerge(*args):
import collections
super_dict = collections.defaultdict(set)
for d in args:
for k, v in d.iteritmes():
super_dict[k].add(v)
return super_dict
def MatVec(M,V):
from sets import Set
Vec = V.list()
MatCols = M.ncols()
D2 = {}
for i in range(MatCols):
ColumnVec = M.column(i).list()
CVec = list()
for k in range(len(ColumnVec)):
CVec.append(int(ColumnVec[k]))
for j in CVec:
dict = {j:Set(int(Vec[i]))}
D2 = DictionaryMerge(dict,D2)
return dict
M = matrix([[(i+1)*(j+2) for i in range(5)] for j in range(6)])
V = vector([int(i+100) for i in range(5)])
MatVec(M,V)
The error I get is:
Traceback (most recent call last): return super_dict
File "", line 1, in <module>
File "/tmp/tmp4m6HiR/___code___.py", line 29, in <module>
exec compile(u'MatVec(M,V)
File "", line 1, in <module>
File "/tmp/tmp4m6HiR/___code___.py", line 22, in MatVec
dict = {j:Set(int(Vec[i]))}
File "/home/usr111/sage/local/lib/python/sets.py", line 414, in __init__
self._update(iterable)
File "/home/usr111/sage/local/lib/python/sets.py", line 368, in _update
for element in iterable:
TypeError: 'int' object is not iterable
I am not so sure why I am getting this error.