11 #define MAX(a, b) ((a)>(b)?(a):(b))
15 static double hypot2(
double x,
double y) {
20 static void tred2(
double V[n][n],
double d[n],
double e[n]) {
27 for (
int j = 0; j < n; j++) {
33 for (
int i = n-1; i > 0; i--) {
39 for (
int k = 0; k < i; k++) {
40 scale = scale + fabs(d[k]);
44 for (
int j = 0; j < i; j++) {
53 for (
int k = 0; k < i; k++) {
65 for (
int j = 0; j < i; j++) {
71 for (
int j = 0; j < i; j++) {
74 g = e[j] + V[j][j] * f;
75 for (
int k = j+1; k <= i-1; k++) {
82 for (
int j = 0; j < i; j++) {
86 double hh = f / (h + h);
87 for (
int j = 0; j < i; j++) {
90 for (
int j = 0; j < i; j++) {
93 for (
int k = j; k <= i-1; k++) {
94 V[k][j] -= (f * e[k] + g * d[k]);
105 for (
int i = 0; i < n-1; i++) {
110 for (
int k = 0; k <= i; k++) {
111 d[k] = V[k][i+1] / h;
113 for (
int j = 0; j <= i; j++) {
115 for (
int k = 0; k <= i; k++) {
116 g += V[k][i+1] * V[k][j];
118 for (
int k = 0; k <= i; k++) {
123 for (
int k = 0; k <= i; k++) {
127 for (
int j = 0; j < n; j++) {
136 static void tql2(
double V[n][n],
double d[n],
double e[n]) {
143 for (
int i = 1; i < n; i++) {
150 double eps = pow(2.0,-52.0);
151 for (
int l = 0; l < n; l++) {
155 tst1 = MAX(tst1,fabs(d[l]) + fabs(e[l]));
158 if (fabs(e[m]) <= eps*tst1) {
175 double p = (d[l+1] - g) / (2.0 * e[l]);
176 double r = hypot2(p,1.0);
180 d[l] = e[l] / (p + r);
181 d[l+1] = e[l] * (p + r);
184 for (
int i = l+2; i < n; i++) {
198 for (
int i = m-1; i >= l; i--) {
208 p = c * d[i] - s * g;
209 d[i+1] = h + s * (c * g + s * d[i]);
213 for (
int k = 0; k < n; k++) {
215 V[k][i+1] = s * V[k][i] + c * h;
216 V[k][i] = c * V[k][i] - s * h;
219 p = -s * s2 * c3 * el1 * e[l] / dl1;
225 }
while (fabs(e[l]) > eps*tst1);
233 for (
int i = 0; i < n-1; i++) {
236 for (
int j = i+1; j < n; j++) {
245 for (
int j = 0; j < n; j++) {
254 void eigen_decomposition(
double A[n][n],
double V[n][n],
double d[n]) {
256 for (
int i = 0; i < n; i++) {
257 for (
int j = 0; j < n; j++) {