On Thu, Aug 21, 2008 at 10:47 AM, Fernando Perez <[EMAIL PROTECTED]> wrote:
>
> On Wed, Aug 20, 2008 at 9:37 PM, mhampton <[EMAIL PROTECTED]> wrote:
>>
>> I used to really enjoy writing programs in mathematica, but maybe I'm
>> a strange person.  I only stopped in order to force myself to get
>> fluent with Sage.  I think it just depends on your background, what
>> you are used to, and what you want to do.  For symbolic calculations
>> and programming I still miss mathematica quite often.
>>
>> -M. Hampton
>>
>> On Aug 20, 12:59 am, "Fernando Perez" <[EMAIL PROTECTED]> wrote:
>>> On Tue, Aug 19, 2008 at 1:05 PM, Ondrej Certik <[EMAIL PROTECTED]> wrote:
>>> > but
>>> > programming in Mathematica is not fun.
>>>
>>> And that would be the understatement of the week.
>
> I don't think you're strange, I was just joking a bit.  Caveat: it's
> been years that I've *programmed* in Mathematica in a serious way
> (recently I've mostly used it as a fancy calculator for quickly
> computing integrals I'm too lazy to do  by hand).  But having said
> that, what I really found Mathematica programming to be was a very
> mixed experience: certain things that should be easy were
> unjustifiably tricky to achieve, even after consulting with local
> hard-core gurus.
>
> But the elegance with which you can  manipulate expressions
> *structurally* is truly something to behold. This comes courtesy of
> the lisp-like facilities you have for accessing any entity, working on
> it as an expression tree you  can manipulate with very powerful
> transformation rules. And there are classes of problems where that
> approach allows you to get pretty much exactly the solution you're
> after with a minimum amount of fuss, where as in more conventional
> languages it would be a lot of work.
>
> So there, I hope this is a slightly more reasoned reply to a topic
> worthy of an honest answer ;)

I need to learn Mathematica to understand what it means. In SymPy, you
can do some fancy stuff as well, e.g.:

In [1]: e = x*y+sin(x*y)**3

In [2]: print e
x*y + sin(x*y)**3

In [3]: from sympy.utilities.iterables import postorder_traversal,
preorder_traversal

In [4]: postorder_traversal(e)
Out[4]: <generator object at 0xa53376c>

In [9]: print list(postorder_traversal(e))
[x, y, x*y, sin(x*y), 3, sin(x*y)**3, x, y, x*y, x*y + sin(x*y)**3]

In [10]: print list(preorder_traversal(e))
[x*y + sin(x*y)**3, sin(x*y)**3, sin(x*y), x*y, x, y, 3, x*y, x, y]

And here is how those two functions are implemented:

def preorder_traversal(node):
    yield node
    for arg in node.args:
        for subtree in preorder_traversal(arg):
            yield subtree

def postorder_traversal(node):
    for arg in node.args:
        for subtree in postorder_traversal(arg):
            yield subtree
    yield node



So you can access all expressions uniformly and iterate over the
expression tree in 4 lines by hand, or just calling one of the
predefined functions above. I don't think this is complicated, but
maybe things can be made even easier.


Ondrej

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to