00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
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 
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     
00068 
00069     
00070     
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; 
00090 
00091       *ix = *ib - tmp; 
00092       *ix /= *dA;
00093     }
00094 
00095     return x;
00096   }
00097 
00098 
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     
00105 
00106     
00107     
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; 
00127 
00128       *ix = *ib - tmp; 
00129     }
00130 
00131     return x;
00132   }
00133 
00134 
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 
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   
00149 
00150 
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     
00157 
00158     
00159     
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; 
00180 
00181       *ix = *ib - tmp; 
00182       *ix /= *dA;
00183     }
00184 
00185     return x;
00186   }
00187 
00188 
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     
00195 
00196     
00197     
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; 
00217 
00218       *ix = *ib - tmp; 
00219     }
00220 
00221     return x;
00222   }
00223 
00224 
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 
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 } 
00239 
00240 #endif
00241