PPCKO: Principal Predictive Components for Estimating an Autoregressive Operator
 
Loading...
Searching...
No Matches
utils.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 KO_UTILS_HPP
21#define KO_UTILS_HPP
22
23#include "traits_ko.hpp"
24#include <limits>
25
31
32
38Rcpp::List
40(const valid_err_variant& var)
41{
42
43 //visitor design pattern to dispatch the error correctly
44 return std::visit([](auto&& arg) -> Rcpp::List
45 {
46 // retrieving the type of the variant object
47 using T = std::decay_t<decltype(arg)>;
48
49 // cv single param: errors in a vector -> only one vector back
50 if constexpr (std::is_same_v<T, std::vector<double>>)
51 {
52 return Rcpp::List::create(Rcpp::Named("Errors") = Rcpp::wrap(arg)); // NumericVector
53 }
54 // cv double param: errors in a vector of vector -> list of vector(one for every alpha) back
55 else if constexpr (std::is_same_v<T, std::vector<std::vector<double>>>)
56 {
57 Rcpp::List res(arg.size());
58 for (size_t i = 0; i < arg.size(); ++i)
59 {
60 res[i] = Rcpp::wrap(arg[i]);
61 }
62
63 return Rcpp::List::create(Rcpp::Named("Errors") = res); // List of NumericVector
64 }
65 // check for the corret variant
66 else
67 {
68 Rcpp::stop("Wrong type in variant!");
69 }
70 }, var);
71}
72
73
81{
82 return KO_Traits::StoringVector(mat.reshaped());
83}
84
85
94from_col_to_matrix(const KO_Traits::StoringVector &col, int rows, int cols)
95{
96 return Eigen::Map<const KO_Traits::StoringMatrix>(col.data(),rows,cols);
97}
98
99
100//function to add nans in the result
101//pred is the vector with the prediction, row ret the vector containing which points actually have evals, complete size is the size of all the points
110add_nans_vec(const KO_Traits::StoringVector &pred, const std::vector<int> &row_ret, int complete_size)
111{
112 if(row_ret.size()==0){return pred;}
113 KO_Traits::StoringVector pred_comp(complete_size);
114
115 pred_comp.setConstant(std::numeric_limits<double>::quiet_NaN());
116
117 // putting values where they actually are
118 int counter=0;
119 std::for_each(row_ret.cbegin(),row_ret.cend(),[&pred_comp,&pred,&counter](int el){pred_comp[el]=pred[counter]; counter++;});
120
121 return pred_comp;
122}
123
124#endif //KO_UTILS_HPP
Eigen::VectorXd StoringVector
Vector data structure.
Definition traits_ko.hpp:51
Eigen::MatrixXd StoringMatrix
Matrix data structure.
Definition traits_ko.hpp:49
Contains customized types and enumerator for customized template parameters, exploited in the algorit...
KO_Traits::StoringMatrix from_col_to_matrix(const KO_Traits::StoringVector &col, int rows, int cols)
Function to map a column vector into a matrix, column by column.
Definition utils.hpp:94
KO_Traits::StoringVector add_nans_vec(const KO_Traits::StoringVector &pred, const std::vector< int > &row_ret, int complete_size)
Function to add dummy NaNs to the curve/surface.
Definition utils.hpp:110
Rcpp::List valid_err_disp(const valid_err_variant &var)
Function to dispacth the error type depending on if cv is performed on one or two parameters,...
Definition utils.hpp:40
KO_Traits::StoringVector from_matrix_to_col(const KO_Traits::StoringMatrix &mat)
Function to map a matrix into a column vector, column by column.
Definition utils.hpp:80