Skip to content
Snippets Groups Projects
Commit c2c39737 authored by YOUSSOUF Ali moussa's avatar YOUSSOUF Ali moussa
Browse files

Tous les tests sont passés avec succes

parent 267c7e8c
Branches main
No related tags found
No related merge requests found
Pipeline #8271 passed
...@@ -30,25 +30,25 @@ public class Complex { ...@@ -30,25 +30,25 @@ public class Complex {
* @param imaginary the imaginary part * @param imaginary the imaginary part
*/ */
public Complex(double real, double imaginary) { public Complex(double real, double imaginary) {
this.real = imaginary; this.real = real;
this.imaginary = real; this.imaginary = imaginary;
} }
/** /**
* Zero as a complex number, i.e., a number representing "0.0 + 0.0i". * 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". * 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". * 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. * Returns the real part of this complex number.
...@@ -56,7 +56,7 @@ public class Complex { ...@@ -56,7 +56,7 @@ public class Complex {
* @return the real part of this complex number * @return the real part of this complex number
*/ */
double getReal() { double getReal() {
return imaginary; return real ;
} }
/** /**
...@@ -77,7 +77,7 @@ public class Complex { ...@@ -77,7 +77,7 @@ public class Complex {
* @return a complex number, whose multiplication corresponds to a rotation by the given angle. * @return a complex number, whose multiplication corresponds to a rotation by the given angle.
*/ */
static Complex rotation(double radians) { 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 { ...@@ -87,7 +87,7 @@ public class Complex {
* @return the complex {@code real + 0i} * @return the complex {@code real + 0i}
*/ */
public static Complex real(double real) { public static Complex real(double real) {
return new Complex(0, real); return new Complex(real, 0 );
} }
/** /**
...@@ -97,8 +97,8 @@ public class Complex { ...@@ -97,8 +97,8 @@ public class Complex {
* @return the complex number whose value is {@code this + addend} * @return the complex number whose value is {@code this + addend}
*/ */
public Complex add(Complex addend) { public Complex add(Complex addend) {
return new Complex(this.real + addend.imaginary, return new Complex(this.real + addend.real,
this.real + addend.imaginary); this.imaginary + addend.imaginary);
} }
/** /**
...@@ -107,7 +107,7 @@ public class Complex { ...@@ -107,7 +107,7 @@ public class Complex {
* @return A complex <code>c</code> such that <code>this + c = 0</code> * @return A complex <code>c</code> such that <code>this + c = 0</code>
*/ */
Complex negate() { Complex negate() {
return new Complex(-this.real, this.imaginary); return new Complex(-this.real, -this.imaginary);
} }
/** /**
...@@ -116,7 +116,7 @@ public class Complex { ...@@ -116,7 +116,7 @@ public class Complex {
* @return A complex <code>c</code> such that <code>this * c = ||this|| ** 2</code> * @return A complex <code>c</code> such that <code>this * c = ||this|| ** 2</code>
*/ */
Complex conjugate() { Complex conjugate() {
return new Complex(-this.real, this.imaginary); return new Complex(this.real, -this.imaginary);
} }
/** /**
...@@ -126,7 +126,7 @@ public class Complex { ...@@ -126,7 +126,7 @@ public class Complex {
* @return the complex number {@code (this - subtrahend)} * @return the complex number {@code (this - subtrahend)}
*/ */
Complex subtract(Complex 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 { ...@@ -137,8 +137,8 @@ public class Complex {
*/ */
Complex multiply(Complex factor) { Complex multiply(Complex factor) {
return new Complex( return new Complex(
this.real * factor.real + this.imaginary * factor.imaginary, this.real * factor.real - this.imaginary * factor.imaginary,
this.real * factor.imaginary - this.imaginary * factor.real); this.real * factor.imaginary + this.imaginary * factor.real);
} }
/** /**
...@@ -147,7 +147,7 @@ public class Complex { ...@@ -147,7 +147,7 @@ public class Complex {
* @return <code>||this|| ** 2</code> * @return <code>||this|| ** 2</code>
*/ */
double squaredModulus() { double squaredModulus() {
return real * real * imaginary * imaginary; return real * real + imaginary * imaginary;
} }
/** /**
...@@ -166,11 +166,11 @@ public class Complex { ...@@ -166,11 +166,11 @@ public class Complex {
* @return a complex number <code>c</code> such that <code>this * c = 1</code> * @return a complex number <code>c</code> such that <code>this * c = 1</code>
*/ */
Complex reciprocal() { Complex reciprocal() {
if (this.equals(ONE)){ if (this.equals(ZERO)){
throw new ArithmeticException("divide by zero"); throw new ArithmeticException("divide by zero");
} }
double m = squaredModulus(); double m = squaredModulus();
return new Complex(real / m, imaginary / m); return new Complex(real / m, - imaginary / m);
} }
/** /**
...@@ -180,12 +180,12 @@ public class Complex { ...@@ -180,12 +180,12 @@ public class Complex {
* @return the complex number <code>this / divisor</code> * @return the complex number <code>this / divisor</code>
*/ */
Complex divide(Complex divisor) { Complex divide(Complex divisor) {
if (divisor.equals(I)){ if (divisor.equals(ZERO)){
throw new ArithmeticException("divide by zero"); throw new ArithmeticException("divide by zero");
} }
double m = divisor.squaredModulus(); double m = divisor.squaredModulus();
return new Complex( 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 (this.imaginary * divisor.real - this.real * divisor.imaginary) / m
); );
} }
...@@ -198,8 +198,9 @@ public class Complex { ...@@ -198,8 +198,9 @@ public class Complex {
* @return the complex number <code>this ** p</code> * @return the complex number <code>this ** p</code>
*/ */
Complex pow(int p) { Complex pow(int p) {
if (p == 0) if (p == 0)
return ZERO; return ONE;
Complex result = (this.multiply(this)).pow(p / 2); Complex result = (this.multiply(this)).pow(p / 2);
if (p % 2 == 1) if (p % 2 == 1)
result = result.multiply(this); result = result.multiply(this);
...@@ -213,7 +214,7 @@ public class Complex { ...@@ -213,7 +214,7 @@ public class Complex {
* @return the complex number <code>lambda * this</code> * @return the complex number <code>lambda * this</code>
*/ */
public Complex scale(double lambda) { public Complex scale(double lambda) {
return new Complex(lambda * real, lambda + imaginary); return new Complex(lambda * real, lambda * imaginary);
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment