<commit_before>#ifndef POLYTOPE_H
#define POLYTOPE_H

#include <assert.h>
#include <iostream>
#include <vector>

#include <Eigen/Dense>


/* The half-space representation is used internally. */
class Polytope
{
  public:
	Polytope( Eigen::MatrixXd incoming_H, Eigen::VectorXd incoming_K );
	Polytope( const Polytope &other );

	bool is_consistent() const;
	bool is_in( Eigen::VectorXd X ) const;

	// Factory functions
	static Polytope * random( int n );

	/** bounds is an array of the form [x1_min, x1_max, x2_min, x2_max, ..., xn_min, xn_max],

	   which defines a polytope in terms of intervals along each axis in
	   R^n. The interval along the first axis is [x1_min, x1_max], the interval
	   along the second axis is [x2_min, x2_max], etc. Thus the length of the
	   given array is 2n.

	   E.g., a unit square in R^2 can be created using:

	       Eigen::Vector4d bounds;
	       bounds << 0, 1,
	                 0, 1;
	       Polytope *square = Polytope::box( bounds );
	*/
	static Polytope * box( const Eigen::VectorXd &bounds );

	/* Output this polytope in JSON */
	friend std::ostream & operator<<( std::ostream &out, const Polytope &P );
	void dumpJSON( std::ostream &out ) const;

  private:
	// { x \in R^n | Hx \leq K }
	Eigen::MatrixXd H;
	Eigen::VectorXd K;
};

std::ostream & operator<<( std::ostream &out, const Polytope &P )
{
	out << "{ ";
	P.dumpJSON( out );
	out << " }";
	return out;
}

void Polytope::dumpJSON( std::ostream &out ) const
{
	int i, j;
	out << "\"H\": [";
	for (i = 0; i < H.rows(); i++) {
		if (i > 0) {
			out << ", ";
		}
		out << "[";
		for (j = 0; j < H.cols(); j++) {
			if (j > 0)
				out << ", ";
			out << H(i,j);
		}
		out << "]";
	}
	out << "], \"K\": [";
	for (i = 0; i < K.rows(); i++) {
		if (i > 0) {
			out << ", ";
		}
		out << K(i);
	}
	out << "]";
}

Polytope * Polytope::random( int n )
{
	return new Polytope( Eigen::MatrixXd::Random( n, n ),
						 Eigen::VectorXd::Random( n ) );
}

Polytope * Polytope::box( const Eigen::VectorXd &bounds )
{
	assert( bounds.size() % 2 == 0 );<commit_msg>Improve documentation; change random to Polytope::randomH()<commit_after>#ifndef POLYTOPE_H
#define POLYTOPE_H

#include <assert.h>
#include <iostream>
#include <vector>

#include <Eigen/Dense>


/* The half-space representation is used internally. */
class Polytope
{
  public:
	Polytope( Eigen::MatrixXd incoming_H, Eigen::VectorXd incoming_K );
	Polytope( const Polytope &other );

	bool is_consistent() const;
	bool is_in( Eigen::VectorXd X ) const;

	// Factory functions
	/** Create random Polytope in R^n using Eigen::MatrixXd::Random().

		\param n dimension of the containing space.

		Random matrices as provided by Eigen::MatrixXd::Random() are used to
		instantiate Polytope in half-space representation. The matrices have
		\c n+1 rows, corresponding to \c n+1 inequalities.
	*/
	static Polytope * randomH( int n );

	/** Construct Polytope that is an axis-aligned rectangle.

	   \param bounds an array of the form
	   \code
	   [x1_min, x1_max, x2_min, x2_max, ..., xn_min, xn_max],
       \endcode
	   which defines a polytope in terms of intervals along each axis in R^n.
	   The interval along the first axis is [x1_min, x1_max], the interval along
	   the second axis is [x2_min, x2_max], etc. Thus the length of the given
	   array is 2n.

	   E.g., a unit square in R^2 can be created using
	   \code
	       Eigen::Vector4d bounds;
	       bounds << 0, 1,
	                 0, 1;
	       Polytope *square = Polytope::box( bounds );
	   \endcode
	*/
	static Polytope * box( const Eigen::VectorXd &bounds );

	/* Output this polytope in JSON */
	friend std::ostream & operator<<( std::ostream &out, const Polytope &P );
	void dumpJSON( std::ostream &out ) const;

  private:
	// { x \in R^n | Hx \leq K }
	Eigen::MatrixXd H;
	Eigen::VectorXd K;
};

std::ostream & operator<<( std::ostream &out, const Polytope &P )
{
	out << "{ ";
	P.dumpJSON( out );
	out << " }";
	return out;
}

void Polytope::dumpJSON( std::ostream &out ) const
{
	int i, j;
	out << "\"H\": [";
	for (i = 0; i < H.rows(); i++) {
		if (i > 0) {
			out << ", ";
		}
		out << "[";
		for (j = 0; j < H.cols(); j++) {
			if (j > 0)
				out << ", ";
			out << H(i,j);
		}
		out << "]";
	}
	out << "], \"K\": [";
	for (i = 0; i < K.rows(); i++) {
		if (i > 0) {
			out << ", ";
		}
		out << K(i);
	}
	out << "]";
}

Polytope * Polytope::randomH( int n )
{
	return new Polytope( Eigen::MatrixXd::Random( n+1, n ),
						 Eigen::VectorXd::Random( n+1 ) );
}

Polytope * Polytope::box( const Eigen::VectorXd &bounds )
{
	assert( bounds.size() % 2 == 0 );
{'fraction_non_alphanumeric': 0.16477768090671316, 'fraction_numerical': 0.010897994768962511, 'mean_word_length': 3.000871839581517, 'pattern_counts': {'":': 4, '<': 77, '<?xml version=': 0, '>': 17, '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': '933164', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1710, 'n_words': 531}