Java Code Examples for org.apache.commons.math3.util.ArithmeticUtils#factorial()

The following examples show how to use org.apache.commons.math3.util.ArithmeticUtils#factorial() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DSCompiler.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Evaluate Taylor expansion of a derivative structure.
 * @param ds array holding the derivative structure
 * @param dsOffset offset of the derivative structure in its array
 * @param delta parameters offsets (Δx, Δy, ...)
 * @return value of the Taylor expansion at x + Δx, y + Δy, ...
 * @throws MathArithmeticException if factorials becomes too large
 */
public double taylor(final double[] ds, final int dsOffset, final double ... delta)
   throws MathArithmeticException {
    double value = 0;
    for (int i = getSize() - 1; i >= 0; --i) {
        final int[] orders = getPartialDerivativeOrders(i);
        double term = ds[dsOffset + i];
        for (int k = 0; k < orders.length; ++k) {
            if (orders[k] > 0) {
                try {
                    term *= FastMath.pow(delta[k], orders[k]) /
                            ArithmeticUtils.factorial(orders[k]);
                } catch (NotPositiveException e) {
                    // this cannot happen
                    throw new MathInternalError(e);
                }
            }
        }
        value += term;
    }
    return value;
}
 
Example 2
Source File: DSCompiler.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Evaluate Taylor expansion of a derivative structure.
 * @param ds array holding the derivative structure
 * @param dsOffset offset of the derivative structure in its array
 * @param delta parameters offsets (&Delta;x, &Delta;y, ...)
 * @return value of the Taylor expansion at x + &Delta;x, y + &Delta;y, ...
 * @throws MathArithmeticException if factorials becomes too large
 */
public double taylor(final double[] ds, final int dsOffset, final double ... delta)
   throws MathArithmeticException {
    double value = 0;
    for (int i = getSize() - 1; i >= 0; --i) {
        final int[] orders = getPartialDerivativeOrders(i);
        double term = ds[dsOffset + i];
        for (int k = 0; k < orders.length; ++k) {
            if (orders[k] > 0) {
                try {
                    term *= FastMath.pow(delta[k], orders[k]) /
                            ArithmeticUtils.factorial(orders[k]);
                } catch (NotPositiveException e) {
                    // this cannot happen
                    throw new MathInternalError(e);
                }
            }
        }
        value += term;
    }
    return value;
}
 
Example 3
Source File: Math_10_DSCompiler_t.java    From coming with MIT License 5 votes vote down vote up
/** Evaluate Taylor expansion of a derivative structure.
 * @param ds array holding the derivative structure
 * @param dsOffset offset of the derivative structure in its array
 * @param delta parameters offsets (&Delta;x, &Delta;y, ...)
 * @return value of the Taylor expansion at x + &Delta;x, y + &Delta;y, ...
 */
public double taylor(final double[] ds, final int dsOffset, final double ... delta) {
    double value = 0;
    for (int i = getSize() - 1; i >= 0; --i) {
        final int[] orders = getPartialDerivativeOrders(i);
        double term = ds[dsOffset + i];
        for (int k = 0; k < orders.length; ++k) {
            if (orders[k] > 0) {
                term *= FastMath.pow(delta[k], orders[k]) / ArithmeticUtils.factorial(orders[k]);
            }
        }
        value += term;
    }
    return value;
}
 
Example 4
Source File: Math_10_DSCompiler_s.java    From coming with MIT License 5 votes vote down vote up
/** Evaluate Taylor expansion of a derivative structure.
 * @param ds array holding the derivative structure
 * @param dsOffset offset of the derivative structure in its array
 * @param delta parameters offsets (&Delta;x, &Delta;y, ...)
 * @return value of the Taylor expansion at x + &Delta;x, y + &Delta;y, ...
 */
public double taylor(final double[] ds, final int dsOffset, final double ... delta) {
    double value = 0;
    for (int i = getSize() - 1; i >= 0; --i) {
        final int[] orders = getPartialDerivativeOrders(i);
        double term = ds[dsOffset + i];
        for (int k = 0; k < orders.length; ++k) {
            if (orders[k] > 0) {
                term *= FastMath.pow(delta[k], orders[k]) / ArithmeticUtils.factorial(orders[k]);
            }
        }
        value += term;
    }
    return value;
}
 
Example 5
Source File: DSCompiler.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Evaluate Taylor expansion of a derivative structure.
 * @param ds array holding the derivative structure
 * @param dsOffset offset of the derivative structure in its array
 * @param delta parameters offsets (&Delta;x, &Delta;y, ...)
 * @return value of the Taylor expansion at x + &Delta;x, y + &Delta;y, ...
 */
public double taylor(final double[] ds, final int dsOffset, final double ... delta) {
    double value = 0;
    for (int i = getSize() - 1; i >= 0; --i) {
        final int[] orders = getPartialDerivativeOrders(i);
        double term = ds[dsOffset + i];
        for (int k = 0; k < orders.length; ++k) {
            if (orders[k] > 0) {
                term *= FastMath.pow(delta[k], orders[k]) / ArithmeticUtils.factorial(orders[k]);
            }
        }
        value += term;
    }
    return value;
}
 
Example 6
Source File: DerivativeStructureTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testReciprocal() {
    for (double x = 0.1; x < 1.2; x += 0.1) {
        DerivativeStructure r = new DerivativeStructure(1, 6, 0, x).reciprocal();
        Assert.assertEquals(1 / x, r.getValue(), 1.0e-15);
        for (int i = 1; i < r.getOrder(); ++i) {
            double expected = ArithmeticUtils.pow(-1, i) * ArithmeticUtils.factorial(i) /
                              FastMath.pow(x, i + 1);
            Assert.assertEquals(expected, r.getPartialDerivative(i), 1.0e-15 * FastMath.abs(expected));
        }
    }
}
 
Example 7
Source File: DerivativeStructureTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testLog() {
    double[] epsilon = new double[] { 1.0e-16, 1.0e-16, 3.0e-14, 7.0e-13, 3.0e-11 };
    for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
        for (double x = 0.1; x < 1.2; x += 0.001) {
            DerivativeStructure log = new DerivativeStructure(1, maxOrder, 0, x).log();
            Assert.assertEquals(FastMath.log(x), log.getValue(), epsilon[0]);
            for (int n = 1; n <= maxOrder; ++n) {
                double refDer = -ArithmeticUtils.factorial(n - 1) / FastMath.pow(-x, n);
                Assert.assertEquals(refDer, log.getPartialDerivative(n), epsilon[n]);
            }
        }
    }
}
 
Example 8
Source File: DerivativeStructureTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testReciprocal() {
    for (double x = 0.1; x < 1.2; x += 0.1) {
        DerivativeStructure r = new DerivativeStructure(1, 6, 0, x).reciprocal();
        Assert.assertEquals(1 / x, r.getValue(), 1.0e-15);
        for (int i = 1; i < r.getOrder(); ++i) {
            double expected = ArithmeticUtils.pow(-1, i) * ArithmeticUtils.factorial(i) /
                              FastMath.pow(x, i + 1);
            Assert.assertEquals(expected, r.getPartialDerivative(i), 1.0e-15 * FastMath.abs(expected));
        }
    }
}
 
Example 9
Source File: DerivativeStructureTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testLog() {
    double[] epsilon = new double[] { 1.0e-16, 1.0e-16, 3.0e-14, 7.0e-13, 3.0e-11 };
    for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
        for (double x = 0.1; x < 1.2; x += 0.001) {
            DerivativeStructure log = new DerivativeStructure(1, maxOrder, 0, x).log();
            Assert.assertEquals(FastMath.log(x), log.getValue(), epsilon[0]);
            for (int n = 1; n <= maxOrder; ++n) {
                double refDer = -ArithmeticUtils.factorial(n - 1) / FastMath.pow(-x, n);
                Assert.assertEquals(refDer, log.getPartialDerivative(n), epsilon[n]);
            }
        }
    }
}
 
Example 10
Source File: DerivativeStructureTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testReciprocal() {
    for (double x = 0.1; x < 1.2; x += 0.1) {
        DerivativeStructure r = new DerivativeStructure(1, 6, 0, x).reciprocal();
        Assert.assertEquals(1 / x, r.getValue(), 1.0e-15);
        for (int i = 1; i < r.getOrder(); ++i) {
            double expected = ArithmeticUtils.pow(-1, i) * ArithmeticUtils.factorial(i) /
                              FastMath.pow(x, i + 1);
            Assert.assertEquals(expected, r.getPartialDerivative(i), 1.0e-15 * FastMath.abs(expected));
        }
    }
}
 
Example 11
Source File: DerivativeStructureTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test
public void testLog() {
    double[] epsilon = new double[] { 1.0e-16, 1.0e-16, 3.0e-14, 7.0e-13, 3.0e-11 };
    for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
        for (double x = 0.1; x < 1.2; x += 0.001) {
            DerivativeStructure log = new DerivativeStructure(1, maxOrder, 0, x).log();
            Assert.assertEquals(FastMath.log(x), log.getValue(), epsilon[0]);
            for (int n = 1; n <= maxOrder; ++n) {
                double refDer = -ArithmeticUtils.factorial(n - 1) / FastMath.pow(-x, n);
                Assert.assertEquals(refDer, log.getPartialDerivative(n), epsilon[n]);
            }
        }
    }
}