github.com/google/grumpy@v0.0.0-20171122020858-3ec87959189c/lib/math.py (about) 1 # Copyright 2016 Google Inc. All Rights Reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 15 from '__go__/math' import (Pi, E, Ceil, Copysign, Abs, Floor, Mod, Frexp, IsInf, 16 IsNaN, Exp2, Modf, Trunc, Exp, Expm1, Log, Log1p, Log10, Pow, Sqrt, Acos, 17 Asin, Atan, Atan2, Hypot, Sin, Cos, Tan, Acosh, Asinh, Atanh, Sinh, Cosh, 18 Tanh, Erf, Erfc, Gamma, Lgamma) # pylint: disable=g-multiple-import 19 20 # Constants 21 22 pi = Pi 23 24 25 e = E 26 27 28 # Number-theoretic and representation functions 29 30 def ceil(x): 31 return Ceil(float(x)) 32 33 34 def copysign(x, y): 35 return Copysign(float(x), float(y)) 36 37 38 def fabs(x): 39 return Abs(float(x)) 40 41 42 def factorial(x): 43 try: 44 xi = int(x) 45 except TypeError: 46 xi = None 47 48 try: 49 xf = float(x) 50 except TypeError: 51 xf = None 52 53 if xi is None: 54 xi = int(xf) 55 if xi != xf: 56 raise ValueError("factorial() only accepts integral values") 57 elif xf is None and xi is None: 58 raise TypeError("an integer is required") 59 elif xf is None: 60 pass 61 elif xf != xi: 62 raise ValueError("factorial() only accepts integral values") 63 64 x = xi 65 66 if x < 0: 67 raise ValueError("factorial() not defined for negative values") 68 69 acc = 1 70 71 for value in range(2, x+1): 72 acc *= value 73 74 return acc 75 76 77 def floor(x): 78 return Floor(float(x)) 79 80 81 def fmod(x): 82 return Mod(float(x)) 83 84 85 def frexp(x): 86 return Frexp(float(x)) 87 88 89 # TODO: Implement fsum() 90 # def fsum(x): 91 # pass 92 93 94 def isinf(x): 95 return IsInf(float(x), 0) 96 97 98 def isnan(x): 99 return IsNaN(float(x)) 100 101 102 def ldexp(x, i): 103 return float(x) * Exp2(float(i)) 104 105 106 def modf(x): 107 # Modf returns (int, frac), but python should return (frac, int). 108 a, b = Modf(float(x)) 109 return b, a 110 111 112 def trunc(x): 113 return Trunc(float(x)) 114 115 116 # Power and logarithmic functions 117 118 def exp(x): 119 return Exp(float(x)) 120 121 122 def expm1(x): 123 return Expm1(float(x)) 124 125 126 def log(x, b=None): 127 if b is None: 128 return Log(float(x)) 129 130 # NOTE: We can try and catch more special cases to delegate to specific 131 # Go functions or maybe there is a function that does this and I missed it. 132 return Log(float(x)) / Log(float(b)) 133 134 135 def log1p(x): 136 return Log1p(float(x)) 137 138 139 def log10(x): 140 return Log10(float(x)) 141 142 143 def pow(x, y): 144 return Pow(float(x), float(y)) 145 146 147 def sqrt(x): 148 return Sqrt(float(x)) 149 150 151 # Trigonometric functions 152 153 def acos(x): 154 return Acos(float(x)) 155 156 157 def asin(x): 158 return Asin(float(x)) 159 160 161 def atan(x): 162 return Atan(float(x)) 163 164 165 def atan2(y, x): 166 return Atan2(float(y), float(x)) 167 168 169 def cos(x): 170 return Cos(float(x)) 171 172 173 def hypot(x, y): 174 return Hypot(float(x), float(y)) 175 176 177 def sin(x): 178 return Sin(float(x)) 179 180 181 def tan(x): 182 return Tan(float(x)) 183 184 185 # Angular conversion 186 187 def degrees(x): 188 return (float(x) * 180) / pi 189 190 191 def radians(x): 192 return (float(x) * pi) / 180 193 194 195 # Hyperbolic functions 196 197 def acosh(x): 198 return Acosh(float(x)) 199 200 201 def asinh(x): 202 return Asinh(float(x)) 203 204 205 def atanh(x): 206 return Atanh(float(x)) 207 208 209 def cosh(x): 210 return Cosh(float(x)) 211 212 213 def sinh(x): 214 return Sinh(float(x)) 215 216 217 def tanh(x): 218 return Tanh(float(x)) 219 220 221 # Special functions 222 223 def erf(x): 224 return Erf(float(x)) 225 226 227 def erfc(x): 228 return Erfc(float(x)) 229 230 231 def gamma(x): 232 return Gamma(float(x)) 233 234 235 def lgamma(x): 236 return Lgamma(float(x))