bool least_solve(double * const A,//m*n double * const B,//m*z double *X,//n*z unknown double *AA,//A'A n*n temp double *TAA,//(A'A)^1 n*n temp double *AB,//A'B n*z temp int m,int n,int z) { //A'A mata(A,AA,m,n); //A'B matb(A,B,AB,m,n,z); //(A'A)^-1 if(!gauss_elim(AA,TAA,n)) { return false; } //X mul(TAA,AB,X,n,n,z); return true; }
// t = independent value, y = response value (dependent value) int CCurveFitting::exponential_fitting(double* x, int t_len, double* y, double* a_out, double* c_out) { //determine how many points used int n = t_len; // Solve a first order linear curve fit Ax=b //[n sum(X) [B = [sum(Y) // sum(X) sum(X^2)] A] sum(X*Y)] // where: Y=ln(y), X=x, and finally C=exp(B) Mat matA(2, 2, CV_64F); Mat matx(2, 1, CV_64F); Mat matb(2, 1, CV_64F); double sumX=0.0, sumX2=0.0, sumY=0.0, sumXY=0.0, B; bool bNonSingular; for (int i = 0; i < n; i++) { sumX += (x[i]); sumX2 += (pow(x[i], 2.0)); sumY += (log(y[i])); sumXY += (x[i] * log(y[i])); } matA.ptr<double>(0, 0)[0] = (double)n; matA.ptr<double>(0, 1)[0] = sumX; matA.ptr<double>(1, 0)[0] = sumX; matA.ptr<double>(1, 1)[0] = sumX2; matb.ptr<double>(0, 0)[0] = sumY; matb.ptr<double>(1, 0)[0] = sumXY; bNonSingular = solve(matA, matb, matx, DECOMP_LU); *a_out = matx.ptr<double>(1, 0)[0]; B = matx.ptr<double>(0, 0)[0]; *c_out = exp(B); return 0; }
void mata(double * const A,double *AA,int m,int n) { matb(A,A,AA,m,n,n); }