gonum.org/v1/gonum@v0.14.0/num/dual/dual_hyperbolic.go (about)

     1  // Copyright ©2018 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package dual
     6  
     7  import "math"
     8  
     9  // Sinh returns the hyperbolic sine of d.
    10  //
    11  // Special cases are:
    12  //
    13  //	Sinh(±0) = (±0+Nϵ)
    14  //	Sinh(±Inf) = ±Inf
    15  //	Sinh(NaN) = NaN
    16  func Sinh(d Number) Number {
    17  	if d.Real == 0 {
    18  		return Number{
    19  			Real: d.Real,
    20  			Emag: d.Emag,
    21  		}
    22  	}
    23  	if math.IsInf(d.Real, 0) {
    24  		return Number{
    25  			Real: d.Real,
    26  			Emag: math.Inf(1),
    27  		}
    28  	}
    29  	fn := math.Sinh(d.Real)
    30  	deriv := math.Cosh(d.Real)
    31  	return Number{
    32  		Real: fn,
    33  		Emag: deriv * d.Emag,
    34  	}
    35  }
    36  
    37  // Cosh returns the hyperbolic cosine of d.
    38  //
    39  // Special cases are:
    40  //
    41  //	Cosh(±0) = 1
    42  //	Cosh(±Inf) = +Inf
    43  //	Cosh(NaN) = NaN
    44  func Cosh(d Number) Number {
    45  	if math.IsInf(d.Real, 0) {
    46  		return Number{
    47  			Real: math.Inf(1),
    48  			Emag: d.Real,
    49  		}
    50  	}
    51  	fn := math.Cosh(d.Real)
    52  	deriv := math.Sinh(d.Real)
    53  	return Number{
    54  		Real: fn,
    55  		Emag: deriv * d.Emag,
    56  	}
    57  }
    58  
    59  // Tanh returns the hyperbolic tangent of d.
    60  //
    61  // Special cases are:
    62  //
    63  //	Tanh(±0) = (±0+Nϵ)
    64  //	Tanh(±Inf) = (±1+0ϵ)
    65  //	Tanh(NaN) = NaN
    66  func Tanh(d Number) Number {
    67  	switch d.Real {
    68  	case 0:
    69  		return Number{
    70  			Real: d.Real,
    71  			Emag: d.Emag,
    72  		}
    73  	case math.Inf(1):
    74  		return Number{
    75  			Real: 1,
    76  			Emag: 0,
    77  		}
    78  	case math.Inf(-1):
    79  		return Number{
    80  			Real: -1,
    81  			Emag: 0,
    82  		}
    83  	}
    84  	fn := math.Tanh(d.Real)
    85  	deriv := 1 - fn*fn
    86  	return Number{
    87  		Real: fn,
    88  		Emag: deriv * d.Emag,
    89  	}
    90  }
    91  
    92  // Asinh returns the inverse hyperbolic sine of d.
    93  //
    94  // Special cases are:
    95  //
    96  //	Asinh(±0) = (±0+Nϵ)
    97  //	Asinh(±Inf) = ±Inf
    98  //	Asinh(NaN) = NaN
    99  func Asinh(d Number) Number {
   100  	if d.Real == 0 {
   101  		return Number{
   102  			Real: d.Real,
   103  			Emag: d.Emag,
   104  		}
   105  	}
   106  	fn := math.Asinh(d.Real)
   107  	deriv := 1 / math.Sqrt(d.Real*d.Real+1)
   108  	return Number{
   109  		Real: fn,
   110  		Emag: deriv * d.Emag,
   111  	}
   112  }
   113  
   114  // Acosh returns the inverse hyperbolic cosine of d.
   115  //
   116  // Special cases are:
   117  //
   118  //	Acosh(+Inf) = +Inf
   119  //	Acosh(1) = (0+Infϵ)
   120  //	Acosh(x) = NaN if x < 1
   121  //	Acosh(NaN) = NaN
   122  func Acosh(d Number) Number {
   123  	if d.Real <= 1 {
   124  		if d.Real == 1 {
   125  			return Number{
   126  				Real: 0,
   127  				Emag: math.Inf(1),
   128  			}
   129  		}
   130  		return Number{
   131  			Real: math.NaN(),
   132  			Emag: math.NaN(),
   133  		}
   134  	}
   135  	fn := math.Acosh(d.Real)
   136  	deriv := 1 / math.Sqrt(d.Real*d.Real-1)
   137  	return Number{
   138  		Real: fn,
   139  		Emag: deriv * d.Emag,
   140  	}
   141  }
   142  
   143  // Atanh returns the inverse hyperbolic tangent of d.
   144  //
   145  // Special cases are:
   146  //
   147  //	Atanh(1) = +Inf
   148  //	Atanh(±0) = (±0+Nϵ)
   149  //	Atanh(-1) = -Inf
   150  //	Atanh(x) = NaN if x < -1 or x > 1
   151  //	Atanh(NaN) = NaN
   152  func Atanh(d Number) Number {
   153  	if d.Real == 0 {
   154  		return Number{
   155  			Real: d.Real,
   156  			Emag: d.Emag,
   157  		}
   158  	}
   159  	if math.Abs(d.Real) == 1 {
   160  		return Number{
   161  			Real: math.Inf(int(d.Real)),
   162  			Emag: math.NaN(),
   163  		}
   164  	}
   165  	fn := math.Atanh(d.Real)
   166  	deriv := 1 / (1 - d.Real*d.Real)
   167  	return Number{
   168  		Real: fn,
   169  		Emag: deriv * d.Emag,
   170  	}
   171  }