TBCI Numerical high perf. C++ Library 2.8.0
expm.h
Go to the documentation of this file.
1
4/* $Id: expm.h,v 1.8.2.8 2019/05/28 11:13:02 garloff Exp $ */
5
6#ifndef TBCI_SOLVER_EXPM_H
7#define TBCI_SOLVER_EXPM_H
8
9#include "tbci/basics.h"
10#include "tbci/matrix.h"
11
13
14INST(template < Matrix<T> > class NN friend double norm_1 (const Matrix<T>&);)
15INST(template <BdMatrix<T> > class NN friend double norm_1 (const BdMatrix<T>&);)
16
17template <typename Matrix>
18double norm_1(const Matrix& mat)
19{
20 double max = 0;
21 double sum;
22
23 for (unsigned int i=0; i<mat.rows(); ++i) {
24 sum = MATH__ fabs(mat.get(i, 0));
25 for (unsigned int j=1; j<mat.columns(); ++j)
26 sum += MATH__ fabs(mat.get(i, j));
27 max = MAX(max, sum);
28 }
29 return max;
30}
31
33INST(template <typename T> class TMatrix friend double norm_1 (const TMatrix<T>&);)
34template <typename T>
35double norm_1 (const TMatrix<T>& mat)
36{
37 double max = 0;
38 double sum;
39
40 for (unsigned int i=0; i<mat.rows(); ++i) {
41 sum = MATH__ fabs(mat.get(i, 0));
42 for (unsigned int j=1; j<mat.columns(); ++j)
43 sum += MATH__ fabs(mat.get(i, j));
44 max = MAX(max, sum);
45 }
46 mat.mark_destroy();
47 return max;
48}
49
50INST(template < Matrix<T> > class NN friend void expm (const Matrix<T>&, Matrix<T>&);)
51//INST(template < BdMatrix<T> > class NN friend void expm (const BdMatrix<T>&, BdMatrix<T>&);)
52
53//******************************************************************
54// Exponential Entwicklung nach Pade (siehe MatLab expm1.m)
55//******************************************************************
56template<typename Matrix>
57void expm (const Matrix& exponent, Matrix& E)
58{
59 // Matrizen kopieren
60 Matrix temp(exponent);
61
62 unsigned int dim = exponent.rows();
63 int e,p,q,k;
64
65 //------------------------------------------------
66 // Einheitsmatrix generieren
67 //------------------------------------------------
68 Matrix eye(0.0, dim,dim);
69 for(unsigned int j=0;j<dim;j++) eye(j,j)=1.0;
70
71 //Die Matrix normieren, so dass norm_1 <1/2
72 double z_norm = norm_1(exponent);
73 /*double f =*/ frexp(z_norm,&e);
74 int s = MAX(0, e+1);
75 temp /= (pow(2.0, s));
76
77 // Naeherung fuer exp(exponent) (Pade)
78 Matrix X(temp);
79 Matrix cX(dim,dim);
80 double c=0.5;
81 Matrix D (eye-temp*c);
82 E = eye+temp*c;
83 q = 6;
84 p = 1;
85
86 for(k=2;k<=q;k++)
87 {
88 c = c*(q-k+1)/(k*(2*q-k+1));
89 X = X*temp;
90 cX = X*c;
91 E += cX;
92 if(p)
93 D += cX;
94 else
95 D -= cX;
96 p = !p;
97 }
98
99 //Gleichungssystem loesen
100 E = TBCI__ lu_solve(D,E);
101
102 // Entnormieren
103 for(k=1;k<=s;k++) E = E*E;
104}
105
106
107INST2(template < Matrix<T>, Matrix<T> > class NN friend \
108 void expm2 (const Matrix<T>& exponent, Matrix<T>& erg, double);)
109INST2(template < BdMatrix<T>, Matrix<T> > class NN friend \
110 void expm2 (const BdMatrix<T>& exponent, Matrix<T>& erg, double);)
111
112//******************************************************************
113// Exponential Entwicklung mit Taylorreihe
114//******************************************************************
115template<typename MatrixIn, typename MatrixOut>
116void expm2 (const MatrixIn& exponent, MatrixOut& erg, double ERROR=1e-3)
117{
118 unsigned int dim=exponent.rows();
119
120 //Die Matrix normieren, so dass norm_1 <1/2
121 int e;
122 double z_norm=norm_1(exponent);
123 /*double f =*/ frexp(z_norm,&e);
124 int s = MAX(0, e+1);
125 MatrixIn scaled(exponent);
126 double scale_factor = (pow(2.0, s));
127 scaled/=scale_factor;
128
129// STD__ cout << "\nscale_factor = " << scale_factor << STD__ endl;
130
131 //***************************************************************
132 // Naeherung fuer exp(exponent) (Taylor-Reihe)
133 //***************************************************************
134 MatrixOut temp(scaled);
135 double error=1;
136
137 // Reihenglieder 0 und 1 (umgekehrte Reihenfolge!)
138 erg = temp;
139 for (unsigned int j=0; j<dim; j++) erg(j,j) += 1;
140
141 // weitere Reihenglieder
142 int i;
143 for(i=2; i<100 && error>ERROR; i++)
144 {
145 temp = (temp/i)*scaled;
146 erg += temp;
147
148 error /= i;
149 //STD__ cout << "Genauigkeit=" << error << " " << 1/error << STD__ endl;
150 }
151// cout<<"Iterationen "<<i<<endl;
152
153 // Entnormieren
154 for(int k=1;k<=s;k++) erg = erg*erg;
155}
156
157
158
159INST2(template <Matrix<T>, Matrix<T> > class NN friend \
160 void expm3 (const Matrix<T>& exponent, Matrix<T>& erg, double);)
161INST2(template <BdMatrix<T>, Matrix<T> > class NN friend \
162 void expm3 (const BdMatrix<T>& exponent, Matrix<T>& erg, double);)
163//******************************************************************
164// Exponential Entwicklung als Reihennaeherung
165//******************************************************************
166template<typename MatrixIn, typename MatrixOut>
167void expm3 (const MatrixIn& exponent, MatrixOut& erg, double ERROR=1e-3)
168{
169 unsigned int dim=exponent.rows();
170 double z_norm=0;
171
172 //Die Matrix normieren auf 0.1
173 for (unsigned int i=0; i<dim; i++)
174 for (unsigned int j=0; j<dim; j++)
175 z_norm=MAX(z_norm, (double)MATH__ fabs(exponent(i,j)));
176
177 z_norm /= 0.1;
178
179 int e;
180 /*double f =*/ frexp(z_norm,&e);
181 int s = MAX(0, e+1);
182 double scale_factor = (pow(2.0, s));
183 double error = 10;
184
185 MatrixOut temp(exponent);
186
187// STD__ cout << z_norm << " " << s << " " << scale_factor << STD__ endl;
188
189 for(int iter=0; iter<100 && error>ERROR; iter+=4)
190 {
191
192// STD__ cout << "s=" << s << STD__ endl;
193 scale_factor = (pow(2.0, s));
194 erg = exponent/(scale_factor);
195 for(unsigned int j=0; j<dim; j++) erg(j,j) += 1;
196 for(int k=1;k<=s;k++)
197 erg = erg*erg;
198 error = norm_1 (erg - temp);
199
200 STD__ cout << error << STD__ endl;
201 temp=erg;
202 s++;
203 }
204
205}
206
208
209#endif /* TBCI_SOLVER_EXPM_H */
const Vector< T > const Vector< T > const Vector< T > & p
Definition LM_fit.h:98
int i
Definition LM_fit.h:71
#define STD__
Definition basics.h:338
#define NAMESPACE_END
Definition basics.h:323
#define INST(x)
Definition basics.h:238
#define NAMESPACE_TBCI
Definition basics.h:317
#define TBCI__
Definition basics.h:332
#define MATH__
Definition basics.h:339
#define INST2(x, y)
Definition basics.h:239
#define MAX(a, b)
Definition basics.h:656
The class BdMatrix is an implementation to store and do operations on sparse Matrices with a band str...
unsigned int rows() const
number of rows
Definition matrix.h:317
tbci_traits< T >::const_refval_type get(const unsigned r, const unsigned c) const
get, set and getcref are used internally and not for public consumption
Definition matrix.h:288
void mark_destroy() const
mark destructible
Definition matrix.h:402
unsigned int columns() const
number of columns
Definition matrix.h:315
double fabs(const TBCI__ cplx< T > &c)
Definition cplx.h:746
TBCI__ cplx< T > pow(const TBCI__ cplx< T > &z, const double n)
Definition cplx.h:761
void expm(const Matrix &exponent, Matrix &E)
Definition expm.h:57
Matrix< T > class NN friend void expm3(const BdMatrix< T > &exponent, Matrix< T > &erg, double)
Matrix< T > class NN friend void expm2(const BdMatrix< T > &exponent, Matrix< T > &erg, double)
NAMESPACE_TBCI double norm_1(const Matrix &mat)
Definition expm.h:18
#define max(a, b)
Definition f2c.h:181
return c
Definition f_matrix.h:760
T sum(const FS_Vector< dims, T > &fv)
Definition fs_vector.h:599
F_TMatrix< double > lu_solve(const F_Matrix< double > &A, const F_Matrix< double > &B, int overwriteA=0)
Definition lapack.cpp:156