# Solving Simultaneous Equations: Excluding Imaginary Numbers

Hello all,

I am working on a Calculus problem where you have to find the critical points of a function f(x,y,z) subject to a constraint function c(x,y,z) = constant. I managed to use Sagemath to output the answers, but it is including answers that have complex numbers. How can I exclude the answers with complex numbers?

Here's the code I have written:

var('x y z u p', domain='real')
f = x^6 + y^6 + z^6
c = x^2 + y^2 + z^2
c1 = c == 6
fx = f.diff(x)
fy = f.diff(y)
fz = f.diff(z)
cx = u*c.diff(x)
cy = u*c.diff(y)
cz = u*c.diff(z)
eq1 = fx - cx == 0
eq2 = fy - cy == 0
eq3 = fz - cz == 0

edit retag close merge delete

Sort by ยป oldest newest most voted

The assumption mechanism for symbolic expression is kind of broken, but you can select the real values as follows:

sage: [answer for answer in answers if all([value.rhs() in RDF for value in answer])]
[[x == -sqrt(6), y == 0, z == 0, u == 108],
[x == sqrt(6), y == 0, z == 0, u == 108],
[x == 0, y == -sqrt(6), z == 0, u == 108],
[x == 0, y == sqrt(6), z == 0, u == 108],
[x == -sqrt(3), y == -sqrt(3), z == 0, u == 27],
[x == sqrt(3), y == sqrt(3), z == 0, u == 27],
[x == -sqrt(3), y == sqrt(3), z == 0, u == 27],
[x == sqrt(3), y == -sqrt(3), z == 0, u == 27],
[x == 0, y == 0, z == -sqrt(6), u == 108],
[x == 0, y == 0, z == sqrt(6), u == 108],
[x == -sqrt(3), y == 0, z == -sqrt(3), u == 27],
[x == sqrt(3), y == 0, z == sqrt(3), u == 27],
[x == -sqrt(3), y == 0, z == sqrt(3), u == 27],
[x == sqrt(3), y == 0, z == -sqrt(3), u == 27],
[x == 0, y == -sqrt(3), z == sqrt(3), u == 27],
[x == 0, y == sqrt(3), z == -sqrt(3), u == 27],
[x == 0, y == -sqrt(3), z == -sqrt(3), u == 27],
[x == 0, y == sqrt(3), z == sqrt(3), u == 27],
[x == -sqrt(2), y == -sqrt(2), z == -sqrt(2), u == 12],
[x == sqrt(2), y == sqrt(2), z == sqrt(2), u == 12],
[x == -sqrt(2), y == -sqrt(2), z == sqrt(2), u == 12],
[x == sqrt(2), y == sqrt(2), z == -sqrt(2), u == 12],
[x == -sqrt(2), y == sqrt(2), z == -sqrt(2), u == 12],
[x == sqrt(2), y == -sqrt(2), z == sqrt(2), u == 12],
[x == -sqrt(2), y == sqrt(2), z == sqrt(2), u == 12],
[x == sqrt(2), y == -sqrt(2), z == -sqrt(2), u == 12]]


Also, since you computation involves only polynomials, why not use them directly (here i work with real algebraic numbers) ?

sage: R.<x,y,z,u> = AA[]
sage: P1 = R(eq1.lhs())
sage: P2 = R(eq2.lhs())
sage: P3 = R(eq3.lhs())
sage: P4 = R(c1.lhs()-c1.rhs())
sage: I = R.ideal([P1,P2,P3,P4])
sage: I
Ideal (6*x^5 - 2*x*u, 6*y^5 - 2*y*u, 6*z^5 - 2*z*u, x^2 + y^2 + z^2 - 6) of Multivariate Polynomial Ring in x, y, z, u over Algebraic Real Field
sage: I.dimension()
sage: V = I.variety()
sage: V
[{x: -1.414213562373095?, z: -1.414213562373095?, u: 12, y: -1.414213562373095?},
{x: 1.414213562373095?, z: -1.414213562373095?, u: 12, y: -1.414213562373095?},
{x: -1.414213562373095?, z: -1.414213562373095?, u: 12, y: 1.414213562373095?},
{x: 1.414213562373095?, z: -1.414213562373095?, u: 12, y: 1.414213562373095?},
{x: -1.414213562373095?, z: 1.414213562373095?, u: 12, y: -1.414213562373095?},
{x: 1.414213562373095?, z: 1.414213562373095?, u: 12, y: -1.414213562373095?},
{x: -1.414213562373095?, z: 1.414213562373095?, u: 12, y: 1.414213562373095?},
{x: 1.414213562373095?, z: 1.414213562373095?, u: 12, y: 1.414213562373095?},
{x: 0, z: -1.732050807568878?, u: 27, y: -1 ...
more

Thank you for your help. Could you please explain what's happening in the following line?

If you have time to walk through the logic of each for, in, and if, I would really appreciate it. I have some experience programming, but I'm still a novice with Sage.

As far as polynomials go, all the keywords that you need to "set them up" correctly seems kind of daunting. What is the advantage to using polynomials? Actually, what does it mean to use polynomials instead of, err, normal math?

( 2015-12-01 00:25:10 +0100 )edit

For explanation about list comprehension and the use of all(), you can have a look at https://docs.python.org/2/tutorial/da... and https://docs.python.org/2/library/fun... also rhs() stands for "right-hand side" of the symbolic equality.

( 2015-12-02 17:56:14 +0100 )edit

all( (value.rhs() in RDF) for value in answer)


It's a trick in python that passes an "iterator" to "all" rather than a fully constructed list. In principle it's a little more efficient because it can abandon computations as soon as it finds the first "false" value. Furthermore, in python2 it has the advantage that the variable value doesn't get clobbered in the global scope.

( 2015-12-02 19:27:55 +0100 )edit

@NickS: as for your second question, the solve()function is much less reliable than the one relying on polynomials, see http://trac.sagemath.org/wiki/symboli...

( 2015-12-03 13:31:08 +0100 )edit