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_SCALAR_FUNCTION_QUADRATIC_HH
14#define OPTIMIST_SCALAR_FUNCTION_QUADRATIC_HH
15
16#include "Optimist/TestSet.hh"
18
19namespace Optimist
20{
21
22 namespace TestSet
23 {
24
44 template <typename Real>
45 class Quadratic : public ScalarFunction<Real, Quadratic<Real>>
46 {
47 Real m_a{1.0};
48 Real m_b{1.0};
49 Real m_c{-1.0};
50
51 public:
53
54
58 {
59 Real delta{std::sqrt(this->m_b*this->m_b - 4.0*this->m_a*this->m_c)};
60 this->m_solutions.emplace_back((-this->m_b + delta)/(2.0*this->m_a));
61 this->m_solutions.emplace_back((-this->m_b - delta)/(2.0*this->m_a));
62 this->m_solutions.emplace_back(this->m_b/(2.0*this->m_a));
63 this->m_guesses.emplace_back(0.0);
64 }
65
70 std::string name_impl() const {return "Quadratic";}
71
77 void evaluate_impl(Real x, Real & out) const {out = this->m_a*x*x + this->m_b*x + this->m_c;}
78
84 void first_derivative_impl(Real x, Real & out) const {out = 2.0*this->m_a*x + this->m_b;}
85
91 void second_derivative_impl(Real /*x*/, Real & out) const {out = 2.0*this->m_a;}
92
93 }; // class Quadratic
94
95 } // namespace TestSet
96
97} // namespace Optimist
98
99#endif // OPTIMIST_SCALAR_FUNCTION_QUADRATIC_HH
#define OPTIMIST_BASIC_CONSTANTS(Real)
Definition Optimist.hh:70
std::vector< InputType > m_solutions
Definition Function.hh:52
std::vector< InputType > m_guesses
Definition Function.hh:53
ScalarFunction()
Definition ScalarFunction.hh:38
void first_derivative_impl(Real x, Real &out) const
Definition Quadratic.hh:84
std::string name_impl() const
Definition Quadratic.hh:70
Real m_c
Definition Quadratic.hh:49
Quadratic()
Definition Quadratic.hh:57
void evaluate_impl(Real x, Real &out) const
Definition Quadratic.hh:77
Real m_a
Definition Quadratic.hh:47
void second_derivative_impl(Real, Real &out) const
Definition Quadratic.hh:91
Real m_b
Definition Quadratic.hh:48
Namespace for the Optimist library.
Definition Optimist.hh:87