public void AugmentedReducedRowEchelonFormTest() { var A = new Matrix(new double[,] { { 1, 2, 3 }, { 0, 1, 4 }, { 5, 6, 0 } }); var I = A.GetIdentity(); var aug = new AugmentedMatrix(new Matrix[] { A, I }); var augGE = aug.ReducedRowEchelonForm(); var identity = new Matrix(new double[,] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } }); var inverse = new Matrix(new double[,] { { -24, 18, 5 }, { 20, -15, -4 }, { -5, 4, 1 } }); Assert.That(augGE[0], Is.EqualTo(identity)); Assert.That(augGE[1], Is.EqualTo(inverse)); var B = new Matrix(new double[,] { { 1, 2, 3, 6 }, { 0, 1, 4, 8 }, { 5, 6, 0, 9 }, { 9, 4, 7, 3 } }); I = new Matrix(4, 4).GetIdentity(); var aug2 = new AugmentedMatrix(new Matrix[] { B, I }); var augRref2 = aug2.ReducedRowEchelonForm(); var identity2 = new Matrix(4, 4).GetIdentity(); var inverse2 = new Matrix( new double[,] { { -69.0 / 67.0, 36.0 / 67.0, 11.0 / 67.0, 9.0 / 67.0 }, { 544.0 / 335.0, -69.0 / 67, -44.0 / 335.0, -36.0 / 335.0 }, { 206.0 / 335.0, -18.0 / 67.0, -61.0 / 335.0, 11.0 / 335.0 }, { -171.0 / 335.0, 26.0 / 67.0, 36.0 / 335.0, -1.0 / 335.0 } }); Assert.That(augRref2[0], Is.EqualTo(identity2)); Assert.That(augRref2[1], Is.EqualTo(inverse2)); }
public static double[] Multiply_SIMD_2(Matrix A, Matrix B) { // Abour 50% fateser when matrix size >= 8x8 var vecSize = Vector<double>.Count; var bRemainer = B.Columns % Vector<double>.Count; if (B.Columns % Vector<double>.Count != 0) { B.AddColumns(bRemainer); } var C = new double[A.Rows * B.Columns]; for (int i = 0; i < A.Rows; i++) { for (int k = 0; k < A.Columns; k++) { for (int j = 0; j < B.Columns; j += vecSize) { var vC = new Vector<double>(C, i * A.Rows + j); var vB = new Vector<double>(B.internalArray, k * B.Columns + j); var vA = new Vector<double>(A.internalArray[i * A.Columns + k]); vC += vA * vB; vC.CopyTo(C, i * A.Rows + j); } } } return C.ToArray(); }
public static void Tests() { float[,] A = { { 0, 1 }, { 1, 2 } }; float[,] C = { { 0, 1, 2, 2 }, { 1, 2, 2, 2 } }; float[,] Scale = { { 0, 2 }, { 2, 4 } }; Matrices.Matrix a = new Matrices.Matrix(A); Matrices.Matrix b = new Matrices.Matrix(A); Matrices.Matrix c = new Matrices.Matrix(C); Matrices.Matrix s = new Matrices.Matrix(Scale); Console.WriteLine("Equality Test: " + (a == b ? "passed" : "fail")); Console.WriteLine("Inequality Test: " + (a != b ? "passed" : "fail")); Console.WriteLine("Identity Test: " + (a * Matrices.Matrix.IdentityMatrix(2) == a ? "passed" : "fail")); Console.WriteLine("Scale Test: " + ((2 * a) == s ? "passed" : "fail")); Console.WriteLine(a * c); }
public static Matrix Multiply(Matrix A, Matrix B) { var array = new double[A.Rows * B.Columns]; for (int i = 0; i < A.Rows; i++) { for (int k = 0; k < A.Columns; k++) { for (int j = 0; j < B.Columns; j++) { array[i * A.Rows + j] += A.internalArray[i * A.Columns + k] * B.internalArray[k * B.Columns + j]; } } } return new Matrix(A.Rows, B.Columns, array); }
public void GaussianEliminationTest() { // Example 3.11, pg 35 var ex311 = new Matrix(new double[,] { { 1, 0, 1, 0, 1 }, { 1, 1, 0, 0, 2 }, { 3, 1, 1, 1, 1 }, { 0, 1, 2, 1, 2 } }); var ex311Result = new Matrix( new double[,] { { 1, 0, 1, 0, 1 }, { 0, 1, -1, 0, 1 }, { 0, 0, -1, 1, -3 }, { 0, 0, 0, 4, -8 }, }); // Example 3.12, pg. 36 var ex312 = new Matrix(new double[,] { { 0, 0, 2 }, { 1, -1, 1 }, { -1, 1, 4 } }); var ex312Result = new Matrix(new double[,] { { 1, -1, 1 }, { 0, 0, 2 }, { 0, 0, 0 } }); // Example 3.13, pg. 36 var ex313 = new Matrix(new double[,] { { 1, -2, 1 }, { 2, -4, 2 }, { -1, 2, -1 } }); var ex313Result = new Matrix(new double[,] { { 1, -2, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }); Assert.That(MatrixOperations.GaussianElimination(ex311), Is.EqualTo(ex311Result)); Assert.That(MatrixOperations.GaussianElimination(ex312), Is.EqualTo(ex312Result)); Assert.That(MatrixOperations.GaussianElimination(ex313), Is.EqualTo(ex313Result)); }
public void MatrixAdditionSubtractionTest() { // Addition var add_A = new Matrix(new double[,] { { 2, 0 }, { 1, 2 }, { 1, 1 } }); var add_B = new Matrix(new double[,] { { 5, -1 }, { 1, 1 }, { 9, 0 } }); var add_Result = new Matrix(new double[,] { { 7, -1 }, { 2, 3 }, { 10, 1 } }); // Subtraction var subtract_A = new Matrix(new double[,] { { 2, 0 }, { 1, 2 }, { 1, 1 } }); var subtract_B = new Matrix(new double[,] { { 5, -1 }, { 1, 1 }, { 9, 0 } }); var subtract_Result = new Matrix(new double[,] { { -3, 1 }, { 0, 1 }, { -8, 1 } }); Assert.That(add_A + add_B, Is.EqualTo(add_Result)); Assert.That(subtract_A - subtract_B, Is.EqualTo(subtract_Result)); }
public void TransposeTest() { var a = new Matrix(new double[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }); var aTranspose = new Matrix(new double[,] { { 1, 4, 7 }, { 2, 5, 8 }, { 3, 6, 9 } }); var b = new Matrix(new double[,] { { 1, 2, 3, 4 } }); var bTranspose = new Matrix(new double[,] { { 1 }, { 2 }, { 3 }, { 4 } }); Assert.That(a.GetTranspose(), Is.EqualTo(aTranspose)); Assert.That(b.GetTranspose(), Is.EqualTo(bTranspose)); }
public void SymmetryTest() { var a = new Matrix(new double[,] { { 1, 2, 3 }, { 2, 5, 6 }, { 3, 6, 9 } }); var b = new Matrix(new double[,] { { 1, 2, 3, 4 } }); Assert.That(a.IsSymmetric, Is.EqualTo(true)); Assert.That(b.IsSymmetric, Is.EqualTo(false)); }
public void ReducedRowEchelonFormTest() { var test_1 = new Matrix(new double[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }); var test_1_Result = new Matrix(new double[,] { { 1, 0, -1 }, { 0, 1, 2 }, { 0, 0, 0 } }); var test_2 = new Matrix(new double[,] { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 } }); var test_2_Result = new Matrix(new double[,] { { 1, 0, -1, -2, -3 }, { 0, 1, 2, 3, 4 } }); var test_3 = new Matrix(new double[,] { { 1, 2, 1, 2, 1 }, { 2, 4, 4, 8, 4 }, { 3, 6, 5, 7, 7 } }); var test_3_Result = new Matrix(new double[,] { { 1, 2, 0, 0, 0 }, { 0, 0, 1, 0, 7.0 / 3.0 }, { 0, 0, 0, 1, -2.0 / 3.0 } }); Assert.That(MatrixOperations.ReducedRowEchelonForm(test_1), Is.EqualTo(test_1_Result)); Assert.That(MatrixOperations.ReducedRowEchelonForm(test_2), Is.EqualTo(test_2_Result)); Assert.That(MatrixOperations.ReducedRowEchelonForm(test_3), Is.EqualTo(test_3_Result)); }
public void RankTest() { var test1 = new Matrix(new double[,] { { 0, 0, 2 }, { 1, -1, 1 }, { -1, 1, 4 } }); var test2 = new Matrix(new double[,] { { 1, -2, 1 }, { 2, -4, 2 }, { -1, 2, -1 } }); var test3 = new Matrix(new double[,] { { 1, 2, 3 }, { 1, 3, 0 }, { 7, 8, 9 } }); Assert.That(MatrixOperations.Rank(test1), Is.EqualTo(2)); Assert.That(MatrixOperations.Rank(test2), Is.EqualTo(1)); Assert.That(MatrixOperations.Rank(test3), Is.EqualTo(3)); }
public void MatrixMultiplicationTest() { // Example 1.4, pg 7 var ex14_A = new Matrix(new double[,] { { 0, 1, 0 }, { 2, 3, 1 } }); // Example 1.4, pg 7 var ex14_B = new Matrix(new double[,] { { 2, 0 }, { 1, 2 }, { 1, 1 } }); var ex14_Result = new Matrix(new double[,] { { 1, 2 }, { 8, 7 } }); // Exercise 1.12 var ex112_A = new Matrix(new double[,] { { 3, 1, -2 }, { 2, -2, 0 }, { -1, 1, 2 } }); var ex112_B = new Matrix(new double[,] { { 1, 1, 1 }, { 1, -1, 1 }, { 0, 1, 2 } }); var ex112_Result = new Matrix(new double[,] { { 4, 0, 0 }, { 0, 4, 0 }, { 0, 0, 4 } }); // Exercise 1.13 var ex113_A = new Matrix(new double[,] { { 1, 1, 1 }, { 0, 1, 1 }, { 0, 0, 1 } }); var ex113_Result = new Matrix(new double[,] { { 3, 2, 1 }, { 2, 2, 1 }, { 1, 1, 1 } }); var ex113_B = new Matrix(new double[,] { { 1, 0, 0 }, { 1, 1, 0 }, { 1, 1, 1 } }); Assert.That(ex14_A * ex14_B, Is.EqualTo(ex14_Result)); Assert.That(ex112_A * ex112_B, Is.EqualTo(ex112_Result)); Assert.That(ex113_A * ex113_B, Is.EqualTo(ex113_Result)); }
/// <summary> /// Initializes a new instance of the <see cref="AugmentedMatrix"/> class representing an augmented matrix. All elementary row operations on an augmented matrix must be performed on the first /// element of the <paramref name="matrices"/> array. In order to ensure that elementary row operations are erformed correctly, you must use the row operation methods for an <see cref="AugmentedMatrix"/> only. /// </summary> /// <param name="matrices">An array of <see cref="Matrix"/> objects.</param> public AugmentedMatrix(Matrix[] matrices) { this.Matricies = matrices; }