[Tutor] cannonical matrix representation?
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? Thanks! -Mike p.s. This seems to me like it ought to be built into the base language - multidimensional object arrays. (Indeed, maybe it is, and I'm just too dense to notice!) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cannonical matrix representation?
On 2/10/06, Mike Cheponis [EMAIL PROTECTED] wrote: 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? Thanks! -Mike If it is a truly sparse matrix, I would use a dictionary with tuples as keys; i.e., m{(x, y, z, t) = element} André p.s. This seems to me like it ought to be built into the base language - multidimensional object arrays. (Indeed, maybe it is, and I'm just too dense to notice!) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cannonical matrix representation?
p.s. This seems to me like it ought to be built into the base language - multidimensional object arrays. The only languages I know that truly support anything like what you want are programmes like Mathematica and arguably MS Excel Basic... There is a pseudo mathematica somewhere in Python but otherwise its roll your own time ... I think... Which usually means its time for somebody to find a ready wrapped solution :-) Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cannonical matrix representation?
| 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. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor