Java Code Examples for org.apache.commons.math3.util.FastMath#tan()

The following examples show how to use org.apache.commons.math3.util.FastMath#tan() . 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: SparseGradientTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testTrigo() {
    double epsilon = 2.0e-12;
        for (double x = 0.1; x < 1.2; x += 0.1) {
            SparseGradient sgX = SparseGradient.createVariable(0, x);
            for (double y = 0.1; y < 1.2; y += 0.1) {
                SparseGradient sgY = SparseGradient.createVariable(1, y);
                for (double z = 0.1; z < 1.2; z += 0.1) {
                    SparseGradient sgZ = SparseGradient.createVariable(2, z);
                    SparseGradient f = sgX.divide(sgY.cos().add(sgZ.tan())).sin();
                    double a = FastMath.cos(y) + FastMath.tan(z);
                    double f0 = FastMath.sin(x / a);
                    Assert.assertEquals(f0, f.getValue(), FastMath.abs(epsilon * f0));
                    double dfdx = FastMath.cos(x / a) / a;
                    Assert.assertEquals(dfdx, f.getDerivative(0), FastMath.abs(epsilon * dfdx));
                    double dfdy =  x * FastMath.sin(y) * dfdx / a;
                    Assert.assertEquals(dfdy, f.getDerivative(1), FastMath.abs(epsilon * dfdy));
                    double cz = FastMath.cos(z);
                    double cz2 = cz * cz;
                    double dfdz = -x * dfdx / (a * cz2);
                    Assert.assertEquals(dfdz, f.getDerivative(2), FastMath.abs(epsilon * dfdz));
                }
            }
        }        
}
 
Example 2
Source File: Tan.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public UnivariateFunction derivative() {
    return new UnivariateFunction() {
        /** {@inheritDoc} */
        public double value(double x) {
            final double tanX = FastMath.tan(x);
            return 1 + tanX * tanX;
        }
    };
}
 
Example 3
Source File: StableRandomGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new generator.
 *
 * @param generator underlying random generator to use
 * @param alpha Stability parameter. Must be in range (0, 2]
 * @param beta Skewness parameter. Must be in range [-1, 1]
 * @throws NullArgumentException if generator is null
 * @throws OutOfRangeException if {@code alpha <= 0} or {@code alpha > 2}
 * or {@code beta < -1} or {@code beta > 1}
 */
public StableRandomGenerator(final RandomGenerator generator,
                             final double alpha, final double beta)
    throws NullArgumentException, OutOfRangeException {
    if (generator == null) {
        throw new NullArgumentException();
    }

    if (!(alpha > 0d && alpha <= 2d)) {
        throw new OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_LEFT,
                alpha, 0, 2);
    }

    if (!(beta >= -1d && beta <= 1d)) {
        throw new OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_SIMPLE,
                beta, -1, 1);
    }

    this.generator = generator;
    this.alpha = alpha;
    this.beta = beta;
    if (alpha < 2d && beta != 0d) {
        zeta = beta * FastMath.tan(FastMath.PI * alpha / 2);
    } else {
        zeta = 0d;
    }
}
 
Example 4
Source File: StableRandomGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new generator.
 *
 * @param generator underlying random generator to use
 * @param alpha Stability parameter. Must be in range (0, 2]
 * @param beta Skewness parameter. Must be in range [-1, 1]
 * @throws NullArgumentException if generator is null
 * @throws OutOfRangeException if {@code alpha <= 0} or {@code alpha > 2} 
 * or {@code beta < -1} or {@code beta > 1} 
 */
public StableRandomGenerator(final RandomGenerator generator,
                             final double alpha, final double beta)
    throws NullArgumentException, OutOfRangeException {
    if (generator == null) {
        throw new NullArgumentException();
    }

    if (!(alpha > 0d && alpha <= 2d)) {
        throw new OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_LEFT,
                alpha, 0, 2);
    }

    if (!(beta >= -1d && beta <= 1d)) {
        throw new OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_SIMPLE,
                beta, -1, 1);
    }

    this.generator = generator;
    this.alpha = alpha;
    this.beta = beta;
    if (alpha < 2d && beta != 0d) {
        zeta = beta * FastMath.tan(FastMath.PI * alpha / 2);
    } else {
        zeta = 0d;
    }
}
 
Example 5
Source File: CauchyDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * Returns {@code Double.NEGATIVE_INFINITY} when {@code p == 0}
 * and {@code Double.POSITIVE_INFINITY} when {@code p == 1}.
 */
@Override
public double inverseCumulativeProbability(double p) throws OutOfRangeException {
    double ret;
    if (p < 0 || p > 1) {
        throw new OutOfRangeException(p, 0, 1);
    } else if (p == 0) {
        ret = Double.NEGATIVE_INFINITY;
    } else  if (p == 1) {
        ret = Double.POSITIVE_INFINITY;
    } else {
        ret = median + scale * FastMath.tan(FastMath.PI * (p - .5));
    }
    return ret;
}
 
Example 6
Source File: CauchyDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * Returns {@code Double.NEGATIVE_INFINITY} when {@code p == 0}
 * and {@code Double.POSITIVE_INFINITY} when {@code p == 1}.
 */
@Override
public double inverseCumulativeProbability(double p) throws OutOfRangeException {
    double ret;
    if (p < 0 || p > 1) {
        throw new OutOfRangeException(p, 0, 1);
    } else if (p == 0) {
        ret = Double.NEGATIVE_INFINITY;
    } else  if (p == 1) {
        ret = Double.POSITIVE_INFINITY;
    } else {
        ret = median + scale * FastMath.tan(FastMath.PI * (p - .5));
    }
    return ret;
}
 
Example 7
Source File: LibSpoofPrimitives.java    From systemds with Apache License 2.0 4 votes vote down vote up
public static double[] vectTanWrite(double[] a, int ai, int len) {
	double[] c = allocVector(len, false);
	for( int j = 0; j < len; j++, ai++)
		c[j] = FastMath.tan(a[ai]);
	return c;
}
 
Example 8
Source File: Tan.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public double value(double x) {
    return FastMath.tan(x);
}
 
Example 9
Source File: StableRandomGenerator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Generate a random scalar with zero location and unit scale.
 *
 * @return a random scalar with zero location and unit scale
 */
public double nextNormalizedDouble() {
    // we need 2 uniform random numbers to calculate omega and phi
    double omega = -FastMath.log(generator.nextDouble());
    double phi = FastMath.PI * (generator.nextDouble() - 0.5);

    // Normal distribution case (Box-Muller algorithm)
    if (alpha == 2d) {
        return FastMath.sqrt(2d * omega) * FastMath.sin(phi);
    }

    double x;
    // when beta = 0, zeta is zero as well
    // Thus we can exclude it from the formula
    if (beta == 0d) {
        // Cauchy distribution case
        if (alpha == 1d) {
            x = FastMath.tan(phi);
        } else {
            x = FastMath.pow(omega * FastMath.cos((1 - alpha) * phi),
                1d / alpha - 1d) *
                FastMath.sin(alpha * phi) /
                FastMath.pow(FastMath.cos(phi), 1d / alpha);
        }
    } else {
        // Generic stable distribution
        double cosPhi = FastMath.cos(phi);
        // to avoid rounding errors around alpha = 1
        if (FastMath.abs(alpha - 1d) > 1e-8) {
            double alphaPhi = alpha * phi;
            double invAlphaPhi = phi - alphaPhi;
            x = (FastMath.sin(alphaPhi) + zeta * FastMath.cos(alphaPhi)) / cosPhi *
                (FastMath.cos(invAlphaPhi) + zeta * FastMath.sin(invAlphaPhi)) /
                 FastMath.pow(omega * cosPhi, (1 - alpha) / alpha);
        } else {
            double betaPhi = FastMath.PI / 2 + beta * phi;
            x = 2d / FastMath.PI * (betaPhi * FastMath.tan(phi) - beta *
                FastMath.log(FastMath.PI / 2d * omega * cosPhi / betaPhi));

            if (alpha != 1d) {
                x = x + beta * FastMath.tan(FastMath.PI * alpha / 2);
            }
        }
    }
    return x;
}
 
Example 10
Source File: DSCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** Compute tangent of a derivative structure.
 * @param operand array holding the operand
 * @param operandOffset offset of the operand in its array
 * @param result array where result must be stored (for
 * tangent the result array <em>cannot</em> be the input
 * array)
 * @param resultOffset offset of the result in its array
 */
public void tan(final double[] operand, final int operandOffset,
                final double[] result, final int resultOffset) {

    // create the function value and derivatives
    final double[] function = new double[1 + order];
    final double t = FastMath.tan(operand[operandOffset]);
    function[0] = t;

    if (order > 0) {

        // the nth order derivative of tan has the form:
        // dn(tan(x)/dxn = P_n(tan(x))
        // where P_n(t) is a degree n+1 polynomial with same parity as n+1
        // P_0(t) = t, P_1(t) = 1 + t^2, P_2(t) = 2 t (1 + t^2) ...
        // the general recurrence relation for P_n is:
        // P_n(x) = (1+t^2) P_(n-1)'(t)
        // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array
        final double[] p = new double[order + 2];
        p[1] = 1;
        final double t2 = t * t;
        for (int n = 1; n <= order; ++n) {

            // update and evaluate polynomial P_n(t)
            double v = 0;
            p[n + 1] = n * p[n];
            for (int k = n + 1; k >= 0; k -= 2) {
                v = v * t2 + p[k];
                if (k > 2) {
                    p[k - 2] = (k - 1) * p[k - 1] + (k - 3) * p[k - 3];
                } else if (k == 2) {
                    p[0] = p[1];
                }
            }
            if ((n & 0x1) == 0) {
                v *= t;
            }

            function[n] = v;

        }
    }

    // apply function composition
    compose(operand, operandOffset, function, result, resultOffset);

}
 
Example 11
Source File: Tan.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public double value(double x) {
    return FastMath.tan(x);
}
 
Example 12
Source File: DSCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** Compute tangent of a derivative structure.
 * @param operand array holding the operand
 * @param operandOffset offset of the operand in its array
 * @param result array where result must be stored (for
 * tangent the result array <em>cannot</em> be the input
 * array)
 * @param resultOffset offset of the result in its array
 */
public void tan(final double[] operand, final int operandOffset,
                final double[] result, final int resultOffset) {

    // create the function value and derivatives
    final double[] function = new double[1 + order];
    final double t = FastMath.tan(operand[operandOffset]);
    function[0] = t;

    if (order > 0) {

        // the nth order derivative of tan has the form:
        // dn(tan(x)/dxn = P_n(tan(x))
        // where P_n(t) is a degree n+1 polynomial with same parity as n+1
        // P_0(t) = t, P_1(t) = 1 + t^2, P_2(t) = 2 t (1 + t^2) ...
        // the general recurrence relation for P_n is:
        // P_n(x) = (1+t^2) P_(n-1)'(t)
        // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array
        final double[] p = new double[order + 2];
        p[1] = 1;
        final double t2 = t * t;
        for (int n = 1; n <= order; ++n) {

            // update and evaluate polynomial P_n(t)
            double v = 0;
            p[n + 1] = n * p[n];
            for (int k = n + 1; k >= 0; k -= 2) {
                v = v * t2 + p[k];
                if (k > 2) {
                    p[k - 2] = (k - 1) * p[k - 1] + (k - 3) * p[k - 3];
                } else if (k == 2) {
                    p[0] = p[1];
                }
            }
            if ((n & 0x1) == 0) {
                v *= t;
            }

            function[n] = v;

        }
    }

    // apply function composition
    compose(operand, operandOffset, function, result, resultOffset);

}
 
Example 13
Source File: StableRandomGenerator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Generate a random scalar with zero location and unit scale.
 *
 * @return a random scalar with zero location and unit scale
 */
public double nextNormalizedDouble() {
    // we need 2 uniform random numbers to calculate omega and phi
    double omega = -FastMath.log(generator.nextDouble());
    double phi = FastMath.PI * (generator.nextDouble() - 0.5);

    // Normal distribution case (Box-Muller algorithm)
    if (alpha == 2d) {
        return FastMath.sqrt(2d * omega) * FastMath.sin(phi);
    }

    double x;
    // when beta = 0, zeta is zero as well
    // Thus we can exclude it from the formula
    if (beta == 0d) {
        // Cauchy distribution case
        if (alpha == 1d) {
            x = FastMath.tan(phi);
        } else {
            x = FastMath.pow(omega * FastMath.cos((1 - alpha) * phi),
                1d / alpha - 1d) *
                FastMath.sin(alpha * phi) /
                FastMath.pow(FastMath.cos(phi), 1d / alpha);
        }
    } else {
        // Generic stable distribution
        double cosPhi = FastMath.cos(phi);
        // to avoid rounding errors around alpha = 1
        if (FastMath.abs(alpha - 1d) > 1e-8) {
            double alphaPhi = alpha * phi;
            double invAlphaPhi = phi - alphaPhi;
            x = (FastMath.sin(alphaPhi) + zeta * FastMath.cos(alphaPhi)) / cosPhi *
                (FastMath.cos(invAlphaPhi) + zeta * FastMath.sin(invAlphaPhi)) /
                 FastMath.pow(omega * cosPhi, (1 - alpha) / alpha);
        } else {
            double betaPhi = FastMath.PI / 2 + beta * phi;
            x = 2d / FastMath.PI * (betaPhi * FastMath.tan(phi) - beta *
                FastMath.log(FastMath.PI / 2d * omega * cosPhi / betaPhi));

            if (alpha != 1d) {
                x = x + beta * FastMath.tan(FastMath.PI * alpha / 2);
            }
        }
    }
    return x;
}
 
Example 14
Source File: DerivativeStructureTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testTrigo() {
    double epsilon = 2.0e-12;
    for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
        for (double x = 0.1; x < 1.2; x += 0.1) {
            DerivativeStructure dsX = new DerivativeStructure(3, maxOrder, 0, x);
            for (double y = 0.1; y < 1.2; y += 0.1) {
                DerivativeStructure dsY = new DerivativeStructure(3, maxOrder, 1, y);
                for (double z = 0.1; z < 1.2; z += 0.1) {
                    DerivativeStructure dsZ = new DerivativeStructure(3, maxOrder, 2, z);
                    DerivativeStructure f = dsX.divide(dsY.cos().add(dsZ.tan())).sin();
                    double a = FastMath.cos(y) + FastMath.tan(z);
                    double f0 = FastMath.sin(x / a);
                    Assert.assertEquals(f0, f.getValue(), FastMath.abs(epsilon * f0));
                    if (f.getOrder() > 0) {
                        double dfdx = FastMath.cos(x / a) / a;
                        Assert.assertEquals(dfdx, f.getPartialDerivative(1, 0, 0), FastMath.abs(epsilon * dfdx));
                        double dfdy =  x * FastMath.sin(y) * dfdx / a;
                        Assert.assertEquals(dfdy, f.getPartialDerivative(0, 1, 0), FastMath.abs(epsilon * dfdy));
                        double cz = FastMath.cos(z);
                        double cz2 = cz * cz;
                        double dfdz = -x * dfdx / (a * cz2);
                        Assert.assertEquals(dfdz, f.getPartialDerivative(0, 0, 1), FastMath.abs(epsilon * dfdz));
                        if (f.getOrder() > 1) {
                            double df2dx2 = -(f0 / (a * a));
                            Assert.assertEquals(df2dx2, f.getPartialDerivative(2, 0, 0), FastMath.abs(epsilon * df2dx2));
                            double df2dy2 = x * FastMath.cos(y) * dfdx / a -
                                            x * x * FastMath.sin(y) * FastMath.sin(y) * f0 / (a * a * a * a) +
                                            2 * FastMath.sin(y) * dfdy / a;
                            Assert.assertEquals(df2dy2, f.getPartialDerivative(0, 2, 0), FastMath.abs(epsilon * df2dy2));
                            double c4 = cz2 * cz2;
                            double df2dz2 = x * (2 * a * (1 - a * cz * FastMath.sin(z)) * dfdx - x * f0 / a ) / (a * a * a * c4);
                            Assert.assertEquals(df2dz2, f.getPartialDerivative(0, 0, 2), FastMath.abs(epsilon * df2dz2));
                            double df2dxdy = dfdy / x  - x * FastMath.sin(y) * f0 / (a * a * a);
                            Assert.assertEquals(df2dxdy, f.getPartialDerivative(1, 1, 0), FastMath.abs(epsilon * df2dxdy));
                        }
                    }
                }
            }
        }        
    }
}
 
Example 15
Source File: StableRandomGenerator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Generate a random scalar with zero location and unit scale.
 *
 * @return a random scalar with zero location and unit scale
 */
public double nextNormalizedDouble() {
    // we need 2 uniform random numbers to calculate omega and phi
    double omega = -FastMath.log(generator.nextDouble());
    double phi = FastMath.PI * (generator.nextDouble() - 0.5);

    // Normal distribution case (Box-Muller algorithm)
    if (alpha == 2d) {
        return FastMath.sqrt(2d * omega) * FastMath.sin(phi);
    }

    double x;
    // when beta = 0, zeta is zero as well
    // Thus we can exclude it from the formula
    if (beta == 0d) {
        // Cauchy distribution case
        if (alpha == 1d) {
            x = FastMath.tan(phi);
        } else {
            x = FastMath.pow(omega * FastMath.cos((1 - alpha) * phi),
                1d / alpha - 1d) *
                FastMath.sin(alpha * phi) /
                FastMath.pow(FastMath.cos(phi), 1d / alpha);
        }
    } else {
        // Generic stable distribution
        double cosPhi = FastMath.cos(phi);
        // to avoid rounding errors around alpha = 1
        if (FastMath.abs(alpha - 1d) > 1e-8) {
            double alphaPhi = alpha * phi;
            double invAlphaPhi = phi - alphaPhi;
            x = (FastMath.sin(alphaPhi) + zeta * FastMath.cos(alphaPhi)) / cosPhi *
                (FastMath.cos(invAlphaPhi) + zeta * FastMath.sin(invAlphaPhi)) /
                 FastMath.pow(omega * cosPhi, (1 - alpha) / alpha);
        } else {
            double betaPhi = FastMath.PI / 2 + beta * phi;
            x = 2d / FastMath.PI * (betaPhi * FastMath.tan(phi) - beta *
                FastMath.log(FastMath.PI / 2d * omega * cosPhi / betaPhi));

            if (alpha != 1d) {
                x = x + beta * FastMath.tan(FastMath.PI * alpha / 2);
            }
        }
    }
    return x;
}
 
Example 16
Source File: DerivativeStructureTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testTrigo() {
    double epsilon = 2.0e-12;
    for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
        for (double x = 0.1; x < 1.2; x += 0.1) {
            DerivativeStructure dsX = new DerivativeStructure(3, maxOrder, 0, x);
            for (double y = 0.1; y < 1.2; y += 0.1) {
                DerivativeStructure dsY = new DerivativeStructure(3, maxOrder, 1, y);
                for (double z = 0.1; z < 1.2; z += 0.1) {
                    DerivativeStructure dsZ = new DerivativeStructure(3, maxOrder, 2, z);
                    DerivativeStructure f = dsX.divide(dsY.cos().add(dsZ.tan())).sin();
                    double a = FastMath.cos(y) + FastMath.tan(z);
                    double f0 = FastMath.sin(x / a);
                    Assert.assertEquals(f0, f.getValue(), FastMath.abs(epsilon * f0));
                    if (f.getOrder() > 0) {
                        double dfdx = FastMath.cos(x / a) / a;
                        Assert.assertEquals(dfdx, f.getPartialDerivative(1, 0, 0), FastMath.abs(epsilon * dfdx));
                        double dfdy =  x * FastMath.sin(y) * dfdx / a;
                        Assert.assertEquals(dfdy, f.getPartialDerivative(0, 1, 0), FastMath.abs(epsilon * dfdy));
                        double cz = FastMath.cos(z);
                        double cz2 = cz * cz;
                        double dfdz = -x * dfdx / (a * cz2);
                        Assert.assertEquals(dfdz, f.getPartialDerivative(0, 0, 1), FastMath.abs(epsilon * dfdz));
                        if (f.getOrder() > 1) {
                            double df2dx2 = -(f0 / (a * a));
                            Assert.assertEquals(df2dx2, f.getPartialDerivative(2, 0, 0), FastMath.abs(epsilon * df2dx2));
                            double df2dy2 = x * FastMath.cos(y) * dfdx / a -
                                            x * x * FastMath.sin(y) * FastMath.sin(y) * f0 / (a * a * a * a) +
                                            2 * FastMath.sin(y) * dfdy / a;
                            Assert.assertEquals(df2dy2, f.getPartialDerivative(0, 2, 0), FastMath.abs(epsilon * df2dy2));
                            double c4 = cz2 * cz2;
                            double df2dz2 = x * (2 * a * (1 - a * cz * FastMath.sin(z)) * dfdx - x * f0 / a ) / (a * a * a * c4);
                            Assert.assertEquals(df2dz2, f.getPartialDerivative(0, 0, 2), FastMath.abs(epsilon * df2dz2));
                            double df2dxdy = dfdy / x  - x * FastMath.sin(y) * f0 / (a * a * a);
                            Assert.assertEquals(df2dxdy, f.getPartialDerivative(1, 1, 0), FastMath.abs(epsilon * df2dxdy));
                        }
                    }
                }
            }
        }        
    }
}
 
Example 17
Source File: StableRandomGenerator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Generate a random scalar with zero location and unit scale.
 *
 * @return a random scalar with zero location and unit scale
 */
public double nextNormalizedDouble() {
    // we need 2 uniform random numbers to calculate omega and phi
    double omega = -FastMath.log(generator.nextDouble());
    double phi = FastMath.PI * (generator.nextDouble() - 0.5);

    // Normal distribution case (Box-Muller algorithm)
    if (alpha == 2d) {
        return FastMath.sqrt(2d * omega) * FastMath.sin(phi);
    }

    double x;
    // when beta = 0, zeta is zero as well
    // Thus we can exclude it from the formula
    if (beta == 0d) {
        // Cauchy distribution case
        if (alpha == 1d) {
            x = FastMath.tan(phi);
        } else {
            x = FastMath.pow(omega * FastMath.cos((1 - alpha) * phi),
                1d / alpha - 1d) *
                FastMath.sin(alpha * phi) /
                FastMath.pow(FastMath.cos(phi), 1d / alpha);
        }
    } else {
        // Generic stable distribution
        double cosPhi = FastMath.cos(phi);
        // to avoid rounding errors around alpha = 1
        if (FastMath.abs(alpha - 1d) > 1e-8) {
            double alphaPhi = alpha * phi;
            double invAlphaPhi = phi - alphaPhi;
            x = (FastMath.sin(alphaPhi) + zeta * FastMath.cos(alphaPhi)) / cosPhi *
                (FastMath.cos(invAlphaPhi) + zeta * FastMath.sin(invAlphaPhi)) /
                 FastMath.pow(omega * cosPhi, (1 - alpha) / alpha);
        } else {
            double betaPhi = FastMath.PI / 2 + beta * phi;
            x = 2d / FastMath.PI * (betaPhi * FastMath.tan(phi) - beta *
                FastMath.log(FastMath.PI / 2d * omega * cosPhi / betaPhi));

            if (alpha != 1d) {
                x = x + beta * FastMath.tan(FastMath.PI * alpha / 2);
            }
        }
    }
    return x;
}
 
Example 18
Source File: Tan.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public double value(double x) {
    return FastMath.tan(x);
}
 
Example 19
Source File: LibSpoofPrimitives.java    From systemds with Apache License 2.0 4 votes vote down vote up
public static double[] vectTanWrite(double[] a, int ai, int len) {
	double[] c = allocVector(len, false);
	for( int j = 0; j < len; j++, ai++)
		c[j] = FastMath.tan(a[ai]);
	return c;
}
 
Example 20
Source File: DSCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** Compute tangent of a derivative structure.
 * @param operand array holding the operand
 * @param operandOffset offset of the operand in its array
 * @param result array where result must be stored (for
 * tangent the result array <em>cannot</em> be the input
 * array)
 * @param resultOffset offset of the result in its array
 */
public void tan(final double[] operand, final int operandOffset,
                final double[] result, final int resultOffset) {

    // create the function value and derivatives
    final double[] function = new double[1 + order];
    final double t = FastMath.tan(operand[operandOffset]);
    function[0] = t;

    if (order > 0) {

        // the nth order derivative of tan has the form:
        // dn(tan(x)/dxn = P_n(tan(x))
        // where P_n(t) is a degree n+1 polynomial with same parity as n+1
        // P_0(t) = t, P_1(t) = 1 + t^2, P_2(t) = 2 t (1 + t^2) ...
        // the general recurrence relation for P_n is:
        // P_n(x) = (1+t^2) P_(n-1)'(t)
        // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array
        final double[] p = new double[order + 2];
        p[1] = 1;
        final double t2 = t * t;
        for (int n = 1; n <= order; ++n) {

            // update and evaluate polynomial P_n(t)
            double v = 0;
            p[n + 1] = n * p[n];
            for (int k = n + 1; k >= 0; k -= 2) {
                v = v * t2 + p[k];
                if (k > 2) {
                    p[k - 2] = (k - 1) * p[k - 1] + (k - 3) * p[k - 3];
                } else if (k == 2) {
                    p[0] = p[1];
                }
            }
            if ((n & 0x1) == 0) {
                v *= t;
            }

            function[n] = v;

        }
    }

    // apply function composition
    compose(operand, operandOffset, function, result, resultOffset);

}