Example usage for java.lang Math tanh

List of usage examples for java.lang Math tanh

Introduction

In this page you can find the example usage for java.lang Math tanh.

Prototype

public static double tanh(double x) 

Source Link

Document

Returns the hyperbolic tangent of a double value.

Usage

From source file:edu.cmu.tetrad.search.FastIca.java

private TetradMatrix icaParallel(TetradMatrix X, int numComponents, double tolerance, int function,
        final double alpha, int maxIterations, boolean verbose, TetradMatrix wInit) {
    int p = X.columns();
    TetradMatrix W = wInit;//  www.ja  v  a  2 s . com

    SingularValueDecomposition sW = new SingularValueDecomposition(W.getRealMatrix());
    TetradMatrix D = new TetradMatrix(sW.getS());
    for (int i = 0; i < D.rows(); i++)
        D.set(i, i, 1.0 / D.get(i, i));

    TetradMatrix WTemp = new TetradMatrix(sW.getU()).times(D);
    WTemp = WTemp.times(new TetradMatrix(sW.getU()).transpose());
    WTemp = WTemp.times(W);
    W = WTemp;

    TetradMatrix W1;
    double _tolerance = Double.POSITIVE_INFINITY;
    int it = 0;

    if (function == LOGCOSH) {
        if (verbose) {
            TetradLogger.getInstance().log("info",
                    "Symmetric FastICA using logcosh approx. to neg-entropy function");
        }

        while (_tolerance > tolerance && it < maxIterations) {
            TetradMatrix wx = W.times(X);
            TetradMatrix gwx = new TetradMatrix(numComponents, p);

            for (int i = 0; i < numComponents; i++) {
                for (int j = 0; j < p; j++) {
                    gwx.set(i, j, Math.tanh(alpha * wx.get(i, j)));
                }
            }

            TetradMatrix v1 = gwx.times(X.transpose().copy().scalarMult(1.0 / p));
            TetradMatrix g_wx = gwx.copy();

            for (int i = 0; i < g_wx.rows(); i++) {
                for (int j = 0; j < g_wx.columns(); j++) {
                    double v = g_wx.get(i, j);
                    double w = alpha * (1.0 - v * v);
                    g_wx.set(i, j, w);
                }
            }

            TetradVector V20 = new TetradVector(numComponents);

            for (int k = 0; k < numComponents; k++) {
                V20.set(k, mean(g_wx.getRow(k)));
            }

            TetradMatrix v2 = V20.diag();
            v2 = v2.times(W);
            W1 = v1.minus(v2);

            SingularValueDecomposition sW1 = new SingularValueDecomposition(W1.getRealMatrix());
            TetradMatrix U = new TetradMatrix(sW1.getU());
            TetradMatrix sD = new TetradMatrix(sW1.getS());
            for (int i = 0; i < sD.rows(); i++)
                sD.set(i, i, 1.0 / sD.get(i, i));

            TetradMatrix W1Temp = U.times(sD);
            W1Temp = W1Temp.times(U.transpose());
            W1Temp = W1Temp.times(W1);
            W1 = W1Temp;

            TetradMatrix d1 = W1.times(W.transpose());
            TetradVector d = d1.diag();
            _tolerance = Double.NEGATIVE_INFINITY;

            for (int i = 0; i < d.size(); i++) {
                double m = Math.abs(Math.abs(d.get(i)) - 1);
                if (m > _tolerance)
                    _tolerance = m;
            }

            W = W1;

            if (verbose) {
                TetradLogger.getInstance().log("fastIcaDetails",
                        "Iteration " + (it + 1) + " tol = " + _tolerance);
            }

            it++;
        }
    } else if (function == EXP) {
        if (verbose) {
            TetradLogger.getInstance().log("info",
                    "Symmetric FastICA using exponential approx. to neg-entropy function");
        }

        while (_tolerance > tolerance && it < maxIterations) {
            TetradMatrix wx = W.times(X);
            TetradMatrix gwx = new TetradMatrix(numComponents, p);

            for (int i = 0; i < numComponents; i++) {
                for (int j = 0; j < p; j++) {
                    double v = wx.get(i, j);
                    gwx.set(i, j, v * Math.exp(-(v * v) / 2.0));
                }
            }

            TetradMatrix v1 = gwx.times(X.transpose().scalarMult(p));
            TetradMatrix g_wx = wx.copy();

            for (int i = 0; i < g_wx.rows(); i++) {
                for (int j = 0; j < g_wx.columns(); j++) {
                    double v = g_wx.get(i, j);
                    double w = (1.0 - v * v) * Math.exp(-(v * v) / 2.0);
                    g_wx.set(i, j, w);
                }
            }

            TetradVector V20 = new TetradVector(numComponents);

            for (int k = 0; k < numComponents; k++) {
                V20.set(k, mean(g_wx.getRow(k)));
            }

            TetradMatrix v2 = V20.diag();
            v2 = v2.times(W);
            W1 = v1.minus(v2);

            SingularValueDecomposition sW1 = new SingularValueDecomposition(W1.getRealMatrix());
            TetradMatrix U = new TetradMatrix(sW1.getU());
            TetradMatrix sD = new TetradMatrix(sW1.getS());
            for (int i = 0; i < sD.rows(); i++)
                sD.set(i, i, 1.0 / sD.get(i, i));

            TetradMatrix W1Temp = U.times(sD);
            W1Temp = W1Temp.times(U.transpose());
            W1Temp = W1Temp.times(W1);
            W1 = W1Temp;

            TetradMatrix d1 = W1.times(W.transpose());
            TetradVector d = d1.diag();
            _tolerance = Double.NEGATIVE_INFINITY;

            for (int i = 0; i < d.size(); i++) {
                double m = Math.abs(Math.abs(d.get(i)) - 1);
                if (m > _tolerance)
                    _tolerance = m;
            }

            W.assign(W1);

            if (verbose) {
                TetradLogger.getInstance().log("fastIcaDetails",
                        "Iteration " + (it + 1) + " tol = " + _tolerance);
            }

            it++;
        }
    }

    return W;
}

From source file:com.facebook.presto.operator.scalar.MathFunctions.java

@Description("hyperbolic tangent")
@ScalarFunction/*  www. j  ava2  s  .c  om*/
@SqlType(StandardTypes.DOUBLE)
public static double tanh(@SqlType(StandardTypes.DOUBLE) double num) {
    return Math.tanh(num);
}

From source file:edu.cmu.tetrad.search.Lofs2.java

private Graph tanhGraph(Graph graph) {
    DataSet dataSet = DataUtils.concatenateData(dataSets);
    graph = GraphUtils.replaceNodes(graph, dataSet.getVariables());
    dataSet = DataUtils.standardizeData(dataSet);
    double[][] data = dataSet.getDoubleData().transpose().toArray();
    Graph _graph = new EdgeListGraph(graph.getNodes());
    List<Node> nodes = dataSet.getVariables();
    Map<Node, Integer> nodesHash = new HashMap<Node, Integer>();

    for (int i = 0; i < nodes.size(); i++) {
        nodesHash.put(nodes.get(i), i);//  w w  w  .j  a v a 2  s  .com
    }

    for (Edge edge : graph.getEdges()) {
        Node x = edge.getNode1();
        Node y = edge.getNode2();

        double sumX = 0.0;
        int countX = 0;

        double[] xData = data[nodesHash.get(edge.getNode1())];
        double[] yData = data[nodesHash.get(edge.getNode2())];

        for (int i = 0; i < xData.length; i++) {
            double x0 = xData[i];
            double y0 = yData[i];

            double termX = (x0 * Math.tanh(y0) - Math.tanh(x0) * y0);

            sumX += termX;
            countX++;
        }

        double R = sumX / countX;

        double rhoX = regressionCoef(xData, yData);
        R *= rhoX;

        if (R > 0) {
            _graph.addDirectedEdge(x, y);
        } else {
            _graph.addDirectedEdge(y, x);
        }
    }

    return _graph;
}

From source file:com.rapidminer.tools.expression.internal.function.AntlrParserTrigonometricTest.java

@Test
public void tanhInt() {
    try {/*from  ww w .  ja va2 s.  co m*/
        Expression expression = getExpressionWithFunctionContext("tanh(16)");
        assertEquals(ExpressionType.DOUBLE, expression.getExpressionType());
        assertEquals(Math.tanh(16), expression.evaluateNumerical(), 1e-15);
    } catch (ExpressionException e) {
        assertNotNull(e.getMessage());
    }
}

From source file:com.rapidminer.tools.expression.internal.function.AntlrParserTrigonometricTest.java

@Test
public void tanhDouble() {
    try {//from w w  w .  j a v  a  2  s. com
        Expression expression = getExpressionWithFunctionContext("tanh(33.3)");
        assertEquals(ExpressionType.DOUBLE, expression.getExpressionType());
        assertEquals(Math.tanh(33.3), expression.evaluateNumerical(), 1e-15);
    } catch (ExpressionException e) {
        assertNotNull(e.getMessage());
    }
}

From source file:com.rapidminer.tools.expression.internal.function.AntlrParserTrigonometricTest.java

@Test
public void tanhNegative() {
    try {/* w w w.  j av a 2s  .c  o  m*/
        Expression expression = getExpressionWithFunctionContext("tanh(-10)");
        assertEquals(ExpressionType.DOUBLE, expression.getExpressionType());
        assertEquals(Math.tanh(-10), expression.evaluateNumerical(), 1e-15);
    } catch (ExpressionException e) {
        assertNotNull(e.getMessage());
    }
}

From source file:com.rapidminer.tools.expression.internal.function.AntlrParserTrigonometricTest.java

@Test
public void tanhNull() {
    try {/* w w w .ja v a  2 s  .  c  om*/
        Expression expression = getExpressionWithFunctionContext("tanh(0)");
        assertEquals(ExpressionType.DOUBLE, expression.getExpressionType());
        assertEquals(Math.tanh(0), expression.evaluateNumerical(), 1e-15);
    } catch (ExpressionException e) {
        assertNotNull(e.getMessage());
    }
}

From source file:com.rapidminer.tools.expression.internal.function.AntlrParserTrigonometricTest.java

@Test
public void tanhNinety() {
    try {/*  w  w w . j  a v a  2 s .  co  m*/
        Expression expression = getExpressionWithFunctionContext("tanh(90)");
        assertEquals(ExpressionType.DOUBLE, expression.getExpressionType());
        assertEquals(Math.tanh(90), expression.evaluateNumerical(), 1e-15);
    } catch (ExpressionException e) {
        assertNotNull(e.getMessage());
    }
}

From source file:com.rapidminer.tools.expression.internal.function.AntlrParserTrigonometricTest.java

@Test
public void tanhPi() {
    try {/*from   ww  w  . j  a  v  a  2s  .c om*/
        Expression expression = getExpressionWithFunctionContext("tanh(pi)");
        assertEquals(ExpressionType.DOUBLE, expression.getExpressionType());
        assertEquals(Math.tanh(Math.PI), expression.evaluateNumerical(), 1e-15);
    } catch (ExpressionException e) {
        assertNotNull(e.getMessage());
    }
}

From source file:com.rapidminer.tools.expression.internal.function.AntlrParserTrigonometricTest.java

@Test
public void tanhPiHalf() {
    try {//from   ww w.  j ava 2  s . c  o m
        Expression expression = getExpressionWithFunctionContext("tanh(pi/2)");
        assertEquals(ExpressionType.DOUBLE, expression.getExpressionType());
        assertEquals(Math.tanh(Math.PI / 2), expression.evaluateNumerical(), 1e-15);
    } catch (ExpressionException e) {
        assertNotNull(e.getMessage());
    }
}