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.Wed, 15 Mar 2017 14:28:55 -0500How to simplify `solve` result ($r_i$ variables)?http://ask.sagemath.org/question/36948/how-to-simplify-solve-result-r_i-variables/ I want to solve a system of linear equations (more variables than equations):
var('a, b, c')
eqn = [a+b+c==4, a+b-c==5]
s = solve(eqn, a, b, c); s
The result is shown like this:
$\left[\left[a = -r_{39} + \frac{9}{2}, b = r_{39}, c =
\left(-\frac{1}{2}\right)\right]\right]$
But I would like to have a simplified representation, i.e., where $r_{39}$ in the first equation is replaced by $b$. Alternatively, is there a way to reset the counter of the $r_i$ variables? Each time I evaluate the code above, the counter is incremented.Wed, 15 Mar 2017 09:18:53 -0500http://ask.sagemath.org/question/36948/how-to-simplify-solve-result-r_i-variables/Answer by tmonteil for <p>I want to solve a system of linear equations (more variables than equations):</p>
<pre><code>var('a, b, c')
eqn = [a+b+c==4, a+b-c==5]
s = solve(eqn, a, b, c); s
</code></pre>
<p>The result is shown like this:</p>
<p>$\left[\left[a = -r_{39} + \frac{9}{2}, b = r_{39}, c =
\left(-\frac{1}{2}\right)\right]\right]$</p>
<p>But I would like to have a simplified representation, i.e., where $r_{39}$ in the first equation is replaced by $b$. Alternatively, is there a way to reset the counter of the $r_i$ variables? Each time I evaluate the code above, the counter is incremented.</p>
http://ask.sagemath.org/question/36948/how-to-simplify-solve-result-r_i-variables/?answer=36956#post-id-36956Note that your case is very particular since in general, there is no reason why the free parameters coincide with some of the variables.
It is probably not be what you are looking for, but here is a generic way to deal with systems of linear equations:
sage: M = matrix(QQ,[[1,1,1],[1,1,-1]])
sage: M
[ 1 1 1]
[ 1 1 -1]
sage: part_sol = M.solve_right(vector([4,5])) ; part_sol
(9/2, 0, -1/2)
sage: kern = M.right_kernel() ; kern
Vector space of degree 3 and dimension 1 over Rational Field
Basis matrix:
[ 1 -1 0]
sage: kernel_basis = kern.basis()
[
(1, -1, 0)
]
Then the solution set is the sum of `part_sol` with any linear combination of the elements of the basis of the kernel, in your case it is : `(9/2, 0, -1/2) + t*(1, -1, 0)` for any `t` in your field (i chosed $\mathbb{Q}$).
Wed, 15 Mar 2017 14:28:55 -0500http://ask.sagemath.org/question/36948/how-to-simplify-solve-result-r_i-variables/?answer=36956#post-id-36956Answer by dan_fulea for <p>I want to solve a system of linear equations (more variables than equations):</p>
<pre><code>var('a, b, c')
eqn = [a+b+c==4, a+b-c==5]
s = solve(eqn, a, b, c); s
</code></pre>
<p>The result is shown like this:</p>
<p>$\left[\left[a = -r_{39} + \frac{9}{2}, b = r_{39}, c =
\left(-\frac{1}{2}\right)\right]\right]$</p>
<p>But I would like to have a simplified representation, i.e., where $r_{39}$ in the first equation is replaced by $b$. Alternatively, is there a way to reset the counter of the $r_i$ variables? Each time I evaluate the code above, the counter is incremented.</p>
http://ask.sagemath.org/question/36948/how-to-simplify-solve-result-r_i-variables/?answer=36953#post-id-36953Restarting the session also resets the $r_j$ Expression instances. The code cannot produce the solution in the form $b=b$ instead of $b=r_{39}$, and if it would, then i would like the other version.
If we know which variables are relevant, we could try to solve then only with respect to those variables. For instance:
sage: var('a,b,c');
sage: solve( [ a+b+c == 4, a+b-c == 5], [a, b] )
[]
sage: solve( [ a+b+c == 4, a+b-c == 5], [a, c] )
[[a == -b + 9/2, c == (-1/2)]]
sage: solve( [ a+b+c == 4, a+b-c == 5], [b, c] )
[[b == -a + 9/2, c == (-1/2)]]
Or the extended matrix of the system may give the necessary information.
A more fancy way to get the "relevant equations" would be to use the ideal generated by the equations, for instance:
sage: R.<a,b,c> = PolynomialRing( QQ )
sage: R
Multivariate Polynomial Ring in a, b, c over Rational Field
sage: J = R.ideal( [ a+b+c - 4, a+b-c -5 ] )
sage: J.groebner_basis()
[a + b - 9/2, c + 1/2]
and the above result can be digested as follows: The given equations $a+b+c - 4=0$, $a+b-c -5=0$ are equivalent with the two (simpler) equations
$a + b - 9/2=0$, $c + 1/2=0$.
Wed, 15 Mar 2017 13:10:53 -0500http://ask.sagemath.org/question/36948/how-to-simplify-solve-result-r_i-variables/?answer=36953#post-id-36953