ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 27 Nov 2017 02:50:26 -0600Use `solve`output to create a functionhttp://ask.sagemath.org/question/39802/use-solveoutput-to-create-a-function/Hi there,
I want to use the ouptut of `solve` to create a function from it.
My system depends on some parameter, say $a$, it is a tiny linear system $2\times 2$, and I know there is always a solution. The right-hand term also contains some $a$ into it. What I want to do is to use the first and the second component to buy functions, calle them `x1(a)` and `x2(a)` that I will call later. I tried something like
`var("v1, v2, a")`
`Sols = solve([v1+a*v2 == a, -a*v1 + v2 ==0],[v1,v2])`
`x1(a) = Sols[0][0].rhs()`
`x2(a) = Sols[0][1].rhs()`
This gives the expected expressions for `x1` and `x2` but I cannot use these to do further computations, for instance, using `x1(1)` wont evaluate the expression when $a=1$, as I would expect.
The very precise example above is not what I actually want to handle, my actual system is actually more complex.
Many thanks!Sun, 26 Nov 2017 14:25:48 -0600http://ask.sagemath.org/question/39802/use-solveoutput-to-create-a-function/Comment by Emmanuel Charpentier for <p>Hi there,</p>
<p>I want to use the ouptut of <code>solve</code> to create a function from it.
My system depends on some parameter, say $a$, it is a tiny linear system $2\times 2$, and I know there is always a solution. The right-hand term also contains some $a$ into it. What I want to do is to use the first and the second component to buy functions, calle them <code>x1(a)</code> and <code>x2(a)</code> that I will call later. I tried something like </p>
<p><code>var("v1, v2, a")</code></p>
<p><code>Sols = solve([v1+a*v2 == a, -a*v1 + v2 ==0],[v1,v2])</code></p>
<p><code>x1(a) = Sols[0][0].rhs()</code></p>
<p><code>x2(a) = Sols[0][1].rhs()</code></p>
<p>This gives the expected expressions for <code>x1</code> and <code>x2</code> but I cannot use these to do further computations, for instance, using <code>x1(1)</code> wont evaluate the expression when $a=1$, as I would expect. </p>
<p>The very precise example above is not what I actually want to handle, my actual system is actually more complex.</p>
<p>Many thanks!</p>
http://ask.sagemath.org/question/39802/use-solveoutput-to-create-a-function/?comment=39810#post-id-39810Works ditto for me on Sage 8.0.Mon, 27 Nov 2017 02:50:26 -0600http://ask.sagemath.org/question/39802/use-solveoutput-to-create-a-function/?comment=39810#post-id-39810Comment by tmonteil for <p>Hi there,</p>
<p>I want to use the ouptut of <code>solve</code> to create a function from it.
My system depends on some parameter, say $a$, it is a tiny linear system $2\times 2$, and I know there is always a solution. The right-hand term also contains some $a$ into it. What I want to do is to use the first and the second component to buy functions, calle them <code>x1(a)</code> and <code>x2(a)</code> that I will call later. I tried something like </p>
<p><code>var("v1, v2, a")</code></p>
<p><code>Sols = solve([v1+a*v2 == a, -a*v1 + v2 ==0],[v1,v2])</code></p>
<p><code>x1(a) = Sols[0][0].rhs()</code></p>
<p><code>x2(a) = Sols[0][1].rhs()</code></p>
<p>This gives the expected expressions for <code>x1</code> and <code>x2</code> but I cannot use these to do further computations, for instance, using <code>x1(1)</code> wont evaluate the expression when $a=1$, as I would expect. </p>
<p>The very precise example above is not what I actually want to handle, my actual system is actually more complex.</p>
<p>Many thanks!</p>
http://ask.sagemath.org/question/39802/use-solveoutput-to-create-a-function/?comment=39806#post-id-39806Works for me on Sage 8.1.rc2:
sage: var("v1, v2, a")
....: Sols = solve([v1+a*v2 == a, -a*v1 + v2 ==0],[v1,v2])
....: x1(a) = Sols[0][0].rhs()
....: x2(a) = Sols[0][1].rhs()
(v1, v2, a)
sage: x1(2)
2/5
sage: x1(1)
1/2Sun, 26 Nov 2017 16:03:15 -0600http://ask.sagemath.org/question/39802/use-solveoutput-to-create-a-function/?comment=39806#post-id-39806