Skip to content

Commit

Permalink
Add helper methods to reduce boilerplate code amount
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-punko committed Oct 22, 2024
1 parent 4707405 commit 66400fe
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 10 deletions.
15 changes: 15 additions & 0 deletions src/main/java/by/andd3dfx/math/Area.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,19 @@

public record Area(Interval x, Interval t) {

public double tLeft() {
return t().left();
}

public double tRight() {
return t().right();
}

public double xLeft() {
return x().left();
}

public double xRight() {
return x().right();
}
}
4 changes: 4 additions & 0 deletions src/main/java/by/andd3dfx/math/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public void set(int i, int j, double value) {
data[i * n + j] = value;
}

public void set(int i, double[] arr) {
System.arraycopy(arr, 0, data, i * n, arr.length);
}

public double get(int i, int j) {
assert (i >= 0 && i < m && j >= 0 && j < n);
return data[i * n + j];
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/by/andd3dfx/math/pde/equation/Equation.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void sUt(String fileName, double t) {
*/
public void sUt(String fileName, double[] t) {
for (var t_i : t) {
assert (area.t().left() <= t_i && t_i <= area.t().right());
assert (area.tLeft() <= t_i && t_i <= area.tRight());
}

var sb = new StringBuilder();
Expand Down Expand Up @@ -140,7 +140,7 @@ public void sUx(String fileName, double x) {
*/
public void sUx(String fileName, double[] x) {
for (var x_i : x) {
assert (area.x().left() <= x_i && x_i <= area.x().right());
assert (area.xLeft() <= x_i && x_i <= area.xRight());
}

var sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public void solve(double h, double tau) {
_2h2_tau2 = 2 * Math.pow(h / tau, 2);

// Set border conditions on layer 1
arr.set(1, 0, calcFirstLayerValue(tau, arr.get(0, 0), area.x().left()));
arr.set(1, N, calcFirstLayerValue(tau, arr.get(0, N), area.x().right()));
arr.set(1, 0, calcFirstLayerValue(tau, arr.get(0, 0), area.xLeft()));
arr.set(1, N, calcFirstLayerValue(tau, arr.get(0, N), area.xRight()));

// Calculate U value on layer 1 which needed to start finite-difference algorithm
//
Expand Down Expand Up @@ -118,9 +118,7 @@ public void solve(double h, double tau) {
}

progonka(A, B, C, F, Mu[1], Nu[1], Mu[2], Nu[2], U);
for (int i = 0; i <= N; i++) {
arr.set(nj, i, U[i]);
}
arr.set(nj, U);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ public void solve(double h, double tau) {
}

progonka(A, B, C, F, Mu[1], Nu[1], Mu[2], Nu[2], U);
for (int i = 0; i <= N; i++) {
arr.set(nj, i, U[i]);
}
arr.set(nj, U);
}
}
}
18 changes: 18 additions & 0 deletions src/test/java/by/andd3dfx/math/AreaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package by.andd3dfx.math;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class AreaTest {

@Test
void xLeftRightNTLeftRight() {
var area = new Area(new Interval(3, 8, 10), new Interval(0, 10, 10));

assertThat(area.xLeft()).isEqualTo(3);
assertThat(area.xRight()).isEqualTo(8);
assertThat(area.tLeft()).isEqualTo(0);
assertThat(area.tRight()).isEqualTo(10);
}
}
21 changes: 21 additions & 0 deletions src/test/java/by/andd3dfx/math/MatrixTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,25 @@ void fill() {
}
assertThat(result).isEqualTo(m);
}

@Test
void copyArrayIntoDefinitePositionOfData() {
var m = new Matrix(2, 4);
m.fill(3);
m.set(0, 0, 5);
m.set(0, 2, -5);
m.set(1, 0, 9);
m.set(1, 3, 1);

m.set(1, new double[]{9, 8, 7, 6});

assertThat(m.get(0, 0)).isEqualTo(5);
assertThat(m.get(0, 1)).isEqualTo(3);
assertThat(m.get(0, 2)).isEqualTo(-5);
assertThat(m.get(0, 3)).isEqualTo(3);
assertThat(m.get(1, 0)).isEqualTo(9);
assertThat(m.get(1, 1)).isEqualTo(8);
assertThat(m.get(1, 2)).isEqualTo(7);
assertThat(m.get(1, 3)).isEqualTo(6);
}
}

0 comments on commit 66400fe

Please sign in to comment.