Skip to content

Commit 667e247

Browse files
authored
Merge pull request #53 from StaticBeagle/matrix-clean-up-updates
Matrix clean-up and updates
2 parents 4076303 + 9e5575b commit 667e247

File tree

6 files changed

+29
-26
lines changed

6 files changed

+29
-26
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ Maven
3636
<dependency>
3737
<groupId>com.wildbitsfoundry</groupId>
3838
<artifactId>etk4j</artifactId>
39-
<version>2.2.1</version>
39+
<version>2.2.2</version>
4040
</dependency>
4141
```
4242
Gradle
4343
```bash
44-
implementation 'com.wildbitsfoundry:etk4j:2.2.1'
44+
implementation 'com.wildbitsfoundry:etk4j:2.2.2'
4545
```
4646

4747
Requirements

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>com.wildbitsfoundry</groupId>
44
<artifactId>etk4j</artifactId>
5-
<version>2.2.1</version>
5+
<version>2.2.2</version>
66
<name>Engineering Toolkit for Java</name>
77
<description>Tools and implementation of mathematical methods or backing math for engineering problems and
88
applications.

src/main/java/com/wildbitsfoundry/etk4j/math/linearalgebra/ComplexLUDecompositionDense.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public ComplexLUDecompositionDense(ComplexMatrixDense matrix) {
7171
@Override
7272
public boolean isSingular() {
7373
for (int j = 0; j < cols; ++j) {
74-
if (_data[j * cols + j].equals(new Complex())) {
74+
if (_data[j * cols + j].abs() == 0) {
7575
return true;
7676
}
7777
}

src/main/java/com/wildbitsfoundry/etk4j/math/linearalgebra/LUDecompositionSparse.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ public MatrixSparse solve(double[] b) {
137137
int var10002 = b.length;
138138
throw new IllegalArgumentException("Unexpected number of rows in B based on shape of A. Found=" + var10002 + " Expected=" + this.rows);
139139
}
140+
if (this.isSingular()) {
141+
throw new RuntimeException("Matrix is singular.");
142+
}
140143

141144
double[] x = MatrixSparseUtils.adjust(this.gx, b.length);
142145
double[] rhs = MatrixSparseUtils.adjust(this.gb, b.length);
@@ -161,6 +164,10 @@ public MatrixSparse solve(MatrixSparse B) {
161164
if (B.getRowCount() != this.rows) {
162165
throw new IllegalArgumentException("Matrix row dimensions must agree.");
163166
}
167+
if (this.isSingular()) {
168+
throw new RuntimeException("Matrix is singular.");
169+
}
170+
164171
MatrixSparse X = new MatrixSparse(0, 0, 0);
165172
X.reshape(cols, B.cols, X.rows);
166173

src/main/java/com/wildbitsfoundry/etk4j/math/linearalgebra/MatrixDense.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ public String toString() {
10301030
if (i > 0 && i % cols == 0) {
10311031
sb.append(System.lineSeparator());
10321032
}
1033-
sb.append(String.format("%.4g", data[i])).append(" ");
1033+
sb.append(String.format("%-10.4g", data[i])).append(" ");
10341034
}
10351035
sb.setLength(sb.length() - 1);
10361036
return sb.toString();

src/main/java/com/wildbitsfoundry/etk4j/math/linearalgebra/MatrixSparse.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -478,30 +478,19 @@ public static MatrixSparse from2DArray(double[][] array) {
478478
}
479479

480480
public static MatrixSparse from2DArray(double[][] array, double tol) {
481-
int nonzero = 0;
482-
for (int i = 0; i != array.length; i++)
483-
for (int j = 0; j != array[i].length; j++) {
484-
if (array[i][j] != 0) {
485-
nonzero++;
486-
}
487-
}
488-
MatrixSparse dst = new MatrixSparse(array.length, array.length, nonzero);
489-
dst.nz_length = 0;
490-
dst.col_idx[0] = 0;
491-
int i, j;
492-
for (i = 0; i != array[0].length; i++) {
493-
for (j = 0; j != array.length; j++) {
494-
double value = array[j][i];
495-
if (Math.abs(value) > tol) {
496-
dst.nz_rows[dst.nz_length] = j;
497-
dst.nz_values[dst.nz_length] = value;
498-
++dst.nz_length;
481+
int rows = array.length;
482+
int cols = array[0].length;
483+
484+
MatrixSparse matrixSparse = new MatrixSparse(rows, cols);
485+
486+
for(int i = 0; i < rows; i++) {
487+
for(int j = 0; j < cols; j++) {
488+
if(Math.abs(array[i][j]) > tol) {
489+
matrixSparse.unsafeSet(i, j, array[i][j]);
499490
}
500491
}
501-
dst.col_idx[i + 1] = dst.nz_length;
502492
}
503-
504-
return dst;
493+
return matrixSparse;
505494
}
506495

507496
/**
@@ -827,6 +816,13 @@ public MatrixDense toDense() {
827816
return matrixDense;
828817
}
829818

819+
@Override
820+
public String toString() {
821+
return toDense().toString()
822+
.replaceAll(" {6}0.000 {6}", " ")
823+
.replaceAll("0.000 {6}", " ");
824+
}
825+
830826
public static class Factory {
831827
private Factory() {}
832828

0 commit comments

Comments
 (0)