From c45e3cc40c179e96a727bf068b8eb18d9b70ea99 Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Mon, 22 Nov 2021 14:22:19 +0100 Subject: [PATCH] simple_linfit implemented --- simple.h | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- t.cc | 26 +++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/simple.h b/simple.h index ef01db5..46dccb9 100644 --- a/simple.h +++ b/simple.h @@ -17,6 +17,7 @@ */ //this header defines some simple algorithms independent of external libraries +//using small runtime-constant size matrices and vectors //particularly intended to embedded computers //it should be compilable separately from LA as well as being a part of LA @@ -37,7 +38,7 @@ namespace LA_Simple { #define SWAP(a,b) {T temp=(a);(a)=(b);(b)=temp;} template -T simple_gaussj(T (&a)[n][n],T (&b)[m][n]) //returns determinant, m is number of rhs to solve +T simple_gaussj(T (&a)[n][n],T (&b)[m][n]) //returns determinant, m is number of rhs to solve, inverse in a, solution in b { int indxc[n],indxr[n],ipiv[n]; int i,icol,irow,j,k,l,ll; @@ -93,14 +94,57 @@ return det; template class simple_linfit { -T mata[n][n]; -T matb[1][n]; public: +T fitmat[n][n]; +T rhsmat[1][n]; +T fitcoef[n]; +int npoints; + void clear() {npoints=0; memset(&fitmat[0][0],0,n*n*sizeof(T)); memset(&rhsmat[0][0],0,1*n*sizeof(T)); memset(&fitcoef[0],0,n*sizeof(T));}; + simple_linfit() {clear();} + void input(const T (&funcs)[n], const T y) + { + ++npoints; + for(int i=0; i +std::ostream& operator<<(std::ostream &o, const simple_linfit &f) +{ +for(int i=0; i fit; +double funcs[3]; +for(int i=0; i<100; ++i) + { + double x = 10*(2.*random()/(1. + RAND_MAX) - 1.); + funcs[0]=1; + funcs[1]=x; + funcs[2]=x*x; + double y = 2*funcs[2] -3*funcs[1] + funcs[0]; + //cout <<"test "<