ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 06 Nov 2017 17:56:01 +0100How can I get back an expression for free variables in solve function.https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/I have a number of linear equations on some symbols A,B,C,D,E,F,G. When I solve them in Sagemath, I get some free variables in the solution.
$A,B,C,D,E,F,G=var('A,B,C,D,E,F,G')$
$eqns=[A+B==0,C+D+E==0,F+G==0,A+E-F==0]$
$solution=solve(eqns,A,B,C,D,E,F,G)$
Sage gives the following solution:
$[[A == r1, B == -r1, C == r1 - r2 - r3, D == r3, E == -r1 + r2, F == r2, G == -r2]]$
Now I can ask Sage to give me an expression of any combination of the symbols, for example:
$(A+C).subs(solution)$, then I get $2*r1 - r2 - r3$.
For my purpose I would now like to have an expression for any of the free variables. If I assign $r1=var('r1')$ and ask Sage $s1.subs(solution)$ I get back $r1$ again. But I would lik to get back an expression in terms of $A,B,C,D,E,F,G$.
Any suggestions on how to do this?Mon, 06 Nov 2017 11:29:40 +0100https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/Comment by TheBeiram for <p>I have a number of linear equations on some symbols A,B,C,D,E,F,G. When I solve them in Sagemath, I get some free variables in the solution. </p>
<p>$A,B,C,D,E,F,G=var('A,B,C,D,E,F,G')$</p>
<p>$eqns=[A+B==0,C+D+E==0,F+G==0,A+E-F==0]$</p>
<p>$solution=solve(eqns,A,B,C,D,E,F,G)$</p>
<p>Sage gives the following solution:
$[[A == r1, B == -r1, C == r1 - r2 - r3, D == r3, E == -r1 + r2, F == r2, G == -r2]]$</p>
<p>Now I can ask Sage to give me an expression of any combination of the symbols, for example:
$(A+C).subs(solution)$, then I get $2*r1 - r2 - r3$.
For my purpose I would now like to have an expression for any of the free variables. If I assign $r1=var('r1')$ and ask Sage $s1.subs(solution)$ I get back $r1$ again. But I would lik to get back an expression in terms of $A,B,C,D,E,F,G$.
Any suggestions on how to do this?</p>
https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/?comment=39400#post-id-39400Yes, I can see that. And I would just like to have one of these solutions, but probably this is not possible. The reason I am looking for this is that I have a C-vectorspace defined over some symbols (in this case, just a random example, the symbols A,B,C,D,E,F,G) and I want to quotient this space by a space spanned by some linear combinations of these symbols (in this case A+B==0, etc). When I did this by hand, I used the solved function, and could then see that a basis for my quotient space would be <A,-G,D>. To implement this process, I thought of also using the solve function, but it needs to give me an expression for the free variables. But probably this is not really the right approach :)Mon, 06 Nov 2017 14:38:21 +0100https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/?comment=39400#post-id-39400Comment by B r u n o for <p>I have a number of linear equations on some symbols A,B,C,D,E,F,G. When I solve them in Sagemath, I get some free variables in the solution. </p>
<p>$A,B,C,D,E,F,G=var('A,B,C,D,E,F,G')$</p>
<p>$eqns=[A+B==0,C+D+E==0,F+G==0,A+E-F==0]$</p>
<p>$solution=solve(eqns,A,B,C,D,E,F,G)$</p>
<p>Sage gives the following solution:
$[[A == r1, B == -r1, C == r1 - r2 - r3, D == r3, E == -r1 + r2, F == r2, G == -r2]]$</p>
<p>Now I can ask Sage to give me an expression of any combination of the symbols, for example:
$(A+C).subs(solution)$, then I get $2*r1 - r2 - r3$.
For my purpose I would now like to have an expression for any of the free variables. If I assign $r1=var('r1')$ and ask Sage $s1.subs(solution)$ I get back $r1$ again. But I would lik to get back an expression in terms of $A,B,C,D,E,F,G$.
Any suggestions on how to do this?</p>
https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/?comment=39399#post-id-39399I am not sure to fully understand your question, but I also think that it is not well-defined. In your example, you want to express `r1` in terms of the variables `A` to `G`. But you can write `r1` in several manners: `r1 = A` works, as well as `r1 = -B` or `r1 = C + D - F`, etc. So it is not clear to me which answer you are looking for.Mon, 06 Nov 2017 14:29:14 +0100https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/?comment=39399#post-id-39399Comment by B r u n o for <p>I have a number of linear equations on some symbols A,B,C,D,E,F,G. When I solve them in Sagemath, I get some free variables in the solution. </p>
<p>$A,B,C,D,E,F,G=var('A,B,C,D,E,F,G')$</p>
<p>$eqns=[A+B==0,C+D+E==0,F+G==0,A+E-F==0]$</p>
<p>$solution=solve(eqns,A,B,C,D,E,F,G)$</p>
<p>Sage gives the following solution:
$[[A == r1, B == -r1, C == r1 - r2 - r3, D == r3, E == -r1 + r2, F == r2, G == -r2]]$</p>
<p>Now I can ask Sage to give me an expression of any combination of the symbols, for example:
$(A+C).subs(solution)$, then I get $2*r1 - r2 - r3$.
For my purpose I would now like to have an expression for any of the free variables. If I assign $r1=var('r1')$ and ask Sage $s1.subs(solution)$ I get back $r1$ again. But I would lik to get back an expression in terms of $A,B,C,D,E,F,G$.
Any suggestions on how to do this?</p>
https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/?comment=39401#post-id-39401You may use the `VectorSpace` constructions (I do not know this well...) :
sage: v = VectorSpace(CC, 7)
sage: A,B,C,D,E,F,G = v.basis()
sage: s = v.subspace([A+B, C+D+E, F+G, A+E-F])
sage: q = v.quotient(s)
sage: [q.lift(x) for x in q.basis()]
[(1.00000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000),
(0.000000000000000, 0.000000000000000, 1.00000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000),
(0.000000000000000, 0.000000000000000, 0.000000000000000, 1.00000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000)]
See more [in the doc](http://doc.sagemath.org/html/en/reference/modules/sage/modules/with_basis/subquotient.html).Mon, 06 Nov 2017 15:12:28 +0100https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/?comment=39401#post-id-39401Comment by TheBeiram for <p>I have a number of linear equations on some symbols A,B,C,D,E,F,G. When I solve them in Sagemath, I get some free variables in the solution. </p>
<p>$A,B,C,D,E,F,G=var('A,B,C,D,E,F,G')$</p>
<p>$eqns=[A+B==0,C+D+E==0,F+G==0,A+E-F==0]$</p>
<p>$solution=solve(eqns,A,B,C,D,E,F,G)$</p>
<p>Sage gives the following solution:
$[[A == r1, B == -r1, C == r1 - r2 - r3, D == r3, E == -r1 + r2, F == r2, G == -r2]]$</p>
<p>Now I can ask Sage to give me an expression of any combination of the symbols, for example:
$(A+C).subs(solution)$, then I get $2*r1 - r2 - r3$.
For my purpose I would now like to have an expression for any of the free variables. If I assign $r1=var('r1')$ and ask Sage $s1.subs(solution)$ I get back $r1$ again. But I would lik to get back an expression in terms of $A,B,C,D,E,F,G$.
Any suggestions on how to do this?</p>
https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/?comment=39402#post-id-39402Yes that sounds nice. Any idea how to get rid of the many zeros?Mon, 06 Nov 2017 16:36:26 +0100https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/?comment=39402#post-id-39402Comment by B r u n o for <p>I have a number of linear equations on some symbols A,B,C,D,E,F,G. When I solve them in Sagemath, I get some free variables in the solution. </p>
<p>$A,B,C,D,E,F,G=var('A,B,C,D,E,F,G')$</p>
<p>$eqns=[A+B==0,C+D+E==0,F+G==0,A+E-F==0]$</p>
<p>$solution=solve(eqns,A,B,C,D,E,F,G)$</p>
<p>Sage gives the following solution:
$[[A == r1, B == -r1, C == r1 - r2 - r3, D == r3, E == -r1 + r2, F == r2, G == -r2]]$</p>
<p>Now I can ask Sage to give me an expression of any combination of the symbols, for example:
$(A+C).subs(solution)$, then I get $2*r1 - r2 - r3$.
For my purpose I would now like to have an expression for any of the free variables. If I assign $r1=var('r1')$ and ask Sage $s1.subs(solution)$ I get back $r1$ again. But I would lik to get back an expression in terms of $A,B,C,D,E,F,G$.
Any suggestions on how to do this?</p>
https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/?comment=39403#post-id-39403Do you mean the trailing zeroes in each value? Using the field `CC`, this is the standard display. There has been [an attempt](https://trac.sagemath.org/ticket/7682) to make this standard display customizable but it never converged. Two options for you if you want a different way of displaying the results: Either you work over another field, such as `CDF` (Complex Double Field) or `CIF` (Complex Interval Field) which have different behaviors, including a different display. Or you print yourself the result, using [`format`](https://docs.python.org/2/library/string.html#format-specification-mini-language). For instance, if you have the element `x = 1.00000000000000`, `print("{:.4}".format(x))` prints `x` as a string of length 4, that is `1.00`.Mon, 06 Nov 2017 17:04:10 +0100https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/?comment=39403#post-id-39403Comment by TheBeiram for <p>I have a number of linear equations on some symbols A,B,C,D,E,F,G. When I solve them in Sagemath, I get some free variables in the solution. </p>
<p>$A,B,C,D,E,F,G=var('A,B,C,D,E,F,G')$</p>
<p>$eqns=[A+B==0,C+D+E==0,F+G==0,A+E-F==0]$</p>
<p>$solution=solve(eqns,A,B,C,D,E,F,G)$</p>
<p>Sage gives the following solution:
$[[A == r1, B == -r1, C == r1 - r2 - r3, D == r3, E == -r1 + r2, F == r2, G == -r2]]$</p>
<p>Now I can ask Sage to give me an expression of any combination of the symbols, for example:
$(A+C).subs(solution)$, then I get $2*r1 - r2 - r3$.
For my purpose I would now like to have an expression for any of the free variables. If I assign $r1=var('r1')$ and ask Sage $s1.subs(solution)$ I get back $r1$ again. But I would lik to get back an expression in terms of $A,B,C,D,E,F,G$.
Any suggestions on how to do this?</p>
https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/?comment=39405#post-id-39405Great, thank you for your help!Mon, 06 Nov 2017 17:56:01 +0100https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/?comment=39405#post-id-39405Answer by ndomes for <p>I have a number of linear equations on some symbols A,B,C,D,E,F,G. When I solve them in Sagemath, I get some free variables in the solution. </p>
<p>$A,B,C,D,E,F,G=var('A,B,C,D,E,F,G')$</p>
<p>$eqns=[A+B==0,C+D+E==0,F+G==0,A+E-F==0]$</p>
<p>$solution=solve(eqns,A,B,C,D,E,F,G)$</p>
<p>Sage gives the following solution:
$[[A == r1, B == -r1, C == r1 - r2 - r3, D == r3, E == -r1 + r2, F == r2, G == -r2]]$</p>
<p>Now I can ask Sage to give me an expression of any combination of the symbols, for example:
$(A+C).subs(solution)$, then I get $2*r1 - r2 - r3$.
For my purpose I would now like to have an expression for any of the free variables. If I assign $r1=var('r1')$ and ask Sage $s1.subs(solution)$ I get back $r1$ again. But I would lik to get back an expression in terms of $A,B,C,D,E,F,G$.
Any suggestions on how to do this?</p>
https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/?answer=39404#post-id-39404Are you looking for something like [this](http://sagecell.sagemath.org/?q=pyjiax) ?Mon, 06 Nov 2017 17:49:28 +0100https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/?answer=39404#post-id-39404