//#####################################################################
// Copyright 2002-2005, Ronald Fedkiw, Geoffrey Irving.
// This file is part of PhysBAM whose distribution is governed by the license contained in the accompanying file PHYSBAM_COPYRIGHT.txt.
//#####################################################################
#ifndef __Polynomial_Roots__
#define __Polynomial_Roots__

#include <Core/Arrays/ARRAY_VIEW.h>
#include <cassert>
namespace PhysBAM{

// Solve real-valued polynomial, storing all complex roots or only the real roots.
// coeffs is the coefficients for the polynomial sum(coeffs(i)*x^i,i=0..coeffs.m-1).
// Assumes coeffs.Last()!=0.
// Requires roots.m==coeffs.m-1.
// For complex roots, exactly coeffs.m-1 roots will be found.
// For real roots, only the real roots will be returned, and the number of real
//   roots found is returned.
// Duplicate roots are returned, but roots are not sorted.

template<class T>
void Polynomial_Roots(ARRAY_VIEW<const T> coeffs,ARRAY_VIEW<std::complex<T> > roots);

template<class T>
int Polynomial_Roots(ARRAY_VIEW<const T> coeffs,ARRAY_VIEW<T> roots);

template<class T,class T_ARRAY1,class T_ARRAY2>
void Polynomial_Roots(const ARRAY_BASE<T,T_ARRAY1>& coeffs,ARRAY_BASE<std::complex<T>,T_ARRAY2>& roots)
{Polynomial_Roots(ARRAY_VIEW<const T>(coeffs),ARRAY_VIEW<std::complex<T> >(roots));}

template<class T,class T_ARRAY1,class T_ARRAY2>
int Polynomial_Roots(const ARRAY_BASE<T,T_ARRAY1>& coeffs,ARRAY_BASE<T,T_ARRAY2>& roots)
{return Polynomial_Roots(ARRAY_VIEW<const T>(coeffs),ARRAY_VIEW<T>(roots));}

}
#endif
