13 int choleski_solve(
double *a,
double *b,
int nsize,
int flag);
14 int inverse (
double *a,
int nsize);
15 int isymp(
int row,
int col);
17 int indeces_from_set(
int *indeces,
int set,
int set_size,
int n);
18 int binomial_coeficient(
int n,
int k);
20 inline int isymp(
int row,
int col)
29 if(row < col) k= row + col *(col -1)/2;
30 else k= col + row * (row - 1)/2;
36 inline int binomial_coeficient(
int n,
int k)
88 inline int indeces_from_set(
int *indeces,
int set,
int set_size,
int n)
99 if( set > binomial_coeficient(n, set_size) )
104 for( i =0,j=0;i<set_size-1;i++,j=indeces[i-1]+1)
106 k = binomial_coeficient( n - j - 1, set_size - i - 1);
111 k = binomial_coeficient( n - j - 1, set_size - i - 1);
116 indeces[i] = j+set-1;
122 int decompose (
double *,
int);
123 int foresub (
double *,
double *,
int);
124 int backsub (
double *,
double *,
int);
127 #define NOT_SOLVABLE 0
131 inline int choleski_solve (
double *a,
double *b,
int nsize,
int flag)
140 if (flag < 1 && flag > 3)
144 if (! decompose (a, nsize))
return NOT_SOLVABLE;
146 if (flag == 1)
return SOLVED;
149 if (! foresub (a, b, nsize))
return NOT_SOLVABLE;
153 if (! backsub (a, b, nsize))
return NOT_SOLVABLE;
155 if (flag == 2)
return SOLVED;
165 inline int decompose (
double *a,
int nsize)
179 int i, j, k, m, n = 2;
182 for (k = 0; k < nsize; k++)
188 for (i = 0; i < k; i++)
193 for (j = 0; j < i; j++)
195 sum += *ap4++ * *ap5++;
199 *ap3 = (*ap3 - sum) / *ap2;ap3++;
204 for (j = 0; j < k; j++)
206 sum += *ap1 * *ap1;ap1++;
214 if ((*ap6 - sum) <= 0)
215 *ap6 = sqrt(-(*ap6 - sum));
217 *ap6 = sqrt(*ap6 - sum);
229 inline int foresub (
double *a,
double *b,
int nsize)
240 for (i = 1; i < nsize; i++)
243 for (j = 0, bp2 = b; j < i; j++)
245 sum += *ap1++ * *bp2++;
249 *bp1 = (*bp1 - sum) / *ap1;bp1++;ap1++;
257 inline int backsub (
double *a,
double *b,
int nsize)
266 double *ap1 = a + (k + k * (k + 1) / 2);
267 double *ap2 = ap1 - 1;
275 *bp1 = *bp1 / *ap1;bp1--;
276 for (i = k - 1; i >= 0; i--)
280 for (j = nsize - 1, ap3 = ap2--, bp3 = bp2; j >= i + 1; j--)
282 sum += *ap3 * *bp3--;
288 *bp1 = (*bp1 - sum) / *ap1;bp1--;
296 inline int inverse (
double *a,
int nsize)
310 while (ap1 < (a + (nsize * (nsize + 1)/2)))
318 for (i = 1; i < nsize; i++)
323 for (j = m; j <= nsize; j++)
330 sum += *ap1 * *ap2++;
334 *ap3 = -(*ap4) * sum;
342 ap1 = ap2 = ap3 = ap4 = a;
343 for (i = 0; i < nsize;)
347 for (j = i; j < nsize;)
350 ap1 = ap3 = ap5 + i + n;
353 for (k = j; k < nsize;)