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
-
bs | a number of input vectors all with the same size |
b | must 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
auto a = dg::least_squares<dg::HVec>( {
x, ones}, y);
thrust::host_vector< double > HVec
Host Vector.
Definition: typedefs.h:19
- Returns
- the minimal coefficients a
- Template Parameters
-
ContainerType | Any 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