Stochastic Loading Module
numeric_utils.cc
1 #include <stdexcept>
2 #include <Eigen/Dense>
3 #include <mkl.h>
4 #include <mkl_vsl.h>
5 #include "numeric_utils.h"
6 
7 namespace numeric_utils {
8 Eigen::MatrixXd corr_to_cov(const Eigen::MatrixXd& corr,
9  const Eigen::VectorXd& std_dev) {
10  Eigen::MatrixXd cov_matrix = Eigen::MatrixXd::Zero(corr.rows(), corr.cols());
11 
12  for (unsigned int i = 0; i < cov_matrix.rows(); ++i) {
13  for (unsigned int j = 0; j < cov_matrix.cols(); ++j) {
14  cov_matrix(i, j) = corr(i, j) * std_dev(i) * std_dev(j);
15  }
16  }
17 
18  return cov_matrix;
19 }
20 
21 bool convolve_1d(const std::vector<double>& input_x,
22  const std::vector<double>& input_y,
23  std::vector<double>& response) {
24  bool status = true;
25  response.resize(input_x.size() + input_y.size() - 1);
26 
27  // Create convolution status and task pointer
28  int conv_status;
29  VSLConvTaskPtr conv_task;
30  // Construct convolution task, with solution mode set to direct
31  conv_status =
32  vsldConvNewTask1D(&conv_task, VSL_CONV_MODE_DIRECT, input_x.size(),
33  input_y.size(), response.size());
34 
35  // Check if convolution construction was successful
36  if (conv_status != VSL_STATUS_OK) {
37  throw std::runtime_error(
38  "\nERROR: in numeric_utils::convolve_1d: Error in convolution "
39  "construction\n");
40  status = false;
41  }
42 
43  // Set convolution to start at first element in input_y
44  vslConvSetStart(conv_task, 0);
45 
46  // Execute convolution
47  conv_status = vsldConvExec1D(conv_task, input_x.data(), 1, input_y.data(), 1,
48  response.data(), 1);
49 
50  // Check if convolution exectution was successful
51  if (conv_status != VSL_STATUS_OK) {
52  throw std::runtime_error(
53  "\nERROR: in numeric_utils::convolve_1d: Error in convolution "
54  "execution\n");
55  status = false;
56  }
57 
58  // Delete convolution task
59  vslConvDeleteTask(&conv_task);
60 
61  return status;
62 }
63 
64 double trapazoid_rule(const std::vector<double>& input_vector, double spacing) {
65  double result = (input_vector[0] + input_vector[input_vector.size() - 1]) / 2.0;
66 
67  for (unsigned int i = 1; i < input_vector.size() - 1; ++i) {
68  result = result + input_vector[i];
69  }
70 
71  return result * spacing;
72 }
73 
74 double trapazoid_rule(const Eigen::VectorXd& input_vector, double spacing) {
75  double result = (input_vector[0] + input_vector[input_vector.size() - 1]) / 2.0;
76 
77  for (unsigned int i = 1; i < input_vector.size() - 1; ++i) {
78  result = result + input_vector[i];
79  }
80 
81  return result * spacing;
82 }
83 } // namespace numeric_utils
bool convolve_1d(const std::vector< double > &input_x, const std::vector< double > &input_y, std::vector< double > &response)
double trapazoid_rule(const std::vector< double > &input_vector, double spacing)
Eigen::MatrixXd corr_to_cov(const Eigen::MatrixXd &corr, const Eigen::VectorXd &std_dev)
Definition: numeric_utils.cc:8