#include "MonotonicFunctionInverse.h"

MonotonicFunctionInverter::MonotonicFunctionInverter(double lb, double ub)
	:
	m_y_to_x([](double x) { return x; }),
	m_lb(lb),
	m_ub(ub)
{}

MonotonicFunctionInverter::MonotonicFunctionInverter(const std::function<double(double)>& y_to_x, double lb, double ub)
	:
	m_y_to_x(y_to_x),
	m_lb(lb),
	m_ub(ub)
{}

double MonotonicFunctionInverter::calculate_inverse(double y, const std::string & error)
{
	if (y > m_ub)
		throw std::runtime_error(error);
	m_fx = y;
	return Algorithm::brent(*this, m_lb, m_ub);
}

double MonotonicFunctionInverter::evaluate(double y)
{
	return m_fx - m_y_to_x(y);
}
