/*
 * The Spar Library - modular math parser
 * Copyright (C) 2000,2001 Davide Angelocola <davide178@inwind.it>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 *
 */

#include <spar/sl_poly.h>
#include <spar/sl_math_library.h>
#include <spar/sl_math.h>

/* solve_cubic.c - finds the real roots of x^3 + a x^2 + b x + c = 0 */

int
sl_poly_solve_cubic (double a, double b, double c,
		     double *x0, double *x1, double *x2)
{
  double q = (a * a - 3 * b);
  double r = (2 * a * a * a - 9 * a * b + 27 * c);

  double Q = q / 9;
  double R = r / 54;

  double Q3 = Q * Q * Q;
  double R2 = R * R;

  double CR2 = 729 * r * r;
  double CQ3 = 2916 * q * q * q;

  if (R == 0 && Q == 0)
    {
      *x0 = -a / 3;
      *x1 = -a / 3;
      *x2 = -a / 3;
      return 3;
    }
  else if (CR2 == CQ3)
    {
      /* this test is actually R2 == Q3, written in a form suitable
         for exact computation with integers */

      /* Due to finite precision some double roots may be missed, and
         considered to be a pair of complex roots z = x +/- epsilon i
         close to the real axis. */

      double sqrtQ = sl_sqrn (Q, 2);

      if (R > 0)
	{
	  *x0 = -2 * sqrtQ - a / 3;
	  *x1 = sqrtQ - a / 3;
	  *x2 = sqrtQ - a / 3;
	}
      else
	{
	  *x0 = -sqrtQ - a / 3;
	  *x1 = -sqrtQ - a / 3;
	  *x2 = 2 * sqrtQ - a / 3;
	}
      return 3;
    }
  else if (CR2 < CQ3)		/* equivalent to R2 < Q3 */
    {
      double sqrtQ = sl_sqrn (Q, 2);
      double sqrtQ3 = sqrtQ * sqrtQ * sqrtQ;
      double theta = sl_arccos (R / sqrtQ3);
      double norm = -2 * sqrtQ;
      *x0 = norm * sl_cos (theta / 3) - a / 3;
      *x1 = norm * sl_cos ((theta + 2.0 * M_PI) / 3) - a / 3;
      *x2 = norm * sl_cos ((theta - 2.0 * M_PI) / 3) - a / 3;

      /* Sort *x0, *x1, *x2 into increasing order */

      if (*x0 > *x1)
	SL_DBL_SWAP (*x0, *x1);

      if (*x1 > *x2)
	{
	  SL_DBL_SWAP (*x1, *x2);

	  if (*x0 > *x1)
	    SL_DBL_SWAP (*x0, *x1);
	}

      return 3;
    }
  else
    {
      double sgnR = SL_SGN (R);
      double A =
	-sgnR * sl_pow (SL_ABS (R) + sl_sqrn (R2 - Q3, 2), 1.0 / 3.0);
      double B = Q / A;
      *x0 = A + B - a / 3;

      return 1;
    }
}


syntax highlighted by Code2HTML, v. 0.9.1