home *** CD-ROM | disk | FTP | other *** search
- //-------------------------------------------------------------------//
-
- // Synopsis: Cholesky factorization with pivoting of a pos.
- // semi-definite matrix.
-
- // Syntax: CL = cholp ( A )
-
- // Description:
-
- // Cholp returns R and a permutation matrix P such that
-
- // R'*R = P'*A*P
-
- // Only the upper triangular part of A is used. In addition the
- // index I of the last positive diagonal element of R. The first
- // I rows of R contain the Cholesky factor of A.
-
- // This routine is based on the LINPACK routine CCHDC. It works
- // for both real and complex matrices.
- //
- // Reference:
- // G.H. Golub and C.F. Van Loan, Matrix Computations, Second
- // Edition, Johns Hopkins University Press, Baltimore, Maryland,
- // 1989, sec. 4.2.9.
-
- // This file is a translation of cholp.m from version 2.0 of
- // "The Test Matrix Toolbox for Matlab", described in Numerical
- // Analysis Report No. 237, December 1993, by N. J. Higham.
- //-------------------------------------------------------------------//
-
- cholp = function ( A, pivot )
- {
- local (A, pivot)
-
- if (exist (pivot)) { piv = pivot; else piv = 1; }
-
- n = A.nc;
- pp = 1:n;
- I = [];
-
- for (k in 1:n)
- {
- if (piv)
- {
- d = diag(A);
- big = max (d[k:n]);
- m = maxi (d[k:n]);
- m = m+k-1;
- else
- big = A[k;k];
- m = k;
- }
- if (big <= 0)
- {
- I = k-1;
- break;
- }
-
- // Symmetric row/column permutations.
- if (m != k)
- {
- j = 1:k-1;
- if (all(all(j)))
- {
- temp = A[j;k];
- A[j;k] = A[j;m];
- A[j;m] = temp;
- }
- temp = A[k;k];
- A[k;k] = A[m;m];
- A[m;m] = temp;
- A[k;m] = conj(A[k;m]);
- j = k+1:m-1;
- if (all(all(j)))
- {
- temp = A[k;j]';
- A[k;j] = A[j;m]';
- A[j;m] = temp;
- }
- j = m+1:n;
- if (all(all(j)))
- {
- temp = A[k;j];
- A[k;j] = A[m;j];
- A[m;j] = temp;
- }
- pp[k, m] = pp[m, k];
- }
-
- A[k;k] = sqrt( A[k;k] );
- if (k == n) { break }
- A[k; k+1:n] = A[k; k+1:n] / A[k;k];
-
- // For simplicity update the whole of the remaining submatrix
- // (rather than just the upper triangle).
-
- j = k+1:n;
- A[j;j] = A[j;j] - A[k;j]'*A[k;j];
-
- } // Main k loop
-
- R = triu(A);
- if (isempty(I)) { I = n; }
- if (!piv)
- {
- P = I;
- else
- P = eye(n,n);
- P = P[;pp];
- }
-
- return << R = R; P = P; I = I >>;
- };
-