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, 15 Apr 2013 05:02:48 -0500solve with "excess" equationshttp://ask.sagemath.org/question/10028/solve-with-excess-equations/why does the following work
solve([a+b-1,a-b],[a,b])
but this
solve([a+b-1,a-b,c+d],[a,b])
gives an empty solution?
Can "solve" be convinced to ignore unneccassary equations?Mon, 15 Apr 2013 02:54:19 -0500http://ask.sagemath.org/question/10028/solve-with-excess-equations/Answer by slelievre for <p>why does the following work</p>
<pre><code>solve([a+b-1,a-b],[a,b])
</code></pre>
<p>but this</p>
<pre><code>solve([a+b-1,a-b,c+d],[a,b])
</code></pre>
<p>gives an empty solution?</p>
<p>Can "solve" be convinced to ignore unneccassary equations?</p>
http://ask.sagemath.org/question/10028/solve-with-excess-equations/?answer=14790#post-id-14790You could solve for `a`, `b`, `c`, `d` and then ignore `c` and `d`.
sage: a, b, c, d = var('a b c d')
sage: solns = solve([a+b-1,a-b,c+d],[a,b,c,d]); solns
[[a == (1/2), b == (1/2), c == -r1, d == r1]]
sage: [s[:2] for s in solns]
[[a == (1/2), b == (1/2)]]
Or you can tweak `solve` to only use the equations which involve the variables
that you want to solve for.
sage: def smart_solve(eqns, vars):
....: return solve([eqn for eqn in eqns if
....: any(v in eqn.variables() for v in vars)], vars)
....:
sage: smart_solve([a+b-1,a-b,c+d],[a,b])
[[a == (1/2), b == (1/2)]]
I don't know what would be the pros and cons of having Sage's `solve` behave
in this way (either by default or as an option).
Mon, 15 Apr 2013 03:51:48 -0500http://ask.sagemath.org/question/10028/solve-with-excess-equations/?answer=14790#post-id-14790Comment by andre for <p>You could solve for <code>a</code>, <code>b</code>, <code>c</code>, <code>d</code> and then ignore <code>c</code> and <code>d</code>.</p>
<pre><code>sage: a, b, c, d = var('a b c d')
sage: solns = solve([a+b-1,a-b,c+d],[a,b,c,d]); solns
[[a == (1/2), b == (1/2), c == -r1, d == r1]]
sage: [s[:2] for s in solns]
[[a == (1/2), b == (1/2)]]
</code></pre>
<p>Or you can tweak <code>solve</code> to only use the equations which involve the variables
that you want to solve for.</p>
<pre><code>sage: def smart_solve(eqns, vars):
....: return solve([eqn for eqn in eqns if
....: any(v in eqn.variables() for v in vars)], vars)
....:
sage: smart_solve([a+b-1,a-b,c+d],[a,b])
[[a == (1/2), b == (1/2)]]
</code></pre>
<p>I don't know what would be the pros and cons of having Sage's <code>solve</code> behave
in this way (either by default or as an option).</p>
http://ask.sagemath.org/question/10028/solve-with-excess-equations/?comment=17896#post-id-17896Thank you, I kind of went the "smart_solve" way, it just meant yet another iteration of ifs and fors in my application which I'd have liked to avoid. I had expected "solve" to be a bit smarter by itself but perhaps there would be too many implications if you'd expect a full test for solvability. Perhaps an error would be better instead of an empty solution though.Mon, 15 Apr 2013 05:02:48 -0500http://ask.sagemath.org/question/10028/solve-with-excess-equations/?comment=17896#post-id-17896