SumToOne { public double[] transform(final double[] fitParms) { ArgumentChecker.isTrue(fitParms.length == _n - 1, "length of fitParms is {}, but must be {} ", fitParms.length, _n - 1); final double[] s2 = new double[_n - 1]; final double[] c2 = new double[_n - 1]; for (int j = 0; j < _n - 1; j++) { double temp = Math.sin(fitParms[j]); temp *= temp; s2[j] = temp; c2[j] = 1.0 - temp; } final double[] res = new double[_n]; for (int i = 0; i < _n; i++) { double prod = 1.0; for (int j = 0; j < _n - 1; j++) { if (_set[i][j] == 1) { prod *= s2[j]; } else if (_set[i][j] == -1) { prod *= c2[j]; } } res[i] = prod; } return res; } SumToOne(final int n); double[] transform(final double[] fitParms); DoubleMatrix1D transform(final DoubleMatrix1D fitParms); double[] inverseTransform(final double[] modelParms); DoubleMatrix1D inverseTransform(final DoubleMatrix1D modelParms); double[][] jacobian(final double[] fitParms); DoubleMatrix2D jacobian(final DoubleMatrix1D fitParms);  }
@Test public void transformTest() { for (int n = 2; n < 13; n++) { double[] from = new double[n - 1]; for (int j = 0; j < n - 1; j++) { from[j] = RANDOM.nextDouble() * Math.PI / 2; } SumToOne trans = new SumToOne(n); DoubleMatrix1D to = trans.transform(new DoubleMatrix1D(from)); assertEquals(n, to.getNumberOfElements()); double sum = 0; for (int i = 0; i < n; i++) { sum += to.getEntry(i); } assertEquals("vector length " + n, 1.0, sum, 1e-9); } }