Example usage for org.apache.commons.math3.util FastMath tanh

List of usage examples for org.apache.commons.math3.util FastMath tanh

Introduction

In this page you can find the example usage for org.apache.commons.math3.util FastMath tanh.

Prototype

public static double tanh(double x) 

Source Link

Document

Compute the hyperbolic tangent of a number.

Usage

From source file:com.clust4j.kernel.HyperbolicTangentKernel.java

@Override
public double getSimilarity(double[] a, double[] b) {
    return FastMath.tanh(getAlpha() * VecUtils.innerProduct(a, b) + getConstant());
}

From source file:com.wwidesigner.geometry.calculation.DefaultHoleCalculator.java

@Override
public TransferMatrix calcTransferMatrix(Hole hole, boolean isOpen, double waveNumber,
        PhysicalParameters parameters) {
    double radius = mFudgeFactor * hole.getDiameter() / 2;
    double boreRadius = hole.getBoreDiameter() / 2;
    Complex Ys = Complex.ZERO; // Shunt admittance == 1/Zs
    Complex Za = Complex.ZERO; // Series impedance

    double Z0h = parameters.calcZ0(radius);
    double delta = radius / boreRadius;
    double delta2 = delta * delta;
    // double Z0 = parameters.calcZ0(boreRadius);
    // Z0 == Z0h * delta*delta

    double tm = (radius * delta / 8.) * (1. + 0.207 * delta * delta2);
    double te = hole.getHeight() + tm;

    double ta = 0.;

    // Complex Gamma = Complex.I.multiply(wave_number);

    if (isOpen) {
        double kb = waveNumber * radius;
        double ka = waveNumber * boreRadius;

        ta = (-0.35 + 0.06 * FastMath.tanh(2.7 * hole.getHeight() / radius)) * radius * delta2;

        Complex Zr = new Complex(0.25 * kb * kb,
                (0.822 - 0.47 * FastMath.pow(radius / (boreRadius + hole.getHeight()), 0.8)) * waveNumber
                        * radius);/*from   w  w  w .j a va2 s. co m*/
        double cos = FastMath.cos(waveNumber * te);
        Complex jsin = new Complex(0, FastMath.sin(waveNumber * te));

        Complex Zo = (Zr.multiply(cos).add(jsin)).divide(Zr.multiply(jsin).add(cos));

        double ti = radius
                * (0.822 + delta
                        * (-0.095 + delta * (-1.566 + delta * (2.138 + delta * (-1.640 + delta * 0.502)))))
                * (1. + (1. - 4.56 * delta + 6.55 * delta2) * ka
                        * (0.17 + ka * (0.92 + ka * (0.16 - 0.29 * ka))));

        Ys = Complex.ONE.divide(Complex.I.multiply(waveNumber * ti).add(Zo).multiply(Z0h));

    } else if (hole.getKey() == null) {
        // Tonehole closed by player's finger.
        if (hole.getHeight() <= AssumedFingerSize) {
            // Finger is likely to fill the hole. Ignore the hole entirely.
            ta = 0.;
            Ys = Complex.ZERO;
        } else {
            ta = (-0.12 - 0.17 * FastMath.tanh(2.4 * (hole.getHeight() - AssumedFingerSize) / radius)) * radius
                    * delta2;
            Ys = Complex.valueOf(0, FastMath.tan(waveNumber * (te - AssumedFingerSize)) / Z0h);
        }
    } else {
        // Tonehole closed by key.
        ta = (-0.12 - 0.17 * FastMath.tanh(2.4 * hole.getHeight() / radius)) * radius * delta2;
        Ys = Complex.valueOf(0, FastMath.tan(waveNumber * te) / Z0h);
    }

    Za = Complex.I.multiply(Z0h * delta2 * waveNumber * ta);
    Complex Za_Zs = Za.multiply(Ys);

    Complex A = Za_Zs.divide(2.).add(1.);
    Complex B = Za.multiply(Za_Zs.divide(4.).add(1.));
    Complex C = Ys;
    // Choose A and D to make the determinant = 1.
    // Complex A = Complex.ONE.add(B.multiply(C)).sqrt();
    TransferMatrix result = new TransferMatrix(A, B, C, A);

    // assert result.determinant() == Complex.ONE;

    return result;
}

From source file:de.tuberlin.uebb.jbop.example.DSCompilerOnlyCompose.java

@Override
public void tanh(final double[] operand, final double[] result) {

    // create the function value and derivatives
    final double[] function = new double[1 + order];
    final double t = FastMath.tanh(operand[0]);
    function[0] = t;//from w  ww.  j a v a  2s  .c om

    if (order > 0) {

        // the nth order derivative of tanh has the form:
        // dn(tanh(x)/dxn = P_n(tanh(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, function, result);

}

From source file:de.tuberlin.uebb.jbop.example.DSCompiler.java

@Override
@Optimizable//from   www  .j a  v a 2s . c  om
@StrictLoops
public void tanh(final double[] operand, final double[] result) {

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

    if (order > 0) {

        // the nth order derivative of tanh has the form:
        // dn(tanh(x)/dxn = P_n(tanh(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, function, result);

}

From source file:objenome.op.trig.HyperbolicTangent.java

/**
 * Evaluates this function. The child node is evaluated, the result of which
 * must be a numeric type (one of Double, Float, Long, Integer). The
 * hyperbolic tangent of this value becomes the result of this method as a
 * double value.// w  w  w . j av a  2 s  .c  om
 *
 * @return hyperbolic tangent of the value returned by the child
 */
@Override
public double value(double x) {
    return FastMath.tanh(x);
}

From source file:org.apache.sysml.runtime.codegen.LibSpoofPrimitives.java

public static void vectTanhAdd(double[] a, double[] c, int ai, int ci, int len) {
    for (int j = ai; j < ai + len; j++, ci++)
        c[ci] += FastMath.tanh(a[j]);
}

From source file:org.apache.sysml.runtime.codegen.LibSpoofPrimitives.java

public static void vectTanhAdd(double[] a, double[] c, int[] aix, int ai, int ci, int alen, int len) {
    for (int j = ai; j < ai + alen; j++)
        c[ci + aix[j]] += FastMath.tanh(a[j]);
}

From source file:org.apache.sysml.runtime.codegen.LibSpoofPrimitives.java

public static double[] vectTanhWrite(double[] a, int ai, int len) {
    double[] c = allocVector(len, false);
    for (int j = 0; j < len; j++, ai++)
        c[j] = FastMath.tanh(a[ai]);
    return c;/*from w  w  w .  j  av  a 2s  .c  o  m*/
}

From source file:org.apache.sysml.runtime.codegen.LibSpoofPrimitives.java

public static double[] vectTanhWrite(double[] a, int[] aix, int ai, int alen, int len) {
    double[] c = allocVector(len, true);
    for (int j = ai; j < ai + alen; j++)
        c[aix[j]] = FastMath.tanh(a[j]);
    return c;/*from w w w .  j  a  v  a 2 s  . c  om*/
}

From source file:org.esa.beam.util.math.FastMathPerformance.java

public void testTanh() {
    System.gc();/*from  w  w w. ja v  a 2 s . c  o m*/
    double x = 0;
    long time = System.nanoTime();
    for (int i = 0; i < RUNS; i++)
        x += StrictMath.tanh(i * F1);
    long strictTime = System.nanoTime() - time;

    System.gc();
    double y = 0;
    time = System.nanoTime();
    for (int i = 0; i < RUNS; i++)
        y += FastMath.tanh(i * F1);
    long fastTime = System.nanoTime() - time;

    System.gc();
    double z = 0;
    time = System.nanoTime();
    for (int i = 0; i < RUNS; i++)
        z += Math.tanh(i * F1);
    long mathTime = System.nanoTime() - time;

    report("tanh", x + y + z, strictTime, fastTime, mathTime);
}