private static void TestMatrixMatrixMultiplication(LinearAlgebraProviderChoice providers) { TestSettings.RunMultiproviderTest(providers, delegate() { var A1 = Matrix.CreateFromArray(SquareSingular10by10.Matrix); var A2 = Matrix.CreateFromArray(RectangularFullRank10by5.Matrix); var expectedA1TimesA2 = Matrix.CreateFromArray( MatrixOperations.MatrixTimesMatrix(SquareSingular10by10.Matrix, RectangularFullRank10by5.Matrix)); var expectedTransposeA2TimesA1 = Matrix.CreateFromArray( MatrixOperations.MatrixTimesMatrix( MatrixOperations.Transpose(RectangularFullRank10by5.Matrix), SquareSingular10by10.Matrix)); // MultiplyRight() without transposition comparer.AssertEqual(expectedA1TimesA2, A1.MultiplyRight(A2, false, false)); // operator* comparer.AssertEqual(expectedA1TimesA2, A1 * A2); // MultiplyRight() with transposition comparer.AssertEqual(expectedTransposeA2TimesA1, A2.MultiplyRight(A1, true, false)); // MultiplyRight() with incorrect dimensions Assert.Throws <NonMatchingDimensionsException>(() => A2.MultiplyRight(A1, false, false)); }); }
public void MatrixInverseTest() { double[][] matrix = MatrixOperations.MatrixCreate(3, 3); matrix[0][0] = 1.0; matrix[0][1] = -1.0; matrix[0][2] = 0.0; matrix[1][0] = 0.0; matrix[1][1] = 1.0; matrix[1][2] = 0.0; matrix[2][0] = 2.0; matrix[2][1] = 0.0; matrix[2][2] = 1.0; double[][] inv = matrix.MatrixInverse(); var identity = MatrixOperations.MatrixIdentity(3); double[][] result = inv.MatrixProduct(matrix); Assert.AreEqual(identity[0][0], result[0][0]); Assert.AreEqual(identity[0][1], result[0][1]); Assert.AreEqual(identity[0][2], result[0][2]); Assert.AreEqual(identity[1][0], result[1][0]); Assert.AreEqual(identity[1][1], result[1][1]); Assert.AreEqual(identity[1][2], result[1][2]); Assert.AreEqual(identity[2][0], result[2][0]); Assert.AreEqual(identity[2][1], result[2][1]); Assert.AreEqual(identity[2][2], result[2][2]); }
/// <summary> /// Получить матрицу парных корреляций /// </summary> /// <returns></returns> public double[][] GetPairCorrelationsMatrix() { double[][] r = new double[matrix.Length][].Select(e => e = new double[matrix.Length]).ToArray(); int n = matrix[0].Length; for (int i = 0; i < matrix.Length; i++) { for (int j = 0; j < i; j++) { double temp = n * MatrixOperations.ScalarMultiplication(matrix[i], matrix[j]) - matrix[i].Sum() * matrix[j].Sum(); temp /= Math.Sqrt(Math.Abs(n * matrix[i].Sum(x => x * x) - matrix[i].Sum() * matrix[i].Sum()) * Math.Abs(n * matrix[j].Sum(y => y * y) - matrix[j].Sum() * matrix[j].Sum())); r[i][j] = temp; } } for (int i = 0; i < matrix.Length; i++) { for (int j = i; j < matrix.Length; j++) { if (i == j) { r[i][j] = 1; } else { r[i][j] = r[j][i]; } } } return(pairMatrix = r); }
public void MatrixOrder4AnotherTest() { double[][] matrix = MatrixOperations.MatrixCreate(4, 4); matrix[0][0] = 4.0; matrix[0][1] = 3.0; matrix[0][2] = 2.0; matrix[0][3] = 2.0; matrix[1][0] = 0.0; matrix[1][1] = 1.0; matrix[1][2] = -3.0; matrix[1][3] = 3.0; matrix[2][0] = 0.0; matrix[2][1] = -1.0; matrix[2][2] = 3.0; matrix[2][3] = 3.0; matrix[3][0] = 0.0; matrix[3][1] = 3.0; matrix[3][2] = 1.0; matrix[3][3] = 1.0; double det = matrix.MatrixDeterminant(); Assert.AreEqual(-240.0, det); }
public void MatrixMultiplyTest1() { var a = MatrixOperations.FromArray(4, 4, new double[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2 }); var b = MatrixOperations.FromArray(4, 4, new double[]{ -2, 1, 2, 3, 3, 2, 1, -1, 4, 3, 6, 5, 1, 2, 7, 8 }); var c = MatrixOperations.Multiply(a, b); var expected = MatrixOperations.FromArray(4, 4, new double[]{ 20, 22, 50, 48, 44, 54, 114, 108, 40, 58, 110, 102, 16, 26, 46, 42 }); Assert.Equal(expected, c); output.WriteLine(c.ToString()); }
public async Task Multiply_Myltiply2Matrix_ShoulNotdEqual3rdMatix() { // Arrange MatrixOperations manager = new MatrixOperations(); int[,] matrix1 = { { 1, 2, 3, 4, 5, 6 }, { 2, 3, 4, 5, 6, 7 }, { 4, 5, 6, 7, 8, 9 } }; int[,] matrix2 = { { 1, 2, 3 }, { 2, 3, 4 }, { 3, 4, 5 }, { 4, 5, 6 }, { 5, 6, 7 }, { 6, 7, 8 } }; int[,] matrix3 = { { 91, 4, 133 }, { 3, 139, 6 }, { 154, 1, 232 }, { 2, 2, 2 } }; // Act var result = manager.Multiply(matrix1, matrix2); // Assert var equal = true; if (result.Length == matrix3.Length) { for (var row = 0; row < result.GetLength(0); row++) { for (var column = 0; column < result.GetLength(1); column++) { if (result[row, column] != matrix3[row, column]) { equal = false; } } } } else { equal = false; } Assert.IsFalse(equal); }
public void MatrixMultiplyTest2() { var a = MatrixOperations.FromArray(4, 4, new double[]{ 1, 2, 3, 4, 2, 4, 4, 2, 8, 6, 4, 1, 0, 0, 0, 1 }); var b = MatrixOperations.FromArray(4, 1, new double[]{ 1, 2, 3, 1 }); var c = MatrixOperations.Multiply(a, b); var expected = MatrixOperations.FromArray(4, 1, new double[]{ 18, 24, 33, 1 }); Assert.Equal(expected, c); output.WriteLine(c.ToString()); }
public void UpdateInertiaTensor() { Matrix temp = WorldTransform; temp.Translation = Vector3.Zero; _invInertiaTensorWorld = MatrixOperations.Multiply(MatrixOperations.Scaled(WorldTransform, _invInertiaLocal), Matrix.Transpose(temp)); }
static void Main(string[] args) { do { string inputPath = "../../input.txt"; string outputPath = "../../output.txt"; if (!File.Exists(inputPath)) { Console.WriteLine("input.txt не существует"); continue; } int[,] arr_1 = MatrixOperations.Parser(File.ReadAllLines(inputPath)); if (arr_1 != null) { Console.WriteLine(MatrixOperations.ToString(arr_1)); int[,] mul = MatrixOperations.Mulitply(arr_1, arr_1); if (mul != null) { string str = MatrixOperations.ToString(mul); Console.WriteLine(str); File.WriteAllLines(outputPath, new string[] { str }); } else Console.WriteLine("Умножение невозможно"); } else Console.WriteLine("Ошибка в файле!"); Console.WriteLine("Для продолжения нажмите любую клавишу."); Console.WriteLine("Для выхода из программы нажмите Escape."); } while (Console.ReadKey(true).Key != ConsoleKey.Escape); }
public void MultiplyConditionFalse() { var mx1 = new List <List <int> >() { new List <int>() { 3, 2, 5 }, new List <int>() { 24, 1, 3 } }; var mx2 = new List <List <int> >() { new List <int>() { 1, 2, 3 }, new List <int>() { 4, 10, 2 }, new List <int>() { 2, 1, 4 } }; Assert.IsFalse(MatrixOperations.CheckMultiplyCondition(mx1, mx2)); }
public void TestThatUnidimensionalArrayToSquareMatrixReturnsSquareMatrixOfExpectedSize() { var result = MatrixOperations.UnidimensionalArrayToSquareMatrix(array); Assert.AreEqual(3, result.GetLength(0)); Assert.AreEqual(3, result.GetLength(1)); }
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 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)); }
private static IFigure CreateHexagonEdge(IMaterial material) { return(new CylinderFigure(MatrixOperations.Multiply(MatrixOperations.Geometry3D.Translation(0, 0, -1), MatrixOperations.Multiply(MatrixOperations.Geometry3D.RotateY(-Math.PI / 6.0), MatrixOperations.Multiply(MatrixOperations.Geometry3D.RotateZ(-Math.PI / 2.0), MatrixOperations.Geometry3D.Scale(0.25, 1, 0.25)))), material, 0, 1, false)); }
public void GenerateMatrixTest() { var matrixOperations = new MatrixOperations(','); var matrix = matrixOperations.GetGeneratedRandomMatrix(10, 10); Assert.AreEqual(matrix.Length, 10); }
/// <summary> /// The resultant "B" matrix when the interpolation function is derived /// du/dx = 1/|A| * [y2 - y3 0 y3 - y1 0 y1 - y2 0] * [u1 v1 u2 v2 u3 v3] -> du/dx = B * [u1 v1 u2 v2 u3 v3] /// dv/dy = 1/|A| * [0 -(x2 - x3) 0 -(x3 - x1) 0 -(x1 -x2)] * [u1 v1 u2 v2 u3 v3] -> dv/dy = B * [u1 v1 u2 v2 u3 v3] /// (du/dy + dv/dx) = 1/|A| * [-(x2 - x3) y2-y3 -(x3 - x1) y3 - y1 -(x1 -x2) y1 - y2] * [u1 v1 u2 v2 u3 v3] -> dv/dy = B * [u1 v1 u2 v2 u3 v3] /// </summary> /// <returns></returns> public override double[][] GetBMatrix() { var matrix = MatrixOperations.MatrixCreate(3, 6); double determinant = GetInterpolationMatrixDeterminant(); matrix[0][0] = (Vertex2.Y - Vertex3.Y) / determinant; matrix[0][1] = 0.0; matrix[0][2] = (Vertex3.Y - Vertex1.Y) / determinant; matrix[0][3] = 0.0; matrix[0][4] = (Vertex1.Y - Vertex2.Y) / determinant; matrix[0][5] = 0.0; matrix[1][0] = 0.0; matrix[1][1] = -(Vertex2.X - Vertex3.X) / determinant; matrix[1][2] = 0.0; matrix[1][3] = -(Vertex3.X - Vertex1.X) / determinant; matrix[1][4] = 0.0; matrix[1][5] = -(Vertex1.X - Vertex2.X) / determinant; matrix[2][0] = -(Vertex2.X - Vertex3.X) / determinant; matrix[2][1] = (Vertex2.Y - Vertex3.Y) / determinant; matrix[2][2] = -(Vertex3.X - Vertex1.X) / determinant; matrix[2][3] = (Vertex3.Y - Vertex1.Y) / determinant; matrix[2][4] = -(Vertex1.X - Vertex2.X) / determinant; matrix[2][5] = (Vertex1.Y - Vertex2.Y) / determinant; return(matrix); }
public void WriteMatrix(int[,] matrix, SaveMode saveModeType) { var valuesToWrite = MatrixOperations.MatrixToUnidimensionalArray(matrix); var saveMode = SaveModeFactory.GetSaveMode(saveModeType); saveMode.WriteValues(valuesToWrite, bitWriter); }
public void Multiply_MultipleMatricesWorks() { IMatrix <int> left = new Matrix(2, 4, new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }); IMatrix <int> right = new Matrix(4, 2, new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }); IMatrix <int> result = left * right; Assert.Equal(28, result[0, 0]); Assert.Equal(34, result[0, 1]); Assert.Equal(76, result[1, 0]); Assert.Equal(98, result[1, 1]); left = new Matrix(1, 2, 1); right = new Matrix(2, 10, 3); /* * Input: * [ 1 1 ] x [ 3 3 3 3 3 3 3 3 3 3 ] * [ 3 3 3 3 3 3 3 3 3 3 ] * Expected: * [ 6 6 6 6 6 6 6 6 6 6 ] */ var array = MatrixOperations <int> .MultiplyMatrices(left, right, left.SameTypedOperations.TwoValueMultiplier, left.SameTypedOperations.TwoRefenceAdder ); foreach (var item in array) { Assert.Equal(6, item); } }
void Update() { // Always add DIRECTION * DELTA to the rotation zRot += zDir * 0.4f; // Change direction with the threshold (0, 60): if (zRot > 60.0f || zRot < 0.0f) { zDir = -zDir; } Matrix4x4 rotZ = MatrixOperations.opRotate(zRot, MatrixOperations.AXIS.AX_Z); Matrix4x4 right = MatrixOperations.opTranslate(1, 0, 0); Matrix4x4 scale = MatrixOperations.opScale(1, 0.5f, 0.5f); Matrix4x4 transformNoScale = rotZ * right; Matrix4x4 transform = rotZ * right * scale; Matrix4x4 rotZ2 = MatrixOperations.opRotate(zRot * 0.75f, MatrixOperations.AXIS.AX_Z); Matrix4x4 transform2 = transformNoScale * right * rotZ2 * right * scale; // Reset both links before transforming them: bc1.resetPoints(); bc2.resetPoints(); // Finally, transform both links from their original points: TransformMesh(bc1.getMesh(), transform); TransformMesh(bc2.getMesh(), transform2); }
public void Then_transpose(string id, DataTable dataTable) { var expectedId = $"transpose({id})"; Given_matrix(expectedId, dataTable); Assert.True(cache[expectedId].Equals(MatrixOperations.Transpose(cache[id], false))); cache.Remove(expectedId); }
/// <summary> /// Получить множественный коэффициент корреляции /// </summary> /// <param name="idx"></param> /// <returns></returns> public double GetOneMultipleCorrelation(int idx) { if (pairMatrix == null) { pairMatrix = GetPairCorrelationsMatrix(); } return(Math.Sqrt(1 - MatrixOperations.DeterminantLU(pairMatrix) / MatrixOperations.ExtraMinor(pairMatrix, idx, idx))); }
public void Then_inverse(string a, string b, int rows, int cols, DataTable dataTable) { var expectedId = $"{a} * {b}"; Given_matrix(expectedId, dataTable); Assert.True(cache[expectedId].Equals(MatrixOperations.Multiply(cache[a], cache[b]))); cache.Remove(expectedId); }
public int[,] ReadMatrix(SaveMode saveModeType) { var saveMode = SaveModeFactory.GetSaveMode(saveModeType); var readValuesFromFile = saveMode.ReadValues(bitsToRead, bitReader); return(MatrixOperations.UnidimensionalArrayToSquareMatrix(readValuesFromFile)); }
static void Main(string[] args) { Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.Black; Console.Clear(); /*double[,] a1 = new double[,] { {0.53637763463723365, 0.028471325537409321, 0.66987587449600727 }, * { 0.96396422198226872, 0.82154775123183976, 0.84203247765173783 }, * { 0.30928549371160824, 0.36416072042852676, 0.19277459904215047 } };*/ Matrix a = Matrix.RandomMatrix3(); // Matrix a = new Matrix(a1); Console.WriteLine("Матрица A"); PrintData(a.ToArray(), 3, 3); SVD svd = MatrixOperations.SVD(a, 1e-15); Console.WriteLine(); Console.WriteLine("Матрица V"); PrintData(svd.V.ToArray(), 3, 3); Console.WriteLine(); Console.WriteLine("Матрица S"); PrintData(svd.S.ToArray(), 3, 3); Console.WriteLine(); Console.WriteLine("Матрица U"); PrintData(svd.U.ToArray(), 3, 3); double result = svd.Error(a); Console.WriteLine(); Console.WriteLine("Погрешность:" + result.ToString()); /*Stopwatch stopwatch = Stopwatch.StartNew(); * LibraryMethod(Random(1000, 1000), 1000, 1000); * stopwatch.Stop(); * TimeSpan time = stopwatch.Elapsed;*/ //Eigendecomp eigendecomp = EIG(); /*List<Vector> vectors = new List<Vector>(); * vectors.Add(new Vector(new double[] { 0, -80, 0 })); * vectors.Add(new Vector(new double[] { -69.3, 40, 0 })); * vectors.Add(new Vector(new double[] { 69.3, 40, 0 })); * vectors.Add(new Vector(new double[] { 0, 150, 0 })); * vectors.Add(new Vector(new double[] { 0, 200, 0 })); * * List<Matrix> matrices = SimpleExperiments.Experiment(vectors, new Vector(new double[] { 50, -30, 5 }), 1e-13); * Console.WriteLine("Матрица R"); * PrintData(matrices[0].ToArray(), 3, 3); * Console.WriteLine("Матрица R'"); * PrintData(matrices[1].ToArray(), 3, 3); * * Console.WriteLine(); * Console.Write("Разница равномерных норм: "); * Console.WriteLine(Math.Abs(matrices[0].InfinityNorm() - matrices[1].InfinityNorm()).ToString());*/ Console.ReadKey(); }
public TestMatrixOperations() { var rotationMock = new Mock <IRotation>(); rotationMock.Setup(m => m.Rotate90Right(It.IsIn <int[, ]>())); rotationMock.Setup(m => m.Rotate90Left(It.IsIn <int[, ]>())); _mo = new MatrixOperations(rotationMock.Object); }
public Regression(double[][] matrix) { this.matrix = matrix; xMatrix = MatrixOperations.Transpose(matrix); for (int i = 0; i < xMatrix.Length; i++) { xMatrix[i][0] = 1; } }
public void Then_inverse(string id, int rows, int cols, DataTable dataTable) { var expectedId = $"inverse({id})"; Given_matrix(expectedId, dataTable); Assert.True(cache[expectedId].Equals(MatrixOperations.Invert(cache[id], MatrixOperation.Cofactor))); Assert.True(cache[expectedId].Equals(MatrixOperations.Invert(cache[id], MatrixOperation.Gauss))); cache.Remove(expectedId); }
/// <summary> /// The resultant "B" matrix when the interpolation function is derived /// du/dx = 1/|A| * [1 1] * [u1 u2] -> du/dx = B * [u1 u2] /// </summary> /// <returns></returns> public override double[][] GetBMatrix() { var matrix = MatrixOperations.MatrixCreate(1, 2); double determinant = GetInterpolationMatrixDeterminant(); matrix[0][0] = -1.0 / determinant; matrix[0][1] = 1.0 / determinant; return(matrix); }
public void MatrixDeterminantTest1() { var a = MatrixOperations.FromArray(2, 2, new double[]{ 1, 5, -3, 2, }); Assert.True(Constants.EpsilonCompare(17, MatrixOperations.Determinant(a, MatrixOperation.Cofactor))); output.WriteLine(MatrixOperations.Determinant(a, MatrixOperation.Cofactor).ToString()); }
public void Then_submatrix_with_dimensions(string id, int row, int col, int rows, int cols, DataTable dataTable) { var minorId = $"submatrix({id}, {row}, {col})"; Given_matrix_with_dimensions(rows, cols, minorId, dataTable); var actualResult = MatrixOperations.Minor(cache[id], row, col); Assert.True(cache[minorId].Equals(actualResult)); cache.Remove(minorId); }