You want to use tuple unpacking.  If you pass a list or tuple to a function in 
Python and put a '*' before it, it "unpacks" the elements of the tuple into 
arguments of the function.  For example:

In [4]: a = [1, 2, 3]

In [5]: def f(a, b, c):
   ...:     return a + b + c
   ...: 

In [6]: f(*a)
Out[6]: 6

You may also need to use something like

In [7]: a[1:]
Out[7]: [2, 3]

to only get the arguments and not the function.

Now, one thing you should be aware of is that operator.add only supports two 
arguments.  However, SymPy's Add() supports multiple arguments.  Actually, 
don't know why you wouldn't use that anyway if you are going to create SymPy 
expressions:

In [8]: Add(x, y, z, 2)
Out[8]: 2 + x + y + z

Unfortunately, there is no such thing as Sub() in SymPy, but you could probably 
write some simple function to do it:

In [9]: def sub(*items):
   ...:     return Add(*[-i for i in items])
   ...: 

In [10]: sub(x, y, z, 2)
Out[10]: -2 - x - y - z

Aaron Meurer

On Dec 30, 2010, at 4:38 PM, Jeff wrote:

> Mateusz,
> 
> thanks for your quick reply.
> 
> Your suggestion handles this case well. If I have a function f has N
> children in the tree,
> 
> e.g. tree = ('add', 'x', ('f', 'a1', 'a2', 'a3', 'a4', ..., 'a100'))
> 
> how can I express mapping[f](a1, a2, ...an)? N here could be
> arbitrary and I don't know its value before hand.
> 
> 
> thanks a lot
> 
> Jeff
> 
> 
> On Dec 30, 5:35 pm, Mateusz Paprocki <matt...@gmail.com> wrote:
>> Hi,
>> 
>> On Thu, Dec 30, 2010 at 01:34:07PM -0800, Jeff Cen wrote:
>> 
>>> Hi,
>> 
>>> Suppose I have already had an expression tree in a tree structure
>>> (like a nested list, say ['add', 'x', ['subtract', 'x', 'y']]), I
>>> would like to convert it to sympy expression and simplify it in sympy.
>> 
>>> When I read my tree structure, I first create an Add function. Then
>>> how can I append x and the rest?
>> 
>> A simple helper function will do the job, e.g.:
>> 
>> In [1]: from sympy import *
>> 
>> In [2]: import operator
>> 
>> In [3]: mapping = {'add': operator.add, 'subtract': operator.sub}
>> 
>> In [4]: tree = ('add', 'x', ('subtract', 'x', 'y'))
>> 
>> In [5]: def convert(tree):
>>   ....:     op, a, b = tree
>>   ....:     if isinstance(a, tuple): a = convert(a)
>>   ....:     else: a = sympify(a)
>>   ....:     if isinstance(b, tuple): b = convert(b)
>>   ....:     else: b = sympify(b)
>>   ....:     return mapping[op](a, b)
>>   ....:
>> 
>> In [6]: convert(tree)
>> Out[6]: -y + 2⋅x
>> 
>>> thanks
>> 
>>> Jeff
>> 
>>> --
>>> You received this message because you are subscribed to the Google Groups 
>>> "sympy" group.
>>> To post to this group, send email to sy...@googlegroups.com.
>>> To unsubscribe from this group, send email to 
>>> sympy+unsubscr...@googlegroups.com.
>>> For more options, visit this group 
>>> athttp://groups.google.com/group/sympy?hl=en.
>> 
>> --
>> Mateusz
>> 
>>  signature.asc
>> < 1KViewDownload
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to 
> sympy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sympy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to 
sympy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to