Skip to content

Commit

Permalink
Merge pull request #51 from robinsonianr/cleanup
Browse files Browse the repository at this point in the history
Refactored and Cleanup
  • Loading branch information
bowring authored Aug 3, 2022
2 parents ab304b6 + 8f04f81 commit 5872285
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public static LinePlotBuilder[] modelTest(Path dataFile, LoggingCallbackInterfac
static LinePlotBuilder[] gatherBeamWidth(PeakShapeOutputDataRecord peakShapeOutputDataRecord, LoggingCallbackInterface loggingCallback) throws RecoverableCondition {

PhysicalStore.Factory<Double, Primitive64Store> storeFactory = Primitive64Store.FACTORY;
double maxBeamIndex, thresholdIntensity;
double maxBeamIndex;
double thresholdIntensity;

// Spline basis Basis
int basisDegree = 3;
// int orderDiff = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public PeakShapeOutputDataRecord prepareInputDataModelFromFile(Path inputDataFil
masses.add(Double.parseDouble(cols[0]));
intensity.add(Double.parseDouble(cols[1]));
}
default -> {
}
}

if (line.startsWith("#START")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,8 @@ public static Primitive64Store max(MatrixStore<Double> matrix, int max) {
*/

public static Primitive64Store diag(MatrixStore<Double> mat) {
int row = mat.getRowDim();
double[][] diagMat = new double[row][row];
int size = Math.max(mat.getRowDim(), mat.getColDim());
double[][] diagMat = new double[size][size];
int dag = 0;

for (int i = 0; i < diagMat.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,32 @@

public class SplineBasisModel {

private final MatrixStore<Double> x; // vector of x values
private final MatrixStore<Double> BSplineMatrix;

private final double basisDegree;

private final double numSegments;

private final Primitive64Store BSplineMatrix;


private SplineBasisModel() {
this.x = null;
this.basisDegree = 0;
this.numSegments = 0;
this.BSplineMatrix = null;
}

private SplineBasisModel(MatrixStore<Double> x, int numSegments, int basisDegree) {
this.x = x;
this.basisDegree = basisDegree;
this.numSegments = numSegments;
public SplineBasisModel(MatrixStore<Double> x, int numSegments, int basisDegree) {
this.BSplineMatrix = bBase(x, numSegments, basisDegree);
}


public static SplineBasisModel initializeSpline(Primitive64Store x, int numSegments, int basisDegree) {
return new SplineBasisModel(x, numSegments, basisDegree);
}


public static Primitive64Store bBase(MatrixStore<Double> x, int numSegments, int basisDegree) {
PhysicalStore.Factory<Double, Primitive64Store> storeFactory = Primitive64Store.FACTORY;
double[][] sk;
double xLower = x.get(0, 0);
double xUpper = x.get(x.getRowDim() - 1, x.getColDim() - 1);

x = x.transpose();
MatrixStore<Double> transposeX = x.transpose();

double dx = (xUpper - xLower) / numSegments;
Primitive64Store knots = MatLab.linspace(xLower - basisDegree * dx, xUpper + basisDegree * dx, numSegments + 2 * basisDegree + 1);

int nx = x.getRowDim();
int nx = transposeX.getRowDim();
int nt = knots.getColDim();


Primitive64Store kronTerm = storeFactory.make(1, nt);
kronTerm.fillAll(1.0);

MatrixStore<Double> matrixX = MatLab.kron(x, kronTerm);
MatrixStore<Double> matrixX = MatLab.kron(transposeX, kronTerm);


Primitive64Store term2 = storeFactory.make(nx, 1);
Expand All @@ -74,10 +52,9 @@ public static Primitive64Store bBase(MatrixStore<Double> x, int numSegments, int
MatrixStore<Double> Base = matrixP.multiply(matrixD.transpose());

int nb = MatLab.size(Base, 2);
Primitive64Store kronTerm2 = storeFactory.make(nb, 1);
Primitive64Store kronTerm2 = storeFactory.make(1, nb);
kronTerm2.fillAll(1.0);
matrixX = MatLab.kron(x, kronTerm2);
matrixX = matrixX.transpose();
matrixX = MatLab.kron(transposeX, kronTerm2);

sk = new double[1][nb];
for (int i = 0; i < 1; i++) {
Expand Down Expand Up @@ -106,19 +83,19 @@ public static Primitive64Store bBase(MatrixStore<Double> x, double xl, double xr
xLower = xl > x.get(0, 0) ? x.get(0, 0) : xl;
xUpper = xr < x.get(x.getRowDim() - 1, x.getColDim() - 1) ? x.get(x.getRowDim() - 1, x.getColDim() - 1) : xr;

x = x.transpose();
MatrixStore<Double> transposeX = x.transpose();

double dx = (xUpper - xLower) / numSegments;
Primitive64Store knots = MatLab.linspace(xLower - basisDegree * dx, xUpper + basisDegree * dx, numSegments + 2 * basisDegree + 1);

int nx = x.getRowDim();
int nx = transposeX.getRowDim();
int nt = knots.getColDim();


Primitive64Store kronTerm = storeFactory.make(1, nt);
kronTerm.fillAll(1.0);

MatrixStore<Double> matrixX = MatLab.kron(x, kronTerm);
MatrixStore<Double> matrixX = MatLab.kron(transposeX, kronTerm);


Primitive64Store term2 = storeFactory.make(nx, 1);
Expand All @@ -138,7 +115,7 @@ public static Primitive64Store bBase(MatrixStore<Double> x, double xl, double xr
int nb = MatLab.size(Base, 2);
Primitive64Store kronTerm2 = storeFactory.make(1, nb);
kronTerm2.fillAll(1.0);
matrixX = MatLab.kron(x, kronTerm2);
matrixX = MatLab.kron(transposeX, kronTerm2);

sk = new double[1][nb];
for (int i = 0; i < 1; i++) {
Expand All @@ -157,11 +134,7 @@ public static Primitive64Store bBase(MatrixStore<Double> x, double xl, double xr
}


public MatrixStore<Double> getX() {
return x;
}

public Primitive64Store getBSplineMatrix() {
public MatrixStore<Double> getBSplineMatrix() {
return BSplineMatrix;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class MatLabTest {
Expand Down Expand Up @@ -76,22 +77,39 @@ void diffTest() {
}

@Test
void greatEqualTest() {
void greaterOrEqualTest() {
PhysicalStore.Factory<Double, Primitive64Store> storeFactory = Primitive64Store.FACTORY;

Primitive64Store A = storeFactory.rows(new double[][]{{1, 12, 18, 7, 9, 11, 2, 15}});
Primitive64Store actual = MatLab.greaterOrEqual(A, 11);
double[][] expected = new double[][]{{0, 1, 1, 0, 0, 1, 0, 1}};

assertTrue(Arrays.deepEquals(expected, actual.toRawCopy2D()));
}

@Test
void sizeTest() {
PhysicalStore.Factory<Double, Primitive64Store> storeFactory = Primitive64Store.FACTORY;

Primitive64Store A = storeFactory.make(2, 3);
int actual = MatLab.size(A, 1);
int expected = 2;
assertEquals(expected, actual);


actual = MatLab.size(A, 2);
expected = 3;
assertEquals(expected, actual);

}

@Test
void linspaceTest() {
PhysicalStore.Factory<Double, Primitive64Store> storeFactory = Primitive64Store.FACTORY;

Primitive64Store linTest2 = MatLab.linspace(24.06, 25.08, 22);
Primitive64Store test = Primitive64Store.FACTORY.make(5, 5);
Primitive64Store actual = MatLab.linspace(-5, 5, 7);
double[][] expected = new double[][]{{-5.0, -3.333333333333333, -1.6666666666666665, 0.0, 1.666666666666667, 3.333333333333334, 5.0}};

assertTrue(Arrays.deepEquals(expected, actual.toRawCopy2D()));
}

@Test
Expand All @@ -111,20 +129,51 @@ void findTest() {
@Test
void anyTest() {
PhysicalStore.Factory<Double, Primitive64Store> storeFactory = Primitive64Store.FACTORY;

Primitive64Store A = storeFactory.rows(new double[][]{{0, 0, 3}, {0, 0, 3}, {0, 0, 3}});
Primitive64Store actual = MatLab.any(A, 1);
double[][] expected = new double[][]{{0, 0, 1}};

assertTrue(Arrays.deepEquals(expected, actual.toRawCopy2D()));

actual = MatLab.any(A, 2);
expected = new double[][]{{1}, {1}, {1}};

assertTrue(Arrays.deepEquals(expected, actual.toRawCopy2D()));

}

@Test
void rDivideTest() {
PhysicalStore.Factory<Double, Primitive64Store> storeFactory = Primitive64Store.FACTORY;

double[][] testArr = new double[][]{{8, 1, 6}, {3, 5, 7}, {4, 9, 2}};
Primitive64Store A = storeFactory.rows(testArr);

Primitive64Store actual = MatLab.rDivide(A, 5);
double[][] expected = new double[][]{{0.625, 5.0, 0.8333333333333334}, {1.6666666666666667, 1.0, 0.7142857142857143}, {1.25, 0.5555555555555556, 2.5}};

assertTrue(Arrays.deepEquals(expected, actual.toRawCopy2D()));
}

@Test
void maxTest() {
PhysicalStore.Factory<Double, Primitive64Store> storeFactory = Primitive64Store.FACTORY;

Primitive64Store A = storeFactory.rows(new double[][]{{1, 7, 3}, {6, 2, 9}});
Primitive64Store actual = MatLab.max(A, 5);
double[][] expected = new double[][]{{5, 7, 5}, {6, 5, 9}};
assertTrue(Arrays.deepEquals(expected, actual.toRawCopy2D()));
}

@Test
void diagTest() {
PhysicalStore.Factory<Double, Primitive64Store> storeFactory = Primitive64Store.FACTORY;

Primitive64Store A = storeFactory.rows(new double[][]{{2, 1, -1, -2, -5}});
Primitive64Store actual = MatLab.diag(A);
System.out.println(Arrays.deepToString(actual.toRawCopy2D()));
double[][] expected = new double[][]{{2, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, -1, 0, 0}, {0, 0, 0, -2, 0}, {0, 0, 0, 0, -5}};
assertTrue(Arrays.deepEquals(expected, actual.toRawCopy2D()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.ojalgo.matrix.store.MatrixStore;
import org.ojalgo.matrix.store.Primitive64Store;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;

class SplineBasisModelTest {

@BeforeEach
Expand All @@ -18,6 +23,10 @@ void tearDown() {
@Test
void bBase() {
Primitive64Store test = MatLab.linspace(204.83994725925928, 205.10565274074074, 1000);
Primitive64Store bBaseTest = SplineBasisModel.bBase(test, 204.83994725925928, 205.10565274074074, 22, 3);
Primitive64Store actual = SplineBasisModel.bBase(test, 204.83994725925928, 205.10565274074074, 22, 3);
MatrixStore<Double> expected = new SplineBasisModel(test, 22, 3).getBSplineMatrix();

assertTrue(Arrays.deepEquals(expected.toRawCopy2D(), actual.toRawCopy2D()));

}
}

0 comments on commit 5872285

Please sign in to comment.