Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Quadratic.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_QUADRATIC_HH
14#define OPTIMIST_TESTSET_QUADRATIC_HH
15
16#include "Optimist/Function.hh"
17
18namespace Optimist {
19
20 namespace TestSet {
21
41 template <typename Scalar>
42 requires TypeTrait<Scalar>::IsScalar
43 class Quadratic : public Function<Scalar, Scalar, Quadratic<Scalar>> {
44 Scalar m_a{1.0};
45 Scalar m_b{1.0};
46 Scalar m_c{-1.0};
47
48 public:
50
51
55 Scalar delta{static_cast<Scalar>(
56 std::sqrt(this->m_b * this->m_b - 4.0 * this->m_a * this->m_c))};
57 this->m_solutions.emplace_back((-this->m_b + delta) /
58 (2.0 * this->m_a)); // Zero
59 this->m_solutions.emplace_back((-this->m_b - delta) /
60 (2.0 * this->m_a)); // Zero
61 this->m_solutions.emplace_back(-this->m_b /
62 (2.0 * this->m_a)); // Minimum
63 this->m_guesses.emplace_back(0.0);
64 }
65
70 constexpr std::string name_impl() const {
71 return "Quadratic";
72 }
73
80 bool evaluate_impl(const Scalar x, Scalar &out) const {
81 out = this->m_a * x * x + this->m_b * x + this->m_c;
82 return std::isfinite(out);
83 }
84
91 bool first_derivative_impl(const Scalar x, Scalar &out) const {
92 out = 2.0 * this->m_a * x + this->m_b;
93 return std::isfinite(out);
94 }
95
102 bool second_derivative_impl(Scalar /*x*/, Scalar &out) const {
103 out = 2.0 * this->m_a;
104 return std::isfinite(out);
105 }
106
107 }; // class Quadratic
108
109 } // namespace TestSet
110
111} // namespace Optimist
112
113#endif // OPTIMIST_TESTSET_QUADRATIC_HH
#define OPTIMIST_BASIC_CONSTANTS(Scalar)
Definition Optimist.hh:70
typename InputTrait::Scalar Scalar
Definition Function.hh:53
std::vector< Scalar > m_guesses
Definition Function.hh:79
std::vector< Scalar > m_solutions
Definition Function.hh:77
constexpr std::string name_impl() const
Definition Quadratic.hh:70
bool second_derivative_impl(Scalar, Scalar &out) const
Definition Quadratic.hh:102
Scalar m_a
Definition Quadratic.hh:44
bool evaluate_impl(const Scalar x, Scalar &out) const
Definition Quadratic.hh:80
Quadratic()
Definition Quadratic.hh:54
Scalar m_c
Definition Quadratic.hh:46
Scalar m_b
Definition Quadratic.hh:45
bool first_derivative_impl(const Scalar x, Scalar &out) const
Definition Quadratic.hh:91
Namespace for the Optimist library.
Definition Optimist.hh:90