Blame Imath/ImathRoots.h

Packit 8dc392
///////////////////////////////////////////////////////////////////////////
Packit 8dc392
//
Packit 8dc392
// Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
Packit 8dc392
// Digital Ltd. LLC
Packit 8dc392
// 
Packit 8dc392
// All rights reserved.
Packit 8dc392
// 
Packit 8dc392
// Redistribution and use in source and binary forms, with or without
Packit 8dc392
// modification, are permitted provided that the following conditions are
Packit 8dc392
// met:
Packit 8dc392
// *       Redistributions of source code must retain the above copyright
Packit 8dc392
// notice, this list of conditions and the following disclaimer.
Packit 8dc392
// *       Redistributions in binary form must reproduce the above
Packit 8dc392
// copyright notice, this list of conditions and the following disclaimer
Packit 8dc392
// in the documentation and/or other materials provided with the
Packit 8dc392
// distribution.
Packit 8dc392
// *       Neither the name of Industrial Light & Magic nor the names of
Packit 8dc392
// its contributors may be used to endorse or promote products derived
Packit 8dc392
// from this software without specific prior written permission. 
Packit 8dc392
// 
Packit 8dc392
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 8dc392
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 8dc392
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 8dc392
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 8dc392
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 8dc392
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 8dc392
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 8dc392
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 8dc392
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 8dc392
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 8dc392
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 8dc392
//
Packit 8dc392
///////////////////////////////////////////////////////////////////////////
Packit 8dc392
Packit 8dc392
Packit 8dc392
Packit 8dc392
#ifndef INCLUDED_IMATHROOTS_H
Packit 8dc392
#define INCLUDED_IMATHROOTS_H
Packit 8dc392
Packit 8dc392
//---------------------------------------------------------------------
Packit 8dc392
//
Packit 8dc392
//	Functions to solve linear, quadratic or cubic equations
Packit 8dc392
//
Packit 8dc392
//---------------------------------------------------------------------
Packit 8dc392
Packit 8dc392
#include "ImathMath.h"
Packit 8dc392
#include "ImathNamespace.h"
Packit 8dc392
#include <complex>
Packit 8dc392
Packit 8dc392
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
Packit 8dc392
Packit 8dc392
//--------------------------------------------------------------------------
Packit 8dc392
// Find the real solutions of a linear, quadratic or cubic equation:
Packit 8dc392
//
Packit 8dc392
//   	function				   equation solved
Packit 8dc392
//
Packit 8dc392
//   solveLinear (a, b, x)		                      a * x + b == 0
Packit 8dc392
//   solveQuadratic (a, b, c, x)	            a * x*x + b * x + c == 0
Packit 8dc392
//   solveNormalizedCubic (r, s, t, x)	    x*x*x + r * x*x + s * x + t == 0
Packit 8dc392
//   solveCubic (a, b, c, d, x)		a * x*x*x + b * x*x + c * x + d == 0
Packit 8dc392
//
Packit 8dc392
// Return value:
Packit 8dc392
//
Packit 8dc392
//	 3	three real solutions, stored in x[0], x[1] and x[2]
Packit 8dc392
//	 2	two real solutions, stored in x[0] and x[1]
Packit 8dc392
//	 1	one real solution, stored in x[1]
Packit 8dc392
//	 0	no real solutions
Packit 8dc392
//	-1	all real numbers are solutions
Packit 8dc392
//
Packit 8dc392
// Notes:
Packit 8dc392
//
Packit 8dc392
//    * It is possible that an equation has real solutions, but that the
Packit 8dc392
//	solutions (or some intermediate result) are not representable.
Packit 8dc392
//	In this case, either some of the solutions returned are invalid
Packit 8dc392
//	(nan or infinity), or, if floating-point exceptions have been
Packit 8dc392
//	enabled with Iex::mathExcOn(), an Iex::MathExc exception is
Packit 8dc392
//	thrown.
Packit 8dc392
//
Packit 8dc392
//    * Cubic equations are solved using Cardano's Formula; even though
Packit 8dc392
//	only real solutions are produced, some intermediate results are
Packit 8dc392
//	complex (std::complex<T>).
Packit 8dc392
//
Packit 8dc392
//--------------------------------------------------------------------------
Packit 8dc392
Packit 8dc392
template <class T> int	solveLinear (T a, T b, T &x);
Packit 8dc392
template <class T> int	solveQuadratic (T a, T b, T c, T x[2]);
Packit 8dc392
template <class T> int	solveNormalizedCubic (T r, T s, T t, T x[3]);
Packit 8dc392
template <class T> int	solveCubic (T a, T b, T c, T d, T x[3]);
Packit 8dc392
Packit 8dc392
Packit 8dc392
//---------------
Packit 8dc392
// Implementation
Packit 8dc392
//---------------
Packit 8dc392
Packit 8dc392
template <class T>
Packit 8dc392
int
Packit 8dc392
solveLinear (T a, T b, T &x)
Packit 8dc392
{
Packit 8dc392
    if (a != 0)
Packit 8dc392
    {
Packit 8dc392
	x = -b / a;
Packit 8dc392
	return 1;
Packit 8dc392
    }
Packit 8dc392
    else if (b != 0)
Packit 8dc392
    {
Packit 8dc392
	return 0;
Packit 8dc392
    }
Packit 8dc392
    else
Packit 8dc392
    {
Packit 8dc392
	return -1;
Packit 8dc392
    }
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
Packit 8dc392
template <class T>
Packit 8dc392
int
Packit 8dc392
solveQuadratic (T a, T b, T c, T x[2])
Packit 8dc392
{
Packit 8dc392
    if (a == 0)
Packit 8dc392
    {
Packit 8dc392
	return solveLinear (b, c, x[0]);
Packit 8dc392
    }
Packit 8dc392
    else
Packit 8dc392
    {
Packit 8dc392
	T D = b * b - 4 * a * c;
Packit 8dc392
Packit 8dc392
	if (D > 0)
Packit 8dc392
	{
Packit 8dc392
	    T s = Math<T>::sqrt (D);
Packit 8dc392
	    T q = -(b + (b > 0 ? 1 : -1) * s) / T(2);
Packit 8dc392
Packit 8dc392
	    x[0] = q / a;
Packit 8dc392
	    x[1] = c / q;
Packit 8dc392
	    return 2;
Packit 8dc392
	}
Packit 8dc392
	if (D == 0)
Packit 8dc392
	{
Packit 8dc392
	    x[0] = -b / (2 * a);
Packit 8dc392
	    return 1;
Packit 8dc392
	}
Packit 8dc392
	else
Packit 8dc392
	{
Packit 8dc392
	    return 0;
Packit 8dc392
	}
Packit 8dc392
    }
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
Packit 8dc392
template <class T>
Packit 8dc392
int
Packit 8dc392
solveNormalizedCubic (T r, T s, T t, T x[3])
Packit 8dc392
{
Packit 8dc392
    T p  = (3 * s - r * r) / 3;
Packit 8dc392
    T q  = 2 * r * r * r / 27 - r * s / 3 + t;
Packit 8dc392
    T p3 = p / 3;
Packit 8dc392
    T q2 = q / 2;
Packit 8dc392
    T D  = p3 * p3 * p3 + q2 * q2;
Packit 8dc392
Packit 8dc392
    if (D == 0 && p3 == 0)
Packit 8dc392
    {
Packit 8dc392
	x[0] = -r / 3;
Packit 8dc392
	x[1] = -r / 3;
Packit 8dc392
	x[2] = -r / 3;
Packit 8dc392
	return 1;
Packit 8dc392
    }
Packit 8dc392
Packit 8dc392
    std::complex<T> u = std::pow (-q / 2 + std::sqrt (std::complex<T> (D)),
Packit 8dc392
				  T (1) / T (3));
Packit 8dc392
Packit 8dc392
    std::complex<T> v = -p / (T (3) * u);
Packit 8dc392
Packit 8dc392
    const T sqrt3 = T (1.73205080756887729352744634150587); // enough digits
Packit 8dc392
							    // for long double
Packit 8dc392
    std::complex<T> y0 (u + v);
Packit 8dc392
Packit 8dc392
    std::complex<T> y1 (-(u + v) / T (2) +
Packit 8dc392
			 (u - v) / T (2) * std::complex<T> (0, sqrt3));
Packit 8dc392
Packit 8dc392
    std::complex<T> y2 (-(u + v) / T (2) -
Packit 8dc392
			 (u - v) / T (2) * std::complex<T> (0, sqrt3));
Packit 8dc392
Packit 8dc392
    if (D > 0)
Packit 8dc392
    {
Packit 8dc392
	x[0] = y0.real() - r / 3;
Packit 8dc392
	return 1;
Packit 8dc392
    }
Packit 8dc392
    else if (D == 0)
Packit 8dc392
    {
Packit 8dc392
	x[0] = y0.real() - r / 3;
Packit 8dc392
	x[1] = y1.real() - r / 3;
Packit 8dc392
	return 2;
Packit 8dc392
    }
Packit 8dc392
    else
Packit 8dc392
    {
Packit 8dc392
	x[0] = y0.real() - r / 3;
Packit 8dc392
	x[1] = y1.real() - r / 3;
Packit 8dc392
	x[2] = y2.real() - r / 3;
Packit 8dc392
	return 3;
Packit 8dc392
    }
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
Packit 8dc392
template <class T>
Packit 8dc392
int
Packit 8dc392
solveCubic (T a, T b, T c, T d, T x[3])
Packit 8dc392
{
Packit 8dc392
    if (a == 0)
Packit 8dc392
    {
Packit 8dc392
	return solveQuadratic (b, c, d, x);
Packit 8dc392
    }
Packit 8dc392
    else
Packit 8dc392
    {
Packit 8dc392
	return solveNormalizedCubic (b / a, c / a, d / a, x);
Packit 8dc392
    }
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
Packit 8dc392
Packit 8dc392
#endif // INCLUDED_IMATHROOTS_H