# least_squares Namespace Reference

## Typedefs

typedef Vx::value_type T

## Functions

template<class T >
void invert (M(T)&m)
Invert a matrix, in-place, using Gauss-Jordan elimination.
template void invert (M(float)&m)
template void invert (M(double)&m)
template<class T >
V (T) linearFit(const M(T)&x
Returns parameters 'a' via ordinary least squares such that y = x·a.
template V (float) linearFit(const M(float)&x
template V (double) linearFit(const M(double)&x
template<class Vx , class Vy >
V (typename Vx::value_type) polyFit(size_t degree
Returns fit 'a' of degree powers of x: y = [ 1 x .. xⁿ ] · a.
const Vx const Vy &y if (degree >0)
if (y.size()< degree)
M (T) xn(V(T)(1
return linearFit (xn, V(T)(&y[0], N))

## Variables

const Vx & x
const Vx const Vy & y
const size_t N = y.size()
degree

## Detailed Description

Least Squares Solver by Ethan Tira-Thompson, © 2011

Solves linear systems using Ordinary Least Squares.

Provides the namespace 'least_squares'.

There are two solver functions:

linearFit(x,y) Returns the parameters 'a' such that y = x·a minimizes squared error. x should be a valarray< valarray<T> >, where each x[i] is a column of data with length matching that of the valarray y.

polyFit(degree,x,y) Performs a polynomial fit of degree n: y = [ 1 x .. xⁿ ] · a given x and y (as vector or valarray) Results will be in order of increasing power (i.e. index 0 is constant term, 1 is linear term, etc.)

Additionally, there are two helper functions:

polyApply(poly,x) Computes the polynomial of 'x' given polynomial terms 'poly'. These arguments can be either vector or valarray.

invert(x) Inverts a square valarray matrix 'x', overwriting results in-place. Throws std::underflow_error if the matrix is deficient.

TODO: does not handle degenerate/low-rank conditions very well. Ideally, the fit functions should handle internally instead of allowing invert()'s std::underflow_error to pass through

LICENSE: least_squares is free software: you can redistribute it and/or modify it under the terms of the Lesser GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version, waiving any requirement to distribute the license document itself.

least_squares is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public License for more details.

## Typedef Documentation

 typedef Vx::value_type least_squares::T

Definition at line 119 of file least_squares.h.

## Function Documentation

 least_squares::if ( )

Definition at line 115 of file least_squares.h.

 least_squares::if ( degree , 0 )
Type Constraints

Definition at line 122 of file least_squares.h.

 template void least_squares::invert ( M(double)& m )
 template void least_squares::invert ( M(float)& m )
template<class T >
 void least_squares::invert ( M(T)& m )

Invert a matrix, in-place, using Gauss-Jordan elimination.

Throws std::underflow_error if the matrix is deficient.

 return least_squares::linearFit ( xn , V(T)&[0], N )
 least_squares::M ( T )
template<class Vx , class Vy >
 least_squares::V ( typename Vx::value_type )

Returns fit 'a' of degree powers of x: y = [ 1 x .. xⁿ ] · a.

Computes the polynomial of x given terms from poly.

Accepts vector or valarray... results will be in order of increasing power (i.e. 0 is constant term, 1 is linear term, etc.)

TODO: does not handle degenerate/low-rank conditions. Ideally, this should be handled internally instead of allowing invert()'s std::underflow_error to pass through.

Accepts either vector and valarray arguments... poly terms should be in order of increasing degree à la polyFit()

 template const least_squares::V ( double ) const
 template const least_squares::V ( float ) const
template<class T >
 const least_squares::V ( T ) const

Returns parameters 'a' via ordinary least squares such that y = x·a.

Each element of x should be a valarray with length matching that of y. Will then compute invert(x' * x) * x' * y to find the fit.

TODO: does not handle degenerate/low-rank conditions. Ideally, this should be handled internally instead of allowing invert()'s std::underflow_error to pass through.

Definition at line 71 of file least_squares.cc.

## Variable Documentation

Definition at line 121 of file least_squares.h.

 Tekkotsu v5.1CVS Generated Mon May 9 04:59:24 2016 by Doxygen 1.6.3