;; Solution to cubic equations by Sigh_
;;
;; Solves equations of the type ax^3 + bx^2 +cx + d = 0 (a != 0)
;;
;; Usage:
;; $cubic(a,b,c,d)
;; or
;; /cubic a b c d
alias cubic {
var %i = $calc((3*$3 /$1 -$2 ^2/$1 ^2)/3),%x = $calc((2*$2 ^3 /$1 ^3-9*$2 $&
*$3 /$1 ^2+27*$4 /$1)/27),%n = $calc(%x ^2 /4+%i ^3/27),%r = $iif($isid,return,echo -a)
if (%n > 0) {
var %s = $cbrt($calc($sqrt(%n)-%x /2)),%u = $cbrt($calc(-$sqrt(%n)-%x /2)), $&
%p = $x($calc($2 /-3/$1 -(%s +%u)/2)),%t = $iif($calc(3^0.5*(%s -%u)/2) != 1,$x($v1))
%r $x($calc(%s +%u -$2 /3/$1)) , %p + %t $+ i , %p - %t $+ i
return
}
var %k = $calc((%x ^2/4-%n)^0.5),%j = -1 * $cbrt(%k),%t = $acos($calc(%x $&
/-2/%k)) / 3,%m = $cos(%t),%b = $sqrt(3) * $sin(%t),%p = $calc($2 /-3/$1))
%r $x($calc(-2*%j *%m -$2 /3/$1)) , $x($calc(%j *(%m +%b)+%p)) , $x($calc(%j *(%m -%b)+%p))
}
alias -l cbrt return $iif($1 < 0,-) $+ $calc($abs($1)^(1/3))
alias -l x return $round($1,3)