C# Math class question
Asked Answered
P

4

10

I need to calculate Tanh-1 in C#
(and Sinh-1 and Cosh-1)

I did not found it in Math library.. Any suggestions ?

EDIT: Tanh not Tan !!

Pood answered 15/5, 2010 at 16:13 Comment(0)
E
28

You need to derive them yourself using existing functions e.g. Math.sin

You might find this useful:

Secant Sec(X) = 1 / Cos(X) 
Cosecant Cosec(X) = 1 / Sin(X) 
Cotangent Cotan(X) = 1 / Tan(X) 
Inverse Sine Arcsin(X) = Atn(X / Sqr(-X * X + 1)) 
Inverse Cosine Arccos(X) = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1) 
Inverse Secant Arcsec(X) = 2 * Atn(1) - Atn(Sgn(X) / Sqr(X * X - 1)) 
Inverse Cosecant Arccosec(X) = Atn(Sgn(X) / Sqr(X * X - 1)) 
Inverse Cotangent Arccotan(X) = 2 * Atn(1) - Atn(X) 
Hyperbolic Sine HSin(X) = (Exp(X) - Exp(-X)) / 2 
Hyperbolic Cosine HCos(X) = (Exp(X) + Exp(-X)) / 2 
Hyperbolic Tangent HTan(X) = (Exp(X) - Exp(-X)) / (Exp(X) + Exp(-X)) 
Hyperbolic Secant HSec(X) = 2 / (Exp(X) + Exp(-X)) 
Hyperbolic Cosecant HCosec(X) = 2 / (Exp(X) - Exp(-X)) 
Hyperbolic Cotangent HCotan(X) = (Exp(X) + Exp(-X)) / (Exp(X) - Exp(-X)) 
Inverse Hyperbolic Sine HArcsin(X) = Log(X + Sqr(X * X + 1)) 
Inverse Hyperbolic Cosine HArccos(X) = Log(X + Sqr(X * X - 1)) 
Inverse Hyperbolic Tangent HArctan(X) = Log((1 + X) / (1 - X)) / 2 
Inverse Hyperbolic Secant HArcsec(X) = Log((Sqr(-X * X + 1) + 1) / X) 
Inverse Hyperbolic Cosecant HArccosec(X) = Log((Sgn(X) * Sqr(X * X + 1) + 1) / X) 
Inverse Hyperbolic Cotangent HArccotan(X) = Log((X + 1) / (X - 1)) / 2 
Logarithm to base N LogN(X) = Log(X) / Log(N)
Estellestella answered 15/5, 2010 at 16:21 Comment(2)
Just wanted to add: asec(x) = acos(1 / x), acsc(x) = asin(1 / x), acot(x) = atan(1 / x)Maddiemadding
You will also want to know the Domain and Range of each derived math functionGlassco
R
13

To .NET-ify David Relihan's formulas:

public static class MathHelper
{
    // Secant 
    public static double Sec(double x)
    {
        return 1/Math.Cos(x);
    }

    // Cosecant
    public static double Cosec(double x)
    {
        return 1/Math.Sin(x);
    }

    // Cotangent 
    public static double Cotan(double x)
    {
        return 1/Math.Tan(x);
    }

    // Inverse Sine 
    public static double Arcsin(double x)
    {
        return Math.Atan(x / Math.Sqrt(-x * x + 1));
    }

    // Inverse Cosine 
    public static double Arccos(double x)
    {
        return Math.Atan(-x / Math.Sqrt(-x * x + 1)) + 2 * Math.Atan(1);
    }


    // Inverse Secant 
    public static double Arcsec(double x)
    {
        return 2 * Math.Atan(1) - Math.Atan(Math.Sign(x) / Math.Sqrt(x * x - 1));
    }

    // Inverse Cosecant 
    public static double Arccosec(double x)
    {
        return Math.Atan(Math.Sign(x) / Math.Sqrt(x * x - 1));
    }

    // Inverse Cotangent 
    public static double Arccotan(double x)
    {
        return 2 * Math.Atan(1) - Math.Atan(x);
    } 

    // Hyperbolic Sine 
    public static double HSin(double x)
    {
        return (Math.Exp(x) - Math.Exp(-x)) / 2 ;
    }

    // Hyperbolic Cosine 
    public static double HCos(double x)
    {
        return (Math.Exp(x) + Math.Exp(-x)) / 2 ;
    }

    // Hyperbolic Tangent 
    public static double HTan(double x)
    {
        return (Math.Exp(x) - Math.Exp(-x)) / (Math.Exp(x) + Math.Exp(-x));
    } 

    // Hyperbolic Secant 
    public static double HSec(double x)
    {
        return 2 / (Math.Exp(x) + Math.Exp(-x));
    } 

    // Hyperbolic Cosecant 
    public static double HCosec(double x)
    {
        return 2 / (Math.Exp(x) - Math.Exp(-x));
    } 

    // Hyperbolic Cotangent 
    public static double HCotan(double x)
    {
        return (Math.Exp(x) + Math.Exp(-x)) / (Math.Exp(x) - Math.Exp(-x));
    } 

    // Inverse Hyperbolic Sine 
    public static double HArcsin(double x)
    {
        return Math.Log(x + Math.Sqrt(x * x + 1)) ;
    }

    // Inverse Hyperbolic Cosine 
    public static double HArccos(double x)
    {
        return Math.Log(x + Math.Sqrt(x * x - 1));
    }

    // Inverse Hyperbolic Tangent 
    public static double HArctan(double x)
    {
        return Math.Log((1 + x) / (1 - x)) / 2 ;
    }

    // Inverse Hyperbolic Secant 
    public static double HArcsec(double x)
    {
        return Math.Log((Math.Sqrt(-x * x + 1) + 1) / x);
    } 

    // Inverse Hyperbolic Cosecant 
    public static double HArccosec(double x)
    {
        return Math.Log((Math.Sign(x) * Math.Sqrt(x * x + 1) + 1) / x) ;
    }

    // Inverse Hyperbolic Cotangent 
    public static double HArccotan(double x)
    {
        return Math.Log((x + 1) / (x - 1)) / 2;
    } 

    // Logarithm to base N 
    public static double LogN(double x, double n)
    {
        return Math.Log(x) / Math.Log(n);
    }
}
Richey answered 26/4, 2011 at 12:55 Comment(0)
O
8

You need to define them yourself.

http://en.wikipedia.org/wiki/Hyperbolic_function#Inverse_functions_as_logarithms

    -1     1    1 + x
tanh   x = — ln —————
           2    1 - x

    -1               _______
sinh   x = ln ( x + √ x² + 1 )

    -1               _______
cosh   x = ln ( x + √ x² - 1 )
Ondrea answered 15/5, 2010 at 16:17 Comment(2)
Note that the natural logarithm is also no function in the standard math class, however, the general logarithm is. You can just use the general logarithm with base e (which is a constant in the math class). Which is of course exactly the definition of the natural logarithm. Just a note for the sake of completeness @KennyTM +1 for the Math-art :)Laughton
You're right, I was too fast :) Indeed the default overload of Math.Log which takes only a double is the natural log.Laughton
S
0

There is also faster formula for computing tanh, requiring only one exp(), because tanh is related to logistic function:

tanh(x) = 2 / (1 + exp(-2 * x)) - 1
also
tanh(x) = 1 - 2 / (1 + exp(2 * x))

See: http://en.wikipedia.org/wiki/Logistic_function

Sulphathiazole answered 12/4, 2012 at 5:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.