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

The following examples show how to use org.apache.commons.math3.util.FastMath#sin() . 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: DerivativeStructureTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testOneParameterConstructor() {
    double x = 1.2;
    double cos = FastMath.cos(x);
    double sin = FastMath.sin(x);
    DerivativeStructure yRef = new DerivativeStructure(1, 4, 0, x).cos();
    try {
        new DerivativeStructure(1, 4, 0.0, 0.0);
        Assert.fail("an exception should have been thrown");
    } catch (DimensionMismatchException dme) {
        // expected
    } catch (Exception e) {
        Assert.fail("wrong exceptionc caught " + e.getClass().getName());
    }
    double[] derivatives = new double[] { cos, -sin, -cos, sin, cos };
    DerivativeStructure y = new DerivativeStructure(1,  4, derivatives);
    checkEquals(yRef, y, 1.0e-15);
    TestUtils.assertEquals(derivatives, y.getAllDerivatives(), 1.0e-15);
}
 
Example 2
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double[][] computeJacobian(double[] variables) {
    double   x1 = variables[0];
    double   x2 = variables[1];
    double   x3 = variables[2];
    double   x4 = variables[3];
    double[][] jacobian = new double[m][];
    for (int i = 0; i < m; ++i) {
        double temp = (i + 1) / 5.0;
        double ti   = FastMath.sin(temp);
        double tmp1 = x1 + temp * x2 - FastMath.exp(temp);
        double tmp2 = x3 + ti   * x4 - FastMath.cos(temp);
        jacobian[i] = new double[] {
            2 * tmp1, 2 * temp * tmp1, 2 * tmp2, 2 * ti * tmp2
        };
    }
    return jacobian;
}
 
Example 3
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 4
Source File: CircleProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public double[] value(double[] params) {
    final double cx = params[0];
    final double cy = params[1];
    final double r = params[2];

    final double[] model = new double[points.size() * 2];

    final double deltaTheta = MathUtils.TWO_PI / resolution;
    for (int i = 0; i < points.size(); i++) {
        final double[] p = points.get(i);
        final double px = p[0];
        final double py = p[1];

        double bestX = 0;
        double bestY = 0;
        double dMin = Double.POSITIVE_INFINITY;

        // Find the angle for which the circle passes closest to the
        // current point (using a resolution of 100 points along the
        // circumference).
        for (double theta = 0; theta <= MathUtils.TWO_PI; theta += deltaTheta) {
            final double currentX = cx + r * FastMath.cos(theta);
            final double currentY = cy + r * FastMath.sin(theta);
            final double dX = currentX - px;
            final double dY = currentY - py;
            final double d = dX * dX + dY * dY;
            if (d < dMin) {
                dMin = d;
                bestX = currentX;
                bestY = currentY;
            }
        }

        final int index = i * 2;
        model[index] = bestX;
        model[index + 1] = bestY;
    }

    return model;
}
 
Example 5
Source File: KalmanFilterTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public Cannonball(double timeslice, double angle, double initialVelocity) {
    this.timeslice = timeslice;
    
    final double angleInRadians = FastMath.toRadians(angle);
    this.velocity = new double[] {
            initialVelocity * FastMath.cos(angleInRadians),
            initialVelocity * FastMath.sin(angleInRadians)
    };
    
    this.location = new double[] { 0, 0 };
}
 
Example 6
Source File: SubLine.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public SplitSubHyperplane<Euclidean2D> split(final Hyperplane<Euclidean2D> hyperplane) {

    final Line    thisLine  = (Line) getHyperplane();
    final Line    otherLine = (Line) hyperplane;
    final Vector2D crossing  = thisLine.intersection(otherLine);

    if (crossing == null) {
        // the lines are parallel
        final double global = otherLine.getOffset(thisLine);
        return (global < -1.0e-10) ?
               new SplitSubHyperplane<Euclidean2D>(null, this) :
               new SplitSubHyperplane<Euclidean2D>(this, null);
    }

    // the lines do intersect
    final boolean direct = FastMath.sin(thisLine.getAngle() - otherLine.getAngle()) < 0;
    final Vector1D x      = thisLine.toSubSpace(crossing);
    final SubHyperplane<Euclidean1D> subPlus  = new OrientedPoint(x, !direct).wholeHyperplane();
    final SubHyperplane<Euclidean1D> subMinus = new OrientedPoint(x,  direct).wholeHyperplane();

    final BSPTree<Euclidean1D> splitTree = getRemainingRegion().getTree(false).split(subMinus);
    final BSPTree<Euclidean1D> plusTree  = getRemainingRegion().isEmpty(splitTree.getPlus()) ?
                                           new BSPTree<Euclidean1D>(Boolean.FALSE) :
                                           new BSPTree<Euclidean1D>(subPlus, new BSPTree<Euclidean1D>(Boolean.FALSE),
                                                                    splitTree.getPlus(), null);
    final BSPTree<Euclidean1D> minusTree = getRemainingRegion().isEmpty(splitTree.getMinus()) ?
                                           new BSPTree<Euclidean1D>(Boolean.FALSE) :
                                           new BSPTree<Euclidean1D>(subMinus, new BSPTree<Euclidean1D>(Boolean.FALSE),
                                                                    splitTree.getMinus(), null);

    return new SplitSubHyperplane<Euclidean2D>(new SubLine(thisLine.copySelf(), new IntervalsSet(plusTree)),
                                               new SubLine(thisLine.copySelf(), new IntervalsSet(minusTree)));

}
 
Example 7
Source File: JacobianMatricesTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public double[] exactDyDom(double t) {
    double cos = FastMath.cos(omega * t);
    double sin = FastMath.sin(omega * t);
    double dx0 = y0[0] - cx;
    double dy0 = y0[1] - cy;
    return new double[] { -t * (sin * dx0 + cos * dy0) , t * (cos * dx0 - sin * dy0) };
}
 
Example 8
Source File: JacobianMatricesTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public double[] exactDyDom(double t) {
    double cos = FastMath.cos(omega * t);
    double sin = FastMath.sin(omega * t);
    double dx0 = y0[0] - cx;
    double dy0 = y0[1] - cy;
    return new double[] { -t * (sin * dx0 + cos * dy0) , t * (cos * dx0 - sin * dy0) };
}
 
Example 9
Source File: JacobianMatricesTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public double[] exactDyDom(double t) {
    double cos = FastMath.cos(omega * t);
    double sin = FastMath.sin(omega * t);
    double dx0 = y0[0] - cx;
    double dy0 = y0[1] - cy;
    return new double[] { -t * (sin * dx0 + cos * dy0) , t * (cos * dx0 - sin * dy0) };
}
 
Example 10
Source File: FirstOrderConverterTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private double integrateWithSpecifiedStep(double omega,
                                          double t0, double t,
                                          double step) throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException {
  double[] y0 = new double[2];
  y0[0] = FastMath.sin(omega * t0);
  y0[1] = omega * FastMath.cos(omega * t0);
  ClassicalRungeKuttaIntegrator i = new ClassicalRungeKuttaIntegrator(step);
  double[] y = new double[2];
  i.integrate(new FirstOrderConverter(new Equations(1, omega)), t0, y0, t, y);
  return y[0];
}
 
Example 11
Source File: NPEfix_00174_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 12
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 13
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create one point.
 *
 * @return a point.
 */
private Vector2D create() {
    final double t = tP.sample();
    final double pX = cX.sample() + radius * FastMath.cos(t);
    final double pY = cY.sample() + radius * FastMath.sin(t);

    return new Vector2D(pX, pY);
}
 
Example 14
Source File: NPEfix_00173_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 15
Source File: JacobianMatricesTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public double[][] exactDyDy0(double t) {
    double cos = FastMath.cos(omega * t);
    double sin = FastMath.sin(omega * t);
    return new double[][] {
        { cos, -sin },
        { sin,  cos }
    };
}
 
Example 16
Source File: NPEfix_00159_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 17
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 18
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 19
Source File: JacobianMatricesTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public double[] exactDyDcy(double t) {
    double cos = FastMath.cos(omega * t);
    double sin = FastMath.sin(omega * t);
    return new double[] {sin, 1 - cos};
}
 
Example 20
Source File: LibSpoofPrimitives.java    From systemds with Apache License 2.0 4 votes vote down vote up
public static void vectSinAdd(double[] a, double[] c, int ai, int ci, int len) {
	for( int j = ai; j < ai+len; j++, ci++)
		c[ci] +=  FastMath.sin(a[j]);
}