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 like to try and generate the Symbols automatically. I
would wonder if there is a way to obtain the components of the indexed
element, such as ['M', '1', '2', 'n'], and then concatenate them
together as a string or even a variable. 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 variables, such that if the variable name is
already in the list, it is not added. Is it possible to make this a list of
strings?
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
very GOOD reason to make n an element of M and not an exponent; it is much
easier to code that way.
Agreed.
The following sequence of operations can be used to convert an expression with
indexed elements into a format where the ccode() function can then be used to
output the expression in C/C++ form.
Nicholas
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)?
Aaron Meurer
That's another good question. Thanks for your response, Aaron!
Nicholas
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to
sympy+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.