Dear Nils,
That's great, thanks! For some reason, I overlooked the utility of
operands() when I was trying things out. This seems perfectly adequate
for my purposes. The suggested extension of operands() to take a syntax
like operands([0,1]) would be nice, too! +1 from me if this counts.
You are right, (v + w)[1] should not return w, so I am withdrawing my
original proposal.
Cheers
Stan
On 10/02/10 17:36, Nils Bruin wrote:
On Feb 10, 1:39 am, Stan Schymanski<[email protected]> wrote:
Dear all,
I suppose that the below is not possible yet, so I am forwarding this
to sage-devel. I think it would be great if one was able to access
parts of an expression in a nested way, e.g.
expr1 = (a*x^3 + b*x)/(1 - Sqrt((1 - x)/(x - c))) + c/x
expr1[0]
(a*x^3 + b*x)/(1 - Sqrt((1 - x)/(x - c)))
expr1[1]
c/x
expr1[0][0]
(a*x^3 + b*x)
I'm not sure you want to reserve indexing notation for that purpose,
since it can have legitimate mathematical roles inside the expression
already: If you are doing vector calculus then v[1] might make perfect
sense. By extension, if you want the second coordinate of a vector sum
v+w, then one would write (v+w)[1]. With the proposed extension this
would be "w" instead, which would probably surprise the user.
You can access expressions in a nested way already, but you have to
accept a bit more pythonic notation:
expr.operands()[0]
expr.operands()[1]
expr.operands()[0].operands()[1]
It might make sense to introduce a shorthand
expr.operand(0)
expr.operand(1)
expr.operand(0).operand(1)
or, if one finds it's a frequent usage pattern, abbreviate the last to
expr.operand([0,1])
maple has such a routine, called "op".
beware that any code that makes assumptions about what it finds
several levels deep in an expression is going to be very fragile.
--
To post to this group, send an email to [email protected]
To unsubscribe from this group, send an email to
[email protected]
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org