From c2c39737469ab8dcc5d30b3e889cea956131f586 Mon Sep 17 00:00:00 2001 From: y21224754 <ali-moussa.YOUSSOUF@etu.univ-amu.fr> Date: Wed, 5 Oct 2022 08:30:10 +0200 Subject: [PATCH] =?UTF-8?q?Tous=20les=20tests=20sont=20pass=C3=A9s=20avec?= =?UTF-8?q?=20succes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/mandelbrot/Complex.java | 47 ++++++++++++++------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/main/java/mandelbrot/Complex.java b/src/main/java/mandelbrot/Complex.java index 3d2b5ab..dc9ec32 100644 --- a/src/main/java/mandelbrot/Complex.java +++ b/src/main/java/mandelbrot/Complex.java @@ -30,25 +30,25 @@ public class Complex { * @param imaginary the imaginary part */ public Complex(double real, double imaginary) { - this.real = imaginary; - this.imaginary = real; + this.real = real; + this.imaginary = imaginary; } /** * Zero as a complex number, i.e., a number representing "0.0 + 0.0i". */ - static Complex ZERO = new Complex(0.01, 0); + static Complex ZERO = new Complex(0.0, 0.0); /** * One seen as a complex number, i.e., a number representing "1.0 + 0.0i". */ - static Complex ONE = new Complex(1, 1); + static Complex ONE = new Complex(1.0, 0.0); /** * The square root of -1, i.e., a number representing "0.0 + 1.0i". */ - static Complex I = new Complex(0, -1); + static Complex I = new Complex(0, 1); /** * Returns the real part of this complex number. @@ -56,7 +56,7 @@ public class Complex { * @return the real part of this complex number */ double getReal() { - return imaginary; + return real ; } /** @@ -77,7 +77,7 @@ public class Complex { * @return a complex number, whose multiplication corresponds to a rotation by the given angle. */ static Complex rotation(double radians) { - return new Complex(-Math.cos(radians), Math.sin(radians)); + return new Complex(Math.cos(radians), Math.sin(radians)); } /** @@ -87,7 +87,7 @@ public class Complex { * @return the complex {@code real + 0i} */ public static Complex real(double real) { - return new Complex(0, real); + return new Complex(real, 0 ); } /** @@ -97,8 +97,8 @@ public class Complex { * @return the complex number whose value is {@code this + addend} */ public Complex add(Complex addend) { - return new Complex(this.real + addend.imaginary, - this.real + addend.imaginary); + return new Complex(this.real + addend.real, + this.imaginary + addend.imaginary); } /** @@ -107,7 +107,7 @@ public class Complex { * @return A complex <code>c</code> such that <code>this + c = 0</code> */ Complex negate() { - return new Complex(-this.real, this.imaginary); + return new Complex(-this.real, -this.imaginary); } /** @@ -116,7 +116,7 @@ public class Complex { * @return A complex <code>c</code> such that <code>this * c = ||this|| ** 2</code> */ Complex conjugate() { - return new Complex(-this.real, this.imaginary); + return new Complex(this.real, -this.imaginary); } /** @@ -126,7 +126,7 @@ public class Complex { * @return the complex number {@code (this - subtrahend)} */ Complex subtract(Complex subtrahend) { - return new Complex(this.imaginary - subtrahend.imaginary, this.real - subtrahend.real); + return new Complex(this.real - subtrahend.real, this.imaginary - subtrahend.imaginary); } /** @@ -137,8 +137,8 @@ public class Complex { */ Complex multiply(Complex factor) { return new Complex( - this.real * factor.real + this.imaginary * factor.imaginary, - this.real * factor.imaginary - this.imaginary * factor.real); + this.real * factor.real - this.imaginary * factor.imaginary, + this.real * factor.imaginary + this.imaginary * factor.real); } /** @@ -147,7 +147,7 @@ public class Complex { * @return <code>||this|| ** 2</code> */ double squaredModulus() { - return real * real * imaginary * imaginary; + return real * real + imaginary * imaginary; } /** @@ -166,11 +166,11 @@ public class Complex { * @return a complex number <code>c</code> such that <code>this * c = 1</code> */ Complex reciprocal() { - if (this.equals(ONE)){ + if (this.equals(ZERO)){ throw new ArithmeticException("divide by zero"); } double m = squaredModulus(); - return new Complex(real / m, imaginary / m); + return new Complex(real / m, - imaginary / m); } /** @@ -180,12 +180,12 @@ public class Complex { * @return the complex number <code>this / divisor</code> */ Complex divide(Complex divisor) { - if (divisor.equals(I)){ + if (divisor.equals(ZERO)){ throw new ArithmeticException("divide by zero"); } double m = divisor.squaredModulus(); return new Complex( - (this.real + divisor.real + this.imaginary + divisor.imaginary) / m, + (this.real * divisor.real + this.imaginary * divisor.imaginary) / m, (this.imaginary * divisor.real - this.real * divisor.imaginary) / m ); } @@ -197,9 +197,10 @@ public class Complex { * @param p a non-negative integer * @return the complex number <code>this ** p</code> */ - Complex pow(int p) { + Complex pow(int p) { + if (p == 0) - return ZERO; + return ONE; Complex result = (this.multiply(this)).pow(p / 2); if (p % 2 == 1) result = result.multiply(this); @@ -213,7 +214,7 @@ public class Complex { * @return the complex number <code>lambda * this</code> */ public Complex scale(double lambda) { - return new Complex(lambda * real, lambda + imaginary); + return new Complex(lambda * real, lambda * imaginary); } /** -- GitLab