private void btnGerarTranspostM2_Click(object sender, EventArgs e) { if (Matriz2 == null) { MessageBox.Show("Matriz nula !", "Error - Matriz"); return; } float[,] tempResultante = new float[Matriz2.GetLength(0), Matriz2.GetLength(1)]; for (int x = 0; x < Matriz2.GetLength(0); x++) { for (int y = 0; y < Matriz2.GetLength(1); y++) { float n = 0; float.TryParse(Matriz2[x, y].Text, out n); tempResultante[x, y] = n; //tempResultante[x, y] = Convert.ToInt32(Matriz2[x, y].Text); } } float[,] tempMatrizResultante = CalculosMatrizes.GerarTransposta(tempResultante); int TamanhoText = groupBoxMatriz2.Width / Matriz2.GetLength(1); Matriz2 = new TextBox[tempMatrizResultante.GetLength(0), tempMatrizResultante.GetLength(1)]; groupBoxMatriz2.Controls.Clear(); for (int x = 0; x < Matriz2.GetLength(0); x++) { for (int y = 0; y < Matriz2.GetLength(1); y++) { Matriz2[x, y] = new TextBox(); Matriz2[x, y].Text = tempMatrizResultante[x, y].ToString(); Matriz2[x, y].Top = (x * Matriz2[x, y].Height) + 20; Matriz2[x, y].Left = y * TamanhoText + 6; Matriz2[x, y].Width = TamanhoText; groupBoxMatriz2.Controls.Add(Matriz2[x, y]); } } }
private void btnGerarInversaM2_Click(object sender, EventArgs e) { if (Matriz2 == null) { MessageBox.Show("Matriz nula !", "Error - Matriz"); return; } float[,] tempResultante = new float[Matriz2.GetLength(0), Matriz2.GetLength(1)]; float[,] matrizAdjunta = new float[Matriz2.GetLength(0), Matriz2.GetLength(1)]; float[,] matrizCofatora = new float[Matriz2.GetLength(0), Matriz2.GetLength(1)]; float determinante = 0; if (tempResultante.GetLength(0) != 2 || tempResultante.GetLength(1) != 2) { if (tempResultante.GetLength(0) != 3 || tempResultante.GetLength(1) != 3) { MessageBox.Show("Matriz invalida !", "Error - Matriz"); return; } } for (int x = 0; x < Matriz2.GetLength(0); x++) { for (int y = 0; y < Matriz2.GetLength(1); y++) { float n = 0; float.TryParse(Matriz2[x, y].Text, out n); tempResultante[x, y] = n; //tempResultante[x, y] = Convert.ToInt32(Matriz2[x, y].Text); } } /*Matriz Adjunta(A) = Matriz Transposta(Matriz dos cofatores(A)) * Matriz Inversa(A) = (1/ Determinante(A)) * Matriz Adjunta(A)*/ if (tempResultante.GetLength(0) == 2 && tempResultante.GetLength(1) == 2) { matrizCofatora = CalculosMatrizes.GerarCofatora2x2(tempResultante); matrizAdjunta = CalculosMatrizes.GerarTransposta(matrizCofatora); determinante = CalculosMatrizes.GerarDeterminante2x2(tempResultante); } else if (tempResultante.GetLength(0) == 3 && tempResultante.GetLength(1) == 3) { matrizCofatora = CalculosMatrizes.GerarCofatora3x3(tempResultante); matrizAdjunta = CalculosMatrizes.GerarTransposta(matrizCofatora); determinante = CalculosMatrizes.GerarDeterminante3x3(tempResultante); } else { MessageBox.Show("Matriz invalida !", "Error - Matriz"); return; } if (determinante == 0) { MessageBox.Show("Matriz invalida, determinante igual a 0 !", "Error - Matriz"); return; } float[,] tempMatrizResultante = CalculosMatrizes.GerarInversa(determinante, matrizAdjunta); int TamanhoText = groupBoxMatriz2.Width / Matriz2.GetLength(1); for (int x = 0; x < Matriz2.GetLength(0); x++) { for (int y = 0; y < Matriz2.GetLength(1); y++) { Matriz2[x, y].Text = tempMatrizResultante[x, y].ToString(); } } }