@Test public void testPolyval() throws Exception { final double[] x = {0, 1, 2, 3}; final double[] y = {0, 1, 2, 3}; final double[] coeffsTemplate = MathUtils.increment(60, 1, 1); final double[][] values_EXPECTED = {{1, 6, 11, 16}, {1, 21, 71, 151}, {1, 55, 343, 1069}, {1, 120, 1383, 6334}, {1, 231, 4935, 33307}, {1, 406, 16135, 160882}, {1, 666, 49415, 729502}, {1, 1035, 143879, 3150511}, {1, 1540, 402439, 13090426}}; for (int degree = 1; degree < 10; degree++) { int numCoefs = PolyUtils.numberOfCoefficients(degree); double[] coeffs = new double[numCoefs]; System.arraycopy(coeffsTemplate, 0, coeffs, 0, numCoefs); double value_ACTUAL; for (int i = 0; i < x.length; i++) { value_ACTUAL = PolyUtils.polyval(x[i], y[i], new DoubleMatrix(coeffs), degree); Assert.assertEquals(values_EXPECTED[degree - 1][i], value_ACTUAL, DELTA_01); } } }
public static double[][] polyval(final double[] x, final double[] y, final double coeff[], int degree) { setLoggerLevel(); if (degree < -1) { logger.warn("polyValGrid: degree < -1 ????"); } if (x.length > y.length) { logger.warn("polValGrid: x larger than y, while optimized for y larger x"); } if (degree == -1) { degree = degreeFromCoefficients(coeff.length); } double[][] result = new double[x.length][y.length]; int i; int j; double c00, c10, c01, c20, c11, c02, c30, c21, c12, c03, c40, c31, c22, c13, c04, c50, c41, c32, c23, c14, c05; int columns = result[0].length; int rows = result.length; switch (degree) { case 0: result[0][0] = coeff[0]; break; case 1: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; for (j = 0; j < columns; j++) { double c00pc01y1 = c00 + c01 * y[j]; for (i = 0; i < rows; i++) { result[i][j] = c00pc01y1 + c10 * x[i]; } } break; case 2: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; for (j = 0; j < columns; j++) { double y1 = y[j]; double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * Math.pow(y1, 2); double c11y1 = c11 * y1; for (i = 0; i < rows; i++) { double x1 = x[i]; result[i][j] = c00pc01y1 + c10 * x1 + c20 * Math.pow(x1, 2) + c11y1 * x1 + c02y2; } } break; case 3: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; c30 = coeff[6]; c21 = coeff[7]; c12 = coeff[8]; c03 = coeff[9]; for (j = 0; j < columns; j++) { double y1 = y[j]; double y2 = Math.pow(y1, 2); double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * y2; double c11y1 = c11 * y1; double c21y1 = c21 * y1; double c12y2 = c12 * y2; double c03y3 = c03 * y1 * y2; for (i = 0; i < rows; i++) { double x1 = x[i]; double x2 = Math.pow(x1, 2); result[i][j] = c00pc01y1 + c10 * x1 + c20 * x2 + c11y1 * x1 + c02y2 + c30 * x1 * x2 + c21y1 * x2 + c12y2 * x1 + c03y3; } } break; case 4: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; c30 = coeff[6]; c21 = coeff[7]; c12 = coeff[8]; c03 = coeff[9]; c40 = coeff[10]; c31 = coeff[11]; c22 = coeff[12]; c13 = coeff[13]; c04 = coeff[14]; for (j = 0; j < columns; j++) { double y1 = y[j]; double y2 = Math.pow(y1, 2); double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * y2; double c11y1 = c11 * y1; double c21y1 = c21 * y1; double c12y2 = c12 * y2; double c03y3 = c03 * y1 * y2; double c31y1 = c31 * y1; double c22y2 = c22 * y2; double c13y3 = c13 * y2 * y1; double c04y4 = c04 * y2 * y2; for (i = 0; i < rows; i++) { double x1 = x[i]; double x2 = Math.pow(x1, 2); result[i][j] = c00pc01y1 + c10 * x1 + c20 * x2 + c11y1 * x1 + c02y2 + c30 * x1 * x2 + c21y1 * x2 + c12y2 * x1 + c03y3 + c40 * x2 * x2 + c31y1 * x2 * x1 + c22y2 * x2 + c13y3 * x1 + c04y4; } } break; case 5: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; c30 = coeff[6]; c21 = coeff[7]; c12 = coeff[8]; c03 = coeff[9]; c40 = coeff[10]; c31 = coeff[11]; c22 = coeff[12]; c13 = coeff[13]; c04 = coeff[14]; c50 = coeff[15]; c41 = coeff[16]; c32 = coeff[17]; c23 = coeff[18]; c14 = coeff[19]; c05 = coeff[20]; for (j = 0; j < columns; j++) { double y1 = y[j]; double y2 = Math.pow(y1, 2); double y3 = y2 * y1; double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * y2; double c11y1 = c11 * y1; double c21y1 = c21 * y1; double c12y2 = c12 * y2; double c03y3 = c03 * y3; double c31y1 = c31 * y1; double c22y2 = c22 * y2; double c13y3 = c13 * y3; double c04y4 = c04 * y2 * y2; double c41y1 = c41 * y1; double c32y2 = c32 * y2; double c23y3 = c23 * y3; double c14y4 = c14 * y2 * y2; double c05y5 = c05 * y3 * y2; for (i = 0; i < rows; i++) { double x1 = x[i]; double x2 = Math.pow(x1, 2); double x3 = x1 * x2; result[i][j] = c00pc01y1 + c10 * x1 + c20 * x2 + c11y1 * x1 + c02y2 + c30 * x3 + c21y1 * x2 + c12y2 * x1 + c03y3 + c40 * x2 * x2 + c31y1 * x3 + c22y2 * x2 + c13y3 * x1 + c04y4 + c50 * x3 * x2 + c41y1 * x2 * x2 + c32y2 * x3 + c23y3 * x2 + c14y4 * x1 + c05y5; } } break; default: for (j = 0; j < columns; j++) { double yy = y[j]; for (i = 0; i < rows; i++) { double xx = x[i]; result[i][j] = polyval(xx, yy, coeff, degree); } } } return result; }
PolyUtils { public static double[][] polyval(final double[] x, final double[] y, final double coeff[], int degree) { setLoggerLevel(); if (degree < -1) { logger.warn("polyValGrid: degree < -1 ????"); } if (x.length > y.length) { logger.warn("polValGrid: x larger than y, while optimized for y larger x"); } if (degree == -1) { degree = degreeFromCoefficients(coeff.length); } double[][] result = new double[x.length][y.length]; int i; int j; double c00, c10, c01, c20, c11, c02, c30, c21, c12, c03, c40, c31, c22, c13, c04, c50, c41, c32, c23, c14, c05; int columns = result[0].length; int rows = result.length; switch (degree) { case 0: result[0][0] = coeff[0]; break; case 1: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; for (j = 0; j < columns; j++) { double c00pc01y1 = c00 + c01 * y[j]; for (i = 0; i < rows; i++) { result[i][j] = c00pc01y1 + c10 * x[i]; } } break; case 2: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; for (j = 0; j < columns; j++) { double y1 = y[j]; double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * Math.pow(y1, 2); double c11y1 = c11 * y1; for (i = 0; i < rows; i++) { double x1 = x[i]; result[i][j] = c00pc01y1 + c10 * x1 + c20 * Math.pow(x1, 2) + c11y1 * x1 + c02y2; } } break; case 3: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; c30 = coeff[6]; c21 = coeff[7]; c12 = coeff[8]; c03 = coeff[9]; for (j = 0; j < columns; j++) { double y1 = y[j]; double y2 = Math.pow(y1, 2); double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * y2; double c11y1 = c11 * y1; double c21y1 = c21 * y1; double c12y2 = c12 * y2; double c03y3 = c03 * y1 * y2; for (i = 0; i < rows; i++) { double x1 = x[i]; double x2 = Math.pow(x1, 2); result[i][j] = c00pc01y1 + c10 * x1 + c20 * x2 + c11y1 * x1 + c02y2 + c30 * x1 * x2 + c21y1 * x2 + c12y2 * x1 + c03y3; } } break; case 4: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; c30 = coeff[6]; c21 = coeff[7]; c12 = coeff[8]; c03 = coeff[9]; c40 = coeff[10]; c31 = coeff[11]; c22 = coeff[12]; c13 = coeff[13]; c04 = coeff[14]; for (j = 0; j < columns; j++) { double y1 = y[j]; double y2 = Math.pow(y1, 2); double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * y2; double c11y1 = c11 * y1; double c21y1 = c21 * y1; double c12y2 = c12 * y2; double c03y3 = c03 * y1 * y2; double c31y1 = c31 * y1; double c22y2 = c22 * y2; double c13y3 = c13 * y2 * y1; double c04y4 = c04 * y2 * y2; for (i = 0; i < rows; i++) { double x1 = x[i]; double x2 = Math.pow(x1, 2); result[i][j] = c00pc01y1 + c10 * x1 + c20 * x2 + c11y1 * x1 + c02y2 + c30 * x1 * x2 + c21y1 * x2 + c12y2 * x1 + c03y3 + c40 * x2 * x2 + c31y1 * x2 * x1 + c22y2 * x2 + c13y3 * x1 + c04y4; } } break; case 5: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; c30 = coeff[6]; c21 = coeff[7]; c12 = coeff[8]; c03 = coeff[9]; c40 = coeff[10]; c31 = coeff[11]; c22 = coeff[12]; c13 = coeff[13]; c04 = coeff[14]; c50 = coeff[15]; c41 = coeff[16]; c32 = coeff[17]; c23 = coeff[18]; c14 = coeff[19]; c05 = coeff[20]; for (j = 0; j < columns; j++) { double y1 = y[j]; double y2 = Math.pow(y1, 2); double y3 = y2 * y1; double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * y2; double c11y1 = c11 * y1; double c21y1 = c21 * y1; double c12y2 = c12 * y2; double c03y3 = c03 * y3; double c31y1 = c31 * y1; double c22y2 = c22 * y2; double c13y3 = c13 * y3; double c04y4 = c04 * y2 * y2; double c41y1 = c41 * y1; double c32y2 = c32 * y2; double c23y3 = c23 * y3; double c14y4 = c14 * y2 * y2; double c05y5 = c05 * y3 * y2; for (i = 0; i < rows; i++) { double x1 = x[i]; double x2 = Math.pow(x1, 2); double x3 = x1 * x2; result[i][j] = c00pc01y1 + c10 * x1 + c20 * x2 + c11y1 * x1 + c02y2 + c30 * x3 + c21y1 * x2 + c12y2 * x1 + c03y3 + c40 * x2 * x2 + c31y1 * x3 + c22y2 * x2 + c13y3 * x1 + c04y4 + c50 * x3 * x2 + c41y1 * x2 * x2 + c32y2 * x3 + c23y3 * x2 + c14y4 * x1 + c05y5; } } break; default: for (j = 0; j < columns; j++) { double yy = y[j]; for (i = 0; i < rows; i++) { double xx = x[i]; result[i][j] = polyval(xx, yy, coeff, degree); } } } return result; } }
PolyUtils { public static double[][] polyval(final double[] x, final double[] y, final double coeff[], int degree) { setLoggerLevel(); if (degree < -1) { logger.warn("polyValGrid: degree < -1 ????"); } if (x.length > y.length) { logger.warn("polValGrid: x larger than y, while optimized for y larger x"); } if (degree == -1) { degree = degreeFromCoefficients(coeff.length); } double[][] result = new double[x.length][y.length]; int i; int j; double c00, c10, c01, c20, c11, c02, c30, c21, c12, c03, c40, c31, c22, c13, c04, c50, c41, c32, c23, c14, c05; int columns = result[0].length; int rows = result.length; switch (degree) { case 0: result[0][0] = coeff[0]; break; case 1: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; for (j = 0; j < columns; j++) { double c00pc01y1 = c00 + c01 * y[j]; for (i = 0; i < rows; i++) { result[i][j] = c00pc01y1 + c10 * x[i]; } } break; case 2: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; for (j = 0; j < columns; j++) { double y1 = y[j]; double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * Math.pow(y1, 2); double c11y1 = c11 * y1; for (i = 0; i < rows; i++) { double x1 = x[i]; result[i][j] = c00pc01y1 + c10 * x1 + c20 * Math.pow(x1, 2) + c11y1 * x1 + c02y2; } } break; case 3: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; c30 = coeff[6]; c21 = coeff[7]; c12 = coeff[8]; c03 = coeff[9]; for (j = 0; j < columns; j++) { double y1 = y[j]; double y2 = Math.pow(y1, 2); double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * y2; double c11y1 = c11 * y1; double c21y1 = c21 * y1; double c12y2 = c12 * y2; double c03y3 = c03 * y1 * y2; for (i = 0; i < rows; i++) { double x1 = x[i]; double x2 = Math.pow(x1, 2); result[i][j] = c00pc01y1 + c10 * x1 + c20 * x2 + c11y1 * x1 + c02y2 + c30 * x1 * x2 + c21y1 * x2 + c12y2 * x1 + c03y3; } } break; case 4: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; c30 = coeff[6]; c21 = coeff[7]; c12 = coeff[8]; c03 = coeff[9]; c40 = coeff[10]; c31 = coeff[11]; c22 = coeff[12]; c13 = coeff[13]; c04 = coeff[14]; for (j = 0; j < columns; j++) { double y1 = y[j]; double y2 = Math.pow(y1, 2); double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * y2; double c11y1 = c11 * y1; double c21y1 = c21 * y1; double c12y2 = c12 * y2; double c03y3 = c03 * y1 * y2; double c31y1 = c31 * y1; double c22y2 = c22 * y2; double c13y3 = c13 * y2 * y1; double c04y4 = c04 * y2 * y2; for (i = 0; i < rows; i++) { double x1 = x[i]; double x2 = Math.pow(x1, 2); result[i][j] = c00pc01y1 + c10 * x1 + c20 * x2 + c11y1 * x1 + c02y2 + c30 * x1 * x2 + c21y1 * x2 + c12y2 * x1 + c03y3 + c40 * x2 * x2 + c31y1 * x2 * x1 + c22y2 * x2 + c13y3 * x1 + c04y4; } } break; case 5: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; c30 = coeff[6]; c21 = coeff[7]; c12 = coeff[8]; c03 = coeff[9]; c40 = coeff[10]; c31 = coeff[11]; c22 = coeff[12]; c13 = coeff[13]; c04 = coeff[14]; c50 = coeff[15]; c41 = coeff[16]; c32 = coeff[17]; c23 = coeff[18]; c14 = coeff[19]; c05 = coeff[20]; for (j = 0; j < columns; j++) { double y1 = y[j]; double y2 = Math.pow(y1, 2); double y3 = y2 * y1; double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * y2; double c11y1 = c11 * y1; double c21y1 = c21 * y1; double c12y2 = c12 * y2; double c03y3 = c03 * y3; double c31y1 = c31 * y1; double c22y2 = c22 * y2; double c13y3 = c13 * y3; double c04y4 = c04 * y2 * y2; double c41y1 = c41 * y1; double c32y2 = c32 * y2; double c23y3 = c23 * y3; double c14y4 = c14 * y2 * y2; double c05y5 = c05 * y3 * y2; for (i = 0; i < rows; i++) { double x1 = x[i]; double x2 = Math.pow(x1, 2); double x3 = x1 * x2; result[i][j] = c00pc01y1 + c10 * x1 + c20 * x2 + c11y1 * x1 + c02y2 + c30 * x3 + c21y1 * x2 + c12y2 * x1 + c03y3 + c40 * x2 * x2 + c31y1 * x3 + c22y2 * x2 + c13y3 * x1 + c04y4 + c50 * x3 * x2 + c41y1 * x2 * x2 + c32y2 * x3 + c23y3 * x2 + c14y4 * x1 + c05y5; } } break; default: for (j = 0; j < columns; j++) { double yy = y[j]; for (i = 0; i < rows; i++) { double xx = x[i]; result[i][j] = polyval(xx, yy, coeff, degree); } } } return result; }  }
PolyUtils { public static double[][] polyval(final double[] x, final double[] y, final double coeff[], int degree) { setLoggerLevel(); if (degree < -1) { logger.warn("polyValGrid: degree < -1 ????"); } if (x.length > y.length) { logger.warn("polValGrid: x larger than y, while optimized for y larger x"); } if (degree == -1) { degree = degreeFromCoefficients(coeff.length); } double[][] result = new double[x.length][y.length]; int i; int j; double c00, c10, c01, c20, c11, c02, c30, c21, c12, c03, c40, c31, c22, c13, c04, c50, c41, c32, c23, c14, c05; int columns = result[0].length; int rows = result.length; switch (degree) { case 0: result[0][0] = coeff[0]; break; case 1: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; for (j = 0; j < columns; j++) { double c00pc01y1 = c00 + c01 * y[j]; for (i = 0; i < rows; i++) { result[i][j] = c00pc01y1 + c10 * x[i]; } } break; case 2: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; for (j = 0; j < columns; j++) { double y1 = y[j]; double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * Math.pow(y1, 2); double c11y1 = c11 * y1; for (i = 0; i < rows; i++) { double x1 = x[i]; result[i][j] = c00pc01y1 + c10 * x1 + c20 * Math.pow(x1, 2) + c11y1 * x1 + c02y2; } } break; case 3: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; c30 = coeff[6]; c21 = coeff[7]; c12 = coeff[8]; c03 = coeff[9]; for (j = 0; j < columns; j++) { double y1 = y[j]; double y2 = Math.pow(y1, 2); double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * y2; double c11y1 = c11 * y1; double c21y1 = c21 * y1; double c12y2 = c12 * y2; double c03y3 = c03 * y1 * y2; for (i = 0; i < rows; i++) { double x1 = x[i]; double x2 = Math.pow(x1, 2); result[i][j] = c00pc01y1 + c10 * x1 + c20 * x2 + c11y1 * x1 + c02y2 + c30 * x1 * x2 + c21y1 * x2 + c12y2 * x1 + c03y3; } } break; case 4: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; c30 = coeff[6]; c21 = coeff[7]; c12 = coeff[8]; c03 = coeff[9]; c40 = coeff[10]; c31 = coeff[11]; c22 = coeff[12]; c13 = coeff[13]; c04 = coeff[14]; for (j = 0; j < columns; j++) { double y1 = y[j]; double y2 = Math.pow(y1, 2); double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * y2; double c11y1 = c11 * y1; double c21y1 = c21 * y1; double c12y2 = c12 * y2; double c03y3 = c03 * y1 * y2; double c31y1 = c31 * y1; double c22y2 = c22 * y2; double c13y3 = c13 * y2 * y1; double c04y4 = c04 * y2 * y2; for (i = 0; i < rows; i++) { double x1 = x[i]; double x2 = Math.pow(x1, 2); result[i][j] = c00pc01y1 + c10 * x1 + c20 * x2 + c11y1 * x1 + c02y2 + c30 * x1 * x2 + c21y1 * x2 + c12y2 * x1 + c03y3 + c40 * x2 * x2 + c31y1 * x2 * x1 + c22y2 * x2 + c13y3 * x1 + c04y4; } } break; case 5: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; c30 = coeff[6]; c21 = coeff[7]; c12 = coeff[8]; c03 = coeff[9]; c40 = coeff[10]; c31 = coeff[11]; c22 = coeff[12]; c13 = coeff[13]; c04 = coeff[14]; c50 = coeff[15]; c41 = coeff[16]; c32 = coeff[17]; c23 = coeff[18]; c14 = coeff[19]; c05 = coeff[20]; for (j = 0; j < columns; j++) { double y1 = y[j]; double y2 = Math.pow(y1, 2); double y3 = y2 * y1; double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * y2; double c11y1 = c11 * y1; double c21y1 = c21 * y1; double c12y2 = c12 * y2; double c03y3 = c03 * y3; double c31y1 = c31 * y1; double c22y2 = c22 * y2; double c13y3 = c13 * y3; double c04y4 = c04 * y2 * y2; double c41y1 = c41 * y1; double c32y2 = c32 * y2; double c23y3 = c23 * y3; double c14y4 = c14 * y2 * y2; double c05y5 = c05 * y3 * y2; for (i = 0; i < rows; i++) { double x1 = x[i]; double x2 = Math.pow(x1, 2); double x3 = x1 * x2; result[i][j] = c00pc01y1 + c10 * x1 + c20 * x2 + c11y1 * x1 + c02y2 + c30 * x3 + c21y1 * x2 + c12y2 * x1 + c03y3 + c40 * x2 * x2 + c31y1 * x3 + c22y2 * x2 + c13y3 * x1 + c04y4 + c50 * x3 * x2 + c41y1 * x2 * x2 + c32y2 * x3 + c23y3 * x2 + c14y4 * x1 + c05y5; } } break; default: for (j = 0; j < columns; j++) { double yy = y[j]; for (i = 0; i < rows; i++) { double xx = x[i]; result[i][j] = polyval(xx, yy, coeff, degree); } } } return result; }  static double normalize2(double data, final int min, final int max); static double normalize2(double data, final double min, final double max); static DoubleMatrix normalize(DoubleMatrix t); static int degreeFromCoefficients(int numOfCoefficients); static int numberOfCoefficients(final int degree); static double[] polyFitNormalized(DoubleMatrix t, DoubleMatrix y, final int degree); static double[] polyFit2D(final DoubleMatrix x, final DoubleMatrix y, final DoubleMatrix z, final int degree); static double[] polyFit(DoubleMatrix t, DoubleMatrix y, final int degree); static double polyVal1D(double x, double[] coeffs); static double[][] polyval(final double[] x, final double[] y, final double coeff[], int degree); static DoubleMatrix polyval(final DoubleMatrix x, final DoubleMatrix y, final DoubleMatrix coeff, int degree); static double polyval(final double x, final double y, final DoubleMatrix coeff, int degree); static double polyval(final double x, final double y, final double[] coeff); static double polyval(final double x, final double y, final double[] coeff, int degree); }
PolyUtils { public static double[][] polyval(final double[] x, final double[] y, final double coeff[], int degree) { setLoggerLevel(); if (degree < -1) { logger.warn("polyValGrid: degree < -1 ????"); } if (x.length > y.length) { logger.warn("polValGrid: x larger than y, while optimized for y larger x"); } if (degree == -1) { degree = degreeFromCoefficients(coeff.length); } double[][] result = new double[x.length][y.length]; int i; int j; double c00, c10, c01, c20, c11, c02, c30, c21, c12, c03, c40, c31, c22, c13, c04, c50, c41, c32, c23, c14, c05; int columns = result[0].length; int rows = result.length; switch (degree) { case 0: result[0][0] = coeff[0]; break; case 1: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; for (j = 0; j < columns; j++) { double c00pc01y1 = c00 + c01 * y[j]; for (i = 0; i < rows; i++) { result[i][j] = c00pc01y1 + c10 * x[i]; } } break; case 2: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; for (j = 0; j < columns; j++) { double y1 = y[j]; double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * Math.pow(y1, 2); double c11y1 = c11 * y1; for (i = 0; i < rows; i++) { double x1 = x[i]; result[i][j] = c00pc01y1 + c10 * x1 + c20 * Math.pow(x1, 2) + c11y1 * x1 + c02y2; } } break; case 3: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; c30 = coeff[6]; c21 = coeff[7]; c12 = coeff[8]; c03 = coeff[9]; for (j = 0; j < columns; j++) { double y1 = y[j]; double y2 = Math.pow(y1, 2); double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * y2; double c11y1 = c11 * y1; double c21y1 = c21 * y1; double c12y2 = c12 * y2; double c03y3 = c03 * y1 * y2; for (i = 0; i < rows; i++) { double x1 = x[i]; double x2 = Math.pow(x1, 2); result[i][j] = c00pc01y1 + c10 * x1 + c20 * x2 + c11y1 * x1 + c02y2 + c30 * x1 * x2 + c21y1 * x2 + c12y2 * x1 + c03y3; } } break; case 4: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; c30 = coeff[6]; c21 = coeff[7]; c12 = coeff[8]; c03 = coeff[9]; c40 = coeff[10]; c31 = coeff[11]; c22 = coeff[12]; c13 = coeff[13]; c04 = coeff[14]; for (j = 0; j < columns; j++) { double y1 = y[j]; double y2 = Math.pow(y1, 2); double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * y2; double c11y1 = c11 * y1; double c21y1 = c21 * y1; double c12y2 = c12 * y2; double c03y3 = c03 * y1 * y2; double c31y1 = c31 * y1; double c22y2 = c22 * y2; double c13y3 = c13 * y2 * y1; double c04y4 = c04 * y2 * y2; for (i = 0; i < rows; i++) { double x1 = x[i]; double x2 = Math.pow(x1, 2); result[i][j] = c00pc01y1 + c10 * x1 + c20 * x2 + c11y1 * x1 + c02y2 + c30 * x1 * x2 + c21y1 * x2 + c12y2 * x1 + c03y3 + c40 * x2 * x2 + c31y1 * x2 * x1 + c22y2 * x2 + c13y3 * x1 + c04y4; } } break; case 5: c00 = coeff[0]; c10 = coeff[1]; c01 = coeff[2]; c20 = coeff[3]; c11 = coeff[4]; c02 = coeff[5]; c30 = coeff[6]; c21 = coeff[7]; c12 = coeff[8]; c03 = coeff[9]; c40 = coeff[10]; c31 = coeff[11]; c22 = coeff[12]; c13 = coeff[13]; c04 = coeff[14]; c50 = coeff[15]; c41 = coeff[16]; c32 = coeff[17]; c23 = coeff[18]; c14 = coeff[19]; c05 = coeff[20]; for (j = 0; j < columns; j++) { double y1 = y[j]; double y2 = Math.pow(y1, 2); double y3 = y2 * y1; double c00pc01y1 = c00 + c01 * y1; double c02y2 = c02 * y2; double c11y1 = c11 * y1; double c21y1 = c21 * y1; double c12y2 = c12 * y2; double c03y3 = c03 * y3; double c31y1 = c31 * y1; double c22y2 = c22 * y2; double c13y3 = c13 * y3; double c04y4 = c04 * y2 * y2; double c41y1 = c41 * y1; double c32y2 = c32 * y2; double c23y3 = c23 * y3; double c14y4 = c14 * y2 * y2; double c05y5 = c05 * y3 * y2; for (i = 0; i < rows; i++) { double x1 = x[i]; double x2 = Math.pow(x1, 2); double x3 = x1 * x2; result[i][j] = c00pc01y1 + c10 * x1 + c20 * x2 + c11y1 * x1 + c02y2 + c30 * x3 + c21y1 * x2 + c12y2 * x1 + c03y3 + c40 * x2 * x2 + c31y1 * x3 + c22y2 * x2 + c13y3 * x1 + c04y4 + c50 * x3 * x2 + c41y1 * x2 * x2 + c32y2 * x3 + c23y3 * x2 + c14y4 * x1 + c05y5; } } break; default: for (j = 0; j < columns; j++) { double yy = y[j]; for (i = 0; i < rows; i++) { double xx = x[i]; result[i][j] = polyval(xx, yy, coeff, degree); } } } return result; }  static double normalize2(double data, final int min, final int max); static double normalize2(double data, final double min, final double max); static DoubleMatrix normalize(DoubleMatrix t); static int degreeFromCoefficients(int numOfCoefficients); static int numberOfCoefficients(final int degree); static double[] polyFitNormalized(DoubleMatrix t, DoubleMatrix y, final int degree); static double[] polyFit2D(final DoubleMatrix x, final DoubleMatrix y, final DoubleMatrix z, final int degree); static double[] polyFit(DoubleMatrix t, DoubleMatrix y, final int degree); static double polyVal1D(double x, double[] coeffs); static double[][] polyval(final double[] x, final double[] y, final double coeff[], int degree); static DoubleMatrix polyval(final DoubleMatrix x, final DoubleMatrix y, final DoubleMatrix coeff, int degree); static double polyval(final double x, final double y, final DoubleMatrix coeff, int degree); static double polyval(final double x, final double y, final double[] coeff); static double polyval(final double x, final double y, final double[] coeff, int degree);  }