Skip to content

Commit a6b4af6

Browse files
committed
add bernoulli function plots
1 parent 232aa70 commit a6b4af6

6 files changed

Lines changed: 149 additions & 0 deletions

File tree

sg_compare/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Scripts for comparing implementations of the Bernoulli function.

sg_compare/foo.pdf

1.18 KB
Binary file not shown.

sg_compare/foo.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
3+
from math import exp
4+
from math import expm1
5+
import numpy
6+
import matplotlib.pyplot as plt
7+
8+
9+
def b1(x):
10+
return x/(exp(x) - 1)
11+
12+
def b2(x):
13+
return 1./(1.0 + 0.5 * x)
14+
15+
def b3(x):
16+
y = expm1(x)
17+
if y != x:
18+
return x/y
19+
return 1.0
20+
21+
22+
x = numpy.linspace(0., 1e-5, 1001)
23+
24+
y = []
25+
#plt.title("comparison of 3 different Bernouilli Function implementations")
26+
fig, ax = plt.subplots(1,2)
27+
for i in b1, b2, b3:
28+
y.append(numpy.array(list(map(i, x))))
29+
ax[0].plot(x, y[-1])
30+
ax[0].legend([r'$B1= \frac{x}{\exp(x)-1)}$', r'$B2=\frac{1}{1+0.5*x}$', r'$B3=\frac{x}{\text{expm1}(x)}$' "\n" r' $x \ne \text{expm1}(x), \text{else} 1$'], loc="lower left",
31+
fancybox=True, framealpha=0.5)
32+
pdiffs = []
33+
for i in range(0,2):
34+
pdiffs.append(abs(y[i]-y[2])/y[2])
35+
ax[1].semilogy(x,pdiffs[-1])
36+
ax[1].legend(['$|B3-B1|/B3$', '$|B3-B2|/B3$'])
37+
plt.show()
38+
plt.savefig('foo.pdf')
39+

sg_compare/foo2.pdf

1.18 KB
Binary file not shown.

sg_compare/foo2.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
3+
from math import exp
4+
from math import expm1
5+
import numpy
6+
import matplotlib.pyplot as plt
7+
8+
9+
def b1(x):
10+
return 1./(1.0 + 0.5 * x + x*x/6. + x*x*x/24.)
11+
12+
def b2(x):
13+
return 1./(1.0 + 0.5 * x)
14+
15+
def b3(x):
16+
y = expm1(x)
17+
if y != x:
18+
return x/y
19+
return 1.0
20+
21+
22+
x = numpy.linspace(0., 1e-3, 1001)
23+
24+
y = []
25+
#plt.title("comparison of 3 different Bernouilli Function implementations")
26+
fig, ax = plt.subplots(1,2)
27+
for i in b1, b2, b3:
28+
y.append(numpy.array(list(map(i, x))))
29+
ax[0].plot(x, y[-1])
30+
ax[0].legend([r'$B4=\frac{1}{1+0.5*x + x*x/6. + x*x*x/24.}$', r'$B2=\frac{1}{1+0.5*x}$', r'$B3=\frac{x}{\text{expm1}(x)}$' "\n" r' $x \ne \text{expm1}(x), \text{else} 1$'], loc="lower left",
31+
fancybox=True, framealpha=0.5)
32+
pdiffs = []
33+
for i in range(0,2):
34+
pdiffs.append(abs(y[i]-y[2])/y[2])
35+
ax[1].semilogy(x,pdiffs[-1])
36+
ax[1].legend(['$|B3-B4|/B3$', '$|B3-B2|/B3$'])
37+
plt.show()
38+
plt.savefig('foo2.pdf')

sg_compare/foo3.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
3+
from math import exp
4+
from math import expm1
5+
import numpy
6+
import matplotlib.pyplot as plt
7+
8+
9+
10+
class Bernoulli:
11+
def __init__(self, maxorder):
12+
self.maxorder = maxorder
13+
self.denominators = [1.0,] * maxorder
14+
for i in range(1, maxorder):
15+
self.denominators[i] = (i+1) * self.denominators[i-1]
16+
self.factors = [1.0/x for x in self.denominators]
17+
18+
def calcbernoulli(self, x):
19+
xv = 1.0
20+
y = 0.0
21+
for i, v in enumerate(self.factors):
22+
y += v * xv
23+
xv *= x
24+
return 1.0 / y
25+
26+
27+
def bexpm(x):
28+
y = expm1(x)
29+
if y != x:
30+
return x/y
31+
return 1.0
32+
33+
34+
35+
def create_bernoulli(i):
36+
fx = Bernoulli(i)
37+
return lambda x : fx.calcbernoulli(x)
38+
39+
def b1(x):
40+
return x / (exp(x) - 1.0)
41+
42+
b2 = create_bernoulli(15)
43+
44+
b3 = bexpm
45+
46+
47+
x = numpy.linspace(0., 1.0, 10001)
48+
49+
50+
y = []
51+
#plt.title("comparison of 3 different Bernouilli Function implementations")
52+
fig, ax = plt.subplots(2,1)
53+
54+
55+
56+
funcs = [b2, b1]
57+
for i in range(2, 20):
58+
funcs.append(create_bernoulli(i))
59+
60+
for f in funcs:
61+
y.append(numpy.array(list(map(f, x))))
62+
ax[0].plot(x, y[-1])
63+
#ax[0].legend([r'$B1= \frac{x}{\exp(x)-1)}$', r'$B2=\frac{1}{1+0.5*x}$', r'$B3=\frac{x}{\text{expm1}(x)}$' "\n" r' $x \ne \text{expm1}(x), \text{else} 1$'], loc="lower left",
64+
#fancybox=True, framealpha=0.5)
65+
pdiffs=[]
66+
for i in range(1,len(y)):
67+
pdiffs.append(abs(y[i]-y[0])/y[0])
68+
ax[1].semilogy(x,pdiffs[-1])
69+
#ax[1].legend(['$|B3-B1|/B3$', '$|B3-B2|/B3$'])
70+
plt.show()
71+

0 commit comments

Comments
 (0)