/* LA: linear algebra C++ interface library Copyright (C) 2021 Jiri Pittner or This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ //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 #ifndef _SIMPLE_H_ #define _SIMPLE_H_ #include #ifndef AVOID_STDSTREAM #include #endif #include #include #include namespace LA_Simple { //a simple gauss elimination as a template also for larger-size matrices in form of C-style arrays #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, inverse in a, solution in b { int indxc[n],indxr[n],ipiv[n]; int i,j,k,l,ll; int irow=0,icol=0; T det,big,dum,pivinv; det=1; for (j=0;j= big) { big=abs(a[j][k]); irow=j; icol=k; } } else if (ipiv[k] > 1) {return 0;} } ++(ipiv[icol]); if (irow != icol) { det = (-det); for (l=0;l=0;l--) { if (indxr[l] != indxc[l]) for (k=0;k class simple_linfit { public: T fitmat[n][n]; T rhsmat[1][n]; T fitcoef[n]; int npoints; void clear(bool keepresults=false) {npoints=0; memset(&fitmat[0][0],0,n*n*sizeof(T)); memset(&rhsmat[0][0],0,1*n*sizeof(T)); if(!keepresults) memset(&fitcoef[0],0,n*sizeof(T));}; simple_linfit() {clear(false);} const T &operator[](int i) const {return fitcoef[i];} 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