Discontinuous Galerkin Library
#include "dg/algorithm.h"
Loading...
Searching...
No Matches
Collaboration diagram for Extrapolation:

Classes

struct  dg::LeastSquaresExtrapolation< ContainerType0, ContainerType1 >
 Evaluate a least squares fit More...
 
struct  dg::Extrapolation< ContainerType >
 Extrapolate a polynomial passing through up to three points. More...
 

Functions

template<class ContainerType0 , class ContainerType1 , class ContainerType2 >
std::vector< double > dg::least_squares (const std::vector< ContainerType0 > &bs, const ContainerType1 &b, const ContainerType2 &weights)
 Compute \( a = (B^T W B)^{-1} WB^T b\) for given \( B\), weights \( W\) and right hand side \( b\).
 
template<class ContainerType0 , class ContainerType1 >
std::vector< double > dg::least_squares (const std::vector< ContainerType0 > &bs, const ContainerType1 &b)
 An alias for least_squares( bs, b, 1.)
 

Detailed Description

Function Documentation

◆ least_squares() [1/2]

template<class ContainerType0 , class ContainerType1 >
std::vector< double > dg::least_squares ( const std::vector< ContainerType0 > & bs,
const ContainerType1 & b )

An alias for least_squares( bs, b, 1.)

◆ least_squares() [2/2]

template<class ContainerType0 , class ContainerType1 , class ContainerType2 >
std::vector< double > dg::least_squares ( const std::vector< ContainerType0 > & bs,
const ContainerType1 & b,
const ContainerType2 & weights )

Compute \( a = (B^T W B)^{-1} WB^T b\) for given \( B\), weights \( W\) and right hand side \( b\).

This is the normal form of a least squares problem: given vectors \( b_i\) find coefficients \( a_i\) such that \( a_i b_i\) is as close as possible to a target vector \( b\), i.e. \( \min_a || B a - b||_W \) where the \( b_i\) constitute the columns of the matrix \( B\) and \( W\) are weights. This can be transformed into the solution of the normal equation

\[ B^T W B a = B^T W b\]

Note
With a little trick this function can be used to compute the least squares fit through a given list of points
// Least squares fit through three points
std::array<double,3> ones = {1,1,1};
std::array<double,3> xs = {0,1,2};
std::array<double,3> ys = {6,0,0};
// The ones refer to the constant a_0 in the formula y = a_0 + a_1 x
auto a = dg::least_squares( std::vector{ones, xs}, ys);
REQUIRE( a.size() == 2);
INFO( "Least squares fit: a[0] "<<a[0]<< " (5), a[1] "<<a[1]<<" (-3)");
CHECK( a[0]== 5);
CHECK( a[1]== -3);
Note
the algorithm used directly computes the components of \( B^T W B\) followed by an LU decomposition.
Parameters
bsa number of input vectors all with the same size
bmust have the same size as the bs
weightsdefine the norm that is minimized
Returns
the minimal coefficients a
Template Parameters
ContainerTypeAny class for which a specialization of TensorTraits exists and which fulfills the requirements of the there defined data and execution policies derived from AnyVectorTag and AnyPolicyTag. Among others
  • dg::HVec (serial), dg::DVec (cuda / omp), dg::MHVec (mpi + serial) or dg::MDVec (mpi + cuda / omp)
  • std::vector<dg::DVec> (vector of shared device vectors), std::array<double, 4> (array of 4 doubles) or std::map < std::string, dg::DVec> ( a map of named vectors)
  • double (scalar) and other primitive types ...
If there are several ContainerTypes in the argument list, then TensorTraits must exist for all of them
See also
See The dg dispatch system for a detailed explanation of our type dispatch system