/// <summary> /// Faz a multiplicação de duas matrizes esparsas. /// </summary> /// <param name="outraMatriz"></param> /// <returns>Uma matriz esparsa representando a multiplicação.</returns> public ListaLigadaCruzada MultiplicarMatrizes(ListaLigadaCruzada outraMatriz) { if (this.Colunas != outraMatriz.Linhas) { throw new ArgumentException("Número de colunas de uma matriz deve ser igual o número de linhas da outra"); } ListaLigadaCruzada matrizResultado; matrizResultado = new ListaLigadaCruzada(this.Linhas, outraMatriz.Colunas); double total = 0; for (int i = 0; i < this.Linhas; i++) { for (int j = 0; j < outraMatriz.Colunas; j++) { total = 0; int k; for (k = 0; k < this.Colunas; k++) { total += this.ValorDe(i, k) * outraMatriz.ValorDe(k, j); } if (total != 0) { matrizResultado.InserirElemento(total, i, j); } } } return(matrizResultado); }
/// <summary> /// Faz a soma de duas matrizes esparsas. /// </summary> /// <param name="outraMatriz"></param> /// <returns>Uma matriz esparsa representando a soma.</returns> public ListaLigadaCruzada SomarMatrizes(ListaLigadaCruzada outraMatriz) { if (this.linhas != outraMatriz.linhas || this.colunas != outraMatriz.colunas) { throw new ArgumentException("As Matrizes dever ser de mesma dimensão!"); } ListaLigadaCruzada soma = new ListaLigadaCruzada(this.linhas, this.colunas); Celula atual = this.cabecaPrincipal.Abaixo.Direita; // Copia a matriz this na matriz soma for (int l = 0; l < this.linhas; l++) { for (int c = atual.Coluna; c >= 0; c = atual.Coluna) { if (atual.Valor != null) { atual = atual.Direita; soma.InserirElemento(this.ValorDe(l, c), l, c); } } atual = atual.Abaixo.Direita; } atual = outraMatriz.cabecaPrincipal.Abaixo.Direita; for (int l = 0; l < outraMatriz.linhas; l++) { for (int c = atual.Coluna; c >= 0; c = atual.Coluna) { if (atual.Valor != null) { double elem = soma.ValorDe(l, c) != 0 ? soma.ValorDe(l, c) + outraMatriz.ValorDe(l, c) : outraMatriz.ValorDe(l, c); soma.InserirElemento(elem, l, c); atual = atual.Direita; } } atual = atual.Abaixo.Direita; } return(soma); }
private void btnPesquisar_Click(object sender, EventArgs e) { if (numLinhaPesquisa.Value >= 0 && numColunaPesquisa.Value >= 0 && !matrizEsparsa.EstaDesalocada) { MessageBox.Show("Elemento: " + matrizEsparsa.ValorDe(Convert.ToInt32(numLinhaPesquisa.Value), Convert.ToInt32(numColunaPesquisa.Value))); } else { MessageBox.Show("Não é possível pesquisar com os valores dados." + " Verifique se está no intervalo da matriz esparsa ou se há matriz para pesquisar.", "Atenção!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void BtnRetornar_Click(object sender, EventArgs e) { if (rbLista1.Checked) { if (lista == null) { throw new Exception("Matriz Vazia"); } //Chamamos o método já implementados na classe ListaLigadaCruzada e o exibimos txtValor.Text = lista.ValorDe(Convert.ToInt32(numLinha.Value), Convert.ToInt32(numColuna.Value)) + ""; } else { if (lista2 == null) { throw new Exception("Matriz Vazia"); } txtValor.Text = lista2.ValorDe(Convert.ToInt32(numLinha.Value), Convert.ToInt32(numColuna.Value)) + ""; } }