Skip to content

Commit

Permalink
Remove ending space on each row from result of saving Matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-punko committed Oct 28, 2024
1 parent ebb754d commit 264c002
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
27 changes: 15 additions & 12 deletions src/main/java/by/andd3dfx/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import by.andd3dfx.math.Interval;
import by.andd3dfx.math.Matrix;
import lombok.SneakyThrows;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* Util to serialize data into files
Expand Down Expand Up @@ -42,24 +44,25 @@ public static void saveFunc(Interval timeInterval, Function<Double, Double> xFun
serialize(fileName, sb);
}

@SneakyThrows
public static void save(Matrix m, String fileName, boolean rowsToColumns) {
var file = new FileWriter(fileName);
if (rowsToColumns) {
public static void save(Matrix m, String fileName, boolean rotate) {
var sb = new StringBuilder();
if (rotate) {
for (int j = 0; j < m.getN(); j++) {
var list = new ArrayList<Double>();
for (int i = 0; i < m.getM(); i++) {
file.write(m.get(i, j) + " ");
list.add(m.get(i, j));
}
file.write("\n");
sb.append(list.stream().map(String::valueOf)
.collect(Collectors.joining(" ")) + "\n");
}
} else {
for (int i = 0; i < m.getM(); i++) {
for (int j = 0; j < m.getN(); j++) {
file.write(m.get(i, j) + " ");
}
file.write("\n");
var string = Arrays.stream(m.get(i))
.mapToObj(String::valueOf)
.collect(Collectors.joining(" "));
sb.append(string + "\n");
}
}
file.close();
serialize(fileName, sb);
}
}
16 changes: 6 additions & 10 deletions src/test/java/by/andd3dfx/util/FileUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ void saveFuncForParameterizedXAndY() throws IOException {
@Test
void saveMatrixInOriginalView() throws IOException {
final var fileName = "matrix-original.txt";
var m = new Matrix(2, 2);
m.set(0, 0, 5);
m.set(0, 1, 6);
m.set(1, 0, 9);
m.set(1, 1, 1);
var m = new Matrix(2, 3);
m.set(0, new double[]{50, 51, 52});
m.set(1, new double[]{65, 66, 67});

FileUtil.save(m, BUILD_PATH + fileName, false);

Expand All @@ -60,11 +58,9 @@ void saveMatrixInOriginalView() throws IOException {
@Test
void saveMatrixWithConversionRowsToColumns() throws IOException {
final var fileName = "matrix-not-original.txt";
var m = new Matrix(2, 2);
m.set(0, 0, 5);
m.set(0, 1, 6);
m.set(1, 0, 9);
m.set(1, 1, 1);
var m = new Matrix(2, 3);
m.set(0, new double[]{50, 51, 52});
m.set(1, new double[]{65, 66, 67});

FileUtil.save(m, BUILD_PATH + fileName, true);

Expand Down
5 changes: 3 additions & 2 deletions src/test/resources/matrix-not-original.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
5.0 9.0
6.0 1.0
50.0 65.0
51.0 66.0
52.0 67.0
4 changes: 2 additions & 2 deletions src/test/resources/matrix-original.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
5.0 6.0
9.0 1.0
50.0 51.0 52.0
65.0 66.0 67.0

0 comments on commit 264c002

Please sign in to comment.