PPCKO: Principal Predictive Components for Estimating an Autoregressive Operator
 
Loading...
Searching...
No Matches
cv_eval_valid_err.hpp
Go to the documentation of this file.
1// Copyright (c) 2024 Andrea Enrico Franzoni (andreaenrico.franzoni@gmail.com)
2//
3// This file is part of PPCKO
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of PPCKO and associated documentation files (the PPCKO software), to deal
7// PPCKO without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of PPCKO, and to permit persons to whom PPCKO is
10// furnished to do so, subject to the following conditions:
11//
12// PPCKO IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17// OUT OF OR IN CONNECTION WITH PPCKO OR THE USE OR OTHER DEALINGS IN
18// PPCKO.
19
20#ifndef CV_EVAL_VALID_ERR_HPP
21#define CV_EVAL_VALID_ERR_HPP
22
23#include <algorithm>
24#include <numeric>
25
26#include "traits_ko.hpp"
27
28#ifdef _OPENMP
29#include <omp.h>
30#endif
31
32
38
39
48template<typename T>
49double mse(const KO_Traits::StoringVector &diff, int number_threads)
50{
51 double mse = 0.0;
52 int num_comp = diff.size();
53
54 //if OMP: going parallel
55#ifdef _OPENMP
56#pragma omp parallel for shared(diff,num_comp) num_threads(number_threads) reduction(+:mse)
57 for(std::size_t i = 0; i < num_comp; ++i)
58 {
59 mse += std::pow(diff(i),2);
60 }
61#else
62 // if not OMP: STL algorithms
63 mse = std::transform_reduce(diff.begin(),
64 diff.end(),
65 0.0,
66 std::plus{},
67 [] (T const &x) {return std::pow(x,2);});
68#endif
69
70 return mse/static_cast<double>(num_comp);
71};
72
73#endif /*CV_EVAL_VALID_ERR_HPP*/
double mse(const KO_Traits::StoringVector &diff, int number_threads)
Template function for the estimate of the L2 norm.
Definition cv_eval_valid_err.hpp:49
Eigen::VectorXd StoringVector
Vector data structure.
Definition traits_ko.hpp:51
Contains customized types and enumerator for customized template parameters, exploited in the algorit...