@Override public DoubleMatrix calculateJacobian(DoubleArray x) { ArgChecker.notNull(x, "x"); ArgChecker.isTrue( x.size() == getLengthOfDomain(), "Incorrect length of x. Is {} but should be {}", x.size(), getLengthOfDomain()); double[][] jac = new double[getLengthOfRange()][getLengthOfDomain()]; int posInput = 0; int pos1 = 0; int pos2 = 0; for (int i = 0; i < nPartitions; i++) { int nRows = yPartition[i]; int nCols = xPartition[i]; DoubleArray sub = x.subArray(posInput, posInput + nCols); DoubleMatrix subJac = functions[i].calculateJacobian(sub); if (nCols > 0) { for (int r = 0; r < nRows; r++) { System.arraycopy(subJac.toArrayUnsafe()[r], 0, jac[pos1++], pos2, nCols); } pos2 += nCols; } else { pos1 += nRows; } posInput += nCols; } return DoubleMatrix.copyOf(jac); }
@Test public void conCatTest() { DoubleArray cx = X[0].concat(X[1]).concat(X[2]); DoubleArray cyExp = Y_EXP[0].concat(Y_EXP[1]).concat(Y_EXP[2]); ConcatenatedVectorFunction cf = new ConcatenatedVectorFunction(F); DoubleArray cy = cf.apply(cx); AssertMatrix.assertEqualsVectors(cyExp, cy, 1e-15); DoubleMatrix cJac = cf.calculateJacobian(cx); DoubleMatrix fdJac = DIFF.differentiate(cf).apply(cx); AssertMatrix.assertEqualsMatrix(fdJac, cJac, 1e-10); }
@Override public DoubleArray apply(DoubleArray x) { ArgChecker.notNull(x, "x"); ArgChecker.isTrue( x.size() == getLengthOfDomain(), "Incorrect length of x. Is {} but should be {}", x.size(), getLengthOfDomain()); double[] y = new double[getLengthOfRange()]; int posInput = 0; int posOutput = 0; //evaluate each function (with the appropriate sub vector) and concatenate the results for (int i = 0; i < nPartitions; i++) { int length = xPartition[i]; DoubleArray sub = x.subArray(posInput, posInput + length); DoubleArray eval = functions[i].apply(sub); eval.copyInto(y, posOutput); posInput += length; posOutput += eval.size(); } return DoubleArray.copyOf(y); }