/** Compute composition of the instance by a univariate function. * @param f array of value and derivatives of the function at * the current point (i.e. [f({@link #getValue()}), * f'({@link #getValue()}), f''({@link #getValue()})...]). * @return f(this) * @exception DimensionMismatchException if the number of derivatives * in the array is not equal to {@link #getOrder() order} + 1 */ public DerivativeStructure compose(final double ... f) throws DimensionMismatchException { if (f.length != getOrder() + 1) { throw new DimensionMismatchException(f.length, getOrder() + 1); } final DerivativeStructure result = new DerivativeStructure(compiler); compiler.compose(data, 0, f, result.data, 0); return result; }
/** * Get a hashCode for the derivative structure. * @return a hash code value for this object * @since 3.2 */ @Override public int hashCode() { return 227 + 229 * getFreeParameters() + 233 * getOrder() + 239 * MathUtils.hash(data); }
/** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return new DerivativeStructure(t.getFreeParameters(), t.getOrder(), c); }
/** Create a constant compatible with instance order and number of parameters. * <p> * This method is a convenience factory method, it simply calls * {@code new DerivativeStructure(getFreeParameters(), getOrder(), c)} * </p> * @param c value of the constant * @return a constant compatible with instance order and number of parameters * @see #DerivativeStructure(int, int, double) * @since 3.3 */ public DerivativeStructure createConstant(final double c) { return new DerivativeStructure(getFreeParameters(), getOrder(), c); }
/** * Test for the equality of two derivative structures. * <p> * Derivative structures are considered equal if they have the same number * of free parameters, the same derivation order, and the same derivatives. * </p> * @param other Object to test for equality to this * @return true if two derivative structures are equal * @since 3.2 */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof DerivativeStructure) { final DerivativeStructure rhs = (DerivativeStructure)other; return (getFreeParameters() == rhs.getFreeParameters()) && (getOrder() == rhs.getOrder()) && MathArrays.equals(data, rhs.data); } return false; }
/** {@inheritDoc} */ public DerivativeStructure value(final DerivativeStructure t) throws MathIllegalArgumentException { // check we can achieve the requested derivation order with the sample if (t.getOrder() >= nbPoints) { throw new NumberIsTooLargeException(t.getOrder(), nbPoints, false); } // compute sample position, trying to be centered if possible final double t0 = FastMath.max(FastMath.min(t.getValue(), tMax), tMin) - halfSampleSpan; // compute sample points final double[] y = new double[nbPoints]; for (int i = 0; i < nbPoints; ++i) { y[i] = function.value(t0 + i * stepSize); } // evaluate derivatives return evaluate(t, t0, y); }
/** {@inheritDoc} */ public DerivativeStructure[] value(final DerivativeStructure t) throws MathIllegalArgumentException { // check we can achieve the requested derivation order with the sample if (t.getOrder() >= nbPoints) { throw new NumberIsTooLargeException(t.getOrder(), nbPoints, false); } // compute sample position, trying to be centered if possible final double t0 = FastMath.max(FastMath.min(t.getValue(), tMax), tMin) - halfSampleSpan; // compute sample points double[][] y = null; for (int i = 0; i < nbPoints; ++i) { final double[] v = function.value(t0 + i * stepSize); if (i == 0) { y = new double[v.length][nbPoints]; } for (int j = 0; j < v.length; ++j) { y[j][i] = v[j]; } } // evaluate derivatives final DerivativeStructure[] value = new DerivativeStructure[y.length]; for (int j = 0; j < value.length; ++j) { value[j] = evaluate(t, t0, y[j]); } return value; }
/** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) throws DimensionMismatchException { final double x = t.getValue(); double[] f = new double[t.getOrder() + 1]; final double alpha = omega * x + phase; f[0] = amplitude * FastMath.cos(alpha); if (f.length > 1) { f[1] = -amplitude * omega * FastMath.sin(alpha); final double mo2 = - omega * omega; for (int i = 2; i < f.length; ++i) { f[i] = mo2 * f[i - 2]; } } return t.compose(f); }
/** * {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { verifyInputArray(a, c); final int n = c.length; DerivativeStructure value = new DerivativeStructure(t.getFreeParameters(), t.getOrder(), a[n]); for (int i = n - 1; i >= 0; i--) { value = t.subtract(c[i]).multiply(value).add(a[i]); } return value; }
/** {@inheritDoc} * @since 3.1 * @throws NoDataException if {@code coefficients} is empty. * @throws NullArgumentException if {@code coefficients} is {@code null}. */ public DerivativeStructure value(final DerivativeStructure t) throws NullArgumentException, NoDataException { MathUtils.checkNotNull(coefficients); int n = coefficients.length; if (n == 0) { throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY); } DerivativeStructure result = new DerivativeStructure(t.getFreeParameters(), t.getOrder(), coefficients[n - 1]); for (int j = n - 2; j >= 0; j--) { result = result.multiply(t).add(coefficients[j]); } return result; }
/** {@inheritDoc} * @exception NumberIsTooLargeException if derivation order is greater than 1 */ public DerivativeStructure value(final DerivativeStructure t) throws NumberIsTooLargeException { switch (t.getOrder()) { case 0 : return new DerivativeStructure(t.getFreeParameters(), 0, f.value(t.getValue())); case 1 : { final int parameters = t.getFreeParameters(); final double[] derivatives = new double[parameters + 1]; derivatives[0] = f.value(t.getValue()); final double fPrime = f.derivative().value(t.getValue()); int[] orders = new int[parameters]; for (int i = 0; i < parameters; ++i) { orders[i] = 1; derivatives[i + 1] = fPrime * t.getPartialDerivative(orders); orders[i] = 0; } return new DerivativeStructure(parameters, 1, derivatives); } default : throw new NumberIsTooLargeException(t.getOrder(), 1, true); } }
/** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure linearCombination(final double a1, final DerivativeStructure b1, final double a2, final DerivativeStructure b2) throws DimensionMismatchException { // compute an accurate value, taking care of cancellations final double accurateValue = MathArrays.linearCombination(a1, b1.getValue(), a2, b2.getValue()); // compute a simple value, with all partial derivatives final 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) final double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return new DerivativeStructure(getFreeParameters(), getOrder(), all); }
/** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure linearCombination(final double[] a, final DerivativeStructure[] b) throws DimensionMismatchException { // compute an accurate value, taking care of cancellations final double[] bDouble = new double[b.length]; for (int i = 0; i < b.length; ++i) { bDouble[i] = b[i].getValue(); } final double accurateValue = MathArrays.linearCombination(a, bDouble); // compute a simple value, with all partial derivatives DerivativeStructure simpleValue = b[0].getField().getZero(); for (int i = 0; i < a.length; ++i) { simpleValue = simpleValue.add(b[i].multiply(a[i])); } // create a result with accurate value and all derivatives (not necessarily as accurate as the value) final double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return new DerivativeStructure(simpleValue.getFreeParameters(), simpleValue.getOrder(), all); }
/** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure linearCombination(final DerivativeStructure a1, final DerivativeStructure b1, final DerivativeStructure a2, final DerivativeStructure b2) throws DimensionMismatchException { // compute an accurate value, taking care of cancellations final double accurateValue = MathArrays.linearCombination(a1.getValue(), b1.getValue(), a2.getValue(), b2.getValue()); // compute a simple value, with all partial derivatives final DerivativeStructure simpleValue = a1.multiply(b1).add(a2.multiply(b2)); // create a result with accurate value and all derivatives (not necessarily as accurate as the value) final double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return new DerivativeStructure(getFreeParameters(), getOrder(), all); }
/** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure linearCombination(final DerivativeStructure[] a, final DerivativeStructure[] b) throws DimensionMismatchException { // compute an accurate value, taking care of cancellations final double[] aDouble = new double[a.length]; for (int i = 0; i < a.length; ++i) { aDouble[i] = a[i].getValue(); } final double[] bDouble = new double[b.length]; for (int i = 0; i < b.length; ++i) { bDouble[i] = b[i].getValue(); } final 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) final double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return new DerivativeStructure(simpleValue.getFreeParameters(), simpleValue.getOrder(), all); }
/** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure linearCombination(final double a1, final DerivativeStructure b1, final double a2, final DerivativeStructure b2, final double a3, final DerivativeStructure b3) throws DimensionMismatchException { // compute an accurate value, taking care of cancellations final double accurateValue = MathArrays.linearCombination(a1, b1.getValue(), a2, b2.getValue(), a3, b3.getValue()); // compute a simple value, with all partial derivatives final DerivativeStructure simpleValue = b1.multiply(a1).add(b2.multiply(a2)).add(b3.multiply(a3)); // create a result with accurate value and all derivatives (not necessarily as accurate as the value) final double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return new DerivativeStructure(getFreeParameters(), getOrder(), all); }
/** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure linearCombination(final DerivativeStructure a1, final DerivativeStructure b1, final DerivativeStructure a2, final DerivativeStructure b2, final DerivativeStructure a3, final DerivativeStructure b3) throws DimensionMismatchException { // compute an accurate value, taking care of cancellations final double accurateValue = MathArrays.linearCombination(a1.getValue(), b1.getValue(), a2.getValue(), b2.getValue(), a3.getValue(), b3.getValue()); // compute a simple value, with all partial derivatives final DerivativeStructure simpleValue = a1.multiply(b1).add(a2.multiply(b2)).add(a3.multiply(b3)); // create a result with accurate value and all derivatives (not necessarily as accurate as the value) final double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return new DerivativeStructure(getFreeParameters(), getOrder(), all); }
/** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure linearCombination(final double a1, final DerivativeStructure b1, final double a2, final DerivativeStructure b2, final double a3, final DerivativeStructure b3, final double a4, final DerivativeStructure b4) throws DimensionMismatchException { // compute an accurate value, taking care of cancellations final double accurateValue = MathArrays.linearCombination(a1, b1.getValue(), a2, b2.getValue(), a3, b3.getValue(), a4, b4.getValue()); // compute a simple value, with all partial derivatives final DerivativeStructure simpleValue = b1.multiply(a1).add(b2.multiply(a2)).add(b3.multiply(a3)).add(b4.multiply(a4)); // create a result with accurate value and all derivatives (not necessarily as accurate as the value) final double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return new DerivativeStructure(getFreeParameters(), getOrder(), all); }
/** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure linearCombination(final DerivativeStructure a1, final DerivativeStructure b1, final DerivativeStructure a2, final DerivativeStructure b2, final DerivativeStructure a3, final DerivativeStructure b3, final DerivativeStructure a4, final DerivativeStructure b4) throws DimensionMismatchException { // compute an accurate value, taking care of cancellations final double accurateValue = MathArrays.linearCombination(a1.getValue(), b1.getValue(), a2.getValue(), b2.getValue(), a3.getValue(), b3.getValue(), a4.getValue(), b4.getValue()); // compute a simple value, with all partial derivatives final DerivativeStructure simpleValue = a1.multiply(b1).add(a2.multiply(b2)).add(a3.multiply(b3)).add(a4.multiply(b4)); // create a result with accurate value and all derivatives (not necessarily as accurate as the value) final double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return new DerivativeStructure(getFreeParameters(), getOrder(), all); }