ROL
ROL_RegressionError.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_REGRESSIONERROR_H
11 #define ROL_REGRESSIONERROR_H
12 
13 #include "ROL_StdVector.hpp"
14 #include "ROL_StdObjective.hpp"
15 
24 namespace ROL {
25 
26 template <class Real>
27 class RegressionError : public StdObjective<Real> {
28 private:
29  void checkSize(const std::vector<Real> &x) {
30  const std::vector<Real> data = Objective<Real>::getParameter();
31  if (data.size() != x.size()) {
32  throw Exception::NotImplemented("ROL::RegressionError : Data dimension does not match input dimension!");
33  }
34  }
35 
36 public:
37  RegressionError(void) {}
38 
39  Real value( const std::vector<Real> &x, Real &tol ) {
40  checkSize(x);
41  const std::vector<Real> data = Objective<Real>::getParameter();
42  const unsigned dim = x.size();
43  Real val = data[0] - x[0];
44  for (unsigned i = 1; i < dim; ++i) val -= data[i] * x[i];
45  return val;
46  }
47 
48  void gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
49  checkSize(g);
50  const std::vector<Real> data = Objective<Real>::getParameter();
51  const unsigned dim = g.size();
52  g[0] = static_cast<Real>(-1);
53  for (unsigned i = 1; i < dim; ++i) g[i] = -data[i];
54  }
55 
56  void hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
57  checkSize(hv);
58  hv.assign(hv.size(),static_cast<Real>(0));
59  }
60 }; // class RegressionError
61 
62 } // namespace ROL
63 
64 #endif
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
Provides the interface to evaluate linear regression error.
void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Real value(const std::vector< Real > &x, Real &tol)
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector&#39;s.
const std::vector< Real > getParameter(void) const
void checkSize(const std::vector< Real > &x)
constexpr auto dim