CEPS  24.01
Cardiac ElectroPhysiology Simulator
LeastSquares.cpp
Go to the documentation of this file.
1 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2  This file is part of CEPS.
3 
4  CEPS is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  CEPS is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with CEPS (see file LICENSE at root of project).
16  If not, see <https://www.gnu.org/licenses/>.
17 
18 
19  Copyright 2019-2024 Inria, Universite de Bordeaux
20 
21  Authors, in alphabetical order:
22 
23  Pierre-Elliott BECUE, Florian CARO, Yves COUDIERE(*), Andjela DAVIDOVIC,
24  Charlie DOUANLA-LONTSI, Marc FUENTES, Mehdi JUHOOR, Michael LEGUEBE(*),
25  Pauline MIGERDITICHAN, Valentin PANNETIER(*), Nejib ZEMZEMI.
26  * : currently active authors
27 
28 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
32 
33 void
34 ceps::linearRegression(std::vector<CepsReal>& xs, std::vector<CepsReal>& fs, CepsReal& a, CepsReal &b)
35 {
36  CepsReal xm(0.),fm(0.);
37  CepsUInt size = std::min(xs.size(),fs.size());
38 
39  for (CepsUInt i=0; i<size; i++)
40  {
41  xm += xs[i];
42  fm += fs[i];
43  }
44  xm /= 1.*size;
45  fm /= 1.*size;
46 
47  CepsReal num(0.),den(0.);
48 
49  for (CepsUInt i=0; i<size; i++)
50  {
51  num += xs[i]*(fs[i]-fm);
52  den += xs[i]*(xs[i]-xm);
53  }
54  a = num/den;
55  b = fm-a*xm;
56 }
std::make_unsigned_t< CepsInt > CepsUInt
Unsigned version on CepsInt.
Definition: CepsTypes.hpp:109
float CepsReal
Need single precision floating point.
Definition: CepsTypes.hpp:100
void linearRegression(std::vector< CepsReal > &xs, std::vector< CepsReal > &fs, CepsReal &a, CepsReal &b)
Performs a linear regression with least squares, put results in a and b coefficients.
const _Type & min(const CepsVector< _Type, _Alloc > &vec)
Returns the minimum of the vector, const version.
Definition: CepsVector.hpp:448