/// <inheritdoc/> /// <exception cref="DimensionMismatchException"> if number of free parameters /// or orders do not match</exception> public DerivativeStructure linearCombination(DerivativeStructure[] a, DerivativeStructure[] b) { // compute an accurate value, taking care of cancellations double[] aDouble = new double[a.Length]; for (int i = 0; i < a.Length; ++i) { aDouble[i] = a[i].getValue(); } double[] bDouble = new double[b.Length]; for (int i = 0; i < b.Length; ++i) { bDouble[i] = b[i].getValue(); } double accurateValue = MathArrays.linearCombination(aDouble, bDouble); // compute a simple value, with all partial derivatives DerivativeStructure simpleValue = a[0].getField().getZero(); for (int i = 0; i < a.Length; ++i) { simpleValue = simpleValue.add(a[i].multiply(b[i])); } // create a result with accurate value and all derivatives (not necessarily as accurate as the value) double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return(new DerivativeStructure(simpleValue.getFreeParameters(), simpleValue.getOrder(), all)); }
/// <inheritdoc/> /// <exception cref="DimensionMismatchException"> if number of free parameters /// or orders do not match</exception> public DerivativeStructure linearCombination(double a1, DerivativeStructure b1, double a2, DerivativeStructure b2) { // compute an accurate value, taking care of cancellations double accurateValue = MathArrays.linearCombination(a1, b1.getValue(), a2, b2.getValue()); // compute a simple value, with all partial derivatives DerivativeStructure simpleValue = b1.multiply(a1).add(b2.multiply(a2)); // create a result with accurate value and all derivatives (not necessarily as accurate as the value) double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return(new DerivativeStructure(getFreeParameters(), getOrder(), all)); }