public void Test_Matrix_Plus() { MatrixT <int> MatA = new MatrixT <int>(new int[, ] { { 1, 1 }, { 1, 1 } }); MatrixT <int> MatB = new MatrixT <int>(new int[, ] { { 2, 2 }, { 2, 2 } }); MatrixT <int> Ans = MatA + MatB; MatrixT <int> MatC = new MatrixT <int>(new int[, ] { { 3, 3 }, { 3, 3 } }); int[,] ansArr = new int[2, 2]; int[,] expArr = new int[2, 2]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { ansArr[i, j] = Ans[i, j]; expArr[i, j] = 3; } } CollectionAssert.AreEqual(expArr, ansArr); }
private void SortRows(MatrixT <double> matrix, ref double[] rightPart, int sortIndex) { double maxElement = matrix[sortIndex, sortIndex]; int maxElementIndex = sortIndex; for (int i = sortIndex + 1; i < matrix.Rows; i++) { if (matrix[i, sortIndex] > maxElement) { maxElement = matrix[i, sortIndex]; maxElementIndex = i; } } if (maxElement > sortIndex) { double temp; temp = rightPart[maxElementIndex]; rightPart[maxElementIndex] = rightPart[sortIndex]; rightPart[sortIndex] = temp; for (int i = 0; i < matrix.Columns; i++) { temp = matrix[maxElementIndex, i]; matrix[maxElementIndex, i] = matrix[sortIndex, i]; matrix[sortIndex, i] = temp; } } }
/// <summary> /// Method is used to calculate a solution for algebraic equation by Gauss method. /// </summary> /// <param name="pointsAround">points around the argument.</param> /// <returns>Array of calculated values.</returns> private double[] GetSolutionByGauss(List <Point> pointsAround) { MatrixT <double> matrix = new MatrixT <double>(pointsAround.Count, pointsAround.Count); double[] rightPart = new double[pointsAround.Count]; for (int i = 0; i < rightPart.Length; i++) { rightPart[i] = pointsAround[i].Y; } for (int i = 0; i < matrix.Rows; i++) { for (int j = 0; j < matrix.Columns; j++) { matrix[i, j] = Math.Pow(pointsAround[i].X, j); } } double[] answer = new double[pointsAround.Count]; for (int i = 0; i < matrix.Rows - 1; i++) { SortRows(matrix, ref rightPart, i); for (int j = i + 1; j < matrix.Rows; j++) { if (matrix[i, i] != 0) { double multElement = matrix[j, i] / matrix[i, i]; for (int k = i; k < matrix.Columns; k++) { matrix[j, k] -= matrix[i, k] * multElement; } rightPart[j] -= rightPart[i] * multElement; } } } for (int i = matrix.Rows - 1; i >= 0; i--) { answer[i] = rightPart[i]; for (int j = matrix.Rows - 1; j > i; j--) { answer[i] -= matrix[i, j] * answer[j]; } if (matrix[i, i] == 0) { throw new Exception("Could not find the solution."); } answer[i] /= matrix[i, i]; } return(answer); }
static void PrintResult(MatrixT<int> input) // print results { for (int row = 0; row < 2; row++) { for (int column = 0; column < 2; column++) { Console.Write(input[row, column] + " "); } Console.WriteLine(); } }
static void Main() { MatrixT<int> testMatrix = new MatrixT<int>(2, 2); testMatrix[0,0] = 11; // assign values and /problem 9/ test the indexer testMatrix[0,1] = 12; testMatrix[1,0] = 21; testMatrix[1,1] = 22; Console.WriteLine(testMatrix[0,1]); //test the input data and indexer /must print 12/ MatrixT<int> testMatrixTwo = new MatrixT<int>(2, 2); testMatrixTwo[0, 0] = 11; testMatrixTwo[0, 1] = 12; testMatrixTwo[1, 0] = 21; testMatrixTwo[1, 1] = 22; MatrixT<int> addition = testMatrix + testMatrixTwo; // test + Console.WriteLine("Addition:\n"); PrintResult(addition); MatrixT<int> substraction = testMatrix - testMatrixTwo; // test - Console.WriteLine("Substraction:\n"); PrintResult(substraction); MatrixT<int> product = testMatrix * testMatrixTwo; // test * Console.WriteLine("Multiplication:\n"); PrintResult(product); if (testMatrix) // test without a zero elements { Console.WriteLine("There in no zero element!"); } else { Console.WriteLine("There is a zero element!"); } if (substraction) // test with zero elements { Console.WriteLine("There in no zero element!"); } else { Console.WriteLine("There is a zero element!"); } }
MatrixOutput IService.MatrixSum(MatrixInput Input) { MatrixT <int> A = new MatrixT <int>(new int[Input.matrix1[0].Length, Input.matrix1[1].Length]); MatrixT <int> B = new MatrixT <int>(new int[Input.matrix2[0].Length, Input.matrix2[1].Length]); for (int i = 0; i < Input.matrix1[0].Length; i++) { for (int j = 0; j < Input.matrix1[1].Length; j++) { A[i, j] = Input.matrix1[i][j]; } } for (int i = 0; i < Input.matrix2[0].Length; i++) { for (int j = 0; j < Input.matrix2[1].Length; j++) { B[i, j] = Input.matrix2[i][j]; } } MatrixT <int> MatResult = A + B; MatrixOutput result = new MatrixOutput(); result.matrixResult = new int[Input.matrix1[0].Length][]; for (int i = 0; i < Input.matrix1[1].Length; i++) { result.matrixResult[i] = new int[Input.matrix1[0].Length]; } int[][] ansArr = new int[Input.matrix1[0].Length][]; for (int i = 0; i < Input.matrix1[1].Length; i++) { ansArr[i] = new int[Input.matrix1[0].Length]; } for (int i = 0; i < Input.matrix1[0].Length; i++) { for (int j = 0; j < Input.matrix1[1].Length; j++) { ansArr[i][j] = MatResult[i, j]; } } result.matrixResult = ansArr; return(result); }
private void SetResultGrid(MatrixT <double> result) { dataGridViewResult.Rows.Clear(); dataGridViewResult.Columns.Clear(); dataGridViewResult.Enabled = true; dataGridViewResult.AllowUserToAddRows = false; dataGridViewResult.ColumnCount = result.Cols; for (int i = 0; i < result.Rows; i++) { string[] row = new string[dataGridViewResult.ColumnCount]; for (int j = 0; j < dataGridViewResult.ColumnCount; j++) { row[j] = result[i, j].ToString(); } dataGridViewResult.Rows.Add(row); } dataGridViewResult.ReadOnly = true; }
private void buttonCalculate_Click(object sender, EventArgs e) { if (!this.IsOperationApproved) { MessageBox.Show("Операция не может быть выполнена так как параметры матриц заданы не верно!"); return; } if (MatrixClientHelper.VerifyMatrixParameters(dataGridViewA, dataGridViewB)) { double[,] matrixA = MatrixClientHelper.GetArrayFromGrid(dataGridViewA); double[,] matrixB = MatrixClientHelper.GetArrayFromGrid(dataGridViewB); if (checkBoxParallel.Checked) { MatrixT <double> .Paral = true; } MatrixT <double> A = new MatrixT <double>(matrixA); MatrixT <double> B = new MatrixT <double>(matrixB); Operations operation = (Operations)comboBoxOperation.SelectedIndex; Stopwatch stopwatch = new Stopwatch(); switch (operation) { case Operations.Sum: { stopwatch.Start(); this.Result = A + B; stopwatch.Stop(); break; } case Operations.Multiply: { stopwatch.Start(); this.Result = A * B; stopwatch.Stop(); break; } default: { throw new Exception("Операция не установлена"); } } double calcTime = stopwatch.ElapsedMilliseconds / 1000.0; labelTime.Text = "Время: " + calcTime.ToString(); this.SetResultGrid(this.Result); buttonSaveCSV.Enabled = true; } else { MessageBox.Show("Значения матрицы установлены не верно!"); } }