65 cerr <<
"polynomial_fit : x and y must have same dimension" << endl;
69 if(weights.
n() != y.
n()){
70 cerr <<
"polynomial_fit : weights must have same dimension as x and y" << endl;
75 cerr <<
"polynomial_fit : x and y must have at least order+1 elements" 91 for(
ssize_t col=0;col<=order;col++){
92 A(
row,col) = pow(x[
row],(
float)col) * weights[
row];
107 if(!
inverse(At_A,At_A_inv,singularity))
109 cerr <<
"polynomial_fit : inverse failed (";
110 if(singularity == -2)
111 cerr <<
"unspecified reason)" << endl;
112 else if(singularity == -1)
113 cerr <<
"non-square !!)" << endl;
116 cerr <<
"singularity at point : " << singularity;
117 cerr <<
" = " << x[singularity] <<
"," << y[singularity];
118 cerr <<
" )" << endl;
124 co_effs = At_A_inv * At_y1;
165 cerr <<
"diagonalise: non-square matrix ";
193 for (i = I = 0; i < n; ++i, ++I)
197 for (j = J = 0; j < n; ++j, ++J)
216 s(0, i) = a.
a(row, i);
227 s(i, 0) = a.
a(i, col);
260 for (i = 0; i < n; ++i)
261 for (j = 0; j < n; ++j)
262 t(n - i - 1, n - j - 1) = a.
a(i, j);
301 return inverse(a,inv,singularity);
320 ssize_t r=0,this_row,all_zeros;
396 singularity = ((this_row > j) ? this_row : j);
419 return inverse(a,inv,singularity);
428 if (!
inverse(atrans_a,atrans_a_inverse,singularity))
430 multiply(atrans_a_inverse,a_trans,inv);
443 cerr <<
"Tried to take determinant of non-square matrix\n";
457 for (i = 0; i < n; ++i)
459 p = (float)(i + j + 2);
467 for (i = 0; i < n; ++i)
492 cerr <<
"Can't make non-square identity matrix !" << endl;
508 cerr <<
"Can't add vectors of differing lengths !" << endl;
513 for(
ssize_t i=0; i<a_len; i++ )
526 cerr <<
"Can't subtract vectors of differing lengths !" << endl;
531 for(
ssize_t i=0; i<a_len; i++ )
543 cerr <<
"Can't extract diagonal of non-square matrix !" << endl;
574 cerr <<
"Can't symmetrize non-square matrix !" << endl;
606 r = scale * ((double)rand()/(double)
RAND_MAX);
618 r = scale * ((double)rand()/(double)
RAND_MAX);
628 cerr <<
"Can't make non-square symmetric matrix !" << endl;
637 r = scale * ((double)rand()/(double)
RAND_MAX);
648 cerr <<
"Can't make non-square symmetric matrix !" << endl;
664 cerr <<
"Can't make polynomial basis function : dimension mismatch !" << endl;
665 cerr <<
"t.length()=" << t.
length();
666 cerr <<
" T.num_rows()=" << T.
num_rows() << endl;
707 #if !defined(SYSTEM_IS_WIN32) 711 gettimeofday(&tp,&tzp);
712 seed = getpid() * (tp.tv_usec&0x7fff);
713 cerr <<
"seed: " << seed << endl;
726 gettimeofday(&tp,&tzp);
727 seed = (getpid()&0x7f) * (tp.tv_usec&0xff);
728 cerr <<
"seed48: " << seed << endl;
EST_FVector subtract(const EST_FVector &a, const EST_FVector &b)
elementwise subtract
int square(const EST_FMatrix &a)
EST_FMatrix diagonalise(const EST_FMatrix &a)
extract leading diagonal as a matrix
ssize_t floor_matrix(EST_FMatrix &M, const float floor)
EST_FMatrix row(const EST_FMatrix &a, ssize_t row)
ssize_t num_columns() const
return number of columns
A vector class for floating point numbers. EST_FVector x should be used instead of float *x wherever ...
void est_seed()
the user should use est_seed to seed the random number generator
INLINE const T & a_no_check(ssize_t n) const
read-only const access operator: without bounds checking
float polynomial_value(const EST_FVector &coeffs, const float x)
ssize_t num_rows() const
return number of rows
EST_FMatrix triangulate(const EST_FMatrix &a)
const T & a(ssize_t row, ssize_t col) const
EST_FMatrix cov_prod(const EST_FVector &v1, const EST_FVector &v2)
matrix product of two vectors (#rows = length of first vector, #cols = length of second vector) ...
bool polynomial_fit(EST_FVector &x, EST_FVector &y, EST_FVector &co_effs, int order)
least squares fit
void make_random_vector(EST_FVector &V, const float scale)
all elements are randomised
void eye(EST_FMatrix &a, const ssize_t n)
some useful matrix creators make an identity matrix of dimension n
void fill(const T &v)
fill matrix with value v
void symmetrize(EST_FMatrix &a)
enforce symmetry
float matrix_max(const EST_FMatrix &a)
find largest element
EST_FVector add(const EST_FVector &a, const EST_FVector &b)
elementwise add
INLINE ssize_t length() const
number of items in vector.
EST_FMatrix sub(const EST_FMatrix &a, ssize_t row, ssize_t col)
void inplace_diagonalise(EST_FMatrix &a)
inplace diagonalise
EST_FVector diagonal(const EST_FMatrix &a)
extract leading diagonal as a vector
void make_poly_basis_function(EST_FMatrix &T, EST_FVector t)
void multiply(const EST_DMatrix &a, const EST_DMatrix &b, EST_DMatrix &ab)
float time(const EST_Item &item)
int pseudo_inverse(const EST_FMatrix &a, EST_FMatrix &inv)
pseudo inverse (for non-square matrices)
void make_random_symmetric_matrix(EST_FMatrix &M, const float scale)
used for covariance
void transpose(const EST_FMatrix &a, EST_FMatrix &b)
exchange rows and columns
EST_FMatrix column(const EST_FMatrix &a, ssize_t col)
EST_FMatrix fmatrix_abs(const EST_FMatrix &a)
void make_random_diagonal_matrix(EST_FMatrix &M, const float scale)
used for variance
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
float determinant(const EST_FMatrix &a)
float sum(const EST_FMatrix &a)
sum of elements
INLINE ssize_t n() const
number of items in vector.
void make_random_matrix(EST_FMatrix &M, const float scale)
all elements are randomised
EST_FMatrix backwards(EST_FMatrix &a)
int inverse(const EST_FMatrix &a, EST_FMatrix &inv)
inverse
void resize(int n, int set=1)
resize vector
void stack_matrix(const EST_FMatrix &M, EST_FVector &v)
stack columns on top of each other to make a vector