package ejemplos;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class MatrizC extends complejo
{
  int nren, ncol;
  complejo datos[][];

  // *********************************************************************

  MatrizC(double Re[][], double Im[][])
  {
    nren = Re.length;
    ncol = Re[0].length;

    datos = new complejo[nren][ncol];

    for (int i = 0; i < nren; i++)
      for (int j = 0; j < ncol; j++)
        datos[i][j] = new complejo(Re[i][j], Im[i][j]);
  }

  // *********************************************************************

  MatrizC(int n, int m) {
    inicializa(n, m);
  }

  // *********************************************************************

  private void inicializa(int r, int c) {
    // Si la matriz es nula reserva memoria.
    if ( (nren == 0 && ncol == 0) || this == null) {
      nren = r;
      ncol = c;

      datos = new complejo[nren][ncol];
      for (int i = 0; i < r; i++)
        for (int j = 0; j < c; j++)
          datos[i][j] = new complejo(0,0);
    }
  }

  // **************************************************************************

  public void imprime()
  {
    int i, j;
    double aux;
    if ( (nren == 0) && (ncol == 0)) {
      System.out.println("No tiene informacion la matriz");
      return;
    }

    for (i = 0; i < this.nren; i++) {
      for (j = 0; j < this.ncol; j++)
        datos[i][j].imprime();
      System.out.println(" ");
    }
    System.out.println("\n\n ");
  }

  // **************************************************************************

  public void suma(MatrizC a, MatrizC b) {
    if ( (a.nren != b.nren) || (a.ncol != b.ncol))
      return;

    this.inicializa(a.nren, a.ncol);

    if (this.nren == a.nren && this.ncol == a.ncol) {

      for (int i = 0; i < a.nren; i++)
        for (int j = 0; j < a.ncol; j++)
          this.datos[i][j].suma(a.datos[i][j], b.datos[i][j]);

    }
    else
      System.out.println("Matrices con diferente tamaņo");
  }

  // **************************************************************************

  public void resta(MatrizC a, MatrizC b) {
    if ( (a.nren != b.nren) || (a.ncol != b.ncol))
      return;

    this.inicializa(a.nren, a.ncol);

    if (this.nren == a.nren && this.ncol == a.ncol) {

      for (int i = 0; i < a.nren; i++)
        for (int j = 0; j < a.ncol; j++)
          this.datos[i][j].resta(a.datos[i][j], b.datos[i][j]);
    }
    else
      System.out.println("Matrices con diferente tamaņo");
  }

  // **************************************************************************

  public void multiplica(MatrizC a, MatrizC b) {
    int i, j;

    complejo sum = new complejo();
    complejo aux  = new complejo();

    if (a.ncol != b.nren)
      return;

    inicializa(a.nren, b.ncol);

    if (this.nren != a.nren || this.ncol != b.ncol)
      return;

    for (i = 0; i < this.nren; i++)
      for (j = 0; j < this.ncol; j++) {
        sum.real = 0;
        sum.imag = 0;
        for (int k = 0; k < a.ncol; k++)
        {
          aux.multiplica(a.datos[i][k], b.datos[k][j]);
          sum.suma(sum, aux);
        }
        this.datos[i][j].igual(sum);
      }
  }
}