Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

tnt_cholesky.h

Go to the documentation of this file.
00001 /*
00002 *
00003 * Template Numerical Toolkit (SPUC): Linear Algebra Module
00004 *
00005 * Mathematical and Computational Sciences Division
00006 * National Institute of Technology,
00007 * Gaithersburg, MD USA
00008 *
00009 *
00010 * This software was developed at the National Institute of Standards and
00011 * Technology (NIST) by employees of the Federal Government in the course
00012 * of their official duties. Pursuant to title 17 Section 105 of the
00013 * United States Code, this software is not subject to copyright protection
00014 * and is in the public domain.  The Template Numerical Toolkit (SPUC) is
00015 * an experimental system.  NIST assumes no responsibility whatsoever for
00016 * its use by other parties, and makes no guarantees, expressed or implied,
00017 * about its quality, reliability, or any other characteristic.
00018 *
00019 * BETA VERSION INCOMPLETE AND SUBJECT TO CHANGE
00020 * see http://math.nist.gov/tnt for latest updates.
00021 *
00022 */
00023 
00024 
00025 
00026 #ifndef CHOLESKY_H
00027 #define CHOLESKY_H
00028 
00029 #include <cmath>
00030 
00031 // index method
00032 
00033 namespace SPUC
00034 {
00035 
00036 
00037 //
00038 // Only upper part of A is used.  Cholesky factor is returned in
00039 // lower part of L.  Returns 0 if successful, 1 otherwise.
00040 //
00041 template <class SPDMatrix, class SymmMatrix>
00042 int Cholesky_upper_factorization(SPDMatrix &A, SymmMatrix &L)
00043 {
00044     int M = A.dim(1);
00045     int N = A.dim(2);
00046 
00047     assert(M == N);                 // make sure A is square
00048 
00049     // readjust size of L, if necessary
00050 
00051     if (M != L.dim(1) || N != L.dim(2))
00052         L = SymmMatrix(N,N);
00053 
00054     int i,j,k;
00055 
00056 
00057     typename SPDMatrix::element_type dot=0;
00058 
00059 
00060     for (j=1; j<=N; j++)                // form column j of L
00061     {
00062         dot= 0;
00063 
00064         for (i=1; i<j; i++)             // for k= 1 TO j-1
00065             dot = dot +  L(j,i)*L(j,i);
00066 
00067         L(j,j) = A(j,j) - dot;
00068 
00069         for (i=j+1; i<=N; i++)
00070         {
00071             dot = 0;
00072             for (k=1; k<j; k++)
00073                 dot = dot +  L(i,k)*L(j,k);
00074             L(i,j) = A(j,i) - dot;
00075         }
00076 
00077         if (L(j,j) <= 0.0) return 1;
00078 
00079         L(j,j) = sqrt( L(j,j) );
00080 
00081         for (i=j+1; i<=N; i++)
00082             L(i,j) = L(i,j) / L(j,j);
00083 
00084     }
00085 
00086     return 0;
00087 }
00088 
00089 
00090 
00091 
00092 }  
00093 // namespace SPUC
00094 
00095 #endif
00096 // CHOLESKY_H

Generated on Fri Sep 16 11:02:17 2005 for spuc by  doxygen 1.4.4