I want to convert a Sage symoblic expression like:
y = 5*x + 7^x + 3*x^2
to a string which I can access by subscripting ( y[0] = ‘5’, y[1] = ‘*’, etc.
I need to do that because I want to calculate the number of occurrence of a specific variable in an expression. In above example the variable ‘X’ occures 3 times.
I would also appreciate if someone knew another way to achieve that.
I think everyone’s misreading the problem — we’re starting from a Sage symbolic expression.
I would write a little recursive walker using
operands()andoperator():which seems to work:
And if you want to convert the expression to a string, simply call
str: