Discontinuous Galerkin Library
#include "dg/algorithm.h"
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 >
std::vector< double > dg::least_squares (const std::vector< ContainerType0 > &bs, const ContainerType1 &b)
 Compute \( a = (B^T B)^{-1} B^T b\) for given \( B\) and \( a\). More...
 

Detailed Description

Function Documentation

◆ least_squares()

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

Compute \( a = (B^T B)^{-1} B^T b\) for given \( B\) and \( a\).

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|| \) where the \( b_i\) constitute the columns of the matrix \( B\) This can be transformed into the solution of the normal equation

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

Parameters
bsa number of input vectors all with the same size
bmust have the same size as the bs
Note
With a little trick this function can be used to compute the least squares fit through a given list of points
dg::HVec x(100), y(100);
// ... fill in x and y coordinates
dg::HVec ones(100, 1.);
// The ones signify the constant a_1 in the formula y = a_0x + a_1
auto a = dg::least_squares<dg::HVec>( {x, ones}, y);
// size of a is 2
@ y
y direction
@ x
x direction
thrust::host_vector< double > HVec
Host Vector.
Definition: typedefs.h:19
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