Blame poly/solve_quadratic.c

Packit 67cb25
/* poly/solve_quadratic.c
Packit 67cb25
 * 
Packit 67cb25
 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
Packit 67cb25
 * 
Packit 67cb25
 * This program is free software; you can redistribute it and/or modify
Packit 67cb25
 * it under the terms of the GNU General Public License as published by
Packit 67cb25
 * the Free Software Foundation; either version 3 of the License, or (at
Packit 67cb25
 * your option) any later version.
Packit 67cb25
 * 
Packit 67cb25
 * This program is distributed in the hope that it will be useful, but
Packit 67cb25
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 67cb25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 67cb25
 * General Public License for more details.
Packit 67cb25
 * 
Packit 67cb25
 * You should have received a copy of the GNU General Public License
Packit 67cb25
 * along with this program; if not, write to the Free Software
Packit 67cb25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
/* solve_quadratic.c - finds the real roots of a x^2 + b x + c = 0 */
Packit 67cb25
Packit 67cb25
#include <config.h>
Packit 67cb25
#include <math.h>
Packit 67cb25
Packit 67cb25
#include <gsl/gsl_poly.h>
Packit 67cb25
Packit 67cb25
int 
Packit 67cb25
gsl_poly_solve_quadratic (double a, double b, double c, 
Packit 67cb25
                          double *x0, double *x1)
Packit 67cb25
{
Packit 67cb25
  if (a == 0) /* Handle linear case */
Packit 67cb25
    {
Packit 67cb25
      if (b == 0)
Packit 67cb25
        {
Packit 67cb25
          return 0;
Packit 67cb25
        }
Packit 67cb25
      else
Packit 67cb25
        {
Packit 67cb25
          *x0 = -c / b;
Packit 67cb25
          return 1;
Packit 67cb25
        };
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  {
Packit 67cb25
    double disc = b * b - 4 * a * c;
Packit 67cb25
    
Packit 67cb25
    if (disc > 0)
Packit 67cb25
      {
Packit 67cb25
        if (b == 0)
Packit 67cb25
          {
Packit 67cb25
            double r = sqrt (-c / a);
Packit 67cb25
            *x0 = -r;
Packit 67cb25
            *x1 =  r;
Packit 67cb25
          }
Packit 67cb25
        else
Packit 67cb25
          {
Packit 67cb25
            double sgnb = (b > 0 ? 1 : -1);
Packit 67cb25
            double temp = -0.5 * (b + sgnb * sqrt (disc));
Packit 67cb25
            double r1 = temp / a ;
Packit 67cb25
            double r2 = c / temp ;
Packit 67cb25
            
Packit 67cb25
            if (r1 < r2) 
Packit 67cb25
              {
Packit 67cb25
                *x0 = r1 ;
Packit 67cb25
                *x1 = r2 ;
Packit 67cb25
              } 
Packit 67cb25
            else 
Packit 67cb25
              {
Packit 67cb25
                *x0 = r2 ;
Packit 67cb25
                  *x1 = r1 ;
Packit 67cb25
              }
Packit 67cb25
          }
Packit 67cb25
        return 2;
Packit 67cb25
      }
Packit 67cb25
    else if (disc == 0) 
Packit 67cb25
      {
Packit 67cb25
        *x0 = -0.5 * b / a ;
Packit 67cb25
        *x1 = -0.5 * b / a ;
Packit 67cb25
        return 2 ;
Packit 67cb25
      }
Packit 67cb25
    else
Packit 67cb25
      {
Packit 67cb25
        return 0;
Packit 67cb25
      }
Packit 67cb25
  }
Packit 67cb25
}
Packit 67cb25