Real solutions to a system of two equations in Sage

Viewed 332

I have the following code (SageMath 9.3) which determines the common solutions to a system of 2 equations (the first one represents a curve and the second one a horizontal line; the common solutions represent their intersection points):

x, y = var('x, y')
p1 = y^2 - x^3 + x - 1 == 0
p2 = y - 10 == 0
sols = solve([p1, p2], x, y)
sols

Output:

[[x == 4.698114754098361, y == 10.0],
 [x == (-2.349057376230034 + 3.943882816522595*I), y == 10.0],
 [x == (-2.349057376230034 - 3.943882816522595*I), y == 10.0]]

My question: How can I change this so I only get REAL solutions?

1 Answers

Intersection of an elliptic curve and a horizontal line

Here are three ways to find the intersection, or its x-coordinate. There might be more ways!

Using solve

One way is to find all complex solutions as you did:

sage: x, y = var('x, y')
sage: p1 = y^2 - x^3 + x - 1 == 0
sage: p2 = y - 10 == 0
sage: sols = solve([p1, p2], x, y, solution_dict=True)
sage: sols

and then extract those that are real:

sage: real_sols = [sol for sol in sols if all(v in RR for v in sol.values())]
sage: real_sols
[{x: 4.698114754098361, y: 10.0}]

Using a polynomial

Another way is to define a polynomial:

sage: x = polygen(ZZ)
sage: p = x^3 + (-1)*x + 1
sage: p
x^3 - x + 1

and find its roots over the algebraic reals:

sage: (p - 10^2).roots(AA, multiplicities=False)
[4.698114752460068?]

or over the floating-point reals:

sage: (p - 10^2).roots(RR, multiplicities=False)
[4.69811475246007]

Using an elliptic curve

Another way is to define an elliptic curve:

sage: E = EllipticCurve([-1, 1])
sage: E
Elliptic Curve defined by y^2 = x^3 - x + 1 over Rational Field

then its ambient space:

sage: P = E.ambient_space()
sage: P
Projective Space of dimension 2 over Rational Field

then the associated polynomial variables:

sage: P.gens()
(x, y, z)
sage: x, y, z = P.gens()

then to construct the projective line corresponding to "y = 10":

sage: L = P.curve([y - 10*z])
sage: L
Projective Plane Curve over Rational Field defined by y - 10*z

then to compute the intersection of the elliptic curve with the line over the algebraic reals:

sage: E.intersection_points(L, F=AA)
[(4.698114752460068? : 10 : 1)]
Related