public class Matrix { private readonly uint rows = 0; private readonly uint cols = 0; private float[] data; public Matrix(uint rows, uint cols) { this.rows = rows; this.cols = cols; } public static float[,] Transpose(float[,] m) { float[,] r = new float[m.GetLength(1), m.GetLength(0)]; for (uint rowIx = 0; rowIx < m.GetLength(0); rowIx++) { for (uint colIx = 0; colIx < m.GetLength(1); colIx++) r[colIx, rowIx] = m[rowIx, colIx]; } return r; } public static void NegateColumn(float[,] m, uint colIx) { for (uint rowIx = 0; rowIx < m.GetLength(0); rowIx++) { m[rowIx, colIx] = -m[rowIx, colIx]; } } public static float Determinant(float[,] matrix) { int n = matrix.GetLength(0); if (n != matrix.GetLength(1)) throw new System.ArgumentException("Matrix must be square."); if (n == 1) return matrix[0, 0]; // Base case for 1x1 matrix if (n == 2) // Base case for 2x2 matrix return matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0]; float det = 0; for (int col = 0; col < n; col++) { det += (col % 2 == 0 ? 1 : -1) * matrix[0, col] * Determinant(Minor(matrix, 0, col)); } return det; } // Helper function to compute the minor of a matrix private static float[,] Minor(float[,] matrix, int rowToRemove, int colToRemove) { int n = matrix.GetLength(0); float[,] minor = new float[n - 1, n - 1]; int r = 0, c = 0; for (int i = 0; i < n; i++) { if (i == rowToRemove) continue; c = 0; for (int j = 0; j < n; j++) { if (j == colToRemove) continue; minor[r, c] = matrix[i, j]; c++; } r++; } return minor; } public static float[,] MultiplyMatrices(float[,] A, float[,] B) { int rowsA = A.GetLength(0); int colsA = A.GetLength(1); int rowsB = B.GetLength(0); int colsB = B.GetLength(1); if (colsA != rowsB) throw new System.ArgumentException("Number of columns in A must match number of rows in B."); float[,] result = new float[rowsA, colsB]; for (int i = 0; i < rowsA; i++) { for (int j = 0; j < colsB; j++) { float sum = 0; for (int k = 0; k < colsA; k++) { sum += A[i, k] * B[k, j]; } result[i, j] = sum; } } return result; } }