private static int getIndicator(double xValue, DoubleArray knots) { // check for 1 less interval than knots int lowerBound = FunctionUtils.getLowerBoundIndex(knots, xValue); return lowerBound == knots.size() - 1 ? lowerBound - 1 : lowerBound; }
@Test public void testTensorIndexTest2() { final int[] indices = new int[] {2, 3 }; final int[] dimensions = new int[] {5, 7 }; final int index = FunctionUtils.toTensorIndex(indices, dimensions); final int[] res = FunctionUtils.fromTensorIndex(index, dimensions); assertEquals(indices[0], res[0], 0); assertEquals(indices[1], res[1], 0); }
@Override public Double apply(final Double x) { return FunctionUtils.square((x - 50) / 50.0); } };
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullDimensions2() { FunctionUtils.fromTensorIndex(2, null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullDimensions1() { FunctionUtils.toTensorIndex(new int[] {1, 2, 3 }, null); }
@Test public void testCube() { for (int i = 0; i < 100; i++) { final double x = Math.random(); assertEquals(FunctionUtils.cube(x), x * x * x, EPS); } }
/** * Generate a set of N-dimensional b-splines as the produce of 1-dimensional b-splines with a given polynomial degree. * on the specified knots * @param knots holder for the knots and degree in each dimension * @return a List of functions */ public List<Function<double[], Double>> generateSet(BasisFunctionKnots[] knots) { ArgChecker.noNulls(knots, "knots"); int dim = knots.length; int[] nSplines = new int[dim]; int product = 1; List<List<Function<Double, Double>>> oneDSets = new ArrayList<>(dim); for (int i = 0; i < dim; i++) { oneDSets.add(generateSet(knots[i])); nSplines[i] = knots[i].getNumSplines(); product *= nSplines[i]; } final List<Function<double[], Double>> functions = new ArrayList<>(product); for (int i = 0; i < product; i++) { int[] indices = FunctionUtils.fromTensorIndex(i, nSplines); functions.add(generateMultiDim(oneDSets, indices)); } return functions; }
@Test(expectedExceptions = IllegalArgumentException.class) public void testOutOfBounds() { final int[] indices = new int[] {2, 7, 1 }; final int[] dimensions = new int[] {5, 7, 3 }; FunctionUtils.toTensorIndex(indices, dimensions); }
private static double evaluate( double xValue, DoubleArray knots, DoubleMatrix coefMatrix, int dimensions) { // check for 1 less interval than knots int lowerBound = FunctionUtils.getLowerBoundIndex(knots, xValue); int indicator = lowerBound == knots.size() - 1 ? lowerBound - 1 : lowerBound; DoubleArray coefs = coefMatrix.row(dimensions * indicator); return getValue(coefs.toArrayUnsafe(), xValue, knots.get(indicator)); }
private DoubleMatrix getAMatrix(double[][] funcMatrix, double[] invSigmaSqr) { int m = funcMatrix.length; int n = funcMatrix[0].length; double[][] a = new double[m][m]; for (int i = 0; i < m; i++) { double sum = 0; for (int k = 0; k < n; k++) { sum += FunctionUtils.square(funcMatrix[i][k]) * invSigmaSqr[k]; } a[i][i] = sum; for (int j = i + 1; j < m; j++) { sum = 0; for (int k = 0; k < n; k++) { sum += funcMatrix[i][k] * funcMatrix[j][k] * invSigmaSqr[k]; } a[i][j] = sum; a[j][i] = sum; } } return DoubleMatrix.copyOf(a); }
@Test public void testTensorIndexTest1() { final int[] indices = new int[] {2 }; final int[] dimensions = new int[] {5 }; final int index = FunctionUtils.toTensorIndex(indices, dimensions); assertEquals(indices[0], index, 0); final int[] res = FunctionUtils.fromTensorIndex(index, dimensions); assertEquals(indices[0], res[0], 0); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullIndices() { FunctionUtils.toTensorIndex(null, new int[] {1, 2, 3 }); }
private static double evaluate( double xValue, DoubleArray knots, DoubleMatrix coefMatrix, int dimensions) { // check for 1 less interval than knots int lowerBound = FunctionUtils.getLowerBoundIndex(knots, xValue); int indicator = lowerBound == knots.size() - 1 ? lowerBound - 1 : lowerBound; DoubleArray coefs = coefMatrix.row(dimensions * indicator); return getValue(coefs.toArrayUnsafe(), xValue, knots.get(indicator)); }
@Test public void testTensorIndexTest3() { final int[] indices = new int[] {2, 3, 1 }; final int[] dimensions = new int[] {5, 7, 3 }; final int index = FunctionUtils.toTensorIndex(indices, dimensions); final int[] res = FunctionUtils.fromTensorIndex(index, dimensions); assertEquals(indices[0], res[0], 0); assertEquals(indices[1], res[1], 0); assertEquals(indices[2], res[2], 0); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testWrongLength() { FunctionUtils.toTensorIndex(new int[] {1, 2 }, new int[] {1, 2, 3 }); }
private static double evaluate( double xValue, DoubleArray knots, DoubleMatrix coefMatrix, int dimensions) { // check for 1 less interval than knots int lowerBound = FunctionUtils.getLowerBoundIndex(knots, xValue); int indicator = lowerBound == knots.size() - 1 ? lowerBound - 1 : lowerBound; DoubleArray coefs = coefMatrix.row(dimensions * indicator); return getValue(coefs.toArrayUnsafe(), xValue, knots.get(indicator)); }
@Test public void testSquare() { for (int i = 0; i < 100; i++) { final double x = Math.random(); assertEquals(FunctionUtils.square(x), x * x, EPS); } }
@Test public void testTwoD() { BasisFunctionKnots knots1 = BasisFunctionKnots.fromInternalKnots(KNOTS, 2); BasisFunctionKnots knots2 = BasisFunctionKnots.fromInternalKnots(KNOTS, 3); List<Function<double[], Double>> set = GENERATOR.generateSet(new BasisFunctionKnots[] {knots1, knots2 }); //pick of one of the basis functions for testing int index = FunctionUtils.toTensorIndex(new int[] {3, 3 }, new int[] {knots1.getNumSplines(), knots2.getNumSplines() }); Function<double[], Double> func = set.get(index); assertEquals(1. / 3., func.apply(new double[] {2.0, 2.0 }), 0.0); assertEquals(1. / 2., func.apply(new double[] {2.5, 2.0 }), 0.0); assertEquals(1. / 8. / 48., func.apply(new double[] {1.5, 3.5 }), 0.0); assertEquals(0.0, func.apply(new double[] {4.0, 2.5 }), 0.0); }
private static double evaluate( double xValue, DoubleArray knots, DoubleMatrix coefMatrix, int dimensions) { // check for 1 less interval than knots int lowerBound = FunctionUtils.getLowerBoundIndex(knots, xValue); int indicator = lowerBound == knots.size() - 1 ? lowerBound - 1 : lowerBound; DoubleArray coefs = coefMatrix.row(dimensions * indicator); return getValue(coefs.toArrayUnsafe(), xValue, knots.get(indicator)); }