From b991153b8b955313d928b31860cf7fa1825dbd6a Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Wed, 12 Feb 2025 11:38:15 +0100 Subject: [PATCH] Add ChiSquareTest --- LinearAlgebra/Matrix.cs | 122 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/LinearAlgebra/Matrix.cs b/LinearAlgebra/Matrix.cs index 60d260b..1960109 100644 --- a/LinearAlgebra/Matrix.cs +++ b/LinearAlgebra/Matrix.cs @@ -1,5 +1,127 @@ +using System.Diagnostics; using Vector3 = UnityEngine.Vector3; +public class Matrix2 { + public float[,] data { get; } + + public int nRows => data.GetLength(0); + public int nCols => data.GetLength(1); + + public Matrix2(float[,] data) { + this.data = data; + } + + public static Matrix2 Identity(int size) { + float[,] I = new float[size, size]; + for (int i = 0; i < size; i++) I[i, i] = 1.0f; + return new Matrix2(I); + } + + public Matrix2 Transpose() { + float[,] r = new float[this.nCols, this.nRows]; + for (uint rowIx = 0; rowIx < this.nRows; rowIx++) { + for (uint colIx = 0; colIx < this.nCols; colIx++) + r[colIx, rowIx] = this.data[rowIx, colIx]; + } + return new Matrix2(r); + // double checked code + } + + public static Matrix2 operator +(Matrix2 A, Matrix2 B) { + if (A.nRows != B.nRows || A.nCols != B.nCols) + throw new System.ArgumentException("Size of A must match size of B."); + + float[,] result = new float[A.nRows, B.nCols]; + + for (int i = 0; i < A.nRows; i++) { + for (int j = 0; j < A.nCols; j++) { + result[i, j] = A.data[i, j] + B.data[i, j]; + } + } + return new Matrix2(result); + } + + public static Matrix2 operator *(Matrix2 A, Matrix2 B) { + if (A.nCols != B.nRows) + throw new System.ArgumentException("Number of columns in A must match number of rows in B."); + + float[,] result = new float[A.nRows, B.nCols]; + + for (int i = 0; i < A.nRows; i++) { + for (int j = 0; j < B.nCols; j++) { + float sum = 0.0f; + for (int k = 0; k < A.nCols; k++) + sum += A.data[i, k] * B.data[k, j]; + + result[i, j] = sum; + } + } + + return new Matrix2(result); + // double checked code + } + + public static Matrix1 operator *(Matrix2 A, Matrix1 v) { + // int rows = A.GetLength(0); + // int cols = A.GetLength(1); + float[] result = new float[A.nRows]; + + for (int i = 0; i < A.nRows; i++) { + for (int j = 0; j < A.nCols; j++) { + result[i] += A.data[i, j] * v.data[j]; + } + } + + return new Matrix1(result); + } + + + public static Matrix2 operator *(Matrix2 A, float s) { + float[,] result = new float[A.nRows, A.nCols]; + + for (int i = 0; i < A.nRows; i++) { + for (int j = 0; j < A.nCols; j++) + result[i, j] += A.data[i, j] * s; + } + + return new Matrix2(result); + } + public static Matrix2 operator *(float scalar, Matrix2 A) { + return A * scalar; + } + +} + +public class Matrix1 { + public float[] data { get; } + + public int size => data.GetLength(0); + + public Matrix1(float[] data) { + this.data = data; + } + + public Matrix2 Transpose() { + float[,] r = new float[1, this.size]; + for (uint colIx = 0; colIx < this.size; colIx++) + r[1, colIx] = this.data[colIx]; + + return new Matrix2(r); + } + + public static float Dot(Matrix1 a, Matrix1 b) { + if (a.size != b.size) + throw new System.ArgumentException("Vectors must be of the same length."); + + float result = 0.0f; + for (int i = 0; i < a.size; i++) { + result += a.data[i] * b.data[i]; + } + return result; + } + +} + public class Matrix { private readonly uint rows = 0; private readonly uint cols = 0;