1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#include <iostream>
#include <iomanip>
#include <vector>
#include <complex>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <cstdlib>
using namespace std;
const double SMALL = 1.0e-30;
// Prototype functions
template<typename T> bool solve( vector< vector<T> > A, vector<T> B, vector<T> &X );
template<typename T> T polynomial( const vector<T> &P, T z );
//======================================================================
// Pade approximant [N][N] class
template<typename T>
class Pade
{
public:
int N;
vector<T> A, B; // polynomials in numerator and denominator, both of degree N
Pade( const vector<T> &C );
T evaluate( T z ) { return polynomial<T>( A, z ) / polynomial<T>( B, z ); }
};
template <typename T>
Pade<T>::Pade( const vector<T> &C )
{
assert( C.size() % 2 ); // expect C to have degree 2N; i.e. 2N+1 coefficients
N = C.size() / 2;
A = vector<T>(1+N);
B = vector<T>(1+N);
vector< vector<T> > M( N, vector<T>( N ) );
vector<T> RHS( N );
for ( int i = 0; i < N; i++ )
{
for ( int j = 0; j < N; j++ ) M[i][j] = C[N+i-j];
RHS[i] = -C[N+i+1];
}
vector<T> X( N );
if ( !solve( M, RHS, X ) )
{
cerr << "Sorry. Stuffed.\n";
exit( 1 );
}
copy( X.begin(), X.end(), B.begin() + 1 );
B[0] = 1.0;
for ( int i = 0; i <= N; i++ )
{
for ( int j = 0; j <= i; j++ ) A[i] += B[j] * C[i-j];
}
}
//======================================================================
int main()
{
const int N = 7;
// using TYPE = complex<double>;
using TYPE = double;
vector<TYPE> C(2*N+1);
// Let's try exp(); I can manage that
C[0] = 1;
for ( int j = 1; j <= 2 * N; j++ ) C[j] = C[j-1] / ( j + 0.0 );
Pade<TYPE> pade( C );
cout << "Table of approximants to exp(z) for Pade approximant [N][N] with N = " << pade.N << "\n\n";
// Compare
for ( int i = 0; i <= 10; i++ )
{
TYPE z = i * 0.5;
cout << setw( 20 ) << z << setw( 20 ) << exp( z ) << setw( 20 ) << pade.evaluate( z ) << '\n';
}
}
//======================================================================
template<typename T> bool solve( vector< vector<T> > A, vector<T> B, vector<T> &X )
//--------------------------------------
// Solve AX = B by Gaussian elimination
//--------------------------------------
{
int n = A.size();
// Row operations for i = 0, ,,,, n - 2 (n-1 not needed)
for ( int i = 0; i < n - 1; i++ )
{
// Pivot: find row r below with largest element in column i
int r = i;
double maxA = abs( A[i][i] );
for ( int k = i + 1; k < n; k++ )
{
double val = abs( A[k][i] );
if ( val > maxA )
{
r = k;
maxA = val;
}
}
if ( r != i )
{
for ( int j = i; j < n; j++ ) swap( A[i][j], A[r][j] );
swap( B[i], B[r] );
}
// Row operations to make upper-triangular
T pivot = A[i][i];
if ( abs( pivot ) < SMALL ) return false; // Singular matrix
for ( int r = i + 1; r < n; r++ ) // On lower rows
{
T multiple = A[r][i] / pivot; // Multiple of row i to clear element in ith column
for ( int j = i; j < n; j++ ) A[r][j] -= multiple * A[i][j];
B[r] -= multiple * B[i];
}
}
if ( abs( A[n-1][n-1] ) < SMALL ) return false; // Singular matrix
// Back-substitute
X = B;
for ( int i = n - 1; i >= 0; i-- )
{
for ( int j = i + 1; j < n; j++ ) X[i] -= A[i][j] * X[j];
X[i] /= A[i][i];
}
return true;
}
//======================================================================
template<typename T> T polynomial( const vector<T> &P, T z )
{
T result{};
for ( int i = P.size() - 1; i >= 0; i-- ) result = P[i] + result * z;
return result;
}
//======================================================================
|