<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERY.EMath;

namespace TestMath
{
	class Program
	{
		static void Main(string[] args)
		{
			foreach (MatrixTestCase m in GetMatrixTestCases())
			{
				Console.WriteLine("Matrix:\n{0}", m.Matrix.ToString());

				Matrix eigenvals, eigenvecs;
				m.Matrix.EigenValsVecs(out eigenvals, out eigenvecs);

				CheckEigenvectorDifference(eigenvals, eigenvecs, m.Eigenvalues, m.Eigenvectors);

			}
		}

		private static void CheckEigenvectorDifference(Matrix eigenvals_a, Matrix eigenvecs_a, Matrix eigenvals_b, Matrix eigenvecs_b)
		{
			Matrix dot = eigenvecs_a.HermitianConjugate() * eigenvecs_b;
			Matrix evDiff = new Matrix(eigenvecs_a.Rows, 1);
			bool badVector = false;
			bool badValue = false;

			AssertEigenvectorsNormalized(eigenvecs_a);

			for (int i = 0; i < dot.Rows; i++)
			{
				bool found = false;

				for (int j = 0; j < dot.Columns; j++)
				{
					double mag = dot[i, j].Magnitude;
					double mag_one = Math.Abs(dot[i, j].Magnitude - 1);

					if (mag > 1e-5 && mag_one < 1e-5)
					{
						if (found == false)
						{
							found = true;
							evDiff[i, 0] = eigenvals_a[i, 0] - eigenvals_b[j, 0];

							if (evDiff[i, 0].Magnitude > 1e-5)
								badValue = true;
						}
						else
							badVector = true;
					}
					else if (mag > 1e-5 && mag_one > 1e-5)
					{
						evDiff[i, 0] = eigenvals_a[i, 0] - eigenvals_b[j, 0];

						if (evDiff[i, 0].Magnitude > 1e-5)
							badValue = true;

						found = true;
					}
				}

				if (found == false)
					badVector = true;
			}

			if (badVector)
			{
				Console.WriteLine("Eigenvectors differ:");
				Console.WriteLine();
				Console.WriteLine("Eigenvalues:");
				Console.WriteLine(eigenvals_a.ToString());
				Console.WriteLine("Eigenvectors:");
				Console.WriteLine(eigenvecs_a.ToString("0.0000"));
				Console.WriteLine();
				Console.WriteLine("Reference:");
				Console.WriteLine(eigenvecs_b.ToString("0.0000"));
				Console.WriteLine();
				Console.WriteLine("Dot products:");
				Console.WriteLine(dot.ToString("0.0000"));
				throw new Exception();
			}
			if (badValue)
			{
				Console.WriteLine("Eigenvalus differ:");

				throw new Exception();
			}
		}

		private static void AssertEigenvectorsNormalized(Matrix eigenvecs_a)
		{
			Matrix ident = eigenvecs_a.HermitianConjugate() * eigenvecs_a;
			bool bad = false;

			for (int i = 0; i < ident.Rows; i++)
			{
				for (int j = 0; j < ident.Columns; j++)
				{
					if (i == j && (ident[i, j] - 1).Magnitude > 1e-5)
						bad = true;
					else if (i != j && ident[i, j].Magnitude > 1e-5)
						bad = true;
				}
			}

			if (bad == true)
			{
				Console.WriteLine("Eigenvectors not orthonormal.");
				Console.WriteLine(eigenvecs_a.ToString());
				throw new Exception();

			}
		}

		private static IEnumerable<MatrixTestCase> GetMatrixTestCases()
		{<commit_msg>Update testing.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERY.EMath;

namespace TestMath
{
	class Program
	{
		static void Main(string[] args)
		{
			foreach (MatrixTestCase m in GetMatrixTestCases())
			{
				Console.WriteLine("Matrix:\n{0}", m.Matrix.ToString());

				Matrix eigenvals, eigenvecs;
				m.Matrix.EigenValsVecs(out eigenvals, out eigenvecs);

				CheckEigenvectorDifference(eigenvals, eigenvecs, m.Eigenvalues, m.Eigenvectors);

			}
		}

		private static void CheckEigenvectorDifference(Matrix eigenvals_a, Matrix eigenvecs_a, Matrix eigenvals_b, Matrix eigenvecs_b)
		{
			Matrix dot = eigenvecs_a.HermitianConjugate() * eigenvecs_b;
			Matrix evDiff = new Matrix(eigenvecs_a.Rows, 1);
			bool badVector = false;
			bool badValue = false;

			AssertEigenvectorsNormalized(eigenvecs_a);

			for (int i = 0; i < dot.Rows; i++)
			{
				bool found = false;

				for (int j = 0; j < dot.Columns; j++)
				{
					double mag = dot[i, j].Magnitude;
					double mag_one = Math.Abs(dot[i, j].Magnitude - 1);

					if (mag > 1e-5 && mag_one < 1e-5)
					{
						if (found == false)
						{
							found = true;
							evDiff[i, 0] = eigenvals_a[i, 0] - eigenvals_b[j, 0];

							if (evDiff[i, 0].Magnitude > 1e-5)
								badValue = true;
						}
						else
							badVector = true;
					}
					else if (mag > 1e-5 && mag_one > 1e-5)
					{
						evDiff[i, 0] = eigenvals_a[i, 0] - eigenvals_b[j, 0];

						if (evDiff[i, 0].Magnitude > 1e-5)
							badValue = true;

						found = true;
					}
				}

				if (found == false)
					badVector = true;
			}

			if (badVector)
			{
				Console.WriteLine("Eigenvectors differ:");
				Console.WriteLine();
				Console.WriteLine("Eigenvalues:");
				Console.WriteLine(eigenvals_a.ToString());
				Console.WriteLine("Eigenvectors:");
				Console.WriteLine(eigenvecs_a.ToString("0.0000"));
				Console.WriteLine();
				Console.WriteLine("Reference:");
				Console.WriteLine(eigenvecs_b.ToString("0.0000"));
				Console.WriteLine();
				Console.WriteLine("Dot products:");
				Console.WriteLine(dot.ToString("0.0000"));
				throw new Exception();
			}
			if (badValue)
			{
				Console.WriteLine("Eigenvalues differ:");
				Console.WriteLine("Eigenvalues:");
				Console.WriteLine(eigenvals_a.ToString());
				Console.WriteLine("Reference:");
				Console.WriteLine(eigenvals_b.ToString());
				Console.WriteLine("Difference:");
				Console.WriteLine(evDiff.ToString());
				throw new Exception();
			}
		}

		private static void AssertEigenvectorsNormalized(Matrix eigenvecs_a)
		{
			Matrix ident = eigenvecs_a.HermitianConjugate() * eigenvecs_a;
			bool bad = false;

			for (int i = 0; i < ident.Rows; i++)
			{
				for (int j = 0; j < ident.Columns; j++)
				{
					if (i == j && (ident[i, j] - 1).Magnitude > 1e-5)
						bad = true;
					else if (i != j && ident[i, j].Magnitude > 1e-5)
						bad = true;
				}
			}

			if (bad == true)
			{
				Console.WriteLine("Eigenvectors not orthonormal.");
				Console.WriteLine(eigenvecs_a.ToString("0.0000"));
				Console.WriteLine(ident.ToString("0.0000"));
				throw new Exception();

			}
		}

		private static IEnumerable<MatrixTestCase> GetMatrixTestCases()
		{
{'fraction_non_alphanumeric': 0.14092758340113914, 'fraction_numerical': 0.01692432872253865, 'mean_word_length': 2.9296675191815855, 'pattern_counts': {'":': 0, '<': 15, '<?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': '747019', 'n_tokens_mistral': 2828, 'n_tokens_neox': 2119, 'n_words': 454}