Advertisement
rjsantiago0001

ComplexNumbers

Feb 19th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. /*
  2.  * Ricardo Santiago
  3.  * Complex Numbers
  4.  * CSC-112
  5.  * 2/19/2016
  6.  */
  7.  
  8. public class Complex {
  9.  
  10. //finalize the variables to make immutable
  11.     private final double re;
  12.     private final double im;
  13.  
  14.     public Complex(double real, double imag) {
  15.         this.re = real;
  16.         this.im = imag;
  17.     }
  18.  
  19.     public Complex(double real) {
  20.         this.re = real;
  21.         this.im = 0;
  22.     }
  23.  
  24.     public Complex() {
  25.         this.re = 0;
  26.         this.im = 0;
  27.     }
  28.  
  29.     public Complex add(Complex c) {
  30.         Complex a = this;
  31.         double real = a.re + c.re;
  32.         double imag = a.im + c.im;
  33.         return new Complex(real, imag);
  34.     }
  35.  
  36.     public Complex subtract(Complex c) {
  37.         Complex a = this;
  38.         return a.add(c.negate());
  39.     }
  40.  
  41.     public Complex multiply(Complex c) {
  42.         Complex a = this;
  43.         double real = (a.re * c.re) - (a.im * c.im);
  44.         double imag = (a.re * c.im) + (a.im * c.re);
  45.         return new Complex(real, imag);
  46.     }
  47.  
  48.     // I am currently looking for a faster method to calculate this, possibly using conjugate
  49.     public Complex divide(Complex c) {
  50.         Complex a = this;
  51.         double real = ((a.re * c.re) + (a.im * c.im)) / ((c.re * c.re) + (c.im * c.im));
  52.         double imag = ((a.im * c.re) - (a.re * c.im)) / ((c.re * c.re) + (c.im * c.im));
  53.         return new Complex(real, imag);
  54.     }
  55.  
  56.     public double abs() {
  57.         return Math.hypot(re, im);
  58.     }
  59.  
  60.     public Complex negate() {
  61.         return new Complex(-re, -im);
  62.     }
  63.  
  64.     public Complex conjugate() {
  65.         return new Complex(re, -im);
  66.     }
  67.  
  68.     public double distance(Complex c) {
  69.         return this.subtract(c).abs();
  70.  
  71.     }
  72.  
  73.     public boolean equals(Complex c) {
  74.         double diff;
  75.         if (this.greaterThan(c) == true)
  76.             diff = (this.abs() - c.abs()) / this.abs();
  77.         else
  78.             diff = (c.abs() - this.abs()) / c.abs();
  79.        
  80.         if (diff < 1E-6)
  81.             return true;
  82.         else
  83.             return false;
  84.     }
  85.  
  86.     public boolean greaterThan(Complex c) {
  87.         if (this.abs() > c.abs())
  88.             return true;
  89.         else
  90.             return false;
  91.     }
  92.  
  93.     public boolean lessThan(Complex c) {
  94.         if (this.abs() < c.abs())
  95.             return true;
  96.         else
  97.             return false;
  98.     }
  99.  
  100.     public String toString() {
  101.         if (im == 0)
  102.             return re + "";
  103.         if (re == 0)
  104.             return im + "i";
  105.         if (im < 0)
  106.             return re + " - " + (-im) + "i";
  107.         return re + " + " + im + "i";
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement