[Tutor] cannonical matrix representation?
Smith
smiles at worksmail.net
Sat Feb 11 05:42:24 CET 2006
| 3. cannonical matrix representation? (Mike Cheponis)
|
| What's the best way to represent a matrix M with 4 dimensions, such
| as M[x][y][z][t] where each element in the sparse matrix could be a
| simple number, or could be an executable Python function snipped that
| returns a value when that cell is evaluated?
|
| The user of the program will type in Python functions to be inserted
| into particular cells in the 4-D matrix.
|
| I did't see any package that exactly does this; do I write my own
| Matrix class and base it on lists?
I don't know how full-blown of a solution that you need, but a little helper function for dictionaries (as was noted as a good way to deal with a sparse pmatric) might suffice, something like this,maybe.
###
def setdict(d,v,*k):
#args = dictionary, value, key all separated by commas
d[tuple(k)]=str(v)
def evaldict(d,*k):
#return the evaluated entry at key k in dictionary d
return eval(d[tuple(k)])
d={}
setdict(d,1,1,2)
setdict(d,'x+3',2)
print d
x=3
print evaldict(d,1,2)
print evaldict(d,2)
### output
{(1, 2): '1', (2,): 'x+3'}
1
6
###
The evaluation will pull its values from the context in which the evaldict is being made. There are ways to get around this, but just ask if you need/want to go this direction.
More information about the Tutor
mailing list