<commit_before>#include "AmariIndex.h"

#ifdef HAVE_EIGEN3

#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/eigen3.h>

using namespace Eigen;

typedef Matrix< float64_t, Dynamic, 1, ColMajor > EVector;
typedef Matrix< float64_t, Dynamic, Dynamic, ColMajor > EMatrix;

using namespace shogun;

float64_t amari_index(SGMatrix<float64_t> SGW, SGMatrix<float64_t> SGA, bool standardize)
{
	Eigen::Map<EMatrix> W(SGW.matrix,SGW.num_rows,SGW.num_cols);
	Eigen::Map<EMatrix> A(SGA.matrix,SGA.num_rows,SGA.num_cols);
	
	if (W.rows() != W.cols())
		SG_SERROR("amari_index - W must be square\n")
	
	if (A.rows() != A.cols())
		SG_SERROR("amari_index - A must be square\n")
	
	if (W.rows() != A.rows())
		SG_SERROR("amari_index - A and W must be the same size\n")
		
	if (W.rows() < 2)
		SG_SERROR("amari_index - input must be at least 2x2\n")
	
	// normalizing both mixing matrices	
	if (standardize)
	{
		for (int r = 0; r < W.rows(); r++)
		{
			W.row(r).normalize();
			if (W.row(r).maxCoeff() < -1*W.row(r).minCoeff())
				W.row(r) *= -1;
		}
		
		A = A.inverse();
		for (int r = 0; r < A.rows(); r++)
		{
			A.row(r).normalize();
			if (A.row(r).maxCoeff() < -1*A.row(r).minCoeff())
				A.row(r) *= -1;
		}
		A = A.inverse();
		
		bool swap = false;
		do
		{
			swap = false;
			for (int j = 1; j < A.cols(); j++)
			{
				if (A(0,j) < A(0,j-1))
				{
					A.col(j).swap(A.col(j-1));
					swap = true;	
				}						
			}
	
		} while(swap);
	}
	
	// calculating the permutation matrix
	EMatrix P = (W * A).cwiseAbs();
	int k = P.rows();
	
	// summing the error in the permutation matrix
	EMatrix E1(k,k);
	for (int r = 0; r < k; r++)
		E1.row(r) = P.row(r) / P.row(r).maxCoeff();
	
	EMatrix E2(k,k);
	for (int c = 0; c < k; c++)
		E2.col(c) = P.col(c) / P.col(c).maxCoeff();

	return 1.0 / (float)(2*k*(k-1)) * ((E1.rowwise().sum().array()-1).sum() + (E2.colwise().sum().array()-1).sum());	
	
}
#endif //HAVE_EIGEN3<commit_msg>changed AmariIndex calc to use more variables for clarity<commit_after>#include "AmariIndex.h"

#ifdef HAVE_EIGEN3

#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/eigen3.h>

using namespace Eigen;

typedef Matrix< float64_t, Dynamic, 1, ColMajor > EVector;
typedef Matrix< float64_t, Dynamic, Dynamic, ColMajor > EMatrix;

using namespace shogun;

float64_t amari_index(SGMatrix<float64_t> SGW, SGMatrix<float64_t> SGA, bool standardize)
{
	Eigen::Map<EMatrix> W(SGW.matrix,SGW.num_rows,SGW.num_cols);
	Eigen::Map<EMatrix> A(SGA.matrix,SGA.num_rows,SGA.num_cols);
	
	if (W.rows() != W.cols())
		SG_SERROR("amari_index - W must be square\n")
	
	if (A.rows() != A.cols())
		SG_SERROR("amari_index - A must be square\n")
	
	if (W.rows() != A.rows())
		SG_SERROR("amari_index - A and W must be the same size\n")
		
	if (W.rows() < 2)
		SG_SERROR("amari_index - input must be at least 2x2\n")
	
	// normalizing both mixing matrices	
	if (standardize)
	{
		for (int r = 0; r < W.rows(); r++)
		{
			W.row(r).normalize();
			if (W.row(r).maxCoeff() < -1*W.row(r).minCoeff())
				W.row(r) *= -1;
		}
		
		A = A.inverse();
		for (int r = 0; r < A.rows(); r++)
		{
			A.row(r).normalize();
			if (A.row(r).maxCoeff() < -1*A.row(r).minCoeff())
				A.row(r) *= -1;
		}
		A = A.inverse();
		
		bool swap = false;
		do
		{
			swap = false;
			for (int j = 1; j < A.cols(); j++)
			{
				if (A(0,j) < A(0,j-1))
				{
					A.col(j).swap(A.col(j-1));
					swap = true;	
				}						
			}
	
		} while(swap);
	}
	
	// calculating the permutation matrix
	EMatrix P = (W * A).cwiseAbs();
	int k = P.rows();
	
	// summing the error in the permutation matrix
	EMatrix E1(k,k);
	for (int r = 0; r < k; r++)
		E1.row(r) = P.row(r) / P.row(r).maxCoeff();
	
	float64_t row_error = (E1.rowwise().sum().array()-1).sum();
	
	EMatrix E2(k,k);
	for (int c = 0; c < k; c++)
		E2.col(c) = P.col(c) / P.col(c).maxCoeff();
	
	float64_t col_error = (E2.colwise().sum().array()-1).sum();

	return 1.0 / (float)(2*k*(k-1)) * (row_error + col_error);	
	
}
#endif //HAVE_EIGEN3
{'fraction_non_alphanumeric': 0.20090180360721444, 'fraction_numerical': 0.022044088176352707, 'mean_word_length': 3.070336391437309, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 19, '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': '810690', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1748, 'n_words': 377}