39 double *c = NULL, *d = NULL, *col_j, *col_k, *inv_col_k;
40 double temp, scale, sum;
41 int i, j, k, success = 0;
43 assert(inv_A && A && N > 0);
45 if (!(c = (
double *)malloc(
sizeof(
double) * N))
46 || !(d = (
double *)malloc(
sizeof(
double) * N)))
49 for (k = 0, col_k = A; k < N - 1; ++k, col_k += N)
53 for (i = k; i < N; ++i)
54 if ((temp = fabs(col_k[i])) > scale)
60 for (i = k; i < N; ++i)
63 for (sum = 0.0, i = k; i < N; ++i)
64 sum += col_k[i]*col_k[i];
66 temp = (col_k[k] >= 0.0) ? sqrt(sum) : -sqrt(sum);
68 c[k] = temp * col_k[k];
71 for (j = k + 1, col_j = col_k + N; j < N; ++j, col_j += N)
73 for (scale = 0.0, i = k; i < N; ++i)
74 scale += col_k[i] * col_j[i];
78 for (i = k; i < N; ++i)
79 col_j[i] -= scale * col_k[i];
88 for (k = 0, inv_col_k = inv_A; k < N; ++k, inv_col_k += N)
90 for (i = 0; i < N; ++i)
91 inv_col_k[i] = -A[k] * A[i] / c[0];
95 for (j = 1, col_j = A + N; j < N-1; ++j, col_j += N)
97 for (scale = 0.0, i = j; i < N; ++i)
98 scale += col_j[i] * inv_col_k[i];
102 for (i = j; i < N; ++i)
103 inv_col_k[i] -= scale * col_j[i];
106 inv_col_k[j] /= d[N-1];
108 for (i = N - 2; i >= 0; --i)
110 for (sum = 0.0, j = i + 1, col_j = A + N*(i + 1);
111 j < N; ++j, col_j += N)
112 sum += col_j[i] * inv_col_k[j];
114 inv_col_k[i] = (inv_col_k[i] - sum) / d[i];