/* -------------------------------------------------------------------------- * * SimTK Core: SimTKcommon * * -------------------------------------------------------------------------- * * This is part of the SimTK Core biosimulation toolkit originating from * * Simbios, the NIH National Center for Physics-Based Simulation of * * Biological Structures at Stanford, funded under the NIH Roadmap for * * Medical Research, grant U54 GM072970. See https://simtk.org. * * * * Portions copyright (c) 2007 Stanford University and the Authors. * * Authors: Peter Eastman * * Contributors: * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the "Software"), * * to deal in the Software without restriction, including without limitation * * the rights to use, copy, modify, merge, publish, distribute, sublicense, * * and/or sell copies of the Software, and to permit persons to whom the * * Software is furnished to do so, subject to the following conditions: * * * * The above copyright notice and this permission notice shall be included in * * all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * * USE OR OTHER DEALINGS IN THE SOFTWARE. * * -------------------------------------------------------------------------- */ #include "SimTKcommon/internal/PolynomialRootFinder.h" #include "rpoly.h" #include "cpoly.h" namespace SimTK { template void PolynomialRootFinder::findRoots(const Vec<3,T>& coefficients, Vec<2,complex >& roots) { T a = coefficients[0], b = coefficients[1], c = coefficients[2]; if (a == 0.0) SimTK_THROW(ZeroLeadingCoefficient); T b2 = b*b; T discriminant = b2 - (T) 4.0*a*c; T tol = (T) 2.0*NTraits::getEps()*b2; if (discriminant < tol && discriminant > -tol) { // b^2 == 4ac to within machine precision, so make the roots identical. T root = -b/((T) 2.0*a); roots[0] = complex(root, 0.0); roots[1] = complex(root, 0.0); return; } if (b == 0.0) { // The coefficient of the linear term is zero, which makes the formula simpler. if (discriminant >= 0.0) { T root = std::sqrt(discriminant)/(T) 2.0*a; roots[0] = root; roots[1] = -root; } else { T root = std::sqrt(-discriminant)/(T) 2.0*a; roots[0] = Complex(0.0, root); roots[1] = Complex(0.0, -root); } return; } complex q = ((T) -0.5)*(b+(b > 0.0 ? std::sqrt(complex(discriminant)) : -std::sqrt(complex(discriminant)))); roots[0] = q/a; roots[1] = c/q; } template void PolynomialRootFinder::findRoots(const Vec<3,complex >& coefficients, Vec<2,complex >& roots) { complex a = coefficients[0], b = coefficients[1], c = coefficients[2]; if (a == (T) 0.0) SimTK_THROW(ZeroLeadingCoefficient); complex b2 = b*b; complex discriminant = b2 - ((T) 4.0)*a*c; if (b == (T) 0.0) { // The coefficient of the linear term is zero, which makes the formula simpler. complex root = std::sqrt(discriminant)/((T) 2.0)*a; roots[0] = root; roots[1] = -root; return; } T temp = (conj(b)*sqrt(discriminant)).real(); complex q = ((T) -0.5)*(b+(temp > 0.0 ? std::sqrt(discriminant) : -std::sqrt(discriminant))); roots[0] = q/a; roots[1] = c/q; } template void PolynomialRootFinder::findRoots(const Vec<4,T>& coefficients, Vec<3,complex >& roots) { if (coefficients[0] == 0.0) SimTK_THROW(ZeroLeadingCoefficient); T coeff[4] = {coefficients[0], coefficients[1], coefficients[2], coefficients[3]}; T rootr[3]; T rooti[3]; RPoly().findRoots(coeff, 3, rootr, rooti); roots[0] = Complex(rootr[0], rooti[0]); roots[1] = Complex(rootr[1], rooti[1]); roots[2] = Complex(rootr[2], rooti[2]); } template void PolynomialRootFinder::findRoots(const Vec<4,complex >& coefficients, Vec<3,complex >& roots) { if (coefficients[0] == (T) 0.0) SimTK_THROW(ZeroLeadingCoefficient); T coeffr[4] = {coefficients[0].real(), coefficients[1].real(), coefficients[2].real(), coefficients[3].real()}; T coeffi[4] = {coefficients[0].imag(), coefficients[1].imag(), coefficients[2].imag(), coefficients[3].imag()}; T rootr[3]; T rooti[3]; CPoly().findRoots(coeffr, coeffi, 3, rootr, rooti); roots[0] = Complex(rootr[0], rooti[0]); roots[1] = Complex(rootr[1], rooti[1]); roots[2] = Complex(rootr[2], rooti[2]); } template void PolynomialRootFinder::findRoots(const Vector_& coefficients, Vector_ >& roots) { if (coefficients[0] == 0.0) SimTK_THROW(ZeroLeadingCoefficient); int n = roots.size(); assert(coefficients.size() == n+1); T *coeff = new T[n+1]; T *rootr = new T[n]; T *rooti = new T[n]; try { for (int i = 0; i < n+1; ++i) coeff[i] = coefficients[i]; RPoly().findRoots(coeff, n, rootr, rooti); for (int i = 0; i < n; ++i) roots[i] = Complex(rootr[i], rooti[i]); } catch (...) { delete[] coeff; delete[] rootr; delete[] rooti; throw; } delete[] coeff; delete[] rootr; delete[] rooti; } template void PolynomialRootFinder::findRoots(const Vector_ >& coefficients, Vector_ >& roots) { if (coefficients[0] == (T) 0.0) SimTK_THROW(ZeroLeadingCoefficient); int n = roots.size(); assert(coefficients.size() == n+1); T *coeffr = new T[n+1]; T *coeffi = new T[n+1]; T *rootr = new T[n]; T *rooti = new T[n]; try { for (int i = 0; i < n+1; ++i) { coeffr[i] = coefficients[i].real(); coeffi[i] = coefficients[i].imag(); } CPoly().findRoots(coeffr, coeffi, n, rootr, rooti); for (int i = 0; i < n; ++i) roots[i] = Complex(rootr[i], rooti[i]); } catch (...) { delete[] coeffr; delete[] coeffi; delete[] rootr; delete[] rooti; throw; } delete[] coeffr; delete[] coeffi; delete[] rootr; delete[] rooti; } template SimTK_SimTKCOMMON_EXPORT void PolynomialRootFinder::findRoots(const Vec<3,float>& coefficients, Vec<2,complex >& roots); template SimTK_SimTKCOMMON_EXPORT void PolynomialRootFinder::findRoots(const Vec<3,complex >& coefficients, Vec<2,complex >& roots); template SimTK_SimTKCOMMON_EXPORT void PolynomialRootFinder::findRoots(const Vec<4,float>& coefficients, Vec<3,complex >& roots); template SimTK_SimTKCOMMON_EXPORT void PolynomialRootFinder::findRoots(const Vec<4,complex >& coefficients, Vec<3,complex >& roots); template SimTK_SimTKCOMMON_EXPORT void PolynomialRootFinder::findRoots(const Vector_& coefficients, Vector_ >& roots); template SimTK_SimTKCOMMON_EXPORT void PolynomialRootFinder::findRoots(const Vector_ >& coefficients, Vector_ >& roots); template SimTK_SimTKCOMMON_EXPORT void PolynomialRootFinder::findRoots(const Vec<3,double>& coefficients, Vec<2,complex >& roots); template SimTK_SimTKCOMMON_EXPORT void PolynomialRootFinder::findRoots(const Vec<3,complex >& coefficients, Vec<2,complex >& roots); template SimTK_SimTKCOMMON_EXPORT void PolynomialRootFinder::findRoots(const Vec<4,double>& coefficients, Vec<3,complex >& roots); template SimTK_SimTKCOMMON_EXPORT void PolynomialRootFinder::findRoots(const Vec<4,complex >& coefficients, Vec<3,complex >& roots); template SimTK_SimTKCOMMON_EXPORT void PolynomialRootFinder::findRoots(const Vector_& coefficients, Vector_ >& roots); template SimTK_SimTKCOMMON_EXPORT void PolynomialRootFinder::findRoots(const Vector_ >& coefficients, Vector_ >& roots); } // namespace SimTK