Developer documentation

linear_doolittle.hpp
Go to the documentation of this file.
1 /********************************************************************
2  * This file is part of the source code of the realroot library.
3  * Author(s): J.P. Pavone, GALAAD, INRIA
4  * $Id: doolittle.hpp,v 1.1 2005/07/11 10:03:55 jppavone Exp $
5  ********************************************************************/
6 #ifndef realroot_SOLVE_SBDSLV_LINEAR_DOOLITTLE_HPP
7 #define realroot_SOLVE_SBDSLV_LINEAR_DOOLITTLE_HPP
8 //--------------------------------------------------------------------
10 //--------------------------------------------------------------------
11 namespace mmx {
12 //--------------------------------------------------------------------
13 namespace linear
14 {
15 
16  template< typename real_t >
17  void Lsolve(real_t * x , real_t const * const L, real_t const *const b, unsigned n )
18  {
19  for ( unsigned i = 0; i < n ; i ++ )
20  {
21  real_t sum = 0.0;
22  for ( unsigned j = 0; j < i; j ++ ) sum += L[i*n+j]*x[j];
23  x[i] = (b[i]-sum);
24  };
25  };
26 
27  template< typename real_t >
28  bool Usolve( real_t * x, real_t const * const U, real_t const *const b, unsigned n )
29  {
30  for ( int i = n-1; i >=0 ; i -- )
31  {
32  real_t sum = 0.0;
33  for ( int j = n-1; j > i; j -- ) sum += U[i*n+j]*x[j];
34  if ( std::abs(U[i*n+i]) < numerics::epsilon<real_t>::result*n*n) return false;
35  x[i] = (b[i]-sum)/U[i*n+i];
36  };
37  return true;
38  };
39 
40  template< typename real_t >
41  bool doolittle( real_t const * const A, real_t * L, real_t * U, unsigned n )
42  {
43  unsigned k,i,p;
44  for ( k = 0; k < n; k++ )
45  {
46  // valueof(k);
47  for ( i = k; i < n; i ++ )
48  {
49  U[ k*n + i ] = A[ k*n + i ];
50  for ( p = 0; p < k; p ++ ) U[ k*n + i ] -= L[ k*n + p ]*U[ p*n + i ];
51  };
52  for ( i = k + 1; i < n; i ++ )
53  {
54  if ( std::abs( U[ k*n + k ] ) < numerics::epsilon<real_t>::result*n*n ) return false;
55  real_t sum = 0.0;
56  for ( p = 0; p < k; p ++ ) sum += L[i*n+p]*U[p*n+k];
57  L[i*n+k] = (A[i*n + k] - sum)/U[ k*n + k ];
58  };
59  };
60  return true;
61  };
62 
63  template< typename real_t >
64  bool LUsolve( real_t * x, real_t const * const M, real_t const * const b, unsigned n )
65  {
66  real_t tmp[ n*n ];
67  real_t xtmp[n] ;
68  if ( !doolittle( M, tmp, tmp, n ) ) return false;
69  Lsolve( xtmp, tmp, b, n );
70  return Usolve( x, tmp, xtmp, n );
71  };
72 
73  template< typename real_t >
74  bool LUinverse( real_t * iM, real_t const * const M, unsigned n )
75  {
76  real_t lu[n*n];
77  real_t xtmp[n] ;
78  real_t tmp[n];
79  real_t _b[n+1];
80  real_t * b = _b+1;
81  std::fill( b, b + n , (real_t)0.0 );
82  if ( ! doolittle( M, lu, lu, n ) ) return false;
83  for ( unsigned c = 0; c < n; c ++ )
84  {
85  if (c > 0) b[c-1] = 0;
86  b[c] = 1;
87  Lsolve( xtmp, lu, b, n );
88  if ( !Usolve( tmp, lu, xtmp, n ) ) return false;
89  for ( unsigned i = 0; i < n; i ++ ) iM[ i*n + c ] = tmp[i];
90  };
91  return true;
92  };
93 
94 };
95 //--------------------------------------------------------------------
96 } //namespace mmx
97 /********************************************************************/
98 #endif //
Definition: system_epsilon.hpp:19
void fill(C *a, unsigned n, int s, const C &x)
Definition: tensor_vctops.hpp:152
const C & b
Definition: Interval_glue.hpp:25
bool doolittle(real_t const *const A, real_t *L, real_t *U, unsigned n)
Definition: linear_doolittle.hpp:41
bool Usolve(real_t *x, real_t const *const U, real_t const *const b, unsigned n)
Definition: linear_doolittle.hpp:28
void Lsolve(real_t *x, real_t const *const L, real_t const *const b, unsigned n)
Definition: linear_doolittle.hpp:17
real_t sum(real_t const *const src, unsigned sz, int st=1)
Definition: loops_vctops.hpp:197
void abs(Interval< C, r > &x, const Interval< C, r > &a)
Definition: Interval_fcts.hpp:185
bool LUsolve(real_t *x, real_t const *const M, real_t const *const b, unsigned n)
Definition: linear_doolittle.hpp:64
const C & c
Definition: Interval_glue.hpp:45
bool LUinverse(real_t *iM, real_t const *const M, unsigned n)
Definition: linear_doolittle.hpp:74
Definition: array.hpp:12
Home