Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package mandlebrot;
- /**
- *
- * @author Joel
- */
- public class ComplexNumber extends Number implements Cloneable {
- private double real,
- complex;
- ComplexNumber(double _real, double _complex) {
- real = _real;
- complex = _complex;
- }
- @Override
- public int intValue() {
- return (int)real;
- }
- @Override
- public double doubleValue() {
- return real;
- }
- @Override
- public float floatValue() {
- return (float)real;
- }
- @Override
- public long longValue() {
- return (long)real;
- }
- public static String toString(ComplexNumber t) {
- if (t.getReal() == 0) return t.getComplex() + "i";
- if (t.getComplex() == 0) return "" + t.getReal();
- return t.getReal() + " + " + t.getComplex() + "i";
- }
- @Override
- public ComplexNumber clone() {
- return new ComplexNumber(real, complex);
- }
- /* Getters and setters */
- public void setReal(double newreal) {
- real = newreal;
- }
- public void setComplex(double newcomplex) {
- complex = newcomplex;
- }
- public double getReal() {
- return real;
- }
- public double getComplex() {
- return complex;
- }
- /* Addition */
- public void add(ComplexNumber t) {
- real += t.getReal();
- complex += t.getComplex();
- }
- public void add(double t) {
- real += t;
- }
- public void add(float t) {
- real += t;
- }
- public void add(int t) {
- real += t;
- }
- public static ComplexNumber add(ComplexNumber s, ComplexNumber t) {
- ComplexNumber r = s.clone();
- r.add(t);
- return r;
- }
- public static ComplexNumber add(ComplexNumber s, double t) {
- ComplexNumber r = s.clone();
- r.add(t);
- return r;
- }
- public static ComplexNumber add(ComplexNumber s, float t) {
- ComplexNumber r = s.clone();
- r.add(t);
- return r;
- }
- public static ComplexNumber add(ComplexNumber s, int t) {
- ComplexNumber r = s.clone();
- r.add(t);
- return r;
- }
- public static ComplexNumber add(double t, ComplexNumber s) {
- return add(s, t);
- }
- public static ComplexNumber add(float t, ComplexNumber s) {
- return add(s, t);
- }
- public static ComplexNumber add(int t, ComplexNumber s) {
- return add(s, t);
- }
- /* Subtraction */
- public void subtract(ComplexNumber t) {
- real -= t.getReal();
- complex -= t.getComplex();
- }
- public void subtract(double t) {
- real -= t;
- }
- public void subtract(int t) {
- real -= t;
- }
- public static ComplexNumber subtract(ComplexNumber s, ComplexNumber t) {
- ComplexNumber r = s.clone();
- r.subtract(t);
- return r;
- }
- public static ComplexNumber subtract(ComplexNumber s, double t) {
- ComplexNumber r = s.clone();
- r.subtract(t);
- return r;
- }
- public static ComplexNumber subtract(ComplexNumber s, float t) {
- ComplexNumber r = s.clone();
- r.subtract(t);
- return r;
- }
- public static ComplexNumber subtract(ComplexNumber s, int t) {
- ComplexNumber r = s.clone();
- r.subtract(t);
- return r;
- }
- public static ComplexNumber subtract(double t, ComplexNumber s) {
- return subtract(s, t);
- }
- public static ComplexNumber subtract(float t, ComplexNumber s) {
- return subtract(s, t);
- }
- public static ComplexNumber subtract(int t, ComplexNumber s) {
- return subtract(s, t);
- }
- /* Multiplication */
- public void multiply(ComplexNumber t) {
- double newreal = (real * t.getReal()) + (complex * t.getComplex() * -1);
- double newcomplex = (real * t.getComplex()) + (complex * t.getReal());
- real = newreal;
- complex = newcomplex;
- }
- public void multiply(double t) {
- real *= t;
- complex *= t;
- }
- public void multiply(int t) {
- real *= t;
- complex *= t;
- }
- public void square() {
- multiply(this.clone());
- }
- public static ComplexNumber multiply(ComplexNumber s, ComplexNumber t) {
- ComplexNumber r = s.clone();
- r.multiply(t);
- return r;
- }
- public static ComplexNumber multiply(ComplexNumber s, double t) {
- ComplexNumber r = s.clone();
- r.multiply(t);
- return r;
- }
- public static ComplexNumber multiply(ComplexNumber s, float t) {
- ComplexNumber r = s.clone();
- r.multiply(t);
- return r;
- }
- public static ComplexNumber multiply(ComplexNumber s, int t) {
- ComplexNumber r = s.clone();
- r.multiply(t);
- return r;
- }
- public static ComplexNumber multiply(double t, ComplexNumber s) {
- return multiply(s, t);
- }
- public static ComplexNumber multiply(float t, ComplexNumber s) {
- return multiply(s, t);
- }
- public static ComplexNumber multiply(int t, ComplexNumber s) {
- return multiply(s, t);
- }
- /* Division */
- public void divide(ComplexNumber t) {
- ComplexNumber conjugate =
- new ComplexNumber(t.getReal(), -1 * t.getComplex());
- multiply(conjugate);
- ComplexNumber denominator = multiply(t, conjugate);
- // t * conjugate should always produce an imaginary part 0
- assert denominator.getComplex() == 0;
- divide(denominator.getReal());
- }
- public void divide(double t) {
- real /= t;
- complex /= t;
- }
- public void divide(float t) {
- real /= t;
- complex /= t;
- }
- public void divide(int t) {
- real /= t;
- complex /= t;
- }
- public static ComplexNumber divide(ComplexNumber s, ComplexNumber t) {
- ComplexNumber r = s.clone();
- r.divide(t);
- return r;
- }
- public static ComplexNumber divide(ComplexNumber s, double t) {
- ComplexNumber r = s.clone();
- r.divide(t);
- return r;
- }
- public static ComplexNumber divide(ComplexNumber s, float t) {
- ComplexNumber r = s.clone();
- r.divide(t);
- return r;
- }
- public static ComplexNumber divide(ComplexNumber s, int t) {
- ComplexNumber r = s.clone();
- r.divide(t);
- return r;
- }
- public static ComplexNumber divide(double t, ComplexNumber s) {
- return divide(s, t);
- }
- public static ComplexNumber divide(float t, ComplexNumber s) {
- return divide(s, t);
- }
- public static ComplexNumber divide(int t, ComplexNumber s) {
- return divide(s, t);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement