Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.28 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package mandlebrot;
  7.  
  8. /**
  9.  *
  10.  * @author Joel
  11.  */
  12. public class ComplexNumber extends Number implements Cloneable {
  13.    
  14.     private double real,
  15.                    complex;
  16.    
  17.     ComplexNumber(double _real, double _complex) {
  18.         real    = _real;
  19.         complex = _complex;
  20.     }
  21.    
  22.     @Override
  23.     public int intValue() {
  24.         return (int)real;
  25.     }
  26.    
  27.     @Override
  28.     public double doubleValue() {
  29.         return real;
  30.     }
  31.    
  32.     @Override
  33.     public float floatValue() {
  34.         return (float)real;
  35.     }
  36.    
  37.     @Override
  38.     public long longValue() {
  39.         return (long)real;
  40.     }
  41.    
  42.     public static String toString(ComplexNumber t) {
  43.         if (t.getReal()    == 0) return t.getComplex() + "i";
  44.         if (t.getComplex() == 0) return "" + t.getReal();
  45.        
  46.         return t.getReal() + " + " + t.getComplex() + "i";
  47.     }
  48.    
  49.     @Override
  50.     public ComplexNumber clone() {
  51.         return new ComplexNumber(real, complex);
  52.     }
  53.    
  54.    
  55.     /* Getters and setters */
  56.     public void setReal(double newreal) {
  57.         real = newreal;
  58.     }
  59.    
  60.     public void setComplex(double newcomplex) {
  61.         complex = newcomplex;
  62.     }
  63.    
  64.     public double getReal() {
  65.         return real;
  66.     }
  67.    
  68.     public double getComplex() {
  69.         return complex;
  70.     }  
  71.    
  72.    
  73.     /* Addition */
  74.     public void add(ComplexNumber t) {
  75.         real    += t.getReal();
  76.         complex += t.getComplex();
  77.     }
  78.    
  79.     public void add(double t) {
  80.         real += t;
  81.     }
  82.    
  83.     public void add(float t) {
  84.         real += t;
  85.     }
  86.    
  87.     public void add(int t) {
  88.         real += t;
  89.     }
  90.    
  91.     public static ComplexNumber add(ComplexNumber s, ComplexNumber t) {
  92.         ComplexNumber r = s.clone();
  93.         r.add(t);
  94.         return r;
  95.     }
  96.    
  97.     public static ComplexNumber add(ComplexNumber s, double t) {
  98.         ComplexNumber r = s.clone();
  99.         r.add(t);
  100.         return r;
  101.     }
  102.    
  103.     public static ComplexNumber add(ComplexNumber s, float t) {
  104.         ComplexNumber r = s.clone();
  105.         r.add(t);
  106.         return r;
  107.     }
  108.    
  109.     public static ComplexNumber add(ComplexNumber s, int t) {
  110.         ComplexNumber r = s.clone();
  111.         r.add(t);
  112.         return r;
  113.     }
  114.    
  115.     public static ComplexNumber add(double t, ComplexNumber s) {
  116.         return add(s, t);
  117.     }
  118.    
  119.     public static ComplexNumber add(float t, ComplexNumber s) {
  120.         return add(s, t);
  121.     }
  122.    
  123.     public static ComplexNumber add(int t, ComplexNumber s) {
  124.         return add(s, t);
  125.     }
  126.    
  127.    
  128.     /* Subtraction */
  129.     public void subtract(ComplexNumber t) {
  130.         real    -= t.getReal();
  131.         complex -= t.getComplex();
  132.     }
  133.    
  134.     public void subtract(double t) {
  135.         real -= t;
  136.     }
  137.    
  138.     public void subtract(int t) {
  139.         real -= t;
  140.     }
  141.    
  142.     public static ComplexNumber subtract(ComplexNumber s, ComplexNumber t) {
  143.         ComplexNumber r = s.clone();
  144.         r.subtract(t);
  145.         return r;
  146.     }
  147.    
  148.     public static ComplexNumber subtract(ComplexNumber s, double t) {
  149.         ComplexNumber r = s.clone();
  150.         r.subtract(t);
  151.         return r;
  152.     }
  153.    
  154.     public static ComplexNumber subtract(ComplexNumber s, float t) {
  155.         ComplexNumber r = s.clone();
  156.         r.subtract(t);
  157.         return r;
  158.     }
  159.    
  160.     public static ComplexNumber subtract(ComplexNumber s, int t) {
  161.         ComplexNumber r = s.clone();
  162.         r.subtract(t);
  163.         return r;
  164.     }
  165.    
  166.     public static ComplexNumber subtract(double t, ComplexNumber s) {
  167.         return subtract(s, t);
  168.     }
  169.    
  170.     public static ComplexNumber subtract(float t, ComplexNumber s) {
  171.         return subtract(s, t);
  172.     }
  173.    
  174.     public static ComplexNumber subtract(int t, ComplexNumber s) {
  175.         return subtract(s, t);
  176.     }
  177.    
  178.     /* Multiplication */
  179.     public void multiply(ComplexNumber t) {
  180.         double newreal    = (real * t.getReal())    + (complex * t.getComplex() * -1);
  181.         double newcomplex = (real * t.getComplex()) + (complex * t.getReal());
  182.        
  183.         real    = newreal;
  184.         complex = newcomplex;
  185.     }
  186.    
  187.     public void multiply(double t) {
  188.         real    *= t;
  189.         complex *= t;
  190.     }
  191.    
  192.     public void multiply(int t) {
  193.         real    *= t;
  194.         complex *= t;
  195.     }
  196.    
  197.     public void square() {
  198.         multiply(this.clone());
  199.     }
  200.    
  201.     public static ComplexNumber multiply(ComplexNumber s, ComplexNumber t) {
  202.         ComplexNumber r = s.clone();
  203.         r.multiply(t);
  204.         return r;
  205.     }
  206.    
  207.     public static ComplexNumber multiply(ComplexNumber s, double t) {
  208.         ComplexNumber r = s.clone();
  209.         r.multiply(t);
  210.         return r;
  211.     }
  212.    
  213.     public static ComplexNumber multiply(ComplexNumber s, float t) {
  214.         ComplexNumber r = s.clone();
  215.         r.multiply(t);
  216.         return r;
  217.     }
  218.    
  219.     public static ComplexNumber multiply(ComplexNumber s, int t) {
  220.         ComplexNumber r = s.clone();
  221.         r.multiply(t);
  222.         return r;
  223.     }
  224.    
  225.     public static ComplexNumber multiply(double t, ComplexNumber s) {
  226.         return multiply(s, t);
  227.     }
  228.    
  229.     public static ComplexNumber multiply(float t, ComplexNumber s) {
  230.         return multiply(s, t);
  231.     }
  232.    
  233.     public static ComplexNumber multiply(int t, ComplexNumber s) {
  234.         return multiply(s, t);
  235.     }
  236.    
  237.    
  238.     /* Division */
  239.     public void divide(ComplexNumber t) {
  240.         ComplexNumber conjugate =
  241.                 new ComplexNumber(t.getReal(), -1 * t.getComplex());
  242.        
  243.         multiply(conjugate);
  244.         ComplexNumber denominator = multiply(t, conjugate);
  245.        
  246.         // t * conjugate should always produce an imaginary part 0
  247.         assert denominator.getComplex() == 0;
  248.         divide(denominator.getReal());
  249.     }
  250.    
  251.     public void divide(double t) {
  252.         real    /= t;
  253.         complex /= t;
  254.     }
  255.    
  256.     public void divide(float t) {
  257.         real    /= t;
  258.         complex /= t;
  259.     }
  260.    
  261.     public void divide(int t) {
  262.         real    /= t;
  263.         complex /= t;
  264.     }
  265.    
  266.     public static ComplexNumber divide(ComplexNumber s, ComplexNumber t) {
  267.         ComplexNumber r = s.clone();
  268.         r.divide(t);
  269.         return r;
  270.     }
  271.    
  272.     public static ComplexNumber divide(ComplexNumber s, double t) {
  273.         ComplexNumber r = s.clone();
  274.         r.divide(t);
  275.         return r;
  276.     }
  277.    
  278.     public static ComplexNumber divide(ComplexNumber s, float t) {
  279.         ComplexNumber r = s.clone();
  280.         r.divide(t);
  281.         return r;
  282.     }
  283.    
  284.     public static ComplexNumber divide(ComplexNumber s, int t) {
  285.         ComplexNumber r = s.clone();
  286.         r.divide(t);
  287.         return r;
  288.     }
  289.    
  290.     public static ComplexNumber divide(double t, ComplexNumber s) {
  291.         return divide(s, t);
  292.     }
  293.    
  294.     public static ComplexNumber divide(float t, ComplexNumber s) {
  295.         return divide(s, t);
  296.     }
  297.    
  298.     public static ComplexNumber divide(int t, ComplexNumber s) {
  299.         return divide(s, t);
  300.     }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement