BELFEM 0.9.0
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
fn_r2.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
23
24#ifndef BELFEM_FN_R2_HPP
25#define BELFEM_FN_R2_HPP
26
27
28#include "cl_Vector.hpp"
29#include "cl_Matrix.hpp"
30
31#include "fn_sum.hpp"
32#include "fn_norm.hpp"
33
34namespace belfem
35{
36//------------------------------------------------------------------------------
37
49 inline real
50 r2( const Vector< real > & aApproximated,
51 const Vector< real > & aExact )
52 {
53 // calculate average of samples
54 real tAverage = sum( aExact ) / ( real ) aExact.length();
55
56 // sum of square residuals
57 real tRootOfSSres = norm( aExact - aApproximated );
58
59#ifdef BELFEM_BLAZE
60 // the minus operator does not exist in blaze. Using a workaround
61 index_t tN = aExact.length();
62 real tRootOfSStot = 0.0 ;
63 for( uint k=0; k<tN; ++k )
64 {
65 tRootOfSStot += ( aExact( k ) - tAverage ) * ( aExact( k ) - tAverage ) ;
66 }
67 tRootOfSStot = std::sqrt( tRootOfSStot );
68#else
69 // total sum of squares
70 real tRootOfSStot = norm( aExact - tAverage );
71#endif
72 if( tRootOfSStot * tRootOfSStot < BELFEM_EPSILON || tRootOfSSres * tRootOfSSres < BELFEM_EPSILON )
73 {
74 return 1.0;
75 }
76 else
77 {
78 return 1.0 - std::pow( tRootOfSSres / tRootOfSStot, 2 );
79 }
80 }
81
82//------------------------------------------------------------------------------
83
84 inline real
85 r2( const Matrix< real > & aApproximated,
86 const Matrix< real > & aExact )
87 {
88 // number of rows
89 index_t m = aExact.n_rows();
90
91 // number of columns
92 index_t n = aExact.n_cols();
93
94 // calculate average of samples
95 real tAverage = 0.0 ;
96
97 // sum of square residuals
98 real tSSres = 0.0 ;
99
100 for( index_t i=0; i<m; ++i )
101 {
102 for( index_t j=0; j<n; ++j )
103 {
104 tAverage += aExact( i, j );
105 tSSres +=
106 ( aExact( i, j ) - aApproximated( i, j ) )
107 * ( aExact( i, j ) - aApproximated( i, j ) );
108 }
109 }
110
111 tAverage /= m * n ;
112
113 real tSStot = 0.0 ;
114 for( index_t i=0; i<m; ++i )
115 {
116 for( index_t j=0; j<n; ++j )
117 {
118 tSStot +=
119 ( aExact( i, j ) - tAverage )
120 * ( aExact( i, j ) - tAverage );
121 }
122 }
123
124 if( tSStot < BELFEM_EPSILON || tSSres < BELFEM_EPSILON )
125 {
126 return 1.0;
127 }
128 else
129 {
130 return 1.0 - tSSres / tSStot ;
131 }
132 }
133
134//------------------------------------------------------------------------------
135}
136
137#endif //BELFEM_FN_R2_HPP
size_t n_rows() const
Definition cl_AR_Matrix.hpp:205
size_t n_cols() const
Definition cl_AR_Matrix.hpp:213
size_t length() const
get the length of the vector
Definition cl_AR_Vector.hpp:257
Euclidean length of a vector.
Sum of the entries of a vector.
auto norm(const Vector< T > &aA) -> decltype(norm(aA.vector_data()))
Euclidean (L2) norm of a vector.
Definition fn_norm.hpp:56
USER GUIDES:
Definition cl_Capacitor.cpp:16
unsigned int uint
Definition typedefs.hpp:30
auto sum(const Vector< T > &aA) -> decltype(sum(aA.vector_data()))
Definition fn_sum.hpp:49
constexpr real BELFEM_EPSILON
Definition typedefs.hpp:90
uint32_t index_t
Definition typedefs.hpp:52
real r2(const Vector< real > &aApproximated, const Vector< real > &aExact)
calculates the R2 coefficient of determination from values of an evaluated function with respect to g...
Definition fn_r2.hpp:50
double real
Definition typedefs.hpp:36