Java Code Examples for org.apache.commons.math.util.FastMath#pow()

The following examples show how to use org.apache.commons.math.util.FastMath#pow() . 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: LogisticTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGradientComponent1Component2Component3() {
    final double m = 1.2;
    final double k = 3.4;
    final double a = 2.3;
    final double b = 0.567;
    final double q = 1 / FastMath.exp(b * m);
    final double n = 3.4;

    final Logistic.Parametric f = new Logistic.Parametric();
    
    final double x = 0;
    final double qExp1 = 2;

    final double[] gf = f.gradient(x, new double[] {k, m, b, q, a, n});

    final double factor = (a - k) / (n * FastMath.pow(qExp1, 1 / n + 1));
    Assert.assertEquals(factor * b, gf[1], EPS);
    Assert.assertEquals(factor * m, gf[2], EPS);
    Assert.assertEquals(factor / q, gf[3], EPS);
}
 
Example 2
Source File: LogisticTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGradientComponent1Component2Component3() {
    final double m = 1.2;
    final double k = 3.4;
    final double a = 2.3;
    final double b = 0.567;
    final double q = 1 / FastMath.exp(b * m);
    final double n = 3.4;

    final Logistic.Parametric f = new Logistic.Parametric();
    
    final double x = 0;
    final double qExp1 = 2;

    final double[] gf = f.gradient(x, new double[] {k, m, b, q, a, n});

    final double factor = (a - k) / (n * FastMath.pow(qExp1, 1 / n + 1));
    Assert.assertEquals(factor * b, gf[1], EPS);
    Assert.assertEquals(factor * m, gf[2], EPS);
    Assert.assertEquals(factor / q, gf[3], EPS);
}
 
Example 3
Source File: WeibullDistributionImpl.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the probability density for a particular point.
 *
 * @param x The point at which the density should be computed.
 * @return The pdf at point x.
 * @since 2.1
 */
@Override
public double density(double x) {
    if (x < 0) {
        return 0;
    }

    final double xscale = x / scale;
    final double xscalepow = FastMath.pow(xscale, shape - 1);

    /*
     * FastMath.pow(x / scale, shape) =
     * FastMath.pow(xscale, shape) =
     * FastMath.pow(xscale, shape - 1) * xscale
     */
    final double xscalepowshape = xscalepow * xscale;

    return (shape / scale) * xscalepow * FastMath.exp(-xscalepowshape);
}
 
Example 4
Source File: Logistic.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the value of the gradient at {@code x}.
 * The components of the gradient vector are the partial
 * derivatives of the function with respect to each of the
 * <em>parameters</em>.
 *
 * @param x Value at which the gradient must be computed.
 * @param param Values for {@code k}, {@code m}, {@code b}, {@code q},
 * {@code a} and  {@code n}.
 * @return the gradient vector at {@code x}.
 * @throws NullArgumentException if {@code param} is {@code null}.
 * @throws DimensionMismatchException if the size of {@code param} is
 * not 6.
 */
public double[] gradient(double x, double ... param) {
    validateParameters(param);

    final double b = param[2];
    final double q = param[3];

    final double mMinusX = param[1] - x;
    final double oneOverN = 1 / param[5];
    final double exp = FastMath.exp(b * mMinusX);
    final double qExp = q * exp;
    final double qExp1 = qExp + 1;
    final double factor1 = (param[0] - param[4]) * oneOverN / FastMath.pow(qExp1, oneOverN);
    final double factor2 = -factor1 / qExp1;

    // Components of the gradient.
    final double gk = Logistic.value(mMinusX, 1, b, q, 0, oneOverN);
    final double gm = factor2 * b * qExp;
    final double gb = factor2 * mMinusX * qExp;
    final double gq = factor2 * exp;
    final double ga = Logistic.value(mMinusX, 0, b, q, 1, oneOverN);
    final double gn = factor1 * Math.log(qExp1) * oneOverN;

    return new double[] { gk, gm, gb, gq, ga, gn };
}
 
Example 5
Source File: AdamsBashforthIntegratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testIncreasingTolerance()
    throws MathUserException, IntegratorException {

    int previousCalls = Integer.MAX_VALUE;
    for (int i = -12; i < -5; ++i) {
        TestProblem1 pb = new TestProblem1();
        double minStep = 0;
        double maxStep = pb.getFinalTime() - pb.getInitialTime();
        double scalAbsoluteTolerance = FastMath.pow(10.0, i);
        double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance;

        FirstOrderIntegrator integ = new AdamsBashforthIntegrator(4, minStep, maxStep,
                                                                  scalAbsoluteTolerance,
                                                                  scalRelativeTolerance);
        TestProblemHandler handler = new TestProblemHandler(pb, integ);
        integ.addStepHandler(handler);
        integ.integrate(pb,
                        pb.getInitialTime(), pb.getInitialState(),
                        pb.getFinalTime(), new double[pb.getDimension()]);

        // the 31 and 36 factors are only valid for this test
        // and has been obtained from trial and error
        // there is no general relation between local and global errors
        assertTrue(handler.getMaximalValueError() > (31.0 * scalAbsoluteTolerance));
        assertTrue(handler.getMaximalValueError() < (36.0 * scalAbsoluteTolerance));
        assertEquals(0, handler.getMaximalTimeError(), 1.0e-16);

        int calls = pb.getCalls();
        assertEquals(integ.getEvaluations(), calls);
        assertTrue(calls <= previousCalls);
        previousCalls = calls;

    }

}
 
Example 6
Source File: Kurtosis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the kurtosis of the entries in the specified portion of the
 * input array.
 * <p>
 * See {@link Kurtosis} for details on the computing algorithm.</p>
 * <p>
 * Throws <code>IllegalArgumentException</code> if the array is null.</p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @return the kurtosis of the values or Double.NaN if length is less than
 * 4
 * @throws IllegalArgumentException if the input array is null or the array
 * index parameters are not valid
 */
@Override
public double evaluate(final double[] values,final int begin, final int length) {
    // Initialize the kurtosis
    double kurt = Double.NaN;

    if (test(values, begin, length) && length > 3) {

        // Compute the mean and standard deviation
        Variance variance = new Variance();
        variance.incrementAll(values, begin, length);
        double mean = variance.moment.m1;
        double stdDev = FastMath.sqrt(variance.getResult());

        // Sum the ^4 of the distance from the mean divided by the
        // standard deviation
        double accum3 = 0.0;
        for (int i = begin; i < begin + length; i++) {
            accum3 += FastMath.pow(values[i] - mean, 4.0);
        }
        accum3 /= FastMath.pow(stdDev, 4.0d);

        // Get N
        double n0 = length;

        double coefficientOne =
            (n0 * (n0 + 1)) / ((n0 - 1) * (n0 - 2) * (n0 - 3));
        double termTwo =
            (3 * FastMath.pow(n0 - 1, 2.0)) / ((n0 - 2) * (n0 - 3));

        // Calculate kurtosis
        kurt = (coefficientOne * accum3) - termTwo;
    }
    return kurt;
}
 
Example 7
Source File: JGenProg2017_0065_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Computes the n-th roots of this complex number.
 * </p>
 * <p>The nth roots are defined by the formula: <pre>
 * <code> z<sub>k</sub> = abs<sup> 1/n</sup> (cos(phi + 2&pi;k/n) + i (sin(phi + 2&pi;k/n))</code></pre>
 * for <i><code>k=0, 1, ..., n-1</code></i>, where <code>abs</code> and <code>phi</code> are
 * respectively the {@link #abs() modulus} and {@link #getArgument() argument} of this complex number.
 * </p>
 * <p>If one or both parts of this complex number is NaN, a list with just one element,
 *  {@link #NaN} is returned.</p>
 * <p>if neither part is NaN, but at least one part is infinite, the result is a one-element
 * list containing {@link #INF}.</p>
 *
 * @param n degree of root
 * @return List<Complex> all nth roots of this complex number
 * @throws IllegalArgumentException if parameter n is less than or equal to 0
 * @since 2.0
 */
public List<Complex> nthRoot(int n) throws IllegalArgumentException {

    if (n <= 0) {
        throw MathRuntimeException.createIllegalArgumentException(
                LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N,
                n);
    }

    List<Complex> result = new ArrayList<Complex>();

    if (isNaN) {
        result.add(NaN);
        return result;
    }

    if (isInfinite()) {
        result.add(INF);
        return result;
    }

    // nth root of abs -- faster / more accurate to use a solver here?
    final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n);

    // Compute nth roots of complex number with k = 0, 1, ... n-1
    final double nthPhi = getArgument()/n;
    final double slice = 2 * FastMath.PI / n;
    double innerPart = nthPhi;
    for (int k = 0; k < n ; k++) {
        // inner part
        final double realPart      = nthRootOfAbs *  FastMath.cos(innerPart);
        final double imaginaryPart = nthRootOfAbs *  FastMath.sin(innerPart);
        result.add(createComplex(realPart, imaginaryPart));
        innerPart += slice;
    }

    return result;
}
 
Example 8
Source File: PascalDistributionImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * For this distribution, X, this method returns P(X = x).
 * @param x the value at which the PMF is evaluated
 * @return PMF for this distribution
 */
public double probability(int x) {
    double ret;
    if (x < 0) {
        ret = 0.0;
    } else {
        ret = MathUtils.binomialCoefficientDouble(x +
              numberOfSuccesses - 1, numberOfSuccesses - 1) *
              FastMath.pow(probabilityOfSuccess, numberOfSuccesses) *
              FastMath.pow(1.0 - probabilityOfSuccess, x);
    }
    return ret;
}
 
Example 9
Source File: Math_47_Complex_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Computes the n-th roots of this complex number.
 * The nth roots are defined by the formula:
 * <pre>
 *  <code>
 *   z<sub>k</sub> = abs<sup>1/n</sup> (cos(phi + 2&pi;k/n) + i (sin(phi + 2&pi;k/n))
 *  </code>
 * </pre>
 * for <i>{@code k=0, 1, ..., n-1}</i>, where {@code abs} and {@code phi}
 * are respectively the {@link #abs() modulus} and
 * {@link #getArgument() argument} of this complex number.
 * <br/>
 * If one or both parts of this complex number is NaN, a list with just
 * one element, {@link #NaN} is returned.
 * if neither part is NaN, but at least one part is infinite, the result
 * is a one-element list containing {@link #INF}.
 *
 * @param n Degree of root.
 * @return a List<Complex> of all {@code n}-th roots of {@code this}.
 * @throws NotPositiveException if {@code n <= 0}.
 * @since 2.0
 */
public List<Complex> nthRoot(int n) {

    if (n <= 0) {
        throw new NotPositiveException(LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N,
                                       n);
    }

    final List<Complex> result = new ArrayList<Complex>();

    if (isNaN) {
        result.add(NaN);
        return result;
    }
    if (isInfinite()) {
        result.add(INF);
        return result;
    }

    // nth root of abs -- faster / more accurate to use a solver here?
    final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n);

    // Compute nth roots of complex number with k = 0, 1, ... n-1
    final double nthPhi = getArgument() / n;
    final double slice = 2 * FastMath.PI / n;
    double innerPart = nthPhi;
    for (int k = 0; k < n ; k++) {
        // inner part
        final double realPart = nthRootOfAbs *  FastMath.cos(innerPart);
        final double imaginaryPart = nthRootOfAbs *  FastMath.sin(innerPart);
        result.add(createComplex(realPart, imaginaryPart));
        innerPart += slice;
    }

    return result;
}
 
Example 10
Source File: Complex_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Computes the n-th roots of this complex number.
 * </p>
 * <p>The nth roots are defined by the formula: <pre>
 * <code> z<sub>k</sub> = abs<sup> 1/n</sup> (cos(phi + 2&pi;k/n) + i (sin(phi + 2&pi;k/n))</code></pre>
 * for <i><code>k=0, 1, ..., n-1</code></i>, where <code>abs</code> and <code>phi</code> are
 * respectively the {@link #abs() modulus} and {@link #getArgument() argument} of this complex number.
 * </p>
 * <p>If one or both parts of this complex number is NaN, a list with just one element,
 *  {@link #NaN} is returned.</p>
 * <p>if neither part is NaN, but at least one part is infinite, the result is a one-element
 * list containing {@link #INF}.</p>
 *
 * @param n degree of root
 * @return List<Complex> all nth roots of this complex number
 * @throws IllegalArgumentException if parameter n is less than or equal to 0
 * @since 2.0
 */
public List<Complex> nthRoot(int n) throws IllegalArgumentException {

    if (n <= 0) {
        throw MathRuntimeException.createIllegalArgumentException(
                LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N,
                n);
    }

    List<Complex> result = new ArrayList<Complex>();

    if (isNaN) {
        result.add(NaN);
        return result;
    }

    if (isInfinite()) {
        result.add(INF);
        return result;
    }

    // nth root of abs -- faster / more accurate to use a solver here?
    final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n);

    // Compute nth roots of complex number with k = 0, 1, ... n-1
    final double nthPhi = getArgument()/n;
    final double slice = 2 * FastMath.PI / n;
    double innerPart = nthPhi;
    for (int k = 0; k < n ; k++) {
        // inner part
        final double realPart      = nthRootOfAbs *  FastMath.cos(innerPart);
        final double imaginaryPart = nthRootOfAbs *  FastMath.sin(innerPart);
        result.add(createComplex(realPart, imaginaryPart));
        innerPart += slice;
    }

    return result;
}
 
Example 11
Source File: Cardumen_00262_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Computes the n-th roots of this complex number.
 * The nth roots are defined by the formula:
 * <pre>
 *  <code>
 *   z<sub>k</sub> = abs<sup>1/n</sup> (cos(phi + 2&pi;k/n) + i (sin(phi + 2&pi;k/n))
 *  </code>
 * </pre>
 * for <i>{@code k=0, 1, ..., n-1}</i>, where {@code abs} and {@code phi}
 * are respectively the {@link #abs() modulus} and
 * {@link #getArgument() argument} of this complex number.
 * <br/>
 * If one or both parts of this complex number is NaN, a list with just
 * one element, {@link #NaN} is returned.
 * if neither part is NaN, but at least one part is infinite, the result
 * is a one-element list containing {@link #INF}.
 *
 * @param n Degree of root.
 * @return a List<Complex> of all {@code n}-th roots of {@code this}.
 * @throws NotPositiveException if {@code n <= 0}.
 * @since 2.0
 */
public List<Complex> nthRoot(int n) {

    if (n <= 0) {
        throw new NotPositiveException(LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N,
                                       n);
    }

    final List<Complex> result = new ArrayList<Complex>();

    if (isNaN) {
        result.add(NaN);
        return result;
    }
    if (isInfinite()) {
        result.add(INF);
        return result;
    }

    // nth root of abs -- faster / more accurate to use a solver here?
    final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n);

    // Compute nth roots of complex number with k = 0, 1, ... n-1
    final double nthPhi = getArgument() / n;
    final double slice = 2 * FastMath.PI / n;
    double innerPart = nthPhi;
    for (int k = 0; k < n ; k++) {
        // inner part
        final double realPart = nthRootOfAbs *  FastMath.cos(innerPart);
        final double imaginaryPart = nthRootOfAbs *  FastMath.sin(innerPart);
        result.add(createComplex(realPart, imaginaryPart));
        innerPart += slice;
    }

    return result;
}
 
Example 12
Source File: Complex.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the n-th roots of this complex number.
 * The nth roots are defined by the formula:
 * <pre>
 *  <code>
 *   z<sub>k</sub> = abs<sup>1/n</sup> (cos(phi + 2&pi;k/n) + i (sin(phi + 2&pi;k/n))
 *  </code>
 * </pre>
 * for <i>{@code k=0, 1, ..., n-1}</i>, where {@code abs} and {@code phi}
 * are respectively the {@link #abs() modulus} and
 * {@link #getArgument() argument} of this complex number.
 * <br/>
 * If one or both parts of this complex number is NaN, a list with just
 * one element, {@link #NaN} is returned.
 * if neither part is NaN, but at least one part is infinite, the result
 * is a one-element list containing {@link #INF}.
 *
 * @param n Degree of root.
 * @return a List<Complex> of all {@code n}-th roots of {@code this}.
 * @throws NotPositiveException if {@code n <= 0}.
 * @since 2.0
 */
public List<Complex> nthRoot(int n) {

    if (n <= 0) {
        throw new NotPositiveException(LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N,
                                       n);
    }

    final List<Complex> result = new ArrayList<Complex>();

    if (isNaN) {
        result.add(NaN);
        return result;
    }
    if (isInfinite()) {
        result.add(INF);
        return result;
    }

    // nth root of abs -- faster / more accurate to use a solver here?
    final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n);

    // Compute nth roots of complex number with k = 0, 1, ... n-1
    final double nthPhi = getArgument() / n;
    final double slice = 2 * FastMath.PI / n;
    double innerPart = nthPhi;
    for (int k = 0; k < n ; k++) {
        // inner part
        final double realPart = nthRootOfAbs *  FastMath.cos(innerPart);
        final double imaginaryPart = nthRootOfAbs *  FastMath.sin(innerPart);
        result.add(createComplex(realPart, imaginaryPart));
        innerPart += slice;
    }

    return result;
}
 
Example 13
Source File: DormandPrince853IntegratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testIncreasingTolerance()
  throws MathUserException, IntegratorException {

  int previousCalls = Integer.MAX_VALUE;
  AdaptiveStepsizeIntegrator integ =
      new DormandPrince853Integrator(0, Double.POSITIVE_INFINITY,
                                     Double.NaN, Double.NaN);
  for (int i = -12; i < -2; ++i) {
    TestProblem1 pb = new TestProblem1();
    double minStep = 0;
    double maxStep = pb.getFinalTime() - pb.getInitialTime();
    double scalAbsoluteTolerance = FastMath.pow(10.0, i);
    double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance;
    integ.setStepSizeControl(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);

    TestProblemHandler handler = new TestProblemHandler(pb, integ);
    integ.addStepHandler(handler);
    integ.integrate(pb,
                    pb.getInitialTime(), pb.getInitialState(),
                    pb.getFinalTime(), new double[pb.getDimension()]);

    // the 1.3 factor is only valid for this test
    // and has been obtained from trial and error
    // there is no general relation between local and global errors
    Assert.assertTrue(handler.getMaximalValueError() < (1.3 * scalAbsoluteTolerance));
    Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);

    int calls = pb.getCalls();
    Assert.assertEquals(integ.getEvaluations(), calls);
    Assert.assertTrue(calls <= previousCalls);
    previousCalls = calls;

  }

}
 
Example 14
Source File: Cardumen_00218_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Computes the n-th roots of this complex number.
 * The nth roots are defined by the formula:
 * <pre>
 *  <code>
 *   z<sub>k</sub> = abs<sup>1/n</sup> (cos(phi + 2&pi;k/n) + i (sin(phi + 2&pi;k/n))
 *  </code>
 * </pre>
 * for <i>{@code k=0, 1, ..., n-1}</i>, where {@code abs} and {@code phi}
 * are respectively the {@link #abs() modulus} and
 * {@link #getArgument() argument} of this complex number.
 * <br/>
 * If one or both parts of this complex number is NaN, a list with just
 * one element, {@link #NaN} is returned.
 * if neither part is NaN, but at least one part is infinite, the result
 * is a one-element list containing {@link #INF}.
 *
 * @param n Degree of root.
 * @return a List<Complex> of all {@code n}-th roots of {@code this}.
 * @throws NotPositiveException if {@code n <= 0}.
 * @since 2.0
 */
public List<Complex> nthRoot(int n) {

    if (n <= 0) {
        throw new NotPositiveException(LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N,
                                       n);
    }

    final List<Complex> result = new ArrayList<Complex>();

    if (isNaN) {
        result.add(NaN);
        return result;
    }
    if (isInfinite()) {
        result.add(INF);
        return result;
    }

    // nth root of abs -- faster / more accurate to use a solver here?
    final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n);

    // Compute nth roots of complex number with k = 0, 1, ... n-1
    final double nthPhi = getArgument() / n;
    final double slice = 2 * FastMath.PI / n;
    double innerPart = nthPhi;
    for (int k = 0; k < n ; k++) {
        // inner part
        final double realPart = nthRootOfAbs *  FastMath.cos(innerPart);
        final double imaginaryPart = nthRootOfAbs *  FastMath.sin(innerPart);
        result.add(createComplex(realPart, imaginaryPart));
        innerPart += slice;
    }

    return result;
}
 
Example 15
Source File: Complex.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the n-th roots of this complex number.
 * The nth roots are defined by the formula:
 * <pre>
 *  <code>
 *   z<sub>k</sub> = abs<sup>1/n</sup> (cos(phi + 2&pi;k/n) + i (sin(phi + 2&pi;k/n))
 *  </code>
 * </pre>
 * for <i>{@code k=0, 1, ..., n-1}</i>, where {@code abs} and {@code phi}
 * are respectively the {@link #abs() modulus} and
 * {@link #getArgument() argument} of this complex number.
 * <br/>
 * If one or both parts of this complex number is NaN, a list with just
 * one element, {@link #NaN} is returned.
 * if neither part is NaN, but at least one part is infinite, the result
 * is a one-element list containing {@link #INF}.
 *
 * @param n Degree of root.
 * @return a List<Complex> of all {@code n}-th roots of {@code this}.
 * @throws NotPositiveException if {@code n <= 0}.
 * @since 2.0
 */
public List<Complex> nthRoot(int n) {

    if (n <= 0) {
        throw new NotPositiveException(LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N,
                                       n);
    }

    final List<Complex> result = new ArrayList<Complex>();

    if (isNaN) {
        result.add(NaN);
        return result;
    }
    if (isInfinite()) {
        result.add(INF);
        return result;
    }

    // nth root of abs -- faster / more accurate to use a solver here?
    final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n);

    // Compute nth roots of complex number with k = 0, 1, ... n-1
    final double nthPhi = getArgument() / n;
    final double slice = 2 * FastMath.PI / n;
    double innerPart = nthPhi;
    for (int k = 0; k < n ; k++) {
        // inner part
        final double realPart = nthRootOfAbs *  FastMath.cos(innerPart);
        final double imaginaryPart = nthRootOfAbs *  FastMath.sin(innerPart);
        result.add(createComplex(realPart, imaginaryPart));
        innerPart += slice;
    }

    return result;
}
 
Example 16
Source File: Kurtosis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the kurtosis of the entries in the specified portion of the
 * input array.
 * <p>
 * See {@link Kurtosis} for details on the computing algorithm.</p>
 * <p>
 * Throws <code>IllegalArgumentException</code> if the array is null.</p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @return the kurtosis of the values or Double.NaN if length is less than
 * 4
 * @throws IllegalArgumentException if the input array is null or the array
 * index parameters are not valid
 */
@Override
public double evaluate(final double[] values,final int begin, final int length) {
    // Initialize the kurtosis
    double kurt = Double.NaN;

    if (test(values, begin, length) && length > 3) {

        // Compute the mean and standard deviation
        Variance variance = new Variance();
        variance.incrementAll(values, begin, length);
        double mean = variance.moment.m1;
        double stdDev = FastMath.sqrt(variance.getResult());

        // Sum the ^4 of the distance from the mean divided by the
        // standard deviation
        double accum3 = 0.0;
        for (int i = begin; i < begin + length; i++) {
            accum3 += FastMath.pow(values[i] - mean, 4.0);
        }
        accum3 /= FastMath.pow(stdDev, 4.0d);

        // Get N
        double n0 = length;

        double coefficientOne =
            (n0 * (n0 + 1)) / ((n0 - 1) * (n0 - 2) * (n0 - 3));
        double termTwo =
            (3 * FastMath.pow(n0 - 1, 2.0)) / ((n0 - 2) * (n0 - 3));

        // Calculate kurtosis
        kurt = (coefficientOne * accum3) - termTwo;
    }
    return kurt;
}
 
Example 17
Source File: 1_Complex.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Computes the n-th roots of this complex number.
 * </p>
 * <p>The nth roots are defined by the formula: <pre>
 * <code> z<sub>k</sub> = abs<sup> 1/n</sup> (cos(phi + 2&pi;k/n) + i (sin(phi + 2&pi;k/n))</code></pre>
 * for <i><code>k=0, 1, ..., n-1</code></i>, where <code>abs</code> and <code>phi</code> are
 * respectively the {@link #abs() modulus} and {@link #getArgument() argument} of this complex number.
 * </p>
 * <p>If one or both parts of this complex number is NaN, a list with just one element,
 *  {@link #NaN} is returned.</p>
 * <p>if neither part is NaN, but at least one part is infinite, the result is a one-element
 * list containing {@link #INF}.</p>
 *
 * @param n degree of root
 * @return List<Complex> all nth roots of this complex number
 * @throws IllegalArgumentException if parameter n is less than or equal to 0
 * @since 2.0
 */
public List<Complex> nthRoot(int n) throws IllegalArgumentException {

    if (n <= 0) {
        throw MathRuntimeException.createIllegalArgumentException(
                LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N,
                n);
    }

    List<Complex> result = new ArrayList<Complex>();

    if (isNaN) {
        result.add(NaN);
        return result;
    }

    if (isInfinite()) {
        result.add(INF);
        return result;
    }

    // nth root of abs -- faster / more accurate to use a solver here?
    final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n);

    // Compute nth roots of complex number with k = 0, 1, ... n-1
    final double nthPhi = getArgument()/n;
    final double slice = 2 * FastMath.PI / n;
    double innerPart = nthPhi;
    for (int k = 0; k < n ; k++) {
        // inner part
        final double realPart      = nthRootOfAbs *  FastMath.cos(innerPart);
        final double imaginaryPart = nthRootOfAbs *  FastMath.sin(innerPart);
        result.add(createComplex(realPart, imaginaryPart));
        innerPart += slice;
    }

    return result;
}
 
Example 18
Source File: Math_37_Complex_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Computes the n-th roots of this complex number.
 * The nth roots are defined by the formula:
 * <pre>
 *  <code>
 *   z<sub>k</sub> = abs<sup>1/n</sup> (cos(phi + 2&pi;k/n) + i (sin(phi + 2&pi;k/n))
 *  </code>
 * </pre>
 * for <i>{@code k=0, 1, ..., n-1}</i>, where {@code abs} and {@code phi}
 * are respectively the {@link #abs() modulus} and
 * {@link #getArgument() argument} of this complex number.
 * <br/>
 * If one or both parts of this complex number is NaN, a list with just
 * one element, {@link #NaN} is returned.
 * if neither part is NaN, but at least one part is infinite, the result
 * is a one-element list containing {@link #INF}.
 *
 * @param n Degree of root.
 * @return a List<Complex> of all {@code n}-th roots of {@code this}.
 * @throws NotPositiveException if {@code n <= 0}.
 * @since 2.0
 */
public List<Complex> nthRoot(int n) {

    if (n <= 0) {
        throw new NotPositiveException(LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N,
                                       n);
    }

    final List<Complex> result = new ArrayList<Complex>();

    if (isNaN) {
        result.add(NaN);
        return result;
    }
    if (isInfinite()) {
        result.add(INF);
        return result;
    }

    // nth root of abs -- faster / more accurate to use a solver here?
    final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n);

    // Compute nth roots of complex number with k = 0, 1, ... n-1
    final double nthPhi = getArgument() / n;
    final double slice = 2 * FastMath.PI / n;
    double innerPart = nthPhi;
    for (int k = 0; k < n ; k++) {
        // inner part
        final double realPart = nthRootOfAbs *  FastMath.cos(innerPart);
        final double imaginaryPart = nthRootOfAbs *  FastMath.sin(innerPart);
        result.add(createComplex(realPart, imaginaryPart));
        innerPart += slice;
    }

    return result;
}
 
Example 19
Source File: AdaptiveStepsizeIntegrator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** Initialize the integration step.
 * @param equations differential equations set
 * @param forward forward integration indicator
 * @param order order of the method
 * @param scale scaling vector for the state vector (can be shorter than state vector)
 * @param t0 start time
 * @param y0 state vector at t0
 * @param yDot0 first time derivative of y0
 * @param y1 work array for a state vector
 * @param yDot1 work array for the first time derivative of y1
 * @return first integration step
 * @exception MathUserException this exception is propagated to
 * the caller if the underlying user function triggers one
 */
public double initializeStep(final FirstOrderDifferentialEquations equations,
                             final boolean forward, final int order, final double[] scale,
                             final double t0, final double[] y0, final double[] yDot0,
                             final double[] y1, final double[] yDot1)
    throws MathUserException {

  if (initialStep > 0) {
    // use the user provided value
    return forward ? initialStep : -initialStep;
  }

  // very rough first guess : h = 0.01 * ||y/scale|| / ||y'/scale||
  // this guess will be used to perform an Euler step
  double ratio;
  double yOnScale2 = 0;
  double yDotOnScale2 = 0;
  for (int j = 0; j < scale.length; ++j) {
    ratio         = y0[j] / scale[j];
    yOnScale2    += ratio * ratio;
    ratio         = yDot0[j] / scale[j];
    yDotOnScale2 += ratio * ratio;
  }

  double h = ((yOnScale2 < 1.0e-10) || (yDotOnScale2 < 1.0e-10)) ?
             1.0e-6 : (0.01 * FastMath.sqrt(yOnScale2 / yDotOnScale2));
  if (! forward) {
    h = -h;
  }

  // perform an Euler step using the preceding rough guess
  for (int j = 0; j < y0.length; ++j) {
    y1[j] = y0[j] + h * yDot0[j];
  }
  computeDerivatives(t0 + h, y1, yDot1);

  // estimate the second derivative of the solution
  double yDDotOnScale = 0;
  for (int j = 0; j < scale.length; ++j) {
    ratio         = (yDot1[j] - yDot0[j]) / scale[j];
    yDDotOnScale += ratio * ratio;
  }
  yDDotOnScale = FastMath.sqrt(yDDotOnScale) / h;

  // step size is computed such that
  // h^order * max (||y'/tol||, ||y''/tol||) = 0.01
  final double maxInv2 = FastMath.max(FastMath.sqrt(yDotOnScale2), yDDotOnScale);
  final double h1 = (maxInv2 < 1.0e-15) ?
                    FastMath.max(1.0e-6, 0.001 * FastMath.abs(h)) :
                    FastMath.pow(0.01 / maxInv2, 1.0 / order);
  h = FastMath.min(100.0 * FastMath.abs(h), h1);
  h = FastMath.max(h, 1.0e-12 * FastMath.abs(t0));  // avoids cancellation when computing t1 - t0
  if (h < getMinStep()) {
    h = getMinStep();
  }
  if (h > getMaxStep()) {
    h = getMaxStep();
  }
  if (! forward) {
    h = -h;
  }

  return h;

}
 
Example 20
Source File: Power.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public double value(double x) {
    return FastMath.pow(x, p);
}