22 template <
typename Real>
26 typedef std::array<std::array<Real, 4>, 4>
Matrix;
29 uint32_t
operator() (uint32_t maxIterations, Real c0, Real c1, Real c2, Real c3,
30 uint32_t& numRoots, std::array<Real, 4>& roots);
35 uint32_t
operator() (uint32_t maxIterations, Matrix& A,
36 uint32_t& numRoots, std::array<Real, 4>& roots);
39 void DoIteration(std::array<Real, 3>
const& V, Matrix& A);
42 std::array<Real, N>
House(std::array<Real, N>
const& X);
45 void RowHouse(
int rmin,
int rmax,
int cmin,
int cmax,
46 std::array<Real, N>
const& V, std::array<Real, N>
const& MV, Matrix& A);
49 void ColHouse(
int rmin,
int rmax,
int cmin,
int cmax,
50 std::array<Real, N>
const& V, std::array<Real, N>
const& MV, Matrix& A);
53 uint32_t& numRoots, std::array<Real, 4>& roots);
57 template <
typename Real>
59 uint32_t& numRoots, std::array<Real, 4>& roots)
83 std::array<Real, 3> V{
85 (Real)0.36602540378443865,
86 (Real)0.36602540378443865 };
89 return operator()(maxIterations, A, numRoots, roots);
92 template <
typename Real>
94 uint32_t& numRoots, std::array<Real, 4>& roots)
97 std::fill(roots.begin(), roots.end(), (Real)0);
99 for (uint32_t numIterations = 0; numIterations < maxIterations; ++numIterations)
102 Real tr = A[2][2] + A[3][3];
103 Real det = A[2][2] * A[3][3] - A[2][3] * A[3][2];
104 std::array<Real, 3> X{
105 A[0][0] * A[0][0] + A[0][1] * A[1][0] - tr * A[0][0] + det,
106 A[1][0] * (A[0][0] + A[1][1] - tr),
108 std::array<Real, 3> V = House<3>(X);
112 Real tr12 = A[1][1] + A[2][2];
113 if (tr12 + A[2][1] == tr12)
117 return numIterations;
120 Real tr01 = A[0][0] + A[1][1];
121 if (tr01 + A[1][0] == tr01)
130 uint32_t subMaxIterations = maxIterations - numIterations;
132 for (
int r = 0;
r < 3; ++
r)
134 for (
int c = 0;
c < 3; ++
c)
136 B[
r][
c] = A[
r + 1][
c + 1];
140 uint32_t numSubroots = 0;
141 std::array<Real, 3> subroots;
143 numSubroots, subroots);
144 for (uint32_t i = 0; i < numSubroots; ++i)
146 roots[numRoots++] = subroots[i];
148 return numIterations + numSubiterations;
151 Real tr23 = A[2][2] + A[3][3];
152 if (tr23 + A[3][2] == tr23)
161 uint32_t subMaxIterations = maxIterations - numIterations;
163 for (
int r = 0;
r < 3; ++
r)
165 for (
int c = 0;
c < 3; ++
c)
171 uint32_t numSubroots = 0;
172 std::array<Real, 3> subroots;
174 numSubroots, subroots);
175 for (uint32_t i = 0; i < numSubroots; ++i)
177 roots[numRoots++] = subroots[i];
179 return numIterations + numSubiterations;
182 return maxIterations;
185 template <
typename Real>
188 Real multV = ((Real)-2) / (V[0] * V[0] + V[1] * V[1] + V[2] * V[2]);
189 std::array<Real, 3> MV{ multV * V[0], multV * V[1], multV * V[2] };
190 RowHouse<3>(0, 2, 0, 3, V, MV, A);
191 ColHouse<3>(0, 3, 0, 2, V, MV, A);
193 std::array<Real, 3> X{ A[1][0], A[2][0], A[3][0] };
194 std::array<Real, 3> locV = House<3>(X);
195 multV = ((Real)-2) / (locV[0] * locV[0] + locV[1] * locV[1] + locV[2] * locV[2]);
196 MV = { multV * locV[0], multV * locV[1], multV * locV[2] };
197 RowHouse<3>(1, 3, 0, 3, locV, MV, A);
198 ColHouse<3>(0, 3, 1, 3, locV, MV, A);
200 std::array<Real, 2> Y{ A[2][1], A[3][1] };
201 std::array<Real, 2> W = House<2>(Y);
202 Real multW = ((Real)-2) / (W[0] * W[0] + W[1] * W[1]);
203 std::array<Real, 2> MW = { multW * W[0], multW * W[1] };
204 RowHouse<2>(2, 3, 0, 3, W, MW, A);
205 ColHouse<2>(0, 3, 2, 3, W, MW, A);
208 template <
typename Real>
212 std::array<Real, N> V;
214 for (
int i = 0; i < N; ++i)
216 length += X[i] * X[i];
218 length = sqrt(length);
219 if (length != (Real)0)
221 Real sign = (X[0] >= (Real)0 ? (Real)1 : (Real)-1);
222 Real denom = X[0] + sign *
length;
223 for (
int i = 1; i < N; ++i)
232 template <
typename Real>
235 std::array<Real, N>
const& V, std::array<Real, N>
const& MV,
Matrix& A)
237 std::array<Real, 4> W;
239 for (
int c = cmin;
c <= cmax; ++
c)
242 for (
int r = rmin, k = 0;
r <= rmax; ++
r, ++k)
244 W[
c] += V[k] * A[
r][
c];
248 for (
int r = rmin, k = 0;
r <= rmax; ++
r, ++k)
250 for (
int c = cmin;
c <= cmax; ++
c)
252 A[
r][
c] += MV[k] * W[
c];
257 template <
typename Real>
260 std::array<Real, N>
const& V, std::array<Real, N>
const& MV,
Matrix& A)
262 std::array<Real, 4> W;
264 for (
int r = rmin;
r <= rmax; ++
r)
267 for (
int c = cmin, k = 0;
c <= cmax; ++
c, ++k)
269 W[
r] += V[k] * A[
r][
c];
273 for (
int r = rmin;
r <= rmax; ++
r)
275 for (
int c = cmin, k = 0;
c <= cmax; ++
c, ++k)
277 A[
r][
c] += W[
r] * MV[k];
282 template <
typename Real>
284 uint32_t& numRoots, std::array<Real, 4>& roots)
292 Real trace = A[i0][i0] + A[i1][i1];
293 Real halfTrace = trace * (Real)0.5;
294 Real determinant = A[i0][i0] * A[i1][i1] - A[i0][i1] * A[i1][i0];
295 Real discriminant = halfTrace * halfTrace - determinant;
296 if (discriminant >= (Real)0)
298 Real sign = (trace >= (Real)0 ? (Real)1 : (Real)-1);
299 Real root = halfTrace + sign * sqrt(discriminant);
300 roots[numRoots++] = root;
301 roots[numRoots++] = trace - root;
std::array< std::array< Real, 4 >, 4 > Matrix
void RowHouse(int rmin, int rmax, int cmin, int cmax, std::array< Real, N > const &V, std::array< Real, N > const &MV, Matrix &A)
void ColHouse(int rmin, int rmax, int cmin, int cmax, std::array< Real, N > const &V, std::array< Real, N > const &MV, Matrix &A)
uint32_t operator()(uint32_t maxIterations, Real c0, Real c1, Real c2, Real c3, uint32_t &numRoots, std::array< Real, 4 > &roots)
void DoIteration(std::array< Real, 3 > const &V, Matrix &A)
std::array< Real, N > House(std::array< Real, N > const &X)
void GetQuadraticRoots(int i0, int i1, Matrix const &A, uint32_t &numRoots, std::array< Real, 4 > &roots)
GLuint GLsizei GLsizei * length
std::array< std::array< Real, 3 >, 3 > Matrix