public class complejo
{
double
real, imag;
complejo(double r, double x)
{
real = r;
imag = x;
}
complejo()
{
real = 0;
imag = 0;
}
public void suma(complejo a, complejo b)
{
this.real = a.real + b.real;
this.imag = a.imag + b.imag;
}
public void resta(complejo a, complejo b)
{
this.real = a.real - b.real;
this.imag = a.imag - b.imag;
}
public void multiplica(complejo a, complejo
b)
{
this.real = a.real * b.real - a.imag * b.imag;
this.imag = a.real * b.imag + a.imag * b.real;
}
public void division(complejo a, complejo b)
{
double den = b.real * b.real + b.imag * b.imag;
this.real = ( a.real * b.real + a.imag * b.imag)/den;
this.imag = (-a.real * b.imag + a.imag * b.real)/den;
}
void
imprime()
{
if(this.imag >= 0)
System.out.print("(" + this.real + " + j" +
this.imag + ") ");
else
System.out.print("(" +this.real + " - j" +
this.imag*-1+ ") ");
}
void igual(complejo a)
{
this.real = a.real;
this.imag = a.imag;
}
}
Clase Matriz Complejo.
Dada la
clase complejo podemos escribir la clase matriz complejo como:
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);
}
}
}