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, Mattia Piazza and Enrico Bertolazzi. *
3 * *
4 * The Optimist project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Mattia Piazza Enrico Bertolazzi *
7 * University of Trento University of Trento University of Trento *
8 * davide.stocco@unitn.it mattia.piazza@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/TestSet.hh"
17
18namespace Optimist
19{
20
21 namespace TestSet
22 {
23
43 template <typename Real>
44 class Quadratic : public Function<Real, 1, 1, Quadratic<Real>>
45 {
46 Real m_a{1.0};
47 Real m_b{1.0};
48 Real m_c{-1.0};
49
50 public:
52
53
57 {
58 Real delta{std::sqrt(this->m_b*this->m_b - 4.0*this->m_a*this->m_c)};
59 this->m_solutions.emplace_back((-this->m_b + delta)/(2.0*this->m_a));
60 this->m_solutions.emplace_back((-this->m_b - delta)/(2.0*this->m_a));
61 this->m_solutions.emplace_back(this->m_b/(2.0*this->m_a));
62 this->m_guesses.emplace_back(0.0);
63 }
64
69 std::string name_impl() const {return "Quadratic";}
70
77 bool evaluate_impl(Real x, Real & out) const
78 {
79 out = this->m_a*x*x + this->m_b*x + this->m_c;
80 return std::isfinite(out);
81 }
82
89 bool first_derivative_impl(Real x, Real & out) const
90 {
91 out = 2.0*this->m_a*x + this->m_b;
92 return std::isfinite(out);
93 }
94
101 bool second_derivative_impl(Real /*x*/, Real & out) const
102 {
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(Real)
Definition Optimist.hh:71
std::vector< InputType > m_guesses
Definition Function.hh:66
std::vector< InputType > m_solutions
Definition Function.hh:65
bool first_derivative_impl(Real x, Real &out) const
Definition Quadratic.hh:89
std::string name_impl() const
Definition Quadratic.hh:69
Real m_c
Definition Quadratic.hh:48
Quadratic()
Definition Quadratic.hh:56
Real m_a
Definition Quadratic.hh:46
bool second_derivative_impl(Real, Real &out) const
Definition Quadratic.hh:101
Real m_b
Definition Quadratic.hh:47
bool evaluate_impl(Real x, Real &out) const
Definition Quadratic.hh:77
Namespace for the Optimist library.
Definition Optimist.hh:88