Ask Your Question

Two questions about parameters in solutions

asked 2014-02-05 17:04:20 -0500

Alasdair gravatar image

In the Sage documentation, this example is given:

 sols = solve([x+y == 3, 2*x+2*y == 6],x,y); sols

of a solution which includes an extra parameter:

  1. If I run this comand again, the solution is given with r2, then again with r3 etc. Is it possible to "reset" the parameter number?
  2. How does one substitute a particular value for this parameter? I can do it by iterating through the list and using rhs():

    [i.rhs().subs(r1=1/2) for i in sols]

    or by setting solution_dict=True and fiddling another way:

    [sols[i].subs(r1=1/2) for i in [x,y]]

Neither of these seem particularly elegant - is there a more "natural" way?

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2014-02-06 02:11:41 -0500

kcrisman gravatar image

Tab-completion was my friend for this:

sage: var("y")
sage: sols = solve([x+y == 3, 2*x+2*y == 6],x,y); sols
[[x == -r1 + 3, y == r1]]
sage: sols = solve([x+y == 3, 2*x+2*y == 6],x,y); sols
[[x == -r2 + 3, y == r2]]
sage: maxima_calculus.reset()
sage: sols = solve([x+y == 3, 2*x+2*y == 6],x,y); sols
[[x == -r1 + 3, y == r1]]

Note that you may get different things in the reset depending on how many computations you've done already. I suppose one could even go into the depths of the lisp interface and reset just %rnum and %rnumlist but I didn't want to do that.

Unfortunately, I don't think there is any more elegant way to do what you want - the solutions are lists by design. In some sense this is a feature, not a bug... though one it would be nice to do more easily. Maybe map is your friend here, though I doubt it would be fewer characters to type.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-02-05 17:04:20 -0500

Seen: 68 times

Last updated: Feb 06 '14