BELFEM 0.9.0
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
fn_AR_polyfit.hpp
Go to the documentation of this file.
1/*
2 * BELFEM -- The Berkeley Lab Finite Element Framework
3 * Copyright (c) 2026, The Regents of the University of California,
4 * through Lawrence Berkeley National Laboratory (subject to receipt of any required
5 * approvals from the U.S. Dept. of Energy). All rights reserved.
6 *
7 * Developers: Christian Messe, Gregory Giard
8 *
9 * See the top-level LICENSE file for the complete license and disclaimer.
10 */
11
12#ifndef BELFEM_FN_AR_POLYFIT_HPP
13#define BELFEM_FN_AR_POLYFIT_HPP
14
15#include "armadillo.hpp"
16#include "cl_AR_Vector.hpp"
17#include "assert.hpp"
18
19namespace belfem
20{
21//------------------------------------------------------------------------------
22
23 template< typename T >
24 void
25 polyfit( const Vector< T > & aX, const Vector< T > & aY, const uint & aN, Vector< T > & aCoeffs )
26 {
27 BELFEM_ASSERT( aX.length() == aY.length(),
28 "Legnths of X and Y vectors do not match ( %lu and %lu )",
29 ( long unsigned int ) aX.length(),
30 ( long unsigned int ) aY.length());
31
32 BELFEM_ASSERT( aX.length() > aN, "not enough samples in vector to create a polynomial of degree %u",
33 ( unsigned int ) aN );
34
35 // get size of vector
36 int tN = aX.length();
37
38 // get a reference value to scale the polynomial
39 T tXref = ( tN - 1 ) / ( aX( tN - 1 ) - aX( 0 ) );
40
41 // call armadillo
42 aCoeffs.vector_data() = arma::polyfit( aX.vector_data() * tXref , aY.vector_data(), aN );
43
44 // scale back vector
45 T tScale = 1.0;
46
47 for( int i=aN; i>=0; --i )
48 {
49 aCoeffs( i ) *= tScale;
50 tScale *= tXref;
51 }
52 }
53
54//------------------------------------------------------------------------------
55}
56#endif //BELFEM_FN_AR_POLYFIT_HPP
#define BELFEM_ASSERT(aCheck,...)
Definition assert.hpp:244
Column vector.
Definition cl_BZ_Vector.hpp:41
size_t length() const
get the length of the vector
Definition cl_AR_Vector.hpp:257
VectorType & vector_data()
expose the underlying matrix implementation ( writable version )
Definition cl_AR_Vector.hpp:204
USER GUIDES:
Definition cl_Capacitor.cpp:16
unsigned int uint
Definition typedefs.hpp:30
void polyfit(const Vector< T > &aX, const Vector< T > &aY, const uint &aN, Vector< T > &aCoeffs)
Definition fn_AR_polyfit.hpp:25