Linear \( Ax = b\) and non-linear \( f(x) = b\).
More...
Linear \( Ax = b\) and non-linear \( f(x) = b\).
◆ FixedPointIteration
template<class ContainerType >
If you are looking for fixed point iteration: it is a special case of Anderson Acceleration.
◆ bisection1d()
template<typename UnaryOp >
int dg::bisection1d |
( |
UnaryOp && | op, |
|
|
double & | x_min, |
|
|
double & | x_max, |
|
|
const double | eps ) |
Find a root of a 1d function \( f(x) = 0\).
in given boundaries using bisection
It is assumed that a sign change occurs at the root. Function jumps closer to the root by checking the sign.
double xmin = 0, xmax = 2;
CHECK( num == 35);
CHECK( fabs( xmin - sqrt(2)) < 1e-9);
CHECK( fabs( xmax - sqrt(2)) < 1e-9);
- Template Parameters
-
UnaryOp | unary function operator |
- Parameters
-
op | Function or Functor |
x_min | left boundary, contains new left boundary on execution |
x_max | right boundary, contains new right boundary on execution |
eps | accuracy of the root finding. Algorithm successful if \( |x_{\max} - x_{\min}| < \varepsilon |x_{\max}| + \varepsilon \) |
- Returns
- number of function calls to reach the desired accuracy
- Exceptions
-
NoRoot1d | if no root lies between the given boundaries |
std::runtime_error | if after 60 steps the accuracy wasn't reached |
- Note
- If the root is found exactly then x_min = x_max
◆ inverse()
Invert a square matrix.
using lu decomposition in combination with our accurate scalar products
For example
unsigned n = 10;
double d = 1.00 + 1e-3;
for( unsigned u=0; u<n; u++)
t(u,u) = d;
for( unsigned i=0; i<n; i++)
for( unsigned j=0; j<n; j++)
{
double inv = i == j ? (d+n-2.0)/(d*(d+n-2.0) - (n-1.0))
: -1.0/(d*(d+n-2.0)-(n-1.0));
INFO( "Item ("<<i<<" "<<j<<") Inv "<<t_inv(i,j)<<" Ana "
<<inv<<" diff "<<t_inv(i,j)-inv);
CHECK( fabs((t_inv(i,j) - inv)/inv) < 1e-10);
}
- Template Parameters
-
- Parameters
-
- Returns
- the inverse of in if it exists
- Exceptions
-
std::runtime_error | if in is singular |
- Note
- uses extended accuracy of
dg::exblas
which makes it quite robust against almost singular matrices
◆ invert()
Compute inverse of square matrix (alias for dg::create::inverse
)
using lu decomposition in combination with our accurate scalar products
For example
unsigned n = 10;
double d = 1.00 + 1e-3;
for( unsigned u=0; u<n; u++)
t(u,u) = d;
for( unsigned i=0; i<n; i++)
for( unsigned j=0; j<n; j++)
{
double inv = i == j ? (d+n-2.0)/(d*(d+n-2.0) - (n-1.0))
: -1.0/(d*(d+n-2.0)-(n-1.0));
INFO( "Item ("<<i<<" "<<j<<") Inv "<<t_inv(i,j)<<" Ana "
<<inv<<" diff "<<t_inv(i,j)-inv);
CHECK( fabs((t_inv(i,j) - inv)/inv) < 1e-10);
}
- Template Parameters
-
- Parameters
-
- Returns
- the inverse of in if it exists
- Exceptions
-
std::runtime_error | if in is singular |
- Note
- uses extended accuracy of
dg::exblas
which makes it quite robust against almost singular matrices
◆ lu_pivot()
template<class T >
T dg::create::lu_pivot |
( |
dg::SquareMatrix< T > & | m, |
|
|
std::vector< unsigned > & | p ) |
LU Decomposition with partial pivoting.
For example
unsigned n = 10;
double eps = 1e-3;
for( unsigned u=0; u<n; u++)
t(u,u) = (1. + eps);
std::vector<unsigned> p;
double det = pow( eps, n)*(1+n/eps);
INFO( "Det "<<num<<" Ana "<<det<<" diff "<<(num-det)/det);
CHECK( fabs( num-det )/det < 1e-10);
- Template Parameters
-
- Exceptions
-
std::runtime_error | if the matrix is singular |
- Parameters
-
m | contains lu decomposition of input on output (inplace transformation) |
p | contains the pivot elements on output (will be resized) |
- Returns
- determinant of
m
- Note
- uses extended accuracy of
dg::exblas
which makes it quite robust against almost singular matrices
- See also
dg::lu_solve
◆ lu_solve()
template<class T >
void dg::lu_solve |
( |
const dg::SquareMatrix< T > & | lu, |
|
|
const std::vector< unsigned > & | p, |
|
|
std::vector< T > & | b ) |
Solve the linear system with the LU decomposition.
- Template Parameters
-
- Parameters
-
- See also
- dg::create::lu_pivot