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 #963

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
29 changes: 29 additions & 0 deletions src/demo/parallel/Complex.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/
package demo.parallel;

import java.util.Objects;

/**
* A complex number is a number that can be expressed in the form a + b * i, where
Expand Down Expand Up @@ -71,6 +72,22 @@ public Complex plus(Complex b) {
return this;
}

public Complex minus(Complex b) {
re -= b.re;
im -= b.im;
return this;
}

public Complex divide(Complex b) {
if (b.re == 0 && b.im == 0) {
throw new ArithmeticException("Division by zero");
}
double denominator = b.re * b.re + b.im * b.im;
double real = (this.re * b.re + this.im * b.im) / denominator;
double imag = (this.im * b.re - this.re * b.im) / denominator;
return new Complex(real, imag);
}

/**
* Multiply operation.
* @param b multiplier
Expand All @@ -93,4 +110,16 @@ public Complex times(Complex b) {
public double lengthSQ() {
return re * re + im * im;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Complex complex)) return false;
return Double.compare(re, complex.re) == 0 && Double.compare(im, complex.im) == 0;
}

@Override
public int hashCode() {
return Objects.hash(re, im);
}
}
25 changes: 17 additions & 8 deletions src/demo/parallel/MandelbrotSetTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,16 @@ protected Long call() throws Exception {
private int calc(Complex comp) {
int count = 0;
Complex c = new Complex(0, 0);
Complex temp = new Complex(0, 0);
do {
c = c.times(c).plus(comp);
temp = c.times(c);
c = temp.plus(comp);
if (count % 5 == 0) {
c = c.minus(new Complex(0.1, 0));
}
if (count > 5) {
c = c.divide(new Complex(2, 0));
}
count++;
} while (count < CAL_MAX_COUNT && c.lengthSQ() < LENGTH_BOUNDARY);
return count;
Expand Down Expand Up @@ -351,13 +359,14 @@ private Color getColor(int count) {
* Color stops for colors table: color values
*/
Color[] cc = {
Color.rgb(40, 0, 0),
Color.RED,
Color.WHITE,
Color.RED,
Color.rgb(100, 0, 0),
Color.RED,
Color.rgb(50, 0, 0)

Color.rgb(0, 40, 0),
Color.BLUE,
Color.WHITE,
Color.BLUE,
Color.rgb(0, 100, 0),
Color.BLUE,
Color.rgb(0, 50, 0)
};

/**
Expand Down
48 changes: 48 additions & 0 deletions src/test/TestComplexOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package test;

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

public class TestComplexOperation {

Complex firstComplexNum;
Complex secondComplexNum;

@BeforeEach
void setUp() {
firstComplexNum = new Complex(1, 1);
secondComplexNum = new Complex(2, 2);
}

@Test
void testPlusOperation() {
Complex result = firstComplexNum.plus(secondComplexNum);
assertEquals(new Complex(3, 3), result);
}

@Test
void testMinusOperation() {
Complex result = firstComplexNum.minus(secondComplexNum);
assertEquals(new Complex(-1, -1), result);
}

@Test
void testDivideOperation() {
Complex result = firstComplexNum.divide(secondComplexNum);
assertEquals(new Complex(0.5, 0), result);
}

@Test
void testTimesOperation() {
Complex result = firstComplexNum.times(secondComplexNum);
assertEquals(new Complex(0, 4), result);
}

@Test
void testDivideByZero() {
Complex zeroComplex = new Complex(0, 0);
assertThrows(ArithmeticException.class, () -> firstComplexNum.divide(zeroComplex));
}
}