On Jul 9, 6:14 am, Jason Grout <jason-s...@creativetrax.com> wrote: > On 7/8/10 11:38 AM, David Sanders wrote: > > > > > Hi, > > > I am trying to extract part of a symbolic expression. > > The expression -- an eigenvalue of a matrix -- has the form > > > A + B*sqrt(C) > > > where A, B and C are themselves complicated symbolic expressions. > > > I wish to extract the subexpression C from this to test where the > > eigenvalues change type (where C==0). > > > By using introspection and the help (both excellent features!), I > > stumbled across one possible solution, using iterator. But it's very > > fussy: I have to do something like: > > > var('A B C') > > eigval = A + B*sqrt(C) > > terms = list( eigval.iterator() ) > > first = terms[0] > > terms2 = list(first.iterator()) > > desired = list(terms2[1].iterator())[0] > > > to extract the part I want into the variable "desired" > > > To me it would seem more intuitive to use indexing directly on the > > expression, to be able to do something like > > > eigval[0][1][0] > > > which is similar to what is available in Mathematica, for example, but > > this doesn't work, since apparently indexing is not defined for > > symbolic expressions. (Couldn't it be defined to have exactly this > > functionality?) > > > So the question finally is: am I reinventing the wheel here? Is there > > a simple way to do this? > > You could use pattern matching: > > sage: w0=SR.wild(0) > sage: w1=SR.wild(1) > sage: w2=SR.wild(2) > sage: var('a,b,c') > (a, b, c) > sage: m=(a+b*sqrt(c)).match(sqrt(w0)*w1+w2) > sage: m[w0] > c > sage: m=(a+b*sqrt(sqrt(17)*c^2+a)).match(sqrt(w0)*w1+w2) > sage: m[w0] > sqrt(17)*c^2 + a
This seems to be closest to what I was looking for, thanks -- it looks like a very powerful method! David. > > Thanks, > > Jason -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org