/* JRG, the Resource Geology Seismic Processing System for Java, and Viewmat for Java are Copyright 1998, 1999 by John N. Louie The software and methods here are the subject of academic research, not commercial products. I would like to know what use you make of my methods, and have your feedback on their success or failure. Also, by letting me know who you are, I can inform you if bugs or errors are discovered. Please send me an email message with: your name and email address; whether you are a student or faculty member, consultant or employee; the name of your university or company, and department; and a sentence or two describing what use you intend to make of these methods. Send your message to louie@seismo.unr.edu */ import java.util.*; import java.io.*; import java.lang.Math; class testcomplex { public static void main(String args[]) { /* Instantiate the complex structure variables */ Complex z = new Complex(); /* Assign values to the complex numbers z1 and z2 */ /* 1 + 0i */ Complex z1 = new Complex(1F, 0F); /* 0 + 1i */ Complex z2 = new Complex(0F, 1F); /* Print z1 and z2 */ System.out.println("z1, z2: " + z1.re + "+" + z1.im + "i, " + z2.re + "+" + z2.im + "i"); /* Invoke the Complex.add(z1,z2) method defined below to get z1 plus z2 */ z.add(z1,z2); /* Print z1*z2 */ System.out.println("z1+z2: " + z.re + "+" + z.im + "i"); /* Print some other properties of the sum */ System.out.println("Power: " + z.pow() + ", amplitude: " + z.amp() + ", Phase: " + z.phase() + " radians or " + z.phasedeg() + " degrees"); /* Invoke the Complex.mul(z1,z2) method defined below to get z1 times z2 */ z.mul(z1,z2); /* Print z1*z2 */ System.out.println("z1*z2: " + z.re + "+" + z.im + "i"); /* Invoke the Complex.div(z1,z2) method defined below to get z1 over z2 */ z.div(z1,z2); /* Print z1/z2 */ System.out.println("z1/z2: " + z.re + "+" + z.im + "i"); } } public class Complex { float re; float im; static float MINVAL = 10F * Float.MIN_VALUE; static float temp; public void add(Complex x, Complex y) { this.re = (x.re + y.re); this.im = (x.im + y.im); } public void sub(Complex x, Complex y) { this.re = (x.re - y.re); this.im = (x.im - y.im); } public void scale(float fac) { this.re *= fac; this.im *= fac; } public void conj() { this.im = -this.im; } public void mul(Complex x, Complex y) { temp = (x.re * y.re - x.im * y.im); this.im = (x.im * y.re + x.re * y.im); this.re = temp; } public void cmul(Complex x, Complex y) { temp = (x.im * y.re + x.re * y.im); this.im = (x.im * y.re - x.re * y.im); this.re = temp; } public void div(Complex x, Complex y) { temp = ((x.re * y.re + x.im * y.im)/(y.re * y.re + y.im * y.im)); this.im = ((x.im * y.re - x.re * y.im)/(y.re * y.re + y.im * y.im)); this.re = temp; } public float pow() { return (this.re * this.re) + (this.im * this.im); } public float amp() { return (float)(Math.sqrt((double)((this.re * this.re) + (this.im * this.im)))); } public float phase() { if ((this.re <= MINVAL && this.re >= -(MINVAL)) && (this.im <= MINVAL && this.im >= -(MINVAL))) return 0F; else return (float)(Math.atan2((double)(this.im), (double)(this.re))); } public float phasedeg() { return (float)(180.0/Math.PI * this.phase()); } /* Constructors */ public Complex() { this.re = 0F; this.im = 0F; } public Complex(Complex cpx) { this.re = cpx.re; this.im = cpx.im; } public Complex(float re, float im) { this.re = re; this.im = im; } public Complex(double re, double im) { this.re = (float)(re); this.im = (float)(im); } public Complex(float re) { this.re = re; this.im = 0F; } public Complex(double re) { this.re = (float)(re); this.im = 0F; } }