Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Linear.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2025, Davide Stocco and Enrico Bertolazzi. *
3 * *
4 * The Optimist project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Enrico Bertolazzi *
7 * University of Trento University of Trento *
8 * davide.stocco@unitn.it enrico.bertolazzi@unitn.it *
9\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10
11#pragma once
12
13#ifndef OPTIMIST_TESTSET_LINEAR_HH
14#define OPTIMIST_TESTSET_LINEAR_HH
15
16#include "Optimist/Function.hh"
17
18namespace Optimist {
19
20 namespace TestSet {
21
38 template <typename Scalar>
39 requires TypeTrait<Scalar>::IsScalar
40 class Linear : public Function<Scalar, Scalar, Linear<Scalar>> {
41 Scalar m_m{-1.0};
42 Scalar m_q{1.0};
43
44 public:
46
47
51 this->m_solutions.emplace_back(-this->m_q / this->m_m);
52 this->m_guesses.emplace_back(0.0);
53 }
54
59 constexpr std::string name_impl() const {
60 return "Linear";
61 }
62
69 bool evaluate_impl(const Scalar x, Scalar &out) const {
70 out = this->m_m * x + this->m_q;
71 return std::isfinite(out);
72 }
73
80 bool first_derivative_impl(const Scalar /*x*/, Scalar &out) const {
81 out = this->m_m;
82 return std::isfinite(out);
83 }
84
91 bool second_derivative_impl(const Scalar /*x*/, Scalar &out) const {
92 out = 0.0;
93 return std::isfinite(out);
94 }
95
96 }; // class Linear
97
114 template <typename Vector>
117 class Linear1 : public Function<Vector, Vector, Linear1<Vector>> {
118 public:
120 using Scalar = typename Vector::Scalar;
122 using
124
125 private:
126 Scalar m_m{1.0};
127 Scalar m_q{1.0};
128
129 public:
131
132
136 this->m_solutions.resize(1);
137 this->m_guesses.resize(1);
138 Scalar tmp{-this->m_q / this->m_m};
139 if constexpr (VectorTrait::IsFixed) {
140 this->m_solutions[0] << tmp;
141 this->m_guesses[0] << 0.0;
142 } else if constexpr (VectorTrait::IsDynamic) {
143 this->m_solutions[0].resize(1);
144 this->m_solutions[0] << tmp;
145 this->m_guesses[0].resize(1);
146 this->m_guesses[0] << 0.0;
147 } else if constexpr (VectorTrait::IsSparse) {
148 this->m_solutions[0].resize(1);
149 this->m_solutions[0].reserve(1);
150 this->m_solutions[0].coeffRef(0) = tmp;
151 this->m_guesses[0].resize(1);
152 this->m_guesses[0].reserve(1);
153 this->m_guesses[0].coeffRef(0) = 0.0;
154 }
155 }
156
161 constexpr std::string name_impl() const {
162 return "Linear1";
163 }
164
171 bool evaluate_impl(const Vector &x, Vector &out) const {
172#define CMD "Optimist::TestSet::Linear1::evaluate_impl(...): "
173
174 if constexpr (VectorTrait::IsFixed) {
175 out << this->m_m * x(0) + this->m_q;
176 return std::isfinite(out(0));
177 } else if constexpr (VectorTrait::IsDynamic) {
178 out.resize(1);
179 out << this->m_m * x(0) + this->m_q;
180 return std::isfinite(out(0));
181 } else if constexpr (VectorTrait::IsSparse) {
182 out.resize(1);
183 out.reserve(1);
184 out.coeffRef(0) = this->m_m * x.coeff(0) + this->m_q;
185 return std::isfinite(out.coeff(0));
186 } else {
187 OPTIMIST_ERROR(CMD "input type not supported.");
188 return false;
189 }
190
191#undef CMD
192 }
193
200 bool first_derivative_impl(const Vector & /*x*/,
201 FirstDerivative &out) const {
202#define CMD "Optimist::TestSet::Linear1::first_derivative_impl(...): "
203
204 if constexpr (VectorTrait::IsFixed) {
205 out << this->m_m;
206 } else if constexpr (VectorTrait::IsDynamic) {
207 out.resize(1, 1);
208 out << this->m_m;
209 } else if constexpr (VectorTrait::IsSparse) {
210 out.resize(1, 1);
211 out.reserve(1);
212 out.coeffRef(0, 0) = this->m_m;
213 } else {
214 OPTIMIST_ERROR(CMD "input type not supported.");
215 return false;
216 }
217 return std::isfinite(this->m_m);
218
219#undef CMD
220 }
221
228 bool second_derivative_impl(const Vector & /*x*/,
229 SecondDerivative &out) const {
230#define CMD "Optimist::TestSet::Linear1::second_derivative_impl(...): "
231
232 out.resize(1);
233 if constexpr (VectorTrait::IsFixed) {
234 out[0] << 0.0;
235 } else if constexpr (VectorTrait::IsDynamic) {
236 out[0].resize(1, 1);
237 out[0] << 0.0;
238 } else if constexpr (VectorTrait::IsSparse) {
239 out[0].resize(1, 1);
240 out[0].reserve(1);
241 out[0].coeffRef(0, 0) = 0.0;
242 } else {
243 OPTIMIST_ERROR(CMD "input type not supported.");
244 return false;
245 }
246 return true;
247
248#undef CMD
249 }
250
251 }; // class Linear1
252
253 } // namespace TestSet
254
255} // namespace Optimist
256
257#endif // OPTIMIST_TESTSET_LINEAR_HH
#define OPTIMIST_ERROR(MSG)
Definition Optimist.hh:38
#define OPTIMIST_BASIC_CONSTANTS(Scalar)
Definition Optimist.hh:70
#define CMD
typename InputTrait::Scalar Scalar
Definition Function.hh:53
std::conditional_t< InputTrait::IsEigen||OutputTrait::IsEigen, std::conditional_t< InputTrait::IsSparse||OutputTrait::IsSparse, std::vector< Eigen::SparseMatrix< Scalar > >, std::vector< Eigen::Matrix< Scalar, OutputTrait::Dimension, InputTrait::Dimension > > >, Scalar > SecondDerivative
Definition Function.hh:64
std::vector< Scalar > m_guesses
Definition Function.hh:79
std::vector< Scalar > m_solutions
Definition Function.hh:77
std::conditional_t< InputTrait::IsEigen||OutputTrait::IsEigen, std::conditional_t< InputTrait::IsSparse||OutputTrait::IsSparse, Eigen::SparseMatrix< Scalar >, Eigen::Matrix< Scalar, OutputTrait::Dimension, InputTrait::Dimension > >, Scalar > FirstDerivative
Definition Function.hh:56
Linear1()
Definition Linear.hh:135
bool second_derivative_impl(const Vector &, SecondDerivative &out) const
Definition Linear.hh:228
bool evaluate_impl(const Vector &x, Vector &out) const
Definition Linear.hh:171
bool first_derivative_impl(const Vector &, FirstDerivative &out) const
Definition Linear.hh:200
typename Vector::Scalar Scalar
Definition Linear.hh:120
constexpr std::string name_impl() const
Definition Linear.hh:161
Scalar m_m
Definition Linear.hh:126
TypeTrait< Vector > VectorTrait
Definition Linear.hh:119
Scalar m_q
Definition Linear.hh:127
constexpr std::string name_impl() const
Definition Linear.hh:59
bool second_derivative_impl(const Scalar, Scalar &out) const
Definition Linear.hh:91
Scalar m_q
Definition Linear.hh:42
Scalar m_m
Definition Linear.hh:41
Linear()
Definition Linear.hh:50
bool evaluate_impl(const Scalar x, Scalar &out) const
Definition Linear.hh:69
bool first_derivative_impl(const Scalar, Scalar &out) const
Definition Linear.hh:80
Namespace for the Optimist library.
Definition Optimist.hh:90
Definition Optimist.hh:114