1
$\begingroup$

I have this cubic polynomial:

-a^2 + a b + a c - 
 b c + (4 a - 2 a^2 - 2 b - 2 c + 2 b c + a^2 k - a b k - a c k + 
    b c k) x + (-3 + 2 a + 2 b - a b + 2 c - a c - b c - 2 a k + b k +
     a b k + c k + a c k - 2 b c k) x^2 + (k - b k - c k + b c k) x^3

Is it possible in Mathematica to somehow get equation for only Real part of 2nd root assuming all Real numbers and a>1, b>1, c>1, 0 < k < 3 , x > 1 ? I would like to convert this equation to C code and export it (but without using Complex classes etc.)

UPDATE

this is second root, and since it contains this I imaginary part, I cannot transfer it easily to C code and calculate only real part of solution. Is it possible somehow to rewrite following equation as Rx+Iy and then just remove Iy part and use only RX part?

e.g. for values a=2, b=4, c=4, k=1.11 I get 1.84213 + 4.16334*10^-17 I, but I need only 1.84213.

-((3 - 2 a - 2 b + a b - 2 c + a c + b c + 2 a k - b k - a b k - c k -
    a c k + 2 b c k)/(
  3 (-k + b k + c k - 
     b c k))) + ((1 + 
      I Sqrt[3]) (3 (-k + b k + c k - b c k) (-4 a + 2 a^2 + 2 b + 
         2 c - 2 b c - a^2 k + a b k + a c k - b c k) - (3 - 2 a - 
        2 b + a b - 2 c + a c + b c + 2 a k - b k - a b k - c k - 
        a c k + 2 b c k)^2))/(3 2^(
    2/3) (-k + b k + c k - b c k) (-54 + 108 a - 72 a^2 + 16 a^3 + 
      108 b - 198 a b + 120 a^2 b - 24 a^3 b - 72 b^2 + 120 a b^2 - 
      66 a^2 b^2 + 12 a^3 b^2 + 16 b^3 - 24 a b^3 + 12 a^2 b^3 - 
      2 a^3 b^3 + 108 c - 198 a c + 120 a^2 c - 24 a^3 c - 198 b c + 
      312 a b c - 156 a^2 b c + 24 a^3 b c + 120 b^2 c - 
      156 a b^2 c + 60 a^2 b^2 c - 6 a^3 b^2 c - 24 b^3 c + 
      24 a b^3 c - 6 a^2 b^3 c - 72 c^2 + 120 a c^2 - 66 a^2 c^2 + 
      12 a^3 c^2 + 120 b c^2 - 156 a b c^2 + 60 a^2 b c^2 - 
      6 a^3 b c^2 - 66 b^2 c^2 + 60 a b^2 c^2 - 12 a^2 b^2 c^2 + 
      12 b^3 c^2 - 6 a b^3 c^2 + 16 c^3 - 24 a c^3 + 12 a^2 c^3 - 
      2 a^3 c^3 - 24 b c^3 + 24 a b c^3 - 6 a^2 b c^3 + 12 b^2 c^3 - 
      6 a b^2 c^3 - 2 b^3 c^3 + 18 a^2 k - 12 a^3 k - 18 a b k - 
      18 a^2 b k + 18 a^3 b k + 18 b^2 k - 18 a b^2 k + 
      36 a^2 b^2 k - 18 a^3 b^2 k - 12 b^3 k + 18 a b^3 k - 
      18 a^2 b^3 k + 6 a^3 b^3 k - 18 a c k - 18 a^2 c k + 
      18 a^3 c k - 18 b c k + 144 a b c k - 72 a^2 b c k - 
      18 b^2 c k - 72 a b^2 c k + 36 a^2 b^2 c k + 18 b^3 c k + 
      18 c^2 k - 18 a c^2 k + 36 a^2 c^2 k - 18 a^3 c^2 k - 
      18 b c^2 k - 72 a b c^2 k + 36 a^2 b c^2 k + 36 b^2 c^2 k + 
      36 a b^2 c^2 k - 18 a^2 b^2 c^2 k - 18 b^3 c^2 k - 12 c^3 k + 
      18 a c^3 k - 18 a^2 c^3 k + 6 a^3 c^3 k + 18 b c^3 k - 
      18 b^2 c^3 k + 6 b^3 c^3 k - 6 a^3 k^2 + 9 a^2 b k^2 + 
      9 a^3 b k^2 + 9 a b^2 k^2 - 36 a^2 b^2 k^2 + 9 a^3 b^2 k^2 - 
      6 b^3 k^2 + 9 a b^3 k^2 + 9 a^2 b^3 k^2 - 6 a^3 b^3 k^2 + 
      9 a^2 c k^2 + 9 a^3 c k^2 - 36 a b c k^2 + 18 a^2 b c k^2 - 
      36 a^3 b c k^2 + 9 b^2 c k^2 + 18 a b^2 c k^2 + 
      18 a^2 b^2 c k^2 + 9 a^3 b^2 c k^2 + 9 b^3 c k^2 - 
      36 a b^3 c k^2 + 9 a^2 b^3 c k^2 + 9 a c^2 k^2 - 
      36 a^2 c^2 k^2 + 9 a^3 c^2 k^2 + 9 b c^2 k^2 + 18 a b c^2 k^2 + 
      18 a^2 b c^2 k^2 + 9 a^3 b c^2 k^2 - 36 b^2 c^2 k^2 + 
      18 a b^2 c^2 k^2 - 36 a^2 b^2 c^2 k^2 + 9 b^3 c^2 k^2 + 
      9 a b^3 c^2 k^2 - 6 c^3 k^2 + 9 a c^3 k^2 + 9 a^2 c^3 k^2 - 
      6 a^3 c^3 k^2 + 9 b c^3 k^2 - 36 a b c^3 k^2 + 
      9 a^2 b c^3 k^2 + 9 b^2 c^3 k^2 + 9 a b^2 c^3 k^2 - 
      6 b^3 c^3 k^2 + 2 a^3 k^3 - 3 a^2 b k^3 - 3 a^3 b k^3 - 
      3 a b^2 k^3 + 12 a^2 b^2 k^3 - 3 a^3 b^2 k^3 + 2 b^3 k^3 - 
      3 a b^3 k^3 - 3 a^2 b^3 k^3 + 2 a^3 b^3 k^3 - 3 a^2 c k^3 - 
      3 a^3 c k^3 + 12 a b c k^3 - 6 a^2 b c k^3 + 12 a^3 b c k^3 - 
      3 b^2 c k^3 - 6 a b^2 c k^3 - 6 a^2 b^2 c k^3 - 
      3 a^3 b^2 c k^3 - 3 b^3 c k^3 + 12 a b^3 c k^3 - 
      3 a^2 b^3 c k^3 - 3 a c^2 k^3 + 12 a^2 c^2 k^3 - 
      3 a^3 c^2 k^3 - 3 b c^2 k^3 - 6 a b c^2 k^3 - 6 a^2 b c^2 k^3 - 
      3 a^3 b c^2 k^3 + 12 b^2 c^2 k^3 - 6 a b^2 c^2 k^3 + 
      12 a^2 b^2 c^2 k^3 - 3 b^3 c^2 k^3 - 3 a b^3 c^2 k^3 + 
      2 c^3 k^3 - 3 a c^3 k^3 - 3 a^2 c^3 k^3 + 2 a^3 c^3 k^3 - 
      3 b c^3 k^3 + 12 a b c^3 k^3 - 3 a^2 b c^3 k^3 - 
      3 b^2 c^3 k^3 - 3 a b^2 c^3 k^3 + 
      2 b^3 c^3 k^3 + \[Sqrt]((-54 + 108 a - 72 a^2 + 16 a^3 + 
           108 b - 198 a b + 120 a^2 b - 24 a^3 b - 72 b^2 + 
           120 a b^2 - 66 a^2 b^2 + 12 a^3 b^2 + 16 b^3 - 24 a b^3 + 
           12 a^2 b^3 - 2 a^3 b^3 + 108 c - 198 a c + 120 a^2 c - 
           24 a^3 c - 198 b c + 312 a b c - 156 a^2 b c + 
           24 a^3 b c + 120 b^2 c - 156 a b^2 c + 60 a^2 b^2 c - 
           6 a^3 b^2 c - 24 b^3 c + 24 a b^3 c - 6 a^2 b^3 c - 
           72 c^2 + 120 a c^2 - 66 a^2 c^2 + 12 a^3 c^2 + 120 b c^2 - 
           156 a b c^2 + 60 a^2 b c^2 - 6 a^3 b c^2 - 66 b^2 c^2 + 
           60 a b^2 c^2 - 12 a^2 b^2 c^2 + 12 b^3 c^2 - 6 a b^3 c^2 + 
           16 c^3 - 24 a c^3 + 12 a^2 c^3 - 2 a^3 c^3 - 24 b c^3 + 
           24 a b c^3 - 6 a^2 b c^3 + 12 b^2 c^3 - 6 a b^2 c^3 - 
           2 b^3 c^3 + 18 a^2 k - 12 a^3 k - 18 a b k - 18 a^2 b k + 
           18 a^3 b k + 18 b^2 k - 18 a b^2 k + 36 a^2 b^2 k - 
           18 a^3 b^2 k - 12 b^3 k + 18 a b^3 k - 18 a^2 b^3 k + 
           6 a^3 b^3 k - 18 a c k - 18 a^2 c k + 18 a^3 c k - 
           18 b c k + 144 a b c k - 72 a^2 b c k - 18 b^2 c k - 
           72 a b^2 c k + 36 a^2 b^2 c k + 18 b^3 c k + 18 c^2 k - 
           18 a c^2 k + 36 a^2 c^2 k - 18 a^3 c^2 k - 18 b c^2 k - 
           72 a b c^2 k + 36 a^2 b c^2 k + 36 b^2 c^2 k + 
           36 a b^2 c^2 k - 18 a^2 b^2 c^2 k - 18 b^3 c^2 k - 
           12 c^3 k + 18 a c^3 k - 18 a^2 c^3 k + 6 a^3 c^3 k + 
           18 b c^3 k - 18 b^2 c^3 k + 6 b^3 c^3 k - 6 a^3 k^2 + 
           9 a^2 b k^2 + 9 a^3 b k^2 + 9 a b^2 k^2 - 36 a^2 b^2 k^2 + 
           9 a^3 b^2 k^2 - 6 b^3 k^2 + 9 a b^3 k^2 + 9 a^2 b^3 k^2 - 
           6 a^3 b^3 k^2 + 9 a^2 c k^2 + 9 a^3 c k^2 - 36 a b c k^2 + 
           18 a^2 b c k^2 - 36 a^3 b c k^2 + 9 b^2 c k^2 + 
           18 a b^2 c k^2 + 18 a^2 b^2 c k^2 + 9 a^3 b^2 c k^2 + 
           9 b^3 c k^2 - 36 a b^3 c k^2 + 9 a^2 b^3 c k^2 + 
           9 a c^2 k^2 - 36 a^2 c^2 k^2 + 9 a^3 c^2 k^2 + 
           9 b c^2 k^2 + 18 a b c^2 k^2 + 18 a^2 b c^2 k^2 + 
           9 a^3 b c^2 k^2 - 36 b^2 c^2 k^2 + 18 a b^2 c^2 k^2 - 
           36 a^2 b^2 c^2 k^2 + 9 b^3 c^2 k^2 + 9 a b^3 c^2 k^2 - 
           6 c^3 k^2 + 9 a c^3 k^2 + 9 a^2 c^3 k^2 - 6 a^3 c^3 k^2 + 
           9 b c^3 k^2 - 36 a b c^3 k^2 + 9 a^2 b c^3 k^2 + 
           9 b^2 c^3 k^2 + 9 a b^2 c^3 k^2 - 6 b^3 c^3 k^2 + 
           2 a^3 k^3 - 3 a^2 b k^3 - 3 a^3 b k^3 - 3 a b^2 k^3 + 
           12 a^2 b^2 k^3 - 3 a^3 b^2 k^3 + 2 b^3 k^3 - 3 a b^3 k^3 - 
           3 a^2 b^3 k^3 + 2 a^3 b^3 k^3 - 3 a^2 c k^3 - 
           3 a^3 c k^3 + 12 a b c k^3 - 6 a^2 b c k^3 + 
           12 a^3 b c k^3 - 3 b^2 c k^3 - 6 a b^2 c k^3 - 
           6 a^2 b^2 c k^3 - 3 a^3 b^2 c k^3 - 3 b^3 c k^3 + 
           12 a b^3 c k^3 - 3 a^2 b^3 c k^3 - 3 a c^2 k^3 + 
           12 a^2 c^2 k^3 - 3 a^3 c^2 k^3 - 3 b c^2 k^3 - 
           6 a b c^2 k^3 - 6 a^2 b c^2 k^3 - 3 a^3 b c^2 k^3 + 
           12 b^2 c^2 k^3 - 6 a b^2 c^2 k^3 + 12 a^2 b^2 c^2 k^3 - 
           3 b^3 c^2 k^3 - 3 a b^3 c^2 k^3 + 2 c^3 k^3 - 
           3 a c^3 k^3 - 3 a^2 c^3 k^3 + 2 a^3 c^3 k^3 - 
           3 b c^3 k^3 + 12 a b c^3 k^3 - 3 a^2 b c^3 k^3 - 
           3 b^2 c^3 k^3 - 3 a b^2 c^3 k^3 + 2 b^3 c^3 k^3)^2 + 
         4 (3 (-k + b k + c k - b c k) (-4 a + 2 a^2 + 2 b + 2 c - 
               2 b c - a^2 k + a b k + a c k - b c k) - (3 - 2 a - 
              2 b + a b - 2 c + a c + b c + 2 a k - b k - a b k - 
              c k - a c k + 2 b c k)^2)^3))^(
    1/3)) - ((1 - I Sqrt[3]) (-54 + 108 a - 72 a^2 + 16 a^3 + 108 b - 
      198 a b + 120 a^2 b - 24 a^3 b - 72 b^2 + 120 a b^2 - 
      66 a^2 b^2 + 12 a^3 b^2 + 16 b^3 - 24 a b^3 + 12 a^2 b^3 - 
      2 a^3 b^3 + 108 c - 198 a c + 120 a^2 c - 24 a^3 c - 198 b c + 
      312 a b c - 156 a^2 b c + 24 a^3 b c + 120 b^2 c - 
      156 a b^2 c + 60 a^2 b^2 c - 6 a^3 b^2 c - 24 b^3 c + 
      24 a b^3 c - 6 a^2 b^3 c - 72 c^2 + 120 a c^2 - 66 a^2 c^2 + 
      12 a^3 c^2 + 120 b c^2 - 156 a b c^2 + 60 a^2 b c^2 - 
      6 a^3 b c^2 - 66 b^2 c^2 + 60 a b^2 c^2 - 12 a^2 b^2 c^2 + 
      12 b^3 c^2 - 6 a b^3 c^2 + 16 c^3 - 24 a c^3 + 12 a^2 c^3 - 
      2 a^3 c^3 - 24 b c^3 + 24 a b c^3 - 6 a^2 b c^3 + 12 b^2 c^3 - 
      6 a b^2 c^3 - 2 b^3 c^3 + 18 a^2 k - 12 a^3 k - 18 a b k - 
      18 a^2 b k + 18 a^3 b k + 18 b^2 k - 18 a b^2 k + 
      36 a^2 b^2 k - 18 a^3 b^2 k - 12 b^3 k + 18 a b^3 k - 
      18 a^2 b^3 k + 6 a^3 b^3 k - 18 a c k - 18 a^2 c k + 
      18 a^3 c k - 18 b c k + 144 a b c k - 72 a^2 b c k - 
      18 b^2 c k - 72 a b^2 c k + 36 a^2 b^2 c k + 18 b^3 c k + 
      18 c^2 k - 18 a c^2 k + 36 a^2 c^2 k - 18 a^3 c^2 k - 
      18 b c^2 k - 72 a b c^2 k + 36 a^2 b c^2 k + 36 b^2 c^2 k + 
      36 a b^2 c^2 k - 18 a^2 b^2 c^2 k - 18 b^3 c^2 k - 12 c^3 k + 
      18 a c^3 k - 18 a^2 c^3 k + 6 a^3 c^3 k + 18 b c^3 k - 
      18 b^2 c^3 k + 6 b^3 c^3 k - 6 a^3 k^2 + 9 a^2 b k^2 + 
      9 a^3 b k^2 + 9 a b^2 k^2 - 36 a^2 b^2 k^2 + 9 a^3 b^2 k^2 - 
      6 b^3 k^2 + 9 a b^3 k^2 + 9 a^2 b^3 k^2 - 6 a^3 b^3 k^2 + 
      9 a^2 c k^2 + 9 a^3 c k^2 - 36 a b c k^2 + 18 a^2 b c k^2 - 
      36 a^3 b c k^2 + 9 b^2 c k^2 + 18 a b^2 c k^2 + 
      18 a^2 b^2 c k^2 + 9 a^3 b^2 c k^2 + 9 b^3 c k^2 - 
      36 a b^3 c k^2 + 9 a^2 b^3 c k^2 + 9 a c^2 k^2 - 
      36 a^2 c^2 k^2 + 9 a^3 c^2 k^2 + 9 b c^2 k^2 + 18 a b c^2 k^2 + 
      18 a^2 b c^2 k^2 + 9 a^3 b c^2 k^2 - 36 b^2 c^2 k^2 + 
      18 a b^2 c^2 k^2 - 36 a^2 b^2 c^2 k^2 + 9 b^3 c^2 k^2 + 
      9 a b^3 c^2 k^2 - 6 c^3 k^2 + 9 a c^3 k^2 + 9 a^2 c^3 k^2 - 
      6 a^3 c^3 k^2 + 9 b c^3 k^2 - 36 a b c^3 k^2 + 
      9 a^2 b c^3 k^2 + 9 b^2 c^3 k^2 + 9 a b^2 c^3 k^2 - 
      6 b^3 c^3 k^2 + 2 a^3 k^3 - 3 a^2 b k^3 - 3 a^3 b k^3 - 
      3 a b^2 k^3 + 12 a^2 b^2 k^3 - 3 a^3 b^2 k^3 + 2 b^3 k^3 - 
      3 a b^3 k^3 - 3 a^2 b^3 k^3 + 2 a^3 b^3 k^3 - 3 a^2 c k^3 - 
      3 a^3 c k^3 + 12 a b c k^3 - 6 a^2 b c k^3 + 12 a^3 b c k^3 - 
      3 b^2 c k^3 - 6 a b^2 c k^3 - 6 a^2 b^2 c k^3 - 
      3 a^3 b^2 c k^3 - 3 b^3 c k^3 + 12 a b^3 c k^3 - 
      3 a^2 b^3 c k^3 - 3 a c^2 k^3 + 12 a^2 c^2 k^3 - 
      3 a^3 c^2 k^3 - 3 b c^2 k^3 - 6 a b c^2 k^3 - 6 a^2 b c^2 k^3 - 
      3 a^3 b c^2 k^3 + 12 b^2 c^2 k^3 - 6 a b^2 c^2 k^3 + 
      12 a^2 b^2 c^2 k^3 - 3 b^3 c^2 k^3 - 3 a b^3 c^2 k^3 + 
      2 c^3 k^3 - 3 a c^3 k^3 - 3 a^2 c^3 k^3 + 2 a^3 c^3 k^3 - 
      3 b c^3 k^3 + 12 a b c^3 k^3 - 3 a^2 b c^3 k^3 - 
      3 b^2 c^3 k^3 - 3 a b^2 c^3 k^3 + 
      2 b^3 c^3 k^3 + \[Sqrt]((-54 + 108 a - 72 a^2 + 16 a^3 + 
           108 b - 198 a b + 120 a^2 b - 24 a^3 b - 72 b^2 + 
           120 a b^2 - 66 a^2 b^2 + 12 a^3 b^2 + 16 b^3 - 24 a b^3 + 
           12 a^2 b^3 - 2 a^3 b^3 + 108 c - 198 a c + 120 a^2 c - 
           24 a^3 c - 198 b c + 312 a b c - 156 a^2 b c + 
           24 a^3 b c + 120 b^2 c - 156 a b^2 c + 60 a^2 b^2 c - 
           6 a^3 b^2 c - 24 b^3 c + 24 a b^3 c - 6 a^2 b^3 c - 
           72 c^2 + 120 a c^2 - 66 a^2 c^2 + 12 a^3 c^2 + 120 b c^2 - 
           156 a b c^2 + 60 a^2 b c^2 - 6 a^3 b c^2 - 66 b^2 c^2 + 
           60 a b^2 c^2 - 12 a^2 b^2 c^2 + 12 b^3 c^2 - 6 a b^3 c^2 + 
           16 c^3 - 24 a c^3 + 12 a^2 c^3 - 2 a^3 c^3 - 24 b c^3 + 
           24 a b c^3 - 6 a^2 b c^3 + 12 b^2 c^3 - 6 a b^2 c^3 - 
           2 b^3 c^3 + 18 a^2 k - 12 a^3 k - 18 a b k - 18 a^2 b k + 
           18 a^3 b k + 18 b^2 k - 18 a b^2 k + 36 a^2 b^2 k - 
           18 a^3 b^2 k - 12 b^3 k + 18 a b^3 k - 18 a^2 b^3 k + 
           6 a^3 b^3 k - 18 a c k - 18 a^2 c k + 18 a^3 c k - 
           18 b c k + 144 a b c k - 72 a^2 b c k - 18 b^2 c k - 
           72 a b^2 c k + 36 a^2 b^2 c k + 18 b^3 c k + 18 c^2 k - 
           18 a c^2 k + 36 a^2 c^2 k - 18 a^3 c^2 k - 18 b c^2 k - 
           72 a b c^2 k + 36 a^2 b c^2 k + 36 b^2 c^2 k + 
           36 a b^2 c^2 k - 18 a^2 b^2 c^2 k - 18 b^3 c^2 k - 
           12 c^3 k + 18 a c^3 k - 18 a^2 c^3 k + 6 a^3 c^3 k + 
           18 b c^3 k - 18 b^2 c^3 k + 6 b^3 c^3 k - 6 a^3 k^2 + 
           9 a^2 b k^2 + 9 a^3 b k^2 + 9 a b^2 k^2 - 36 a^2 b^2 k^2 + 
           9 a^3 b^2 k^2 - 6 b^3 k^2 + 9 a b^3 k^2 + 9 a^2 b^3 k^2 - 
           6 a^3 b^3 k^2 + 9 a^2 c k^2 + 9 a^3 c k^2 - 36 a b c k^2 + 
           18 a^2 b c k^2 - 36 a^3 b c k^2 + 9 b^2 c k^2 + 
           18 a b^2 c k^2 + 18 a^2 b^2 c k^2 + 9 a^3 b^2 c k^2 + 
           9 b^3 c k^2 - 36 a b^3 c k^2 + 9 a^2 b^3 c k^2 + 
           9 a c^2 k^2 - 36 a^2 c^2 k^2 + 9 a^3 c^2 k^2 + 
           9 b c^2 k^2 + 18 a b c^2 k^2 + 18 a^2 b c^2 k^2 + 
           9 a^3 b c^2 k^2 - 36 b^2 c^2 k^2 + 18 a b^2 c^2 k^2 - 
           36 a^2 b^2 c^2 k^2 + 9 b^3 c^2 k^2 + 9 a b^3 c^2 k^2 - 
           6 c^3 k^2 + 9 a c^3 k^2 + 9 a^2 c^3 k^2 - 6 a^3 c^3 k^2 + 
           9 b c^3 k^2 - 36 a b c^3 k^2 + 9 a^2 b c^3 k^2 + 
           9 b^2 c^3 k^2 + 9 a b^2 c^3 k^2 - 6 b^3 c^3 k^2 + 
           2 a^3 k^3 - 3 a^2 b k^3 - 3 a^3 b k^3 - 3 a b^2 k^3 + 
           12 a^2 b^2 k^3 - 3 a^3 b^2 k^3 + 2 b^3 k^3 - 3 a b^3 k^3 - 
           3 a^2 b^3 k^3 + 2 a^3 b^3 k^3 - 3 a^2 c k^3 - 
           3 a^3 c k^3 + 12 a b c k^3 - 6 a^2 b c k^3 + 
           12 a^3 b c k^3 - 3 b^2 c k^3 - 6 a b^2 c k^3 - 
           6 a^2 b^2 c k^3 - 3 a^3 b^2 c k^3 - 3 b^3 c k^3 + 
           12 a b^3 c k^3 - 3 a^2 b^3 c k^3 - 3 a c^2 k^3 + 
           12 a^2 c^2 k^3 - 3 a^3 c^2 k^3 - 3 b c^2 k^3 - 
           6 a b c^2 k^3 - 6 a^2 b c^2 k^3 - 3 a^3 b c^2 k^3 + 
           12 b^2 c^2 k^3 - 6 a b^2 c^2 k^3 + 12 a^2 b^2 c^2 k^3 - 
           3 b^3 c^2 k^3 - 3 a b^3 c^2 k^3 + 2 c^3 k^3 - 
           3 a c^3 k^3 - 3 a^2 c^3 k^3 + 2 a^3 c^3 k^3 - 
           3 b c^3 k^3 + 12 a b c^3 k^3 - 3 a^2 b c^3 k^3 - 
           3 b^2 c^3 k^3 - 3 a b^2 c^3 k^3 + 2 b^3 c^3 k^3)^2 + 
         4 (3 (-k + b k + c k - b c k) (-4 a + 2 a^2 + 2 b + 2 c - 
               2 b c - a^2 k + a b k + a c k - b c k) - (3 - 2 a - 
              2 b + a b - 2 c + a c + b c + 2 a k - b k - a b k - 
              c k - a c k + 2 b c k)^2)^3))^(1/3))/(6 2^(
    1/3) (-k + b k + c k - b c k))
$\endgroup$
10
  • 1
    $\begingroup$ which one is the "second" root? $\endgroup$ Jul 21 '19 at 13:57
  • 1
    $\begingroup$ Hi Bojan, you need to put a bit more in your question. Explain better what you need, share the code you have tried already and why it doesn't answer you needs. Share all the necessary information for somebody to reproduce your problem, don't make us guess. Show explicitly what is wrong and what you expect in detail. Learn about good questions here. $\endgroup$
    – rhermans
    Jul 21 '19 at 14:01
  • $\begingroup$ @AccidentalFourierTransform OK, I just updated code $\endgroup$ Jul 21 '19 at 14:06
  • 1
    $\begingroup$ @rhermans Any reason for using link shorteners? I don't think it's OK to expect people to click on your URLs without knowing where they'll take them... $\endgroup$ Jul 21 '19 at 14:15
  • 1
    $\begingroup$ In C it would be much easier to call the corresponding GSL function gsl_poly_complex_solve_cubic. No need to reinvent the wheel. $\endgroup$
    – Roman
    Jul 21 '19 at 15:07
1
$\begingroup$

OK, I think I found an answer with help from above comments (this is only for one root that I need):

public static double[] solveCubicOnly2nd(double d, double a, double b, double c) {
        if (d != 1) {
            a = a / d;
            b = b / d;
            c = c / d;
        }

        double p = b / 3 - a * a / 9;
        double q = a * a * a / 27 - a * b / 6 + c / 2;

        double ang = Math.acos(-q / Math.sqrt(-p * p * p));
        double r = 2 * Math.sqrt(-p);

        double theta = ang / 3;
        double rez = r * Math.cos(theta);
        rez = rez - a / 3;


        return new double[] {rez};
    }

where params are as: d * x^3 + a * x^2 + b * x + c = 0

And if you need all roots:

public static double[] solveCubic(double d, double a, double b, double c) {
        double[] result;
        if (d != 1) {
            a = a / d;
            b = b / d;
            c = c / d;
        }

        double p = b / 3 - a * a / 9;
        double q = a * a * a / 27 - a * b / 6 + c / 2;
        double D = p * p * p + q * q;

        if (Double.compare(D, 0) >= 0) {
            if (Double.compare(D, 0) == 0) {
                double r = Math.cbrt(-q);
                result = new double[2];
                result[0] = 2 * r;
                result[1] = -r;
            } else {
                double r = Math.cbrt(-q + Math.sqrt(D));
                double s = Math.cbrt(-q - Math.sqrt(D));
                result = new double[1];
                result[0] = r + s;
            }
        } else {
            double ang = Math.acos(-q / Math.sqrt(-p * p * p));
            double r = 2 * Math.sqrt(-p);
            result = new double[3];
            for (int k = -1; k <= 1; k++) {
                double theta = (ang - 2 * Math.PI * k) / 3;
                result[k + 1] = r * Math.cos(theta);
            }

        }
        for (int i = 0; i < result.length; i++) {
            result[i] = result[i] - a / 3;
        }
        return result;
    }
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.