Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/python/pylang/src/lib/math.py
1398 views
1
###########################################################
2
# pylang Standard Library
3
# Author: Alexander Tsepkov
4
# Copyright 2013 Pyjeon Software LLC
5
# License: Apache License 2.0
6
# This library is covered under Apache license, so that
7
# you can distribute it with your pylang applications.
8
###########################################################
9
10
# basic implementation of Python's 'math' library
11
12
# NOTE: this is only meant to aid those porting lots of Python code into pylang.
13
# If you're writing a new pylang application, in most cases you probably want to
14
# use JavaScript's Math module directly instead
15
16
pi = Math.PI
17
e = Math.E
18
19
20
########################################
21
# Number-theoretic and representation functions
22
########################################
23
def ceil(x):
24
return Math.ceil(x)
25
26
27
def copysign(x, y):
28
x = Math.abs(x)
29
if y < 0:
30
return -x
31
else:
32
return x
33
34
35
def fabs(x):
36
return Math.abs(x)
37
38
39
def factorial(x):
40
if Math.abs(int(x)) is not x:
41
raise ValueError("factorial() only accepts integral values")
42
factorial.cache = []
43
44
if x <= 12:
45
# normal javascript integer
46
def r(n):
47
if n is 0 or n is 1:
48
return 1
49
if not factorial.cache[n]:
50
factorial.cache[n] = r(n - 1) * n
51
return factorial.cache[n]
52
else:
53
# use BigInt to avoid overflow
54
def r(n):
55
if n is 0 or n is 1:
56
return BigInt(1)
57
if not factorial.cache[n]:
58
factorial.cache[n] = r(n - 1) * BigInt(n)
59
return factorial.cache[n]
60
61
return r(x)
62
63
64
def floor(x):
65
return Math.floor(x)
66
67
68
def fmod(x, y):
69
# javascript's % operator isn't consistent with C fmod implementation, this function is
70
while y <= x:
71
x -= y
72
return x
73
74
75
def fsum(iterable):
76
# like Python's fsum, this method is much more resilient to rounding errors than regular sum
77
partials = [] # sorted, non-overlapping partial sums
78
for x in iterable:
79
i = 0
80
for y in partials:
81
if Math.abs(x) < Math.abs(y):
82
x, y = y, x
83
hi = x + y
84
lo = y - (hi - x)
85
if lo:
86
partials[i] = lo
87
i += 1
88
x = hi
89
#partials[i:] = [x]
90
partials.splice(i, partials.length - i, x)
91
return sum(partials)
92
93
94
def isinf(x):
95
return not isFinite(x)
96
97
98
def isnan(x):
99
return isNaN(x)
100
101
102
def modf(x):
103
m = fmod(x, 1)
104
return m, x - m
105
106
107
def trunc(x):
108
return x | 0
109
110
111
########################################
112
# Power and logarithmic functions
113
########################################
114
def exp(x):
115
return Math.exp(x)
116
117
118
def expm1(x):
119
# NOTE: Math.expm1() is currently only implemented in Firefox, this provides alternative implementation
120
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/expm1
121
#return Math.expm1(x)
122
if Math.abs(x) < 1e-5:
123
return x + 0.5 * x * x
124
else:
125
return Math.exp(x) - 1
126
127
128
def log(x, base=e):
129
return Math.log(x) / Math.log(base)
130
131
132
def log1p(x):
133
# NOTE: Math.log1p() is currently only implemented in Firefox, this provides alternative implementation
134
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log1p
135
# this version has been taken from http://phpjs.org/functions/log1p/
136
# admittedly it's not as accurate as MDN version, as you can see from math.log1p(1) result
137
ret = 0
138
n = 50
139
if x <= -1:
140
return Number.NEGATIVE_INFINITY
141
if x < 0 or x > 1:
142
return Math.log(1 + x)
143
for i in range(1, n):
144
if i % 2 is 0:
145
ret -= Math.pow(x, i) / i
146
else:
147
ret += Math.pow(x, i) / i
148
return ret
149
150
151
def log10(x):
152
# NOTE: Math.log10() is currently only implemented in Firefox, this provides alternative implementation
153
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10
154
# I didn't find a more accurate algorithm so I'm using the basic implementation
155
return Math.log(x) / Math.LN10
156
157
158
def pow(x, y):
159
if x < 0 and int(y) is not y:
160
raise ValueError('math domain error')
161
if isnan(y) and x is 1:
162
return 1
163
return Math.pow(x, y)
164
165
166
def sqrt(x):
167
return Math.sqrt(x)
168
169
170
########################################
171
# Trigonometric functions
172
########################################
173
def acos(x):
174
return Math.acos(x)
175
176
177
def asin(x):
178
return Math.asin(x)
179
180
181
def atan(x):
182
return Math.atan(x)
183
184
185
def atan2(y, x):
186
return Math.atan2(y, x)
187
188
189
def cos(x):
190
return Math.cos(x)
191
192
193
def sin(x):
194
return Math.sin(x)
195
196
197
def hypot(x, y):
198
return Math.sqrt(x * x + y * y)
199
200
201
def tan(x):
202
return Math.tan(x)
203
204
205
########################################
206
# Angular conversion
207
########################################
208
def degrees(x):
209
return x * 180 / pi
210
211
212
def radians(x):
213
return x * pi / 180
214
215
216
########################################
217
# Hyperbolic functions
218
########################################
219
def acosh(x):
220
# NOTE: will be replaced with official, when it becomes mainstream
221
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/acosh
222
return Math.log(x + Math.sqrt(x * x - 1))
223
224
225
def asinh(x):
226
# NOTE: will be replaced with official, when it becomes mainstream
227
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/asinh
228
return Math.log(x + Math.sqrt(x * x + 1))
229
230
231
def atanh(x):
232
# NOTE: will be replaced with official, when it becomes mainstream
233
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atanh
234
return 0.5 * Math.log((1 + x) / (1 - x))
235
236
237
def cosh(x):
238
# NOTE: will be replaced with official, when it becomes mainstream
239
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cosh
240
return (Math.exp(x) + Math.exp(-x)) / 2
241
242
243
def sinh(x):
244
# NOTE: will be replaced with official, when it becomes mainstream
245
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sinh
246
return (Math.exp(x) - Math.exp(-x)) / 2
247
248
249
def tanh(x):
250
# NOTE: will be replaced with official, when it becomes mainstream
251
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/tanh
252
return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x))
253
254
255
#import stdlib
256
#print(math.ceil(4.2))
257
#print(math.floor(4.2))
258
#print(math.fabs(-6))
259
#print(math.copysign(-5, 7))
260
#print(math.factorial(4))
261
#print(math.fmod(-1e100, 1e100))
262
#
263
#d = [0.9999999, 1, 2, 3]
264
#print(sum(d), math.fsum(d))
265
#print(math.isinf(5), math.isinf(Infinity))
266
#print(math.modf(5.5))
267
#print(math.trunc(2.6), math.trunc(-2.6))
268
#print(math.exp(1e-5), math.expm1(1e-5))
269
#print(math.log(10), math.log(10, 1000))
270
#print(math.log1p(1e-15), math.log1p(1))
271
#print(math.log10(1000), math.log(1000, 10))
272
#print(math.pow(1, 0), math.pow(1, NaN), math.pow(0, 0), math.pow(NaN, 0), math.pow(4,3), math.pow(100, -2))
273
#print(math.hypot(3,4))
274
#print(math.acosh(2), math.asinh(1), math.atanh(0.5), math.cosh(1), math.cosh(-1), math.sinh(1), math.tanh(1))
275
276