C:/programs/SCPPNT/src/include/trisolve.h

00001 /* \file trisolve.h
00002  \brief Functions for triangular solves.
00003 
00004  Functions
00005  Modified version of trisolve.h from the Template Numerical Toolkit (TNT) 
00006  using matrix iterators rather than matrix subscripting.
00007 
00008  Contains modified version of the functions in trisolve.h from the
00009  Template Numerical Toolkit (TNT) using matrix iterators rather than
00010  matrix subscripting.
00011  */
00012 
00013 /*
00014 
00015  Simple C++ Numerical Toolkit (SCPPNT)
00016  http://www.smallwaters.com/software/cpp/scppnt.html
00017  This release updates original work contributed by 
00018  Brad Hanson (http://www.b-a-h.com/) in 2001.
00019 
00020  */
00021 
00022 /*
00023  *
00024  * Template Numerical Toolkit (TNT): Linear Algebra Module
00025  *
00026  * Mathematical and Computational Sciences Division
00027  * National Institute of Technology,
00028  * Gaithersburg, MD USA
00029  *
00030  *
00031  * This software was developed at the National Institute of Standards and
00032  * Technology (NIST) by employees of the Federal Government in the course
00033  * of their official duties. Pursuant to title 17 Section 105 of the
00034  * United States Code, this software is not subject to copyright protection
00035  * and is in the public domain.  The Template Numerical Toolkit (TNT) is
00036  * an experimental system.  NIST assumes no responsibility whatsoever for
00037  * its use by other parties, and makes no guarantees, expressed or implied,
00038  * about its quality, reliability, or any other characteristic.
00039  *
00040  * BETA VERSION INCOMPLETE AND SUBJECT TO CHANGE
00041  * see http://math.nist.gov/tnt for latest updates.
00042  *
00043  */
00044 
00045 // Triangular Solves
00046 
00047 #ifndef SCPPNT_TRISLV_H
00048 #define SCPPNT_TRISLV_H
00049 
00050 #ifdef SCPPNT_NO_DIR_PREFIX
00051 #include "scppnt.h"
00052 #include "triang.h"
00053 #else
00054 #include "scppnt/scppnt.h"
00055 #include "scppnt/triang.h"
00056 #endif
00057 
00058 namespace SCPPNT
00059 {
00060 
00061   //! Solve linear system Ax=b using lower triangular portion of A including diagonal
00062   template<class MaTriX, class VecToR> VecToR Lower_triangular_solve(const MaTriX &A,
00063       const VecToR &b)
00064   {
00065     Subscript N = A.num_rows();
00066 
00067     // make sure matrix sizes agree; A must be square
00068 
00069     //assert(A.num_columns() == N);
00070     //assert(b.dim() == N);
00071     if (A.num_columns() != N || b.dim() != N)
00072       throw BoundsError("SCPPNT::Lower_triangular_solve");
00073 
00074     VecToR x(N);
00075     x = 0;
00076 
00077     Subscript i;
00078     typename VecToR::iterator ix = x.begin();
00079     typename VecToR::const_iterator ib = b.begin();
00080     typename MaTriX::const_rows_iterator iA = A.begin_rows();
00081     typename MaTriX::const_diag_iterator dA = A.begin_diagonal(1, 1);
00082     for (i=1; i<=N; i++, ++ix, ++ib, ++iA, ++dA)
00083     {
00084       typename MaTriX::element_type tmp=0;
00085 
00086       typename MaTriX::const_row_iterator jA = *iA;
00087       typename VecToR::iterator jx = x.begin();
00088       for (Subscript j=1; j<i; j++, ++jA, ++jx)
00089         tmp += *jA * *jx; // tmp = tmp + A(i,j)*x(j);
00090 
00091       *ix = *ib - tmp; // x(i) =  (b(i) - tmp)/ A(i,i);
00092       *ix /= *dA;
00093     }
00094 
00095     return x;
00096   }
00097 
00098   //! Solve linear system Ax=b using lower triangular portion of A assuming unit diagonal
00099   template<class MaTriX, class VecToR> VecToR Unit_lower_triangular_solve(const MaTriX &A,
00100       const VecToR &b)
00101   {
00102     Subscript N = A.num_rows();
00103 
00104     // make sure matrix sizes agree; A must be square
00105 
00106     //assert(A.num_columns() == N);
00107     //assert(b.dim() == N);
00108     if (A.num_columns() != N || b.dim() != N)
00109       throw BoundsError("SCPPNT::Unit_lower_triangular_solve");
00110 
00111     VecToR x(N);
00112     x = 0;
00113 
00114     Subscript i;
00115     typename VecToR::iterator ix = x.begin();
00116     typename VecToR::const_iterator ib = b.begin();
00117     typename MaTriX::const_rows_iterator iA = A.begin_rows();
00118     for (i=1; i<=N; i++, ++ix, ++ib, ++iA)
00119     {
00120 
00121       typename MaTriX::element_type tmp=0;
00122 
00123       typename MaTriX::const_row_iterator jA = *iA;
00124       typename VecToR::iterator jx = x.begin();
00125       for (Subscript j=1; j<i; j++, ++jA, ++jx)
00126         tmp += *jA * *jx; // tmp = tmp + A(i,j)*x(j);
00127 
00128       *ix = *ib - tmp; // x(i) =  b(i) - tmp;
00129     }
00130 
00131     return x;
00132   }
00133 
00134   //! Solve linear system using lower triangular view A
00135   template<class MaTriX, class VecToR> VecToR linear_solve(const LowerTriangularView<MaTriX> &A,
00136       const VecToR &b)
00137   {
00138     return Lower_triangular_solve(A, b);
00139   }
00140 
00141   //! Solve linear system using unit lower triangular view A
00142   template<class MaTriX, class VecToR> VecToR linear_solve(
00143       const UnitLowerTriangularView<MaTriX> &A, const VecToR &b)
00144   {
00145     return Unit_lower_triangular_solve(A, b);
00146   }
00147 
00148   //********************** Upper triangular section ****************
00149 
00150   //! Solve linear system Ax=b using upper triangular portion of A including diagonal
00151   template<class MaTriX, class VecToR> VecToR Upper_triangular_solve(const MaTriX &A,
00152       const VecToR &b)
00153   {
00154     Subscript N = A.num_rows();
00155 
00156     // make sure matrix sizes agree; A must be square
00157 
00158     //assert(A.num_columns() == N);
00159     //assert(b.dim() == N);
00160     if (A.num_columns() != N || b.dim() != N)
00161       throw BoundsError("SCPPNT::Upper_triangular_solve");
00162 
00163     VecToR x(N);
00164     x = 0;
00165 
00166     Subscript i;
00167     typename VecToR::iterator ix = x.begin() + N - 1;
00168     typename VecToR::const_iterator ib = b.begin() + N - 1;
00169     typename MaTriX::const_rows_iterator iA = A.begin_rows() + N - 1;
00170     typename MaTriX::const_diag_iterator dA = A.begin_diagonal(1, 1) + N - 1;
00171     for (i=N; i>=1; i--, --ix, --ib, --iA, --dA)
00172     {
00173 
00174       typename MaTriX::element_type tmp=0;
00175 
00176       typename MaTriX::const_row_iterator jA = *iA + i;
00177       typename VecToR::iterator jx = x.begin() + i;
00178       for (Subscript j=i+1; j<=N; j++, ++jA, ++jx)
00179         tmp += *jA * *jx; // tmp = tmp + A(i,j)*x(j);
00180 
00181       *ix = *ib - tmp; // x(i) =  (b(i) - tmp)/ A(i,i);
00182       *ix /= *dA;
00183     }
00184 
00185     return x;
00186   }
00187 
00188   //! Solve linear system Ax=b using upper triangular portion of A assuming unit diagonal
00189   template<class MaTriX, class VecToR> VecToR Unit_upper_triangular_solve(const MaTriX &A,
00190       const VecToR &b)
00191   {
00192     Subscript N = A.num_rows();
00193 
00194     // make sure matrix sizes agree; A must be square
00195 
00196     //assert(A.num_columns() == N);
00197     //assert(b.dim() == N);
00198     if (A.num_columns() != N || b.dim() != N)
00199       throw BoundsError("SCPPNT::Unit_upper_triangular_solve");
00200 
00201     VecToR x(N);
00202     x = 0;
00203 
00204     Subscript i;
00205     typename VecToR::iterator ix = x.begin() + N - 1;
00206     typename VecToR::const_iterator ib = b.begin() + N - 1;
00207     typename MaTriX::const_rows_iterator iA = A.begin_rows() + N - 1;
00208     for (i=N; i>=1; i--, --ix, --ib, --iA)
00209     {
00210 
00211       typename MaTriX::element_type tmp=0;
00212 
00213       typename MaTriX::const_row_iterator jA = *iA + i;
00214       typename VecToR::iterator jx = x.begin() + i;
00215       for (Subscript j=i+1; j<i; j++, ++jA, ++jx)
00216         tmp += *jA * *jx; // tmp = tmp + A(i,j)*x(j);
00217 
00218       *ix = *ib - tmp; // x(i) =  b(i) - tmp;
00219     }
00220 
00221     return x;
00222   }
00223 
00224   //! Solve linear system using upper triangular view A
00225   template<class MaTriX, class VecToR> VecToR linear_solve(const UpperTriangularView<MaTriX> &A,
00226       const VecToR &b)
00227   {
00228     return Upper_triangular_solve(A, b);
00229   }
00230 
00231   //! Solve linear system using unit upper triangular view A
00232   template<class MaTriX, class VecToR> VecToR linear_solve(
00233       const UnitUpperTriangularView<MaTriX> &A, const VecToR &b)
00234   {
00235     return Unit_upper_triangular_solve(A, b);
00236   }
00237 
00238 } // namespace SCPPNT
00239 
00240 #endif
00241 // SCPPNT_TRISLV_H

Generated on Tue Dec 18 23:34:06 2007 for SCPPNT by  doxygen 1.5.4