Thanks so much, Felipe.
I appreciate the clarity of your response.
Your suggestion works perfectly, and points out that I should be
reading up on Python as I try to train myself in Sage.

Cheers,
Drew


On Feb 15, 3:16 pm, Luiz Felipe Martins <[email protected]>
wrote:
> The following works:
>
> sage: y=var('y')
> sage: z=var('z')
> sage: solns = solve( [x+y+z==5, y+z==3, x+z==1], x,y,z)
> sage: print solns
> sage: solution = solns[0]
> sage: print solution
> sage: ( x + y + z ).subs_expr(*solution)
> [
> [x == 2, y == 4, z == -1]
> ]
> [x == 2, y == 4, z == -1]
> 5
>
> There are two points to notice:
>
> 1) The ouput of solve is a list of the solutions found, where each
> solution itself is a list of expressions. So, we have to "pick" the
> one we want. In this case there is only one solution, so solution =
> solns[0] gets the solution we want.
>
> 2) In Python, the special notation
>       f(*some_list)
> where some_list is a list, is used to "unpack" the list and pass its
> elements to the function f.
>
> For example, here is a function that returns the sum of its arguments:
>
> sage: def f(*args):
> ...       return sum(args)
> ...
> sage: f(2,3,5,7)
> 17
>
> Now suppose the elements you want to add are in a list. The function
> expects a bunch of arguments that can be added, so that if you simply
> give it the list, it fails:
>
> sage: lst = [2,3,5,7]
> sage: f(lst)
> Traceback (most recent call last):
> ...
> TypeError: unsupported operand type(s) for +: 'int' and 'list'
>
> To "unwrap" the elements of the list, put an asterisk in front of it:
>
> sage: f(*lst)
> 17
> sage: ( x + y + z ).subs_expr(*solns)
> 5
>
> Sage is based on Python, and the * notation with list arguments is a
> common Python idiom. It may take a while to get used too, but is
> actually a great convenience that is not present in other languages.
>
> Part of the learning curve in learning Sage is that one must learn
> some Python to use it efficiently. The advantage is that using a
> "real" computer language like Python, Sage is more consistent than the
> scripting language in other CASes. For example, now you know you can
> use the * trick with any function that requires a variable number of
> inputs :-)
>
> On Sun, Feb 15, 2009 at 2:26 PM, [email protected]
>
>
>
> <[email protected]> wrote:
>
> > Greetings,
>
> > I am a completely new to SAGE as of a few days ago.  I have used Maple
> > and Mathematica for years, and it is easy to do what I am describing
> > below in those systems.  I assume it is also easy to do in sage, but I
> > have not been able to find it in the documentation.
>
> > Here's the story:
>
> > I am attempting to write a sage function where I need to solve a
> > system of equations, and then plug these solutions into an expression,
> > and then process the result further.
>
> > Since the solutions to the system come wrapped in brackets, and
> > subs_expr expects the substitution equations without any brackets
> > around them I do not see how to do this.
>
> > Here is a toy example in interactive mode:
> > ***********************************************************************
> > sage: y=var('y')
> > sage: z=var('z')
> > sage: solns = solve( [x+y+z==5, y+z==3, x+z==1], [x,y,z])
> > sage: solns
> > [[x == 2, y == 4, z == -1]]
> > sage: ( x + y + z ).subs_expr(solns)
> > ---------------------------------------------------------------------------
> > TypeError                                 Traceback (most recent call
> > last)
>
> > /Users/andrewsills/.sage/<ipython console> in <module>()
>
> > /Applications/sage/local/lib/python2.5/site-packages/sage/calculus/
> > calculus.pyc in subs_expr(self, *equations)
> >   3922         for x in equations:
> >   3923             if not isinstance(x, SymbolicEquation):
> > -> 3924                 raise TypeError, "each expression must be an
> > equation"
> >   3925         R = self.parent()
> >   3926         v = ','.join(['%s=%s'%(x.lhs()._maxima_init_(), x.rhs
> > ()._maxima_init_()) \
>
> > TypeError: each expression must be an equation
> > *****************************************************************************
> > However, if I manually cut and paste my solutions into the expression,
> > I get the desired result:
>
> > *******************************************************************
> > sage: ( x + y + z ).subs_expr(x == 2, y == 4, z == -1)
> > 5
> > *******************************************************************
>
> > Of course, I cannot manually cut and paste in the middle of a
> > function.
> > So, it would seem that I either need to find a way to, in effect,
> > remove the brackets that naturally occur in [[ x == 2, y == 4, z ==
> > -1 ]], or alternatively, find another way to substitute into ( x + y +
> > z ) where it is acceptable to have the brackets there.
>
> > I am sure this is extremely simplistic, and I appreciate your
> > patience.
>
> > Thanks,
> > Drew
>
> --
> "The main things which seem to me important on their own account, and
> not merely as means to other things, are knowledge, art, instinctive
> happiness, and relations of friendship or affection."
>    -Bertrand Russell
>
> L. Felipe Martins
> Department of Mathematics
> Cleveland State University
> [email protected]
--~--~---------~--~----~------------~-------~--~----~
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
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to