Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Дмитриев П.А. 250504 #970

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/demo/parallel/Complex.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,22 @@ public Complex times(Complex b) {
public double lengthSQ() {
return re * re + im * im;
}

public Complex divide(Complex b) {
Complex a = this;
double denominator = b.re * b.re + b.im * b.im;
double real = (a.re * b.re + a.im * b.im) / denominator;
double imag = (a.im * b.re - a.re * b.im) / denominator;
re = real;
im = imag;
return this;
}

public double getRe() {
return re;
}

public double getIm() {
return im;
}
}
10 changes: 5 additions & 5 deletions src/demo/parallel/MandelbrotSetTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ protected Long call() throws Exception {
*/
private int calc(Complex comp) {
int count = 0;
Complex c = new Complex(0, 0);
Complex c = new Complex(0.5, 0.376);
do {
c = c.times(c).plus(comp);
c = c.times(c).plus(comp).divide(comp);
count++;
} while (count < CAL_MAX_COUNT && c.lengthSQ() < LENGTH_BOUNDARY);
return count;
Expand Down Expand Up @@ -352,11 +352,11 @@ private Color getColor(int count) {
*/
Color[] cc = {
Color.rgb(40, 0, 0),
Color.RED,
Color.PURPLE,
Color.WHITE,
Color.RED,
Color.PURPLE,
Color.rgb(100, 0, 0),
Color.RED,
Color.PURPLE,
Color.rgb(50, 0, 0)
};

Expand Down
44 changes: 44 additions & 0 deletions src/test/demo/parallel/ComplexTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package test.demo.parallel;

import static org.junit.jupiter.api.Assertions.*;

import demo.parallel.Complex;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class ComplexTest {

private Complex a;
private Complex b;

@BeforeEach
void setUp() {
a = new Complex(4.0, 2.0);
b = new Complex(2.0, 1.0);
}

@Test
void testDivide() {
Complex result = a.divide(b);
assertEquals(2.0, result.getRe(), 1e-10, "Real part should be 2.0");
assertEquals(0.0, result.getIm(), 1e-10, "Imaginary part should be 0.0");
}

@Test
void testDivideWithNegativeImaginary() {
Complex c = new Complex(1.0, -1.0);
Complex d = new Complex(1.0, 1.0);
Complex result = c.divide(d);
assertEquals(0.0, result.getRe(), 1e-10, "Real part should be 0.0");
assertEquals(-1.0, result.getIm(), 1e-10, "Imaginary part should be -1.0");
}

@Test
void testDivideWithLargeNumbers() {
Complex largeA = new Complex(1000.0, 500.0);
Complex largeB = new Complex(200.0, 100.0);
Complex result = largeA.divide(largeB);
assertEquals(5.0, result.getRe(), 1e-10, "Real part should be 5.0");
assertEquals(0.0, result.getIm(), 1e-10, "Imaginary part should be 0.0");
}
}