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

The following examples show how to use org.apache.commons.math3.util.FastMath#cos() . 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: NPEfix_00173_t.java    From coming with MIT License 6 votes vote down vote up
/** Reset the instance as if built from two points.
 * <p>The line is oriented from p1 to p2</p>
 * @param p1 first point
 * @param p2 second point
 */
public void reset(final Vector2D p1, final Vector2D p2) {
    final double dx = p2.getX() - p1.getX();
    final double dy = p2.getY() - p1.getY();
    final double d = FastMath.hypot(dx, dy);
    if (d == 0.0) {
        angle        = 0.0;
        cos          = 1.0;
        sin          = 0.0;
        originOffset = p1.getY();
    } else {
        angle        = FastMath.PI + FastMath.atan2(-dy, -dx);
        cos          = FastMath.cos(angle);
        sin          = FastMath.sin(angle);
        originOffset = (p2.getX() * p1.getY() - p1.getX() * p2.getY()) / d;
    }
}
 
Example 2
Source File: HarmonicOscillatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testParametricGradient() {
    final double amplitude = 2;
    final double omega = 3;
    final double phase = 4;
    final HarmonicOscillator.Parametric f = new HarmonicOscillator.Parametric();

    final double x = 1;
    final double[] grad = f.gradient(1, new double[] {amplitude, omega, phase});
    final double xTimesOmegaPlusPhase = omega * x + phase;
    final double a = FastMath.cos(xTimesOmegaPlusPhase);
    Assert.assertEquals(a, grad[0], EPS);
    final double w = -amplitude * x * FastMath.sin(xTimesOmegaPlusPhase);
    Assert.assertEquals(w, grad[1], EPS);
    final double p = -amplitude * FastMath.sin(xTimesOmegaPlusPhase);
    Assert.assertEquals(p, grad[2], EPS);
}
 
Example 3
Source File: Math_10_DSCompiler_s.java    From coming with MIT License 6 votes vote down vote up
/** Compute sine 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
 * sine the result array <em>cannot</em> be the input
 * array)
 * @param resultOffset offset of the result in its array
 */
public void sin(final double[] operand, final int operandOffset,
                final double[] result, final int resultOffset) {

    // create the function value and derivatives
    double[] function = new double[1 + order];
    function[0] = FastMath.sin(operand[operandOffset]);
    if (order > 0) {
        function[1] = FastMath.cos(operand[operandOffset]);
        for (int i = 2; i <= order; ++i) {
            function[i] = -function[i - 2];
        }
    }

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

}
 
Example 4
Source File: SincTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testEuler() {
    final Sinc s = new Sinc();
    final double x = 123456.789;
    double prod = 1;
    double xOverPow2 = x / 2;
    while (xOverPow2 > 0) {
        prod *= FastMath.cos(xOverPow2);
        xOverPow2 /= 2;
    }
    Assert.assertEquals(prod, s.value(x), 1e-13);
}
 
Example 5
Source File: RootsOfUnity.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Computes the {@code n}-th roots of unity. The roots are stored in
 * {@code omega[]}, such that {@code omega[k] = w ^ k}, where
 * {@code k = 0, ..., n - 1}, {@code w = exp(2 * pi * i / n)} and
 * {@code i = sqrt(-1)}.
 * </p>
 * <p>
 * Note that {@code n} can be positive of negative
 * </p>
 * <ul>
 * <li>{@code abs(n)} is always the number of roots of unity.</li>
 * <li>If {@code n > 0}, then the roots are stored in counter-clockwise order.</li>
 * <li>If {@code n < 0}, then the roots are stored in clockwise order.</p>
 * </ul>
 *
 * @param n the (signed) number of roots of unity to be computed
 * @throws ZeroException if {@code n = 0}
 */
public synchronized void computeRoots(int n) throws ZeroException {

    if (n == 0) {
        throw new ZeroException(
                LocalizedFormats.CANNOT_COMPUTE_0TH_ROOT_OF_UNITY);
    }

    isCounterClockWise = n > 0;

    // avoid repetitive calculations
    final int absN = FastMath.abs(n);

    if (absN == omegaCount) {
        return;
    }

    // calculate everything from scratch
    final double t = 2.0 * FastMath.PI / absN;
    final double cosT = FastMath.cos(t);
    final double sinT = FastMath.sin(t);
    omegaReal = new double[absN];
    omegaImaginaryCounterClockwise = new double[absN];
    omegaImaginaryClockwise = new double[absN];
    omegaReal[0] = 1.0;
    omegaImaginaryCounterClockwise[0] = 0.0;
    omegaImaginaryClockwise[0] = 0.0;
    for (int i = 1; i < absN; i++) {
        omegaReal[i] = omegaReal[i - 1] * cosT -
                omegaImaginaryCounterClockwise[i - 1] * sinT;
        omegaImaginaryCounterClockwise[i] = omegaReal[i - 1] * sinT +
                omegaImaginaryCounterClockwise[i - 1] * cosT;
        omegaImaginaryClockwise[i] = -omegaImaginaryCounterClockwise[i];
    }
    omegaCount = absN;
}
 
Example 6
Source File: JacobianFunctionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public double[][] jacobian(double[] point) {
    final double cLat = FastMath.cos(point[0]);
    final double sLat = FastMath.sin(point[0]);
    final double cLon = FastMath.cos(point[1]);
    final double sLon = FastMath.sin(point[1]);
    return new double[][] {
        { -radius * cLon * sLat, -radius * sLon * cLat },
        { -radius * sLon * sLat,  radius * cLon * cLat },
        {  radius * cLat,         0  }
    };
}
 
Example 7
Source File: JacobianMatricesTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public double[][] exactDyDpDot(double t) {
    double cos  = FastMath.cos(omega * t);
    double sin  = FastMath.sin(omega * t);
    double oCos = omega * cos;
    double oSin = omega * sin;
    double dx0  = y0[0] - cx;
    double dy0  = y0[1] - cy;
    return new double[][] {
        {  oSin, oCos, -sin * dx0 - cos * dy0 - t * ( oCos * dx0 - oSin * dy0) },
        { -oCos, oSin,  cos * dx0 - sin * dy0 + t * (-oSin * dx0 - oCos * dy0) }
    };
}
 
Example 8
Source File: NPEfix_00168_s.java    From coming with MIT License 5 votes vote down vote up
/** Copy constructor.
 * <p>The created instance is completely independent from the
 * original instance, it is a deep copy.</p>
 * @param line line to copy
 */
public Line(final Line line) {
    angle        = MathUtils.normalizeAngle(line.angle, FastMath.PI);
    cos          = FastMath.cos(angle);
    sin          = FastMath.sin(angle);
    originOffset = line.originOffset;
}
 
Example 9
Source File: Line.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Set the angle of the line.
 * @param angle new angle of the line with respect to the abscissa axis
 */
public void setAngle(final double angle) {
    unlinkReverse();
    this.angle = MathUtils.normalizeAngle(angle, FastMath.PI);
    cos        = FastMath.cos(this.angle);
    sin        = FastMath.sin(this.angle);
}
 
Example 10
Source File: NPEfix_00163_t.java    From coming with MIT License 5 votes vote down vote up
/** Copy constructor.
 * <p>The created instance is completely independent from the
 * original instance, it is a deep copy.</p>
 * @param line line to copy
 */
public Line(final Line line) {
    angle        = MathUtils.normalizeAngle(line.angle, FastMath.PI);
    cos          = FastMath.cos(angle);
    sin          = FastMath.sin(angle);
    originOffset = line.originOffset;
}
 
Example 11
Source File: NPEfix_00162_t.java    From coming with MIT License 5 votes vote down vote up
/** Reset the instance as if built from a line and an angle.
 * @param p point belonging to the line
 * @param alpha angle of the line with respect to abscissa axis
 */
public void reset(final Vector2D p, final double alpha) {
    this.angle   = MathUtils.normalizeAngle(alpha, FastMath.PI);
    cos          = FastMath.cos(this.angle);
    sin          = FastMath.sin(this.angle);
    originOffset = cos * p.getY() - sin * p.getX();
}
 
Example 12
Source File: TestProblem4.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Simple constructor. */
public TestProblem4() {
  super();
  a = 1.2;
  double[] y0 = { FastMath.sin(a), FastMath.cos(a) };
  setInitialConditions(0.0, y0);
  setFinalConditions(15);
  double[] errorScale = { 1.0, 0.0 };
  setErrorScale(errorScale);
  y = new double[y0.length];
}
 
Example 13
Source File: SincTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testEuler() {
    final Sinc s = new Sinc();
    final double x = 123456.789;
    double prod = 1;
    double xOverPow2 = x / 2;
    while (xOverPow2 > 0) {
        prod *= FastMath.cos(xOverPow2);
        xOverPow2 /= 2;
    }
    Assert.assertEquals(prod, s.value(x), 1e-13);
}
 
Example 14
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public double[] computeValue(double[] variables) {
    double x1 = variables[0];
    double x2 = variables[1];
    double x3 = variables[2];
    double x4 = variables[3];
    double[] f = new double[m];
    for (int i = 0; i < m; ++i) {
        double temp = (i + 1) / 5.0;
        double tmp1 = x1 + temp * x2 - FastMath.exp(temp);
        double tmp2 = x3 + FastMath.sin(temp) * x4 - FastMath.cos(temp);
        f[i] = tmp1 * tmp1 + tmp2 * tmp2;
    }
    return f;
}
 
Example 15
Source File: NPEfix_00160_t.java    From coming with MIT License 5 votes vote down vote up
/** Reset the instance as if built from a line and an angle.
 * @param p point belonging to the line
 * @param alpha angle of the line with respect to abscissa axis
 */
public void reset(final Vector2D p, final double alpha) {
    this.angle   = MathUtils.normalizeAngle(alpha, FastMath.PI);
    cos          = FastMath.cos(this.angle);
    sin          = FastMath.sin(this.angle);
    originOffset = cos * p.getY() - sin * p.getX();
}
 
Example 16
Source File: CMAESOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public double value(double[] x) {
    double f = 0;
    double fac;
    for (int i = 0; i < x.length; ++i) {
        fac = FastMath.pow(axisratio, (i - 1.) / (x.length - 1.));
        if (i == 0 && x[i] < 0)
            fac *= 1.;
        f += fac * fac * x[i] * x[i] + amplitude
        * (1. - FastMath.cos(2. * FastMath.PI * fac * x[i]));
    }
    return f;
}
 
Example 17
Source File: Arja_0033_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Compute the
 * <a href="http://mathworld.wolfram.com/HyperbolicTangent.html" TARGET="_top">
 * hyperbolic tangent</a> of this complex number.
 * Implements the formula:
 * <pre>
 *  <code>
 *   tan(a + bi) = sinh(2a)/(cosh(2a)+cos(2b)) + [sin(2b)/(cosh(2a)+cos(2b))]i
 *  </code>
 * </pre>
 * where the (real) functions on the right-hand side are
 * {@link FastMath#sin}, {@link FastMath#cos}, {@link FastMath#cosh} and
 * {@link FastMath#sinh}.
 * <br/>
 * Returns {@link Complex#NaN} if either real or imaginary part of the
 * input argument is {@code NaN}.
 * <br/>
 * Infinite values in real or imaginary parts of the input may result in
 * infinite or NaN values returned in parts of the result.
 * <pre>
 *  Examples:
 *  <code>
 *   tanh(a &plusmn; INFINITY i) = NaN + NaN i
 *   tanh(&plusmn;INFINITY + bi) = &plusmn;1 + 0 i
 *   tanh(&plusmn;INFINITY &plusmn; INFINITY i) = NaN + NaN i
 *   tanh(0 + (&pi;/2)i) = NaN + INFINITY i
 *  </code>
 * </pre>
 *
 * @return the hyperbolic tangent of {@code this}.
 * @since 1.2
 */
public Complex tanh() {
    if (isNaN || Double.isInfinite(imaginary)) {
        return NaN;
    }
    if (real > 20.0) {
        return createComplex(1.0, 0.0);
    }
    if (real < -20.0) {
        return createComplex(-1.0, 0.0);
    }
    double real2 = 2.0 * real;
    double imaginary2 = 2.0 * imaginary;
    double d = FastMath.cosh(real2) + FastMath.cos(imaginary2);

    return createComplex(FastMath.sinh(real2) / d,
                         FastMath.sin(imaginary2) / d);
}
 
Example 18
Source File: NPEfix_00166_t.java    From coming with MIT License 4 votes vote down vote up
/** Set the angle of the line.
 * @param angle new angle of the line with respect to the abscissa axis
 */
public void setAngle(final double angle) {
    this.angle = MathUtils.normalizeAngle(angle, FastMath.PI);
    cos        = FastMath.cos(this.angle);
    sin        = FastMath.sin(this.angle);
}
 
Example 19
Source File: JacobianMatricesTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public double[] exactDyDcx(double t) {
    double cos = FastMath.cos(omega * t);
    double sin = FastMath.sin(omega * t);
    return new double[] {1 - cos, -sin};
}
 
Example 20
Source File: JGenProg2017_0061_s.java    From coming with MIT License 3 votes vote down vote up
/** Simple constructor.
 * Build a vector from its azimuthal coordinates
 * @param alpha azimuth (&alpha;) around Z
 *              (0 is +X, &pi;/2 is +Y, &pi; is -X and 3&pi;/2 is -Y)
 * @param delta elevation (&delta;) above (XY) plane, from -&pi;/2 to +&pi;/2
 * @see #getAlpha()
 * @see #getDelta()
 */
public Vector3D(double alpha, double delta) {
    double cosDelta = FastMath.cos(delta);
    this.x = FastMath.cos(alpha) * cosDelta;
    this.y = FastMath.sin(alpha) * cosDelta;
    this.z = FastMath.sin(delta);
}