<commit_before>// <NAME> 2012-2013.

#include <exception>
#include <iostream>
#include <stdexcept>

#include <spii/function.h>
#include <spii/spii.h>

namespace spii {

// These two structs are used by Function to store added
// variables and terms.
struct AddedVariable
{
	int user_dimension;
	int solver_dimension;
	size_t global_index;
	ChangeOfVariables* change_of_variables;
	mutable std::vector<double>  temp_space;
};
struct AddedTerm
{
	const Term* term;
	std::vector<AddedVariable*> user_variables;
	// Temporary storage for a point and hessian.
	mutable std::vector<double*> temp_variables;
	mutable std::vector< std::vector<Eigen::MatrixXd> > hessian;
};

class Function::Implementation
{
public:
	Implementation(Function* function_interface) :
		interface(function_interface) { };

	// Implemenations of functions in the public interface.
	double evaluate(const Eigen::VectorXd& x,
	                Eigen::VectorXd* gradient,
	                Eigen::MatrixXd* hessian) const;
	double evaluate(const Eigen::VectorXd& x,
	                Eigen::VectorXd* gradient,
	                Eigen::SparseMatrix<double>* hessian) const;
	Interval<double> evaluate(const std::vector<Interval<double>>& x) const;

	// Adds a variable to the function. All variables must be added
	// before any terms containing them are added.
	void add_variable_internal(double* variable,
	                           int dimension,
	                           ChangeOfVariables* change_of_variables = 0);

	// Copies variables from a global vector x to the Function's
	// local storage.
	void copy_global_to_local(const Eigen::VectorXd& x) const;
	// Copies variables from a global vector x to the storage
	// provided by the user.
	void copy_global_to_user(const Eigen::VectorXd& x) const;
	// Copies variables from a the storage provided by the user
	// to a global vector x.
	void copy_user_to_global(Eigen::VectorXd* x) const;
	// Copies variables from a the storage provided by the user
	// to the Function's local storage.
	void copy_user_to_local() const;

	// Evaluates the function at the point in the local storage.
	double evaluate_from_local_storage() const;

	// A set of all terms added to the function. This is
	// used when the function is destructed.
	std::set<const Term*> added_terms;

	// All variables added to the function.
	std::map<double*, AddedVariable> variables;

	// Each variable can have several dimensions. This member
	// keeps track of the total number of scalars.
	size_t number_of_scalars;

	// All terms added to the function.
	std::vector<AddedTerm> terms;

	// Number of threads used for evaluation.
	int number_of_threads;

	// Allocates temporary storage for gradient evaluations.
	// Should be called automatically at first evaluate()
	void allocate_local_storage() const;

	// If finalize has been called.
	mutable bool local_storage_allocated;
	// Has to be mutable because the temporary storage
	// needs to be written to.
	mutable std::vector< std::vector<Eigen::VectorXd> >
		thread_gradient_scratch;
	mutable std::vector<Eigen::VectorXd>
		thread_gradient_storage;

	// Stored how many element were used the last time the Hessian
	// was created.
	mutable size_t number_of_hessian_elements;

private:
	Function* interface;
};

Function::Function() :
	impl(new Function::Implementation(this))
{
	impl->number_of_scalars = 0;
	this->term_deletion = DeleteTerms;

	this->hessian_is_enabled = true;

	impl->number_of_hessian_elements = 0;

	this->evaluations_without_gradient = 0;
	this->evaluations_with_gradient    = 0;

	this->evaluate_time               = 0;
	this->evaluate_with_hessian_time  = 0;
	this->write_gradient_hessian_time = 0;
	this->copy_time                   = 0;

	#ifdef USE_OPENMP
		impl->number_of_threads = omp_get_max_threads();
	#else
		this->number_of_threads = 1;
	#endif

	impl->local_storage_allocated = false;<commit_msg>Fixed compilation error without OpenMP.<commit_after>// <NAME> 2012-2013.

#include <exception>
#include <iostream>
#include <stdexcept>

#include <spii/function.h>
#include <spii/spii.h>

namespace spii {

// These two structs are used by Function to store added
// variables and terms.
struct AddedVariable
{
	int user_dimension;
	int solver_dimension;
	size_t global_index;
	ChangeOfVariables* change_of_variables;
	mutable std::vector<double>  temp_space;
};
struct AddedTerm
{
	const Term* term;
	std::vector<AddedVariable*> user_variables;
	// Temporary storage for a point and hessian.
	mutable std::vector<double*> temp_variables;
	mutable std::vector< std::vector<Eigen::MatrixXd> > hessian;
};

class Function::Implementation
{
public:
	Implementation(Function* function_interface) :
		interface(function_interface) { };

	// Implemenations of functions in the public interface.
	double evaluate(const Eigen::VectorXd& x,
	                Eigen::VectorXd* gradient,
	                Eigen::MatrixXd* hessian) const;
	double evaluate(const Eigen::VectorXd& x,
	                Eigen::VectorXd* gradient,
	                Eigen::SparseMatrix<double>* hessian) const;
	Interval<double> evaluate(const std::vector<Interval<double>>& x) const;

	// Adds a variable to the function. All variables must be added
	// before any terms containing them are added.
	void add_variable_internal(double* variable,
	                           int dimension,
	                           ChangeOfVariables* change_of_variables = 0);

	// Copies variables from a global vector x to the Function's
	// local storage.
	void copy_global_to_local(const Eigen::VectorXd& x) const;
	// Copies variables from a global vector x to the storage
	// provided by the user.
	void copy_global_to_user(const Eigen::VectorXd& x) const;
	// Copies variables from a the storage provided by the user
	// to a global vector x.
	void copy_user_to_global(Eigen::VectorXd* x) const;
	// Copies variables from a the storage provided by the user
	// to the Function's local storage.
	void copy_user_to_local() const;

	// Evaluates the function at the point in the local storage.
	double evaluate_from_local_storage() const;

	// A set of all terms added to the function. This is
	// used when the function is destructed.
	std::set<const Term*> added_terms;

	// All variables added to the function.
	std::map<double*, AddedVariable> variables;

	// Each variable can have several dimensions. This member
	// keeps track of the total number of scalars.
	size_t number_of_scalars;

	// All terms added to the function.
	std::vector<AddedTerm> terms;

	// Number of threads used for evaluation.
	int number_of_threads;

	// Allocates temporary storage for gradient evaluations.
	// Should be called automatically at first evaluate()
	void allocate_local_storage() const;

	// If finalize has been called.
	mutable bool local_storage_allocated;
	// Has to be mutable because the temporary storage
	// needs to be written to.
	mutable std::vector< std::vector<Eigen::VectorXd> >
		thread_gradient_scratch;
	mutable std::vector<Eigen::VectorXd>
		thread_gradient_storage;

	// Stored how many element were used the last time the Hessian
	// was created.
	mutable size_t number_of_hessian_elements;

private:
	Function* interface;
};

Function::Function() :
	impl(new Function::Implementation(this))
{
	impl->number_of_scalars = 0;
	this->term_deletion = DeleteTerms;

	this->hessian_is_enabled = true;

	impl->number_of_hessian_elements = 0;

	this->evaluations_without_gradient = 0;
	this->evaluations_with_gradient    = 0;

	this->evaluate_time               = 0;
	this->evaluate_with_hessian_time  = 0;
	this->write_gradient_hessian_time = 0;
	this->copy_time                   = 0;

	#ifdef USE_OPENMP
		impl->number_of_threads = omp_get_max_threads();
	#else
		impl->number_of_threads = 1;
	#endif

	impl->local_storage_allocated = false;
{'fraction_non_alphanumeric': 0.0918786944230275, 'fraction_numerical': 0.004626060138781804, 'mean_word_length': 3.888819095477387, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4077286', 'n_tokens_mistral': 2492, 'n_tokens_neox': 2344, 'n_words': 741}