63 cerr <<
"polynomial_fit : order must be >= 1" << endl;
68 cerr <<
"polynomial_fit : x and y must have same dimension" << endl;
72 if(weights.
n() != y.
n()){
73 cerr <<
"polynomial_fit : weights must have same dimension as x and y" << endl;
78 cerr <<
"polynomial_fit : x and y must have at least order+1 elements" 94 for(
ssize_t col=0;col<=order;col++){
95 A(
row,col) = pow(x[
row],(
double)col) * weights[
row];
110 if(!
inverse(At_A,At_A_inv,singularity))
112 cerr <<
"polynomial_fit : inverse failed (";
113 if(singularity == -2)
114 cerr <<
"unspecified reason)" << endl;
115 else if(singularity == -1)
116 cerr <<
"non-square !!)" << endl;
119 cerr <<
"singularity at point : " << singularity;
120 cerr <<
" = " << x[singularity] <<
"," << y[singularity];
121 cerr <<
" )" << endl;
127 co_effs = At_A_inv * At_y1;
168 cerr <<
"diagonalise: non-square matrix ";
196 for (i = I = 0; i < n; ++i, ++I)
200 for (j = J = 0; j < n; ++j, ++J)
219 s(0, i) = a.
a(row, i);
230 s(i, 0) = a.
a(i, col);
263 for (i = 0; i < n; ++i)
264 for (j = 0; j < n; ++j)
265 t(n - i - 1, n - j - 1) = a.
a(i, j);
304 return inverse(a,inv,singularity);
400 singularity = ((this_row > j) ? this_row : j);
423 return inverse(a,inv,singularity);
432 if (!
inverse(atrans_a,atrans_a_inverse,singularity))
434 multiply(atrans_a_inverse,a_trans,inv);
447 cerr <<
"Tried to take determinant of non-square matrix\n";
461 for (i = 0; i < n; ++i)
463 p = (double)(i + j + 2);
471 for (i = 0; i < n; ++i)
496 cerr <<
"Can't make non-square identity matrix !" << endl;
513 cerr <<
"Can't subtract vectors of differing lengths !" << endl;
534 cerr <<
"Can't subtract vectors of differing lengths !" << endl;
553 cerr <<
"Can't extract diagonal of non-square matrix !" << endl;
584 cerr <<
"Can't symmetrize non-square matrix !" << endl;
616 r = scale * ((double)rand()/(double)
RAND_MAX);
628 r = scale * ((double)rand()/(double)
RAND_MAX);
638 cerr <<
"Can't make non-square symmetric matrix !" << endl;
647 r = scale * ((double)rand()/(double)
RAND_MAX);
658 cerr <<
"Can't make non-square symmetric matrix !" << endl;
674 cerr <<
"Can't make polynomial basis function : dimension mismatch !" << endl;
675 cerr <<
"t.length()=" << t.
length();
676 cerr <<
" T.num_rows()=" << T.
num_rows() << endl;
int floor_matrix(EST_DMatrix &M, const double floor)
EST_DMatrix backwards(EST_DMatrix &a)
int inverse(const EST_DMatrix &a, EST_DMatrix &inv)
inverse
void stack_matrix(const EST_DMatrix &M, EST_DVector &v)
stack columns on top of each other to make a vector
void transpose(const EST_DMatrix &a, EST_DMatrix &b)
exchange rows and columns
ssize_t num_columns() const
return number of columns
INLINE const T & a_no_check(ssize_t n) const
read-only const access operator: without bounds checking
EST_DMatrix triangulate(const EST_DMatrix &a)
ssize_t num_rows() const
return number of rows
const T & a(ssize_t row, ssize_t col) const
bool polynomial_fit(EST_DVector &x, EST_DVector &y, EST_DVector &co_effs, int order)
least squares fit
void inplace_diagonalise(EST_DMatrix &a)
inplace diagonalise
EST_DVector subtract(const EST_DVector &a, const EST_DVector &b)
elementwise subtract
void symmetrize(EST_DMatrix &a)
enforce symmetry
void fill(const T &v)
fill matrix with value v
INLINE ssize_t length() const
number of items in vector.
EST_DVector diagonal(const EST_DMatrix &a)
extract leading diagonal as a vector
double polynomial_value(const EST_DVector &coeffs, const double x)
EST_DMatrix sub(const EST_DMatrix &a, ssize_t row, ssize_t col)
void make_poly_basis_function(EST_DMatrix &T, EST_DVector t)
double determinant(const EST_DMatrix &a)
EST_DMatrix cov_prod(const EST_DVector &v1, const EST_DVector &v2)
matrix product of two vectors (#rows = length of first vector, #cols = length of second vector) ...
void make_random_diagonal_matrix(EST_DMatrix &M, const double scale)
used for variance
EST_DMatrix diagonalise(const EST_DMatrix &a)
extract leading diagonal as a matrix
void multiply(const EST_DMatrix &a, const EST_DMatrix &b, EST_DMatrix &ab)
EST_DVector add(const EST_DVector &a, const EST_DVector &b)
elementwise add
void make_random_matrix(EST_DMatrix &M, const double scale)
all elements are randomised
A vector class for double precision floating point numbers. EST_DVector x should be used instead of f...
void make_random_vector(EST_DVector &V, const double scale)
all elements are randomised
double matrix_max(const EST_DMatrix &a)
int square(const EST_DMatrix &a)
double sum(const EST_DMatrix &a)
sum of elements
INLINE const T & a_no_check(ssize_t row, ssize_t col) const
const access with no bounds check, care recommend
void resize(int rows, int cols, int set=1)
resize matrix
void eye(EST_DMatrix &a, const int n)
some useful matrix creators make an identity matrix of dimension n
INLINE ssize_t n() const
number of items in vector.
EST_DMatrix row(const EST_DMatrix &a, ssize_t row)
void make_random_symmetric_matrix(EST_DMatrix &M, const double scale)
used for covariance
int pseudo_inverse(const EST_DMatrix &a, EST_DMatrix &inv)
pseudo inverse (for non-square matrices)
void resize(int n, int set=1)
resize vector
EST_DMatrix DMatrix_abs(const EST_DMatrix &a)
EST_DMatrix column(const EST_DMatrix &a, ssize_t col)