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

The following examples show how to use org.apache.commons.math.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: BitsStreamGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public double nextGaussian() {

    final double random;
    if (Double.isNaN(nextGaussian)) {
        // generate a new pair of gaussian numbers
        final double x = nextDouble();
        final double y = nextDouble();
        final double alpha = 2 * FastMath.PI * x;
        final double r      = FastMath.sqrt(-2 * FastMath.log(y));
        random       = r * FastMath.cos(alpha);
        nextGaussian = r * FastMath.sin(alpha);
    } else {
        // use the second element of the pair already generated
        random = nextGaussian;
        nextGaussian = Double.NaN;
    }

    return random;

}
 
Example 2
Source File: Math_53_Complex_t.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 3
Source File: 1_Complex.java    From SimFix 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 4
Source File: JGenProg2017_0065_t.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 5
Source File: FastFourierTransformer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Computes the n<sup>th</sup> roots of unity.
 * <p>The computed omega[] = { 1, w, w<sup>2</sup>, ... w<sup>(n-1)</sup> } where
 * w = exp(-2 &pi; i / n), i = &sqrt;(-1).</p>
 * <p>Note that n is positive for
 * forward transform and negative for inverse transform.</p>
 * @param n number of roots of unity to compute,
 * positive for forward transform, negative for inverse transform
 * @throws IllegalArgumentException if n = 0
 */
public synchronized void computeOmega(int n) throws IllegalArgumentException {

  if (n == 0) {
    throw MathRuntimeException.createIllegalArgumentException(
            LocalizedFormats.CANNOT_COMPUTE_0TH_ROOT_OF_UNITY);
  }

  isForward = n > 0;

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

  if (absN == omegaCount) {
      return;
  }

  // calculate everything from scratch, for both forward and inverse versions
  final double t    = 2.0 * FastMath.PI / absN;
  final double cosT = FastMath.cos(t);
  final double sinT = FastMath.sin(t);
  omegaReal             = new double[absN];
  omegaImaginaryForward = new double[absN];
  omegaImaginaryInverse = new double[absN];
  omegaReal[0]             = 1.0;
  omegaImaginaryForward[0] = 0.0;
  omegaImaginaryInverse[0] = 0.0;
  for (int i = 1; i < absN; i++) {
    omegaReal[i] =
      omegaReal[i-1] * cosT + omegaImaginaryForward[i-1] * sinT;
    omegaImaginaryForward[i] =
       omegaImaginaryForward[i-1] * cosT - omegaReal[i-1] * sinT;
    omegaImaginaryInverse[i] = -omegaImaginaryForward[i];
  }
  omegaCount = absN;

}
 
Example 6
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 7
Source File: HarmonicCoefficientsGuesser.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Estimate a first guess of the &phi; coefficient.
 */
private void guessPhi() {

    // initialize the means
    double fcMean = 0.0;
    double fsMean = 0.0;

    double currentX = observations[0].getX();
    double currentY = observations[0].getY();
    for (int i = 1; i < observations.length; ++i) {

        // one step forward
        final double previousX = currentX;
        final double previousY = currentY;
        currentX = observations[i].getX();
        currentY = observations[i].getY();
        final double currentYPrime = (currentY - previousY) / (currentX - previousX);

        double   omegaX = omega * currentX;
        double   cosine = FastMath.cos(omegaX);
        double   sine   = FastMath.sin(omegaX);
        fcMean += omega * currentY * cosine - currentYPrime *   sine;
        fsMean += omega * currentY *   sine + currentYPrime * cosine;

    }

    phi = FastMath.atan2(-fsMean, fcMean);

}
 
Example 8
Source File: SinFunction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public UnivariateRealFunction derivative() {
    return new UnivariateRealFunction() {
        public double value(double x) {
            return FastMath.cos(x);
        }
    };
}
 
Example 9
Source File: JGenProg2017_0028_t.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 10
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 11
Source File: TestProblem3.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public double[] computeTheoreticalState(double t) {

  // solve Kepler's equation
  double E = t;
  double d = 0;
  double corr = 999.0;
  for (int i = 0; (i < 50) && (FastMath.abs(corr) > 1.0e-12); ++i) {
    double f2  = e * FastMath.sin(E);
    double f0  = d - f2;
    double f1  = 1 - e * FastMath.cos(E);
    double f12 = f1 + f1;
    corr  = f0 * f12 / (f1 * f12 - f0 * f2);
    d -= corr;
    E = t + d;
  }

  double cosE = FastMath.cos(E);
  double sinE = FastMath.sin(E);

  y[0] = cosE - e;
  y[1] = FastMath.sqrt(1 - e * e) * sinE;
  y[2] = -sinE / (1 - e * cosE);
  y[3] = FastMath.sqrt(1 - e * e) * cosE / (1 - e * cosE);

  return y;
}
 
Example 12
Source File: HarmonicFitter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public double[] gradient(double x, double[] parameters) {
    final double a     = parameters[0];
    final double omega = parameters[1];
    final double phi   = parameters[2];
    final double alpha = omega * x + phi;
    final double cosAlpha = FastMath.cos(alpha);
    final double sinAlpha = FastMath.sin(alpha);
    return new double[] { cosAlpha, -a * x * sinAlpha, -a * sinAlpha };
}
 
Example 13
Source File: Math_37_Complex_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Compute the
 * <a href="http://mathworld.wolfram.com/Tangent.html" TARGET="_top">
 * tangent</a> of this complex number.
 * Implements the formula:
 * <pre>
 *  <code>
 *   tan(a + bi) = sin(2a)/(cos(2a)+cosh(2b)) + [sinh(2b)/(cos(2a)+cosh(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 (or critical) 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>
 *   tan(a &plusmn; INFINITY i) = 0 &plusmn; i
 *   tan(&plusmn;INFINITY + bi) = NaN + NaN i
 *   tan(&plusmn;INFINITY &plusmn; INFINITY i) = NaN + NaN i
 *   tan(&plusmn;&pi;/2 + 0 i) = &plusmn;INFINITY + NaN i
 *  </code>
 * </pre>
 *
 * @return the tangent of {@code this}.
 * @since 1.2
 */
public Complex tan() {
    if (isNaN || Double.isInfinite(real)) {
        return NaN;
    }
    if (imaginary > 20.0) {
        return createComplex(0.0, 1.0);
    }
    if (imaginary < -20.0) {
        return createComplex(0.0, -1.0);
    }

    double real2 = 2.0 * real;
    double imaginary2 = 2.0 * imaginary;
    double d = FastMath.cos(real2) + FastMath.cosh(imaginary2);

    return createComplex(FastMath.sin(real2) / d,
                         FastMath.sinh(imaginary2) / d);
}
 
Example 14
Source File: Rotation.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** Build a rotation from an axis and an angle.
 * <p>We use the convention that angles are oriented according to
 * the effect of the rotation on vectors around the axis. That means
 * that if (i, j, k) is a direct frame and if we first provide +k as
 * the axis and &pi;/2 as the angle to this constructor, and then
 * {@link #applyTo(Vector3D) apply} the instance to +i, we will get
 * +j.</p>
 * <p>Another way to represent our convention is to say that a rotation
 * of angle &theta; about the unit vector (x, y, z) is the same as the
 * rotation build from quaternion components { cos(-&theta;/2),
 * x * sin(-&theta;/2), y * sin(-&theta;/2), z * sin(-&theta;/2) }.
 * Note the minus sign on the angle!</p>
 * <p>On the one hand this convention is consistent with a vectorial
 * perspective (moving vectors in fixed frames), on the other hand it
 * is different from conventions with a frame perspective (fixed vectors
 * viewed from different frames) like the ones used for example in spacecraft
 * attitude community or in the graphics community.</p>
 * @param axis axis around which to rotate
 * @param angle rotation angle.
 * @exception ArithmeticException if the axis norm is zero
 */
public Rotation(Vector3D axis, double angle) {

  double norm = axis.getNorm();
  if (norm == 0) {
    throw MathRuntimeException.createArithmeticException(LocalizedFormats.ZERO_NORM_FOR_ROTATION_AXIS);
  }

  double halfAngle = -0.5 * angle;
  double coeff = FastMath.sin(halfAngle) / norm;

  q0 = FastMath.cos (halfAngle);
  q1 = coeff * axis.getX();
  q2 = coeff * axis.getY();
  q3 = coeff * axis.getZ();

}
 
Example 15
Source File: TricubicSplineInterpolatorTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Test of interpolator for a sine wave.
 * <p>
 * <p>
 *  f(x, y, z) = a cos [&omega; z - k<sub>y</sub> x - k<sub>y</sub> y]
 * </p>
 * with A = 0.2, &omega; = 0.5, k<sub>x</sub> = 2, k<sub>y</sub> = 1.
 */
@Test
public void testWave() {
    double[] xval = new double[] {3, 4, 5, 6.5};
    double[] yval = new double[] {-4, -3, -1, 2, 2.5};
    double[] zval = new double[] {-12, -8, -5.5, -3, 0, 4};

    final double a = 0.2;
    final double omega = 0.5;
    final double kx = 2;
    final double ky = 1;

    // Function values
    TrivariateRealFunction f = new TrivariateRealFunction() {
            public double value(double x, double y, double z) {
                return a * FastMath.cos(omega * z - kx * x - ky * y);
            }
        };
    
    double[][][] fval = new double[xval.length][yval.length][zval.length];
    for (int i = 0; i < xval.length; i++) {
        for (int j = 0; j < yval.length; j++) {
            for (int k = 0; k < zval.length; k++) {
                fval[i][j][k] = f.value(xval[i], yval[j], zval[k]);
            }
        }
    }

    TrivariateRealGridInterpolator interpolator = new TricubicSplineInterpolator();

    TrivariateRealFunction p = interpolator.interpolate(xval, yval, zval, fval);
    double x, y, z;
    double expected, result;
    
    x = 4;
    y = -3;
    z = 0;
    expected = f.value(x, y, z);
    result = p.value(x, y, z);
    Assert.assertEquals("On sample point",
                        expected, result, 1e-12);

    x = 4.5;
    y = -1.5;
    z = -4.25;
    expected = f.value(x, y, z);
    result = p.value(x, y, z);
    Assert.assertEquals("Half-way between sample points (middle of the patch)",
                        expected, result, 0.1);

    x = 3.5;
    y = -3.5;
    z = -10;
    expected = f.value(x, y, z);
    result = p.value(x, y, z);
    Assert.assertEquals("Half-way between sample points (border of the patch)",
                        expected, result, 0.1);
}
 
Example 16
Source File: Complex_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 17
Source File: Cardumen_00218_s.java    From coming with MIT License 3 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 java.lang.Math#sin}, {@link java.lang.Math#cos},
 * {@link MathUtils#cosh} and {@link MathUtils#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(1 &plusmn; INFINITY i) = NaN + NaN i
 *   tanh(&plusmn;INFINITY + i) = NaN + 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) {
        return NaN;
    }

    double real2 = 2.0 * real;
    double imaginary2 = 2.0 * imaginary;
    double d = MathUtils.cosh(real2) + FastMath.cos(imaginary2);

    return createComplex(MathUtils.sinh(real2) / d,
                         FastMath.sin(imaginary2) / d);
}
 
Example 18
Source File: Complex.java    From astor with GNU General Public License v2.0 3 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 java.lang.Math#sin}, {@link java.lang.Math#cos},
 * {@link MathUtils#cosh} and {@link MathUtils#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(1 &plusmn; INFINITY i) = NaN + NaN i
 *   tanh(&plusmn;INFINITY + i) = NaN + 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) {
        return NaN;
    }

    double real2 = 2.0 * real;
    double imaginary2 = 2.0 * imaginary;
    double d = MathUtils.cosh(real2) + FastMath.cos(imaginary2);

    return createComplex(MathUtils.sinh(real2) / d,
                         FastMath.sin(imaginary2) / d);
}
 
Example 19
Source File: Cardumen_0044_s.java    From coming with MIT License 3 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 java.lang.Math#sin}, {@link java.lang.Math#cos},
 * {@link MathUtils#cosh} and {@link MathUtils#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(1 &plusmn; INFINITY i) = NaN + NaN i
 *   tanh(&plusmn;INFINITY + i) = NaN + 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) {
        return NaN;
    }

    double real2 = 2.0 * real;
    double imaginary2 = 2.0 * imaginary;
    double d = MathUtils.cosh(real2) + FastMath.cos(imaginary2);

    return createComplex(MathUtils.sinh(real2) / d,
                         FastMath.sin(imaginary2) / d);
}
 
Example 20
Source File: Cardumen_00168_t.java    From coming with MIT License 3 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 java.lang.Math#sin}, {@link java.lang.Math#cos},
 * {@link MathUtils#cosh} and {@link MathUtils#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(1 &plusmn; INFINITY i) = NaN + NaN i
 *   tanh(&plusmn;INFINITY + i) = NaN + 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) {
        return NaN;
    }

    double real2 = 2.0 * real;
    double imaginary2 = 2.0 * imaginary;
    double d = MathUtils.cosh(real2) + FastMath.cos(imaginary2);

    return createComplex(MathUtils.sinh(real2) / d,
                         FastMath.sin(imaginary2) / d);
}