deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
fe_series.cc
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2016 - 2021 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13
14
15#include <deal.II/base/config.h>
16
18
19#include <iostream>
20
21
23
24namespace FESeries
25{
26 std::pair<double, double>
27 linear_regression(const std::vector<double> &x, const std::vector<double> &y)
28 {
29 FullMatrix<double> K(2, 2), invK(2, 2);
30 Vector<double> X(2), B(2);
31
32 Assert(x.size() == y.size(),
33 ExcMessage("x and y are expected to have the same size"));
34
35 Assert(x.size() >= 2,
37 "at least two points are required for linear regression fit"));
38
39 double sum_1 = 0.0, sum_x = 0.0, sum_x2 = 0.0, sum_y = 0.0, sum_xy = 0.0;
40
41 for (unsigned int i = 0; i < x.size(); ++i)
42 {
43 sum_1 += 1.0;
44 sum_x += x[i];
45 sum_x2 += x[i] * x[i];
46 sum_y += y[i];
47 sum_xy += x[i] * y[i];
48 }
49
50 K(0, 0) = sum_1;
51 K(0, 1) = sum_x;
52 K(1, 0) = sum_x;
53 K(1, 1) = sum_x2;
54
55 B(0) = sum_y;
56 B(1) = sum_xy;
57
58 invK.invert(K);
59 invK.vmult(X, B, false);
60
61 return std::make_pair(X(1), X(0));
62 }
63} // namespace FESeries
64
void vmult(Vector< number2 > &w, const Vector< number2 > &v, const bool adding=false) const
void invert(const FullMatrix< number2 > &M)
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)
std::pair< double, double > linear_regression(const std::vector< double > &x, const std::vector< double > &y)
Definition fe_series.cc:27