10-09-2020, 05:21 PM

I was being stupid. Why solve cubic equation for a

This is the way to get cube roots simplified:

\(\sqrt[3]{A ± \sqrt{R}} = a ± \sqrt{r} \quad ⇒ \quad a = \large\frac{\sqrt[3]{A+\sqrt{R}}

\;+\; \sqrt[3]{A-\sqrt{R}}}{2} \)

Round a to closest halves, get r, and double check if it round-trip back to (A, R)

lua> simp_cbrt4(1859814842094, -59687820010, 415)

11589 -145 415

lua> simp_cbrt4(300940299,103940300,101)

99 100 101

lua> simp_cbrt(180, 23, 157) -- (a,b) can be halves

1.5 0.5 157

lua> simp_cbrt4(-36, 20, -7) -- work with complex roots too.

3 1 -7

lua> simp_cbrt4(81,30,-3) -- this is simplest, see comment from previous post.

4.5 0.5 -3

This is the way to get cube roots simplified:

\(\sqrt[3]{A ± \sqrt{R}} = a ± \sqrt{r} \quad ⇒ \quad a = \large\frac{\sqrt[3]{A+\sqrt{R}}

\;+\; \sqrt[3]{A-\sqrt{R}}}{2} \)

Round a to closest halves, get r, and double check if it round-trip back to (A, R)

Code:

`function simp_cbrt4(A,B,k) -- simplify cbrt(A + B * sqrt(k))`

local R = B*B*k

local a = sqrt(abs(R))

if R<0 then a = (A*A-R)^(1/6) * cos(atan2(a,A)/3)

else a = (cbrt(A+a) + cbrt(A-a))/2

end

a = a + 0x3p50 - 0x3p50 -- round to halves

local r = (A/a-a*a)/3

local b = B/(3*a*a+r)

if r == b*b*k then return a,b,k end

end

lua> simp_cbrt4(1859814842094, -59687820010, 415)

11589 -145 415

lua> simp_cbrt4(300940299,103940300,101)

99 100 101

lua> simp_cbrt(180, 23, 157) -- (a,b) can be halves

1.5 0.5 157

lua> simp_cbrt4(-36, 20, -7) -- work with complex roots too.

3 1 -7

lua> simp_cbrt4(81,30,-3) -- this is simplest, see comment from previous post.

4.5 0.5 -3