Ask Your Question

Revision history [back]

The output of solve is a list of solutions in the form of equations x == ...:

sage: sol = solve(x^3 - x == 3*x^2 - 1, x)

Get the value of each solution as the right-hand side ("rhs"):

sage: a, b, c = [s.rhs() for s in sol]

Or use solution_dict=True to get solutions in dictionary form:

sage: sol = solve(x^3 - x == 3*x^2 - 1, x, solution_dict=True)

Then each solution s is a dictionary of the form {x: ...} and one can get its value as s[x]:

sage: a, b, c = [s[x] for s in sol]

In each case, we get:

sage: a
-1/2*(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3)*(I*sqrt(3) + 1) - 2/3*(-I*sqrt(3) + 1)/(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3) + 1
sage: b
-1/2*(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3)*(-I*sqrt(3) + 1) - 2/3*(I*sqrt(3) + 1)/(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3) + 1
sage: c
(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3) + 4/3/(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3) + 1

so it looks as though the solutions are non-real, since they involve I.

This is because there are well-known formulas to express solutions of cubic equations using radicals, sometimes involving square roots of negative numbers even when the final result is real.

Indeed, the results are real here, as we can check with the numerical_approx method (n for short`):

sage: a.n()
0.460811127189111
sage: b.n()
-0.675130870566646 - 1.11022302462516e-16*I
sage: c.n()
3.21431974337754 - 5.55111512312578e-17*I

The numerical approximation is imperfect, and leaves a tiny imaginary component which however is only there due to rounding errors.

To get the results directly as exact algebraic numbers, and see directly whether or not they are real, and what their decimal expansion looks like, work with polynomials and their roots method rather than with the symbolic ring and the solve function.

sage: x = polygen(ZZ)
sage: p = x^3 - x
sage: q = p - p.derivative()
sage: q
x^3 - 3*x^2 - x + 1

The method roots will show (root, multiplicity) pairs:

sage: q.roots(AA)
[(-0.6751308705666461?, 1), (0.4608111271891109?, 1), (3.214319743377535?, 1)]

unless one specifies multiplicities=False:

sage: q.roots(AA, multiplicities=False)
[-0.6751308705666461?, 0.4608111271891109?, 3.214319743377535?]

The output of solve is a list of solutions in the form of equations x == ...:

sage: sol = solve(x^3 - x == 3*x^2 - 1, x)

x)

Get the value of each solution as the right-hand side ("rhs"):

sage: a, b, c = [s.rhs() for s in sol]

sol]

Or use solution_dict=True to get solutions in dictionary form:

sage: sol = solve(x^3 - x == 3*x^2 - 1, x, solution_dict=True)

Then each solution s is a dictionary of the form {x: ...} and one can get its value as s[x]:

sage: a, b, c = [s[x] for s in sol]

In each case, we get:

sage: a
-1/2*(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3)*(I*sqrt(3) + 1) - 2/3*(-I*sqrt(3) + 1)/(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3) + 1
sage: b
-1/2*(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3)*(-I*sqrt(3) + 1) - 2/3*(I*sqrt(3) + 1)/(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3) + 1
sage: c
(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3) + 4/3/(1/9*I*sqrt(37)*sqrt(3) + 1)^(1/3) + 1

so it looks as though the solutions are non-real, since they involve I.

This is because there are well-known formulas to express solutions of cubic equations using radicals, sometimes involving square roots of negative numbers even when the final result is real.

Indeed, the results are real here, as we can check with the numerical_approx method (n for short`):

sage: a.n()
0.460811127189111
sage: b.n()
-0.675130870566646 - 1.11022302462516e-16*I
sage: c.n()
3.21431974337754 - 5.55111512312578e-17*I

The numerical approximation is imperfect, and leaves a tiny imaginary component (e-16 means *10^-16) which however is only there due to rounding errors.

To get the results directly as exact algebraic numbers, and see directly whether or not they are real, and what their decimal expansion looks like, work with polynomials and their roots method rather than with the symbolic ring and the solve function.

sage: x = polygen(ZZ)
sage: p = x^3 - x
sage: q = p - p.derivative()
sage: q
x^3 - 3*x^2 - x + 1

The method roots will show (root, multiplicity) pairs:

sage: q.roots(AA)
[(-0.6751308705666461?, 1), (0.4608111271891109?, 1), (3.214319743377535?, 1)]

unless one specifies multiplicities=False:

sage: q.roots(AA, multiplicities=False)
[-0.6751308705666461?, 0.4608111271891109?, 3.214319743377535?]