github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/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  //	Sinh(±0) = (±0+Nϵ)
    13  //	Sinh(±Inf) = ±Inf
    14  //	Sinh(NaN) = NaN
    15  func Sinh(d Number) Number {
    16  	if d.Real == 0 {
    17  		return Number{
    18  			Real: d.Real,
    19  			Emag: d.Emag,
    20  		}
    21  	}
    22  	if math.IsInf(d.Real, 0) {
    23  		return Number{
    24  			Real: d.Real,
    25  			Emag: math.Inf(1),
    26  		}
    27  	}
    28  	fn := math.Sinh(d.Real)
    29  	deriv := math.Cosh(d.Real)
    30  	return Number{
    31  		Real: fn,
    32  		Emag: deriv * d.Emag,
    33  	}
    34  }
    35  
    36  // Cosh returns the hyperbolic cosine of d.
    37  //
    38  // Special cases are:
    39  //	Cosh(±0) = 1
    40  //	Cosh(±Inf) = +Inf
    41  //	Cosh(NaN) = NaN
    42  func Cosh(d Number) Number {
    43  	if math.IsInf(d.Real, 0) {
    44  		return Number{
    45  			Real: math.Inf(1),
    46  			Emag: d.Real,
    47  		}
    48  	}
    49  	fn := math.Cosh(d.Real)
    50  	deriv := math.Sinh(d.Real)
    51  	return Number{
    52  		Real: fn,
    53  		Emag: deriv * d.Emag,
    54  	}
    55  }
    56  
    57  // Tanh returns the hyperbolic tangent of d.
    58  //
    59  // Special cases are:
    60  //	Tanh(±0) = (±0+Nϵ)
    61  //	Tanh(±Inf) = (±1+0ϵ)
    62  //	Tanh(NaN) = NaN
    63  func Tanh(d Number) Number {
    64  	switch d.Real {
    65  	case 0:
    66  		return Number{
    67  			Real: d.Real,
    68  			Emag: d.Emag,
    69  		}
    70  	case math.Inf(1):
    71  		return Number{
    72  			Real: 1,
    73  			Emag: 0,
    74  		}
    75  	case math.Inf(-1):
    76  		return Number{
    77  			Real: -1,
    78  			Emag: 0,
    79  		}
    80  	}
    81  	fn := math.Tanh(d.Real)
    82  	deriv := 1 - fn*fn
    83  	return Number{
    84  		Real: fn,
    85  		Emag: deriv * d.Emag,
    86  	}
    87  }
    88  
    89  // Asinh returns the inverse hyperbolic sine of d.
    90  //
    91  // Special cases are:
    92  //	Asinh(±0) = (±0+Nϵ)
    93  //	Asinh(±Inf) = ±Inf
    94  //	Asinh(NaN) = NaN
    95  func Asinh(d Number) Number {
    96  	if d.Real == 0 {
    97  		return Number{
    98  			Real: d.Real,
    99  			Emag: d.Emag,
   100  		}
   101  	}
   102  	fn := math.Asinh(d.Real)
   103  	deriv := 1 / math.Sqrt(d.Real*d.Real+1)
   104  	return Number{
   105  		Real: fn,
   106  		Emag: deriv * d.Emag,
   107  	}
   108  }
   109  
   110  // Acosh returns the inverse hyperbolic cosine of d.
   111  //
   112  // Special cases are:
   113  //	Acosh(+Inf) = +Inf
   114  //	Acosh(1) = (0+Infϵ)
   115  //	Acosh(x) = NaN if x < 1
   116  //	Acosh(NaN) = NaN
   117  func Acosh(d Number) Number {
   118  	if d.Real <= 1 {
   119  		if d.Real == 1 {
   120  			return Number{
   121  				Real: 0,
   122  				Emag: math.Inf(1),
   123  			}
   124  		}
   125  		return Number{
   126  			Real: math.NaN(),
   127  			Emag: math.NaN(),
   128  		}
   129  	}
   130  	fn := math.Acosh(d.Real)
   131  	deriv := 1 / math.Sqrt(d.Real*d.Real-1)
   132  	return Number{
   133  		Real: fn,
   134  		Emag: deriv * d.Emag,
   135  	}
   136  }
   137  
   138  // Atanh returns the inverse hyperbolic tangent of d.
   139  //
   140  // Special cases are:
   141  //	Atanh(1) = +Inf
   142  //	Atanh(±0) = (±0+Nϵ)
   143  //	Atanh(-1) = -Inf
   144  //	Atanh(x) = NaN if x < -1 or x > 1
   145  //	Atanh(NaN) = NaN
   146  func Atanh(d Number) Number {
   147  	if d.Real == 0 {
   148  		return Number{
   149  			Real: d.Real,
   150  			Emag: d.Emag,
   151  		}
   152  	}
   153  	if math.Abs(d.Real) == 1 {
   154  		return Number{
   155  			Real: math.Inf(int(d.Real)),
   156  			Emag: math.NaN(),
   157  		}
   158  	}
   159  	fn := math.Atanh(d.Real)
   160  	deriv := 1 / (1 - d.Real*d.Real)
   161  	return Number{
   162  		Real: fn,
   163  		Emag: deriv * d.Emag,
   164  	}
   165  }