Object-oriented Scientific Computing Library: Version 0.910
lu.h
Go to the documentation of this file.
00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006-2012, Andrew W. Steiner
00005   
00006   This file is part of O2scl.
00007   
00008   O2scl is free software; you can redistribute it and/or modify
00009   it under the terms of the GNU General Public License as published by
00010   the Free Software Foundation; either version 3 of the License, or
00011   (at your option) any later version.
00012   
00013   O2scl is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016   GNU General Public License for more details.
00017   
00018   You should have received a copy of the GNU General Public License
00019   along with O2scl. If not, see <http://www.gnu.org/licenses/>.
00020 
00021   -------------------------------------------------------------------
00022 */
00023 /** \file lu.h 
00024     \brief File for LU decomposition and associated solver
00025 */
00026 #ifndef O2SCL_LU_H
00027 #define O2SCL_LU_H
00028 
00029 #include <o2scl/err_hnd.h>
00030 #include <o2scl/permutation.h>
00031 #include <o2scl/cblas.h>
00032 #include <o2scl/vec_arith.h>
00033 
00034 namespace o2scl_linalg {
00035 
00036   /** \brief Specialized version of LU_decomp for C-style 2D arrays
00037 
00038       \note Note that \c N and \c n must be equal, by no checking
00039       is done to ensure that this is the case
00040    */
00041   template<size_t N>
00042     int LU_decomp_array_2d(const size_t n, double A[][N], 
00043                            o2scl::permutation &p, 
00044                            int &signum) {
00045     
00046     size_t i, j, k;
00047 
00048     signum=1;
00049     p.init();
00050   
00051     for (j = 0; j < N - 1; j++) {
00052     
00053       /* Find maximum in the j-th column */
00054       double ajj, max = fabs(A[j][j]);
00055       size_t i_pivot = j;
00056       
00057       for (i = j + 1; i < N; i++) {
00058         double aij = fabs (A[i][j]);
00059       
00060         if (aij > max) {
00061           max = aij;
00062           i_pivot = i;
00063         }
00064       }
00065 
00066       if (i_pivot != j) {
00067 
00068         // Swap rows j and i_pivot
00069         double temp;
00070         double *r1=&(A[j][0]);
00071         double *r2=&(A[i_pivot][0]);
00072         for (k=0;k<N;k++) {
00073           temp=r1[k];
00074           r1[k]=r2[k];
00075           r2[k]=temp;
00076         }
00077         p.swap(j,i_pivot);
00078         signum=-signum;
00079       }
00080       
00081       ajj = A[j][j];
00082       
00083       if (ajj != 0.0) {
00084         for (i = j + 1; i < N; i++) {
00085           double aij = A[i][j] / ajj;
00086           A[i][j]=aij;
00087           for (k = j + 1; k < N; k++) {
00088             double aik = A[i][k];
00089             double ajk = A[j][k];
00090             A[i][k]=aik - aij * ajk;
00091           }
00092         }
00093       }
00094     }
00095   
00096     return o2scl::gsl_success;
00097   }
00098   
00099 #define O2SCL_IX(V,i) V[i]
00100 #define O2SCL_IX2(M,i,j) M[i][j]
00101 #include <o2scl/lu_base.h>  
00102 #undef O2SCL_IX
00103 #undef O2SCL_IX2
00104 
00105 }
00106 
00107 namespace o2scl_linalg_paren {
00108   
00109 #define O2SCL_IX(V,i) V(i)
00110 #define O2SCL_IX2(M,i,j) M(i,j)
00111 #include <o2scl/lu_base.h>  
00112 #undef O2SCL_IX
00113 #undef O2SCL_IX2
00114   
00115 }
00116 
00117 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).

Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads.