[sympy] Replacing indexed variables in an expression

2010-09-24 Thread Nicholas Kinar
Hello, Suppose that I have an expression such as the one given below: M = Indexed('M') var('A B C') i,j,n = symbols('i j n', integer = True) expr = A*M(1,2)**n + B*M(-2,1)**n + C*M(-1,-1)**n Is there a way to iterate through this expression and apply the following sequence of operations? (1

Re: [sympy] Replacing indexed variables in an expression

2010-09-24 Thread Aaron S. Meurer
On Sep 24, 2010, at 9:20 AM, Nicholas Kinar wrote: > Hello, > > Suppose that I have an expression such as the one given below: > > M = Indexed('M') > var('A B C') > i,j,n = symbols('i j n', integer = True) > > expr = A*M(1,2)**n + B*M(-2,1)**n + C*M(-1,-1)**n > > Is there a way to iterate thro

Re: [sympy] Replacing indexed variables in an expression

2010-09-24 Thread Nicholas Kinar
There's no need to iterate through the expression to do this. Just use subs, i.e., expr.subs({M(1, 2)**n: Symbol('M_1_2_n'), M(-2, 1)**n: Symbol('M_neg2_1_n'), …}) Of course, if you want to generate the Symbols automatically, that would require you to be a little more clever. Yes, I would

Re: [sympy] Replacing indexed variables in an expression

2010-09-24 Thread Aaron S. Meurer
On Sep 24, 2010, at 9:43 AM, Nicholas Kinar wrote: > >> There's no need to iterate through the expression to do this. Just use >> subs, i.e., expr.subs({M(1, 2)**n: Symbol('M_1_2_n'), M(-2, 1)**n: >> Symbol('M_neg2_1_n'), …}) >> >> Of course, if you want to generate the Symbols automatically

Re: [sympy] Replacing indexed variables in an expression

2010-09-24 Thread Nicholas Kinar
There is (I gave it below :) Aaron Meurer Then I think that .subs() could be used to make the substitution. (2) For every replacement, a list (or similar data structure) is created with the names of the variables [M_1_2_n, M_neg2_1_n, M_neg1_neg1_n]. The list contains the names of unique

Re: [sympy] Replacing indexed variables in an expression

2010-09-24 Thread Øyvind Jensen
fr., 24.09.2010 kl. 09.34 -0600, skrev Aaron S. Meurer: > On Sep 24, 2010, at 9:20 AM, Nicholas Kinar wrote: > > > Hello, > > > > Suppose that I have an expression such as the one given below: > > > > M = Indexed('M') > > var('A B C') > > i,j,n = symbols('i j n', integer = True) > > > > expr =

Re: [sympy] Replacing indexed variables in an expression

2010-09-24 Thread Nicholas Kinar
Hmm. To me, it seems like ccode() should be able to do this on its own. Maybe you could submit it as a patch to ccode(). Or maybe it really can and neither of us know about it. Øyvind (or anyone)? There is a lot of pending patches that will improve the support for Indexed in ccode. You ca

Re: [sympy] Replacing indexed variables in an expression

2010-09-24 Thread Øyvind Jensen
fr., 24.09.2010 kl. 10.25 -0600, skrev Nicholas Kinar: > > Hmm. To me, it seems like ccode() should be able to do this on its own. > > Maybe you could submit it as a patch to ccode(). Or maybe it really can > > and neither of us know about it. Øyvind (or anyone)? > > There is a lot of pending

Re: [sympy] Replacing indexed variables in an expression

2010-09-24 Thread Nicholas Kinar
So it sounds like that is what you want to do. I guess you need to combine the expr.atoms(IndexedElement) with str() or .args and some regular expression replacement. Something like '_'.join([str(t) for t in index.args]).replace('-', 'neg'), where index is a M(1, 2, …) element (again a ver