Advertisement
CK101

Complex Number v6

Mar 4th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.96 KB | None | 0 0
  1. /*
  2.  * Canaan   Khatib
  3.  * CSE-112 
  4.  * Complex Input
  5.  * Prof Silvestri  
  6.  * Cakhatib-colon0001@student.stcc.edu
  7.  */
  8.  
  9.     import java.util.StringTokenizer;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12.  
  13.     public class Complex extends Number implements Comparable<Complex> {
  14.         private enum STATES {
  15.             INIT_STATE,
  16.             LEADING_OPTIONAL_SIGN_READ,
  17.             POSSIBLE_REALPART,
  18.             REALPARTREAD_NOW_IMAGINARY_PART,
  19.             IMAGINARY_PART_VALUE,
  20.             COMPLEX_READ_IN_TOTAL,
  21.             ERROR_STATE
  22.         }
  23.        
  24.         private double imag = 0;
  25.         private double real = 0;
  26.         public final static Complex ZERO = new Complex();
  27.  
  28.         /*** Constructors ***/
  29.  
  30.         public Complex() {
  31.             this.real = 0;
  32.             this.imag = 0;
  33.         }
  34.  
  35.         public Complex(double real, double imag) {
  36.             this.real = real;
  37.             this.imag = imag;
  38.         }
  39.  
  40.         public Complex(double real) {
  41.             this.real = real;
  42.             this.imag = 0;
  43.         }
  44.  
  45.         public Complex(String complexNumber) {
  46.             double values[] = complexString(complexNumber);
  47.             this.real = values[0];
  48.             this.imag = values[1];
  49.         }
  50.  
  51.         private double[] complexString(String str) {
  52.             double values[] = new double[2];
  53.             String strReg[] = {"^[+-]?(\\d+(\\.\\d*)?|\\.\\d+)$", "^([+-]?(\\d+(\\.\\d*)?|\\.\\d+))?)i$",
  54.                                 "^([+-]?(\\d+)?(\\.\\d*)?|\\.\\d+)(\\s+)?(([+-](\\s+)?((\\d+(\\.\\d*)?|\\.\\d+))?)i)$"};
  55.             Pattern pattern [] = new Pattern [5];
  56.             Matcher match [] = new Matcher [5];
  57.            
  58.             for(int i = 0; i < strReg.length; i++) {
  59.                 pattern[i] = Pattern.compile(strReg[i]);
  60.                 match[i] = pattern[i].matcher(str);
  61.             }
  62.             // Only for real inputs
  63.             if (match[0].find()) {
  64.                 real = Double.parseDouble(match[0].group());
  65.                 imag = 0;
  66.                 values[0]=Double.parseDouble(match[0].group());
  67.                 values[1] = 0;
  68.             }
  69.             return values;
  70.         }
  71.          
  72.             /*** Getters ***/
  73.            
  74.             public double getImag() {
  75.                 return this.imag;
  76.             }
  77.          
  78.             public double getReal() {
  79.                 return this.real;
  80.             }
  81.  
  82.         /*** Setters ***/
  83.  
  84.         public void setReal(double real) {
  85.             this.real = real;
  86.         }
  87.  
  88.         public void setImag(double imag) {
  89.             this.imag = imag;
  90.         }
  91.    
  92.         /*** Operations ***/
  93.  
  94.         public Complex add(Complex other) {
  95.             // a+bi + c+di is (a+c)+(b+d)i
  96.             return new Complex(this.real + other.real, this.imag + other.imag);
  97.         }
  98.  
  99.         public Complex subtract(Complex other) {
  100.             // a+bi - c+di is (a-c)+(b-d)i
  101.             return add(other.negate());
  102.         }
  103.  
  104.         public Complex multiply(Complex other) {
  105.             // (a+bi) * (c+di) is (ac-bd)+(ad+bc)i
  106.             double ac = this.real * other.real;
  107.             double bd = this.imag * other.imag;
  108.             double ad = this.real * other.imag;
  109.             double bc = this.imag * other.real;
  110.  
  111.             return new Complex(ac - bd, ad + bc);
  112.         }
  113.  
  114.         public Complex divide(Complex other) {
  115.             // (a+bi) / (c+di) is (ac+bd)/(c^2+d^2) + (bc-ad)/(c^2+d^2)i
  116.             double a = this.real;
  117.             double b = this.imag;
  118.             double c = other.real;
  119.             double d = other.imag;
  120.  
  121.             double quotientReal = ((a * c) + (b * d)) / ((c * c) + (d * d));
  122.             double quotientImag = ((b * c) - (a * d)) / ((c * c) + (d * d));
  123.  
  124.             return new Complex(quotientReal, quotientImag);
  125.         }
  126.  
  127.         public double abs() {
  128.             // |a + bi| is sqrt(a^2 + b^2)
  129.             return Math.sqrt(Math.pow(this.real, 2) + Math.pow(this.imag, 2));
  130.         }
  131.  
  132.         public Complex conjugate() {
  133.             // Complex conjugate of a+bi is a-bi
  134.             return new Complex(this.real, -this.imag);
  135.         }
  136.  
  137.         public double distance(Complex other) {
  138.             // Distance between a+bi and c+di is sqrt((a-c)^2 + (b-d)^2)
  139.             return Math.sqrt(Math.pow(this.real - other.real, 2) + Math.pow(this.imag - other.imag, 2));
  140.         }
  141.  
  142.         public Complex negate() {
  143.             // -(a+bi) is -a-bi
  144.             return new Complex(-this.real, -this.imag);
  145.         }
  146.  
  147.         /*** Comparisons ***/
  148.  
  149.         public boolean equals(Complex other) {
  150.             /*
  151.              * If A is the larger complex number and B is the smaller one, A and B
  152.              * are approximately equal if the following is true: (|A| - |B|)/|A| <
  153.              * 1E-6 (one millionth)
  154.              */
  155.             return this.distance(other) / (this.greaterThan(other) ? this.abs() : other.abs()) < 1E-6;
  156.         }
  157.  
  158.         public boolean greaterThan(Complex other) {
  159.             return this.abs() > other.abs();
  160.         }
  161.  
  162.         public boolean lessThan(Complex other) {
  163.             return this.abs() < other.abs();
  164.         }
  165.  
  166.         public String toString() {
  167.             String toString = "";
  168.             if (this.imag == 0.0)
  169.                 toString += this.real;
  170.             else if (this.real == 0.0)
  171.                 toString += this.imag + "i";
  172.             else {
  173.                 toString += this.real;
  174.                 toString += this.imag < 0 ? " - " : " + ";
  175.                 toString += Math.abs(this.imag) + "i";
  176.             }
  177.             return toString;
  178.         }
  179.  
  180.         // Round components to the specified number of decimal places
  181.         public String getRoundedtoString(int decimalPlaces) {
  182.             // Assume negative input should be changed to positive
  183.             decimalPlaces = Math.abs(decimalPlaces);
  184.  
  185.             // Ensure that any number which rounds to 0 is displayed as positive 0
  186.             double realPart = Math.abs(this.real) < Math.pow(10, -decimalPlaces) ? 0.0 : this.real;
  187.             double imagPart = Math.abs(this.imag) < Math.pow(10, -decimalPlaces) ? 0.0 : this.imag;
  188.  
  189.             // Format numbers according to decimal places
  190.             String format = "%." + decimalPlaces + "f";
  191.             String toString = realPart < 0 ? "-" : "";
  192.             toString += String.format(format, Math.abs(realPart));
  193.             toString += imagPart < 0 ? " - " : " + ";
  194.             return toString + String.format(format + "i", Math.abs(imagPart));
  195.         }
  196.  
  197.         @Override
  198.         public int compareTo(Complex other) {
  199.             double diff = this.abs() - other.abs();
  200.             if (diff > 0)
  201.                 return 1;
  202.             if (diff == 0)
  203.                 return 0;
  204.             return -1;
  205.         }
  206.  
  207.         @Override
  208.         public double doubleValue() {
  209.             return this.abs();
  210.         }
  211.  
  212.         @Override
  213.         public float floatValue() {
  214.             return (float)doubleValue();
  215.         }
  216.  
  217.         @Override
  218.         public int intValue() {
  219.             return (int)doubleValue();
  220.         }
  221.  
  222.         @Override
  223.         public long longValue() {
  224.             return (long)doubleValue();
  225.         }  
  226.        
  227.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement