1
///////////////////////////////////////////////////////////////////////////
3
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
6
// All rights reserved.
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions are
11
// * Redistributions of source code must retain the above copyright
12
// notice, this list of conditions and the following disclaimer.
13
// * Redistributions in binary form must reproduce the above
14
// copyright notice, this list of conditions and the following disclaimer
15
// in the documentation and/or other materials provided with the
17
// * Neither the name of Industrial Light & Magic nor the names of
18
// its contributors may be used to endorse or promote products derived
19
// from this software without specific prior written permission.
21
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
///////////////////////////////////////////////////////////////////////////
37
#ifndef INCLUDED_IMATHROOTS_H
38
#define INCLUDED_IMATHROOTS_H
40
//---------------------------------------------------------------------
42
// Functions to solve linear, quadratic or cubic equations
44
//---------------------------------------------------------------------
46
#include <ImathMath.h>
51
//--------------------------------------------------------------------------
52
// Find the real solutions of a linear, quadratic or cubic equation:
54
// function equation solved
56
// solveLinear (a, b, x) a * x + b == 0
57
// solveQuadratic (a, b, c, x) a * x*x + b * x + c == 0
58
// solveNormalizedCubic (r, s, t, x) x*x*x + r * x*x + s * x + t == 0
59
// solveCubic (a, b, c, d, x) a * x*x*x + b * x*x + c * x + d == 0
63
// 3 three real solutions, stored in x[0], x[1] and x[2]
64
// 2 two real solutions, stored in x[0] and x[1]
65
// 1 one real solution, stored in x[1]
66
// 0 no real solutions
67
// -1 all real numbers are solutions
71
// * It is possible that an equation has real solutions, but that the
72
// solutions (or some intermediate result) are not representable.
73
// In this case, either some of the solutions returned are invalid
74
// (nan or infinity), or, if floating-point exceptions have been
75
// enabled with Iex::mathExcOn(), an Iex::MathExc exception is
78
// * Cubic equations are solved using Cardano's Formula; even though
79
// only real solutions are produced, some intermediate results are
80
// complex (std::complex<T>).
82
//--------------------------------------------------------------------------
84
template <class T> int solveLinear (T a, T b, T &x);
85
template <class T> int solveQuadratic (T a, T b, T c, T x[2]);
86
template <class T> int solveNormalizedCubic (T r, T s, T t, T x[3]);
87
template <class T> int solveCubic (T a, T b, T c, T d, T x[3]);
96
solveLinear (T a, T b, T &x)
116
solveQuadratic (T a, T b, T c, T x[2])
120
return solveLinear (b, c, x[0]);
124
T D = b * b - 4 * a * c;
128
T s = Math<T>::sqrt (D);
129
T q = -(b + (b > 0 ? 1 : -1) * s) / T(2);
150
solveNormalizedCubic (T r, T s, T t, T x[3])
152
T p = (3 * s - r * r) / 3;
153
T q = 2 * r * r * r / 27 - r * s / 3 + t;
156
T D = p3 * p3 * p3 + q2 * q2;
158
if (D == 0 && p3 == 0)
166
std::complex<T> u = std::pow (-q / 2 + std::sqrt (std::complex<T> (D)),
169
std::complex<T> v = -p / (T (3) * u);
171
const T sqrt3 = T (1.73205080756887729352744634150587); // enough digits
173
std::complex<T> y0 (u + v);
175
std::complex<T> y1 (-(u + v) / T (2) +
176
(u - v) / T (2) * std::complex<T> (0, sqrt3));
178
std::complex<T> y2 (-(u + v) / T (2) -
179
(u - v) / T (2) * std::complex<T> (0, sqrt3));
183
x[0] = y0.real() - r / 3;
188
x[0] = y0.real() - r / 3;
189
x[1] = y1.real() - r / 3;
194
x[0] = y0.real() - r / 3;
195
x[1] = y1.real() - r / 3;
196
x[2] = y2.real() - r / 3;
204
solveCubic (T a, T b, T c, T d, T x[3])
208
return solveQuadratic (b, c, d, x);
212
return solveNormalizedCubic (b / a, c / a, d / a, x);