Ask Your Question
2

solve with "excess" equations

asked 2013-04-15 09:54:19 +0200

andre gravatar image

updated 2020-08-07 13:13:50 +0200

slelievre gravatar image

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 unnecessary equations?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2013-04-15 10:51:48 +0200

slelievre gravatar image

updated 2020-08-07 13:15:13 +0200

You 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).

edit flag offensive delete link more

Comments

Thank 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.

andre gravatar imageandre ( 2013-04-15 12:02:48 +0200 )edit
1

answered 2020-08-08 01:12:05 +0200

rburing gravatar image

Excess variables are interpreted as parameters, and only solutions that are valid for all values of the parameters are sought. This explains the empty list in the second case.

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

Stats

Asked: 2013-04-15 09:54:19 +0200

Seen: 460 times

Last updated: Aug 07 '20