-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArrayMatrix.java
More file actions
114 lines (99 loc) · 3.82 KB
/
ArrayMatrix.java
File metadata and controls
114 lines (99 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.mygdx.linear;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.IntStream;
public class ArrayMatrix implements SystemSolveMatrix {
private static final double EPS = 1e-15;
private final List<List<MatrixElementImpl>> matrix;
public ArrayMatrix(int rows, int columns) {
matrix = Stream.generate(() ->
Stream.generate(MatrixElementImpl::new).limit(columns).collect(Collectors.toList())
).limit(rows).collect(Collectors.toList());
}
public ArrayMatrix(final List<List<MatrixElementImpl>> matrix, int y) {
this.matrix = matrix;
}
public ArrayMatrix(final List<? extends List<Double>> m) {
matrix = Stream.generate(() ->
Stream.generate(MatrixElementImpl::new).limit(m.size()).collect(Collectors.toList())
).limit(m.size()).collect(Collectors.toList());
for (int i = 0; i < m.size(); i++) {
for (int j = 0; j < m.get(i).size(); j++) {
get(i, j).set(m.get(i).get(j));
}
}
}
public ArrayMatrix(final Matrix m) {
matrix = Stream.generate(() ->
Stream.generate(MatrixElementImpl::new).limit(m.nColumns()).collect(Collectors.toList())
).limit(m.nRows()).collect(Collectors.toList());
for (int i = 0; i < m.nRows(); i++) {
for (int j = 0; j < m.nColumns(); j++) {
get(i, j).set(m.get(i, j).get());
}
}
}
@Override
public MatrixElement get(int x, int y) {
return matrix.get(x).get(y);
}
@Override
public int nRows() {
return matrix.size();
}
@Override
public int nColumns() {
return matrix.size() == 0 ? 0 : matrix.get(0).size();
}
private void addVec(final int row, final double coefficient, final List<? extends MatrixElement> vec) {
IntStream.range(0, nColumns()).forEach(col ->
get(row, col).set(get(row, col).get() + (vec.get(col).get() * coefficient)));
}
private void mulVec(final int row, final MatrixElementImpl coeff) {
IntStream.range(0, nColumns()).forEach(col -> get(row, col).set(get(row, col).get() * coeff.get()));
}
@Override
public String toString() {
StringBuilder r = new StringBuilder();
for (int row = 0; row < nRows(); row++) {
for (int col = 0; col < nColumns(); col++) {
r.append(get(row, col).get()).append(" ");
}
r.append(System.lineSeparator());
}
return r.toString();
}
@Override
public List<Double> solve(final List<Double> free) {
List<Double> result = new ArrayList<>(free);
for (int i = 0; i < nRows(); i++) {
int indMax = i;
for (int r = i; r < nRows(); r++) {
if (Math.abs(get(indMax, i).get()) < Math.abs(get(r, i).get())) {
indMax = r;
}
}
if (Math.abs(get(indMax, i).get()) < EPS) {
throw new IllegalStateException("Many solutions");
}
Collections.swap(matrix, i, indMax);
Collections.swap(result, i, indMax);
final double aii = get(i, i).get();
MatrixElementImpl d = new MatrixElementImpl(1 / aii);
mulVec(i, d);
result.set(i, result.get(i) * d.get());
List<MatrixElementImpl> vec = matrix.get(i);
for (int j = 0; j < nRows(); j++) {
if (i != j) {
final double coefficient = -get(j, i).get();
addVec(j, coefficient, vec);
result.set(j, result.get(j) + coefficient * result.get(i));
}
}
}
return result;
}
}