On Jul 9, 5:16 am, kcrisman <[email protected]> wrote: > On Jul 8, 2:38 pm, David Sanders <[email protected]> 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" > > I don't know if this would be better, but... > > sage: eigval > B*sqrt(C) + A > sage: eigval.operands()[0].operands()[1].operands()[0] > C > > At least it is using the things specific to symbolic expressions.
Thanks, this is certainly better than the previous option! It still feels a bit clumsy to me though. > > > 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?) > > This seems intriguing, but I have no idea if it's possible. Me neither, but it would seem to me (with no deep understanding of either Python or Sage...) that it's a question of defining the __getitem__ method to return the corresponding element by iterating. Perhaps this is just hopelessly naive though, I'm afraid I have no understanding of the undoubted complexity of the symbolic objects. David. > > - kcrisman -- To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org
