// Copyright 2022 James Pace
// All Rights Reserved.
//
// For a license to this software contact
// James Pace at jpace121@gmail.com.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#include "j7s-optimization/CostFunction.hpp"

#include <cmath>

namespace j7s
{
CostFunction::CostFunction(double a, double b, double c) : m_a{a}, m_b{b}, m_c{c} {}

double CostFunction::eval(double input) const
{
    return m_a * std::pow(input, 2) + m_b * input + m_c;
}

double CostFunction::actualBest() const
{
    // y = a*x**2 + b*x + c
    // dy/dx = 2*a*x + b
    // 0 = 2*a*x + b
    // -b/2*a = x

    return (-m_b / (2 * m_a));
}

}  // namespace j7s
