I know the formulas for solving cubic equation, but when I try to use them in both Cardano's method and https://en.wikipedia.org/wiki/Cubic_function#Algebraic_solution, I usually encounter something like Cube root of numbers such as $2+11i$ or nested radicals, where square root is inside of cube root. According to hypergeometric's answer to my previous question, I would need to solve another cubic equation to trisects the argument angle to find the cube root and there is similiar problem with the denesting the cube root. So is there some algorithm to analytically solve cubic equation without guessing and rounding (I want to solve it symbolically)? How do the computer algebra systems like Wolfram Alpha do it?
[Math] Is there really analytic solution to cubic equation
cubicssymbolic computation
Related Solutions
In your question and comment there are a few different things that might warrant addressing. I'll try to respond to them separately, starting with some minor points of clarification, and then answering the closest thing to what you want.
"I'm not sure if my way is correct."
Unfortunately, your work seems to be looking for when the "first $t$" is positive in the solution of the depressed cubic equation $t^3+pt+q=0$. But since $x=t-\frac{b}{3a}$, answering that question is not the same as answering when $x$ is positive, because of the shift by $\dfrac{b}{3a}$. (It turns out that when the equation has three real roots, the $\frac{b}{3a}$ shift makes the biggest $t$ root positive no matter what.)
I am talking about specifically $x_1$.
Unfortunately, there's a significant issue with this idea. When there's exactly one real root, it's $x_1$, and then you're asking "is the real root positive?", which is a very sensible question. But when there are more real roots, then you're taking cube roots of non-real numbers (and you can't really avoid it: you're likely in the casus irreducibilis), which sort of depends on how you how you define them.
Now, if you arbitrarily pick a particular definition of complex cube root, you can answer this, or if you arbitrarily pick an ordering of the trigonometric formulas for the solutions then you have a concrete question. But since you didn't mention either of those approaches in your question I'm going to assume that you didn't really have them in mind. If you're interested in something like that, I suppose you could ask a separate question about it.
The real question:
What I am primarily looking to identify are these cases: where the polynomial could have 1 real positive root and 2 imaginary roots, and the case where it has 3 real roots with at least one of them positive.
Two key facts that can help us answer your question.
- For a cubic polynomial with real coefficients, there is a number called the discriminant (which I will denote by $\Delta$) which is positive when the polynomial has three distinct real roots, is zero when the polynomial has three real roots with a repeated root, and is negative when the polynomial has exactly one real root. If the cubic equation is $ax^3+bx^2+cx+d=0$, then it turns out $\Delta=-27 a^2 d^2+18 a b c d-4 a c^3-4 b^3 d+b^2 c^2$.
- There is a simple algebraic rule of thumb called Descartes' Rule of Signs that can help you count the positive and negative roots of a polynomial. Luckily, a cubic doesn't have that many roots, so Descartes' Rule of Signs will be extremely helpful. You count the number of sign changes (positive to negative or vice versa) in the coefficients, and the number of positive roots is either that number, or that number minus 2.
What do these two facts tell us?
- If there are no sign changes in the coefficients, there cannot be any positive roots.
- If there is exactly one sign change, then there must be exactly one positive root (and if $\Delta>0$ it's your "$x_1$").
- If there are two sign changes, then there might be two positive roots or zero positive roots. If $\Delta>0$ then there's only one real root, so there can't be two and there must be zero so the real root isn't positive. But if $\Delta\le0$ so that there are three roots, then by applying the Rule of Signs to the polynomial with $-x$ substituted in for $x$ we see that there is at most one negative root, and since we can't have $c=d=0$ in a case like this, there is at most one root equal to zero: there must be two positive roots.
- If there are three sign changes, then there might be one positive root or three positive roots. If $\Delta>0$ then the unique real root is positive. If $\Delta\le0$ then the rule of signs applied to the polynomial with $-x$ substituted in tells us there are no negative roots, and since we can't have $d=0$ in a case like this: there must be three positive roots.
What are all the cases, though?
For simplicity, divide through your polynomial $ax^3+bx^2+cx+d$ by the leading coefficient $a$ to give the the form $x^3+bx^2+cx+d$. The roots will be the same, but now we have cut the cases in half since we don't have to worry about $a$ being negative. (The discriminant is now a little simpler, too: $\Delta=-4 b^3 d+b^2 c^2+18 b c d-4 c^3-27 d^2$.)
Now we can make a table of possibilities for the signs of the coefficients. I'll use inequality symbols as shorthand for comparison with $0$, so that $\ge$ will mean $\ge0$ in this table. The numbers of sign changes come from that fact that the leading coefficient has been made $1$. The last column is the number of positive roots.
\begin{array}{cccc} b & c & d & \text{#}\\ \ge & \ge & \ge & 0\\ \ge & \ge & < & 1\\ \ge & < & \le & 1\\ \ge & < & > & 0\text{ or }2\\ < & \le & \le & 1\\ < & \le & > & 0\text{ or }2\\ < & > & \ge & 0\text{ or }2\\ < & > & < & 1\text{ or }3 \end{array} And from our notes above, every single one of those "or" cases is distinguished by the discriminant: If $\Delta>0$ then it's the lower number, and if $\Delta\le0$ then it must be the higher number.
Closing remarks
The discriminant is kind of an ugly expression, and this table has a bunch of cases, but unfortunately there's not much you can do about that. You can regroup the cases a bit if you like, but I don't think it can really be reduced in general. One general rewriting that still has 8 cases is: \begin{array}{cccc} b & c & d & \text{#}\\ \ge & \ge & \ge & 0\\ \ge & ? & < & 1\\ ? & < & = & 1\\ < & \le & \le & 1\\ \ge & < & > & 0\text{ or }2\\ < & ? & > & 0\text{ or }2\\ < & > & = & 0\text{ or }2\\ < & > & < & 1\text{ or }3 \end{array}
That said, if you happen to know that $\Delta>0$ so that there's only one real root (and hence it's $x_1$), then things do simplify somewhat nicely: \begin{array}{cccc} b & c & d & x_1>0\text{?}\\ ? & ? & > & \text{no}\\ ? & > & = & \text{no}\\ \ge & = & = & \text{no}\\ ? & ? & < & \text{yes}\\ ? & < & = & \text{yes}\\ < & = & = & \text{yes} \end{array}
You can use the rational root theorem to guess some roots.
Rational root theorem. All rational roots have the form $\frac{p}{q}$,with $p$ a divisior of the constant term and $q$ a divisior of the first coefficient.
For example, in the integer case, one can take $q=1$, and one only has to test $p = \pm 1$, $p = \pm 2$, $p= \pm 5$, and then you have it. (you of course could test $\pm 7$, $\pm 10$, $\pm 14$, $\pm 35$ and $\pm 70$ as well, but it would be less work to just divide by the $x-5$)
Another technique:
Descartes rule of Signs. The number of positive roots of $P(x)$ is equal to the number of sign changes is the sequence formed by the coefficients of $P(x)$ or it is less by an even number.
The number of negative roots of $P(x)$ is equal to the number of sign changes is the sequence formed by the coefficients of $P(-x)$ or it is less by an even number.
Roots with multiplicity $n$ are counted $n$ times.
In this case it is useless. But if it is a polynomial like $x^3+3x^2+4x+2$, you now know that there are no positive roots.
Last technique:
Estimating roots. If you've tested all eligible roots that are smaller than, say, 10, then you can use this. If the first two coefficients are not extremely small compared to the last two, then its a good idea to look to you eligible roots near to $$-\frac{\mathrm{second} \; \mathrm{coefficient}}{\mathrm{first} \; \mathrm{coefficient}}$$
If the root is larger than 10, the $x^3$ and $x^2$ term are large enough to be able to ignore the smaller terms, thus this is a good estimation. Write $a$ for the first coefficient and $b$ for the second, $c$ for the third and $d$ for the fourth.
Now, if $x$ is large, then $ax^3+bx^2+cx+d \sim ax^3+bx^2$. Therefore the roots will be comparable. But $ax^3+bx^2=0$ gives $x^2=0$ or $ax+b=0$. In the first case $x$ isn't large enough. In the second case we have $ax+b=0$, thus $ax=-b$, thus $x=\frac{-b}{a}=-\frac{\mathrm{second} \; \mathrm{coefficient}}{\mathrm{first} \; \mathrm{coefficient}}$.
Best Answer
There are a number of similar questions, for example
Cubic roots and Cardano formula
Cube root of numbers such as $2+11i$
The core of all questions seems to be an uneasy feeling about cubic roots of complex numbers because "they are not really computable".
What is the difference between a purely real expression like $\sqrt[3]{2}$ and a complex expression like $\sqrt[3]{2+11i}$?
$\sqrt[3]{2}$ seems to be very familiar whereas one might be a little skeptical about $\sqrt[3]{2+11i}$ - how to compute the latter?
But is it really "easier"to compute $\sqrt[3]{2}$ than $\sqrt[3]{2+11i}$? I believe the answer is "no". If you want to compute $\sqrt[3]{2}$ you need an algorithm producing a sequence of rational approximations $x_n$ converging to $\sqrt[3]{2}$ plus a concrete estimate of $\lvert \sqrt[3]{2} - x_n \rvert$ so that you know when to stop the computation. There are very simple algorithms to compute cubic roots of real numbers, but there are also algorithms to compute cubic roots of complex numbers. These are somewhat more complicated, but there is no philosophical difference concerning the iterative approach.
Let me close with some remarks concerning the Cardano formula. In any cubic equation $$x^3 + a_2x^2 +a_1x + a_0 = 0 \tag{1}$$ we can substitute $x = y - \frac{1}{3}a_2$ and obtain the cubic equation $$y^3 +ay = b \tag{2}$$ where $a = a_1 - \frac{1}{3}a_2^2$ and $b = -\frac{2}{27}a_2^3 + \frac{1}{3}a_1a_2 - a_0$.
The solutions of $(2)$ are given as follows. Set
$$R = \frac{b^2}{4} + \frac{a^3}{27} , \tag{3}$$
$$w_+ = \sqrt[3]{\frac{b}{2} + \sqrt{R}}. \tag{4}$$
Although at first glance $w_+$ seems to be uniquely determined, it involves two choices: The square root has two values, the cubic root three values in $\mathbb{C}$. Let us adopt the following conventions:
For $x \ge 0$ we let $\sqrt{x}$ denote the nonnegative square root of $x$, for $x <0$ we define $\sqrt{x} = i\sqrt{-x}$.
For any $x \in \mathbb{R}$ we let $\sqrt[3]{x}$ denote the real cubic root of $x$.
For a non-real $z \in \mathbb{C}$ we do not get explicit about the choice of $\sqrt[3]{z}$ among the the three complex (non-real) cubic roots of $z$.
This gives a standard interpretation of $\sqrt[3]{\frac{b}{2} + \sqrt{R}}$ as a real number for $R \ge 0$ and leaves a single choice (of a complex cubic root) for $R < 0$.
Note that $w_+ = 0$ if and only $\sqrt{R} = -\frac{b}{2}$, i.e. $b \le 0$ and $a = 0$. This case (in which $y^3 = b$) is trivial and could also be omitted. Observe that $\frac{b}{2} - \sqrt{R} = b$ in this case.
Now define
$$w_- = \begin{cases} -\dfrac{a}{3w_+} & w_+ \ne 0 \\ \phantom{..} \sqrt[3]{b} & w_+ = 0 \end{cases} \tag{5}$$
It is easily verified that $w_-$ is a cubic root of $\frac{b}{2} - \sqrt{R}$. If $w_+$ is real (which happens precisely when $R \ge 0$), then also $w_-$ is real so that it is the standard interpretation of $\sqrt[3]{\frac{b}{2} - \sqrt{R}}$ as a real number.
So far we specified cubic roots $w_\pm$ of $\frac{b}{2} \pm \sqrt{R}$. These cubic roots are not independent, but satisfy the relation $$3w_+w_- + a = 0 . \tag{6}$$
The solutions of $(2)$ are then $$y_0 = w_+ + w_- \tag{7}$$ $$y_1 = \zeta_1 w_+ + \zeta_2 w_- \tag{8}$$ $$y_2 = \zeta_2 w_+ + \zeta_1 w_- \tag{9}$$ where $\zeta_1 = -\frac{1}{2} + \frac{\sqrt{3}}{2}i, \zeta_2 = -\frac{1}{2} - \frac{\sqrt{3}}{2}i = \zeta_1^2 = \overline{\zeta_1}$ are the two complex third unit roots. This is easily verified by inserting into $(2)$. With $\zeta_0 = 1$ we may also write $$y_k = \zeta_k w_+ + \zeta_k^2 w_- = \zeta_k w_+ + \overline{\zeta_k} w_- \tag{10}$$ Note that the $\zeta_kw_+$ are the three complex cubic roots of $\frac{b}{2} + \sqrt{R}$ and the $\zeta_kw_-$ are the three complex cubic roots of $\frac{b}{2} - \sqrt{R}$. Thus all solutions of $(2)$ are sums of certain complex cubic roots of $\frac{b}{2} + \sqrt{R}$ and $\frac{b}{2} - \sqrt{R}$.
The Cardano formula is frequently reported in the sloppy form that $$\sqrt[3]{\frac{b}{2} + \sqrt{R}} + \sqrt[3]{\frac{b}{2} - \sqrt{R}} \tag{11}$$ is a solution of $(2)$. This is misleading since it suggests that
there is always a standard interpretation of $\sqrt[3]{\frac{b}{2} \pm \sqrt{R}}$,
two separate computations for $\sqrt[3]{\frac{b}{2} \pm \sqrt{R}}$ must be performed.
But 1. is true only for $R \ge 0$, and in that case $(11)$ is in fact a solution of $(2)$. However, there are two more solutions described by $(10)$ and which involve other values of the cubic roots of $\frac{b}{2} \pm \sqrt{R}$. And, more important, 2. is not true because $w_-$ can easily be derived from $w_+$ via $(5)$. This is always easier than computing $\sqrt[3]{\frac{b}{2} - \sqrt{R}}$ ab ovo, even for $R \ge 0$. For an even easier alternative in case $R < 0$ see the discussion below.
Let us now consider three cases for $R$.
If $R > 0$, then we get one real solution $y_0$ (which may be expressed by $(11)$ in the standard interpretation) and two non-real solutions $y_1, y_2$ which are complex conjugate. All solutions can be expressed by using only square and cubic roots of real numbers.
If $R = 0$ we obtain the three real solutions $y_0 = 2\sqrt[3]{\frac{b}{2}}$ and $y_1 = y_2 = -\sqrt[3]{\frac{b}{2}}$.
The case $R < 0$ (casus irreducibilis) is most interesting. Note that it can only occur when $a < 0$. We have three distinct real solutions, but $w_+, w_-$ are non-real. They are cubic roots of the complex conjugate numbers $\frac{b}{2} \pm i \sqrt{-R}$, but recall that these cubic roots cannot be chosen independently because they are subject to $(6)$. If we make any choice for $w_+$ as a cubic root of $\frac{b}{2} + i \sqrt{-R}$, then clearly $\overline{w_+}$ is a cubic root of $\overline{\frac{b}{2} + i \sqrt{-R}} = \frac{b}{2} - i \sqrt{-R}$. We have $\lvert w_+ \rvert^3 = \lvert \frac{b}{2} + i \sqrt{-R} \rvert = \sqrt{-a^3/27}$, i.e. $\lvert w_+ \rvert = \sqrt{-a/3}$. This implies $3w_+\overline{w_+} + a = 3\lvert w_+ \rvert^2 + a = 0$ which means $$w_- = \overline{w_+} . \tag{12}$$ This is even simpler than $(5)$ and it moreover seems completely natural to choose complex conjugate cubic roots for the complex conjugate numbers $\frac{b}{2} \pm i \sqrt{-R}$.
Anyway, we get $$y_k = \zeta_k w_+ + \overline{\zeta_k} w_- = \zeta_k w_+ + \overline{\zeta_k} \overline{w_+} = \zeta_k w_+ + \overline{\zeta_k w_+} = 2 Re(\zeta_k w_+) . \tag{13}$$
Note that $\zeta_k w_+$, $k = 0,1,2$, are the three complex cubic roots of $\frac{b}{2} + i \sqrt{-R}$.
It is known that in general it is impossible to express any of these three real solutions in terms of roots of real numbers. See
https://en.wikipedia.org/wiki/Casus_irreducibilis
Those who are historically interested and read German may also look at
Hölder, Otto. "Über den Casus irreducibilis bei der Gleichung dritten Grades." Mathematische Annalen 38.2 (1891): 307-312.
Remark 1:
The special case $a < 0, b = 0$ is interesting. The equation $y^3 + ay = 0$ has the obvious solution $y_1 = 0$. This reduces the problem to $y^2 + a = 0$ and gives $y_{2,3} = \pm \sqrt{-a}$.
Nevertheless we are in the casus irreducibilis: We have $R = \frac{a^3}{27} < 0$. This gives $\sqrt{R} = i \sqrt{\frac{-a^3}{27}}$ and $w_+ = \eta \sqrt{ \frac{-a}{3}} = \eta \frac{\sqrt{-a}}{\sqrt 3}$, where $\eta$ is one of the three complex cubic roots of $i$. Taking $\eta = -i$, we get $Re(w_+) = 0$, thus $y = 0$ is a solution found by Cardano's formula. The solutions $y = \pm \sqrt{-a}$ are obtained by taking $\eta = \pm \frac{\sqrt{3}}{2} + \frac{i}{2}$.
Remark 2:
Cardano mentioned that his formula $(11)$ fails in the casus irreducibilis, certainly because he did not know the general concept of complex numbers (although he had an idea about square roots of negative numbers). Rafael Bombelli (1526 - 1572) seems to be the first who really used complex numbers (although be did not introduce a proper concept of them). For the equation $x^3 = 15x + 4$ - which is casus irreducibilis - he obtained the solution (written in modern terms) $$x = \sqrt[3]{2 + \sqrt{-121}} + \sqrt[3]{2 - \sqrt{-121}}$$ and showed that a cubic root of $2 \pm \sqrt{-121}$ is given by $w_\pm = 2 \pm \sqrt{-1}$. Thus he obtained $x = 4$ which is in fact an exact real solution found by Cardano's formula. However, in a a sense he had good luck since his choices of cubic roots satisfy $(12)$.
Bombelli's example shows that in the casus irreducibilis it is sometimes possible to find a simple explicit expression for $w_+$ which allows to find the exact values of the three real solutions.
Remark 3:
To use Cardano's formula it is essential to determine the values $\sqrt[3]{\frac{b}{2} \pm \sqrt{R}}$. In Remarks 1 and 2 we have done this for some special cases. A more general approach can be found in When does $r + \sqrt s$ with $r,s \in \mathbb Q$ have a cubic root of the form $u \pm \sqrt v$ with $u,v \in \mathbb Q$?
Note, however, that the explicit computation of $\sqrt[3]{\frac{b}{2} \pm \sqrt{R}}$ requires that we know a solution of $(2)$ by applying the Rational root theorem. To put it pointedly: In some cases we can determine $\sqrt[3]{\frac{b}{2} \pm \sqrt{R}}$ by making finitely many trials.