I have managed to get my code working so it generates pi:
while True:
print("how many digits of pi would you like?")
def make_pi():
q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
for j in range(1000000):
if 4 * q + r - t < m * t:
yield m
q, r, t, k, m, x = 10 * q, 10 * (r - m * t), t, k, (10 * (3 * q + r)) // t - 10 * m, x
else:
q, r, t, k, m, x = q * k, (2 * q + r) * x, t * x, k + 1, (q * (7 * k + 2) + r * x) // (t * x), x + 2
digits = make_pi()
pi_list = []
my_array = []
for i in make_pi():
my_array.append(str(i))
number = int(input())+2
my_array = my_array[:1] + ['.'] + my_array[1:]
big_string = "".join(my_array[: number ])
print("here is the string:\n %s" % big_string)
however no matter how much I increase the range the code only outputs a maximum of 2315 digits of pi after the decimal point
how can I fix this?
What about parametrizing that make_pi generator to accept the number of digits?
Something like this:
def make_pi(num_digits):
q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
for j in range(num_digits):
if 4 * q + r - t < m * t:
yield m
q, r, t, k, m, x = 10 * q, 10 * \
(r - m * t), t, k, (10 * (3 * q + r)) // t - 10 * m, x
else:
q, r, t, k, m, x = q * \
k, (2 * q + r) * x, t * x, k + \
1, (q * (7 * k + 2) + r * x) // (t * x), x + 2
num_digits = 10000
pi = "".join([str(d) for d in make_pi(num_digits)])
print("{0}.{1}".format(pi[:1], pi[1:]))
Related
The following code generate adjacency matrix of a specific network. However, I want the node numbering to occur in a certain way and remain fixed and not fluctuate with every run. I present the current and expected output.
import matplotlib.pyplot as plt
import networkx as nx
N = 2
G = nx.Graph()
for u in range(2 * N * (N + 1)):
if u % (2 * N + 1) < N:
for v in (u - 2 * N - 1, u - N - 1, u - N):
if G.has_node(v):
G.add_edge(u, v)
elif u % (2 * N + 1) == N:
G.add_edge(u, u - N)
elif u % (2 * N + 1) < 2 * N:
for v in (u - 1, u - N - 1, u - N):
G.add_edge(u, v)
else:
for v in (u - 1, u - N - 1):
G.add_edge(u, v)
A=nx.adjacency_matrix(G).todense()
print([A])
nx.draw(G,with_labels=True, font_weight='bold')
The current output is
The expected output is
Here is a possible solution:
import networkx as nx
N = 2
def pos():
x, y = 1, N + 3 - 1
for _ in range(2 * N * (N + 1)):
yield (x, y)
y -= (x + 2) // (N + 3)
x = (x + 2) % (N + 3)
G = nx.Graph()
it_pos = pos()
for u in range(2 * N * (N + 1)):
G.add_node(u + 1, pos=next(it_pos))
if u % (2 * N + 1) < N:
for v in (u - 2 * N - 1, u - N - 1, u - N):
if G.has_node(v + 1):
G.add_edge(u + 1, v + 1)
elif u % (2 * N + 1) == N:
G.add_edge(u + 1, u - N + 1)
elif u % (2 * N + 1) < 2 * N:
for v in (u - 1, u - N - 1, u - N):
G.add_edge(u + 1, v + 1)
else:
for v in (u - 1, u - N - 1):
G.add_edge(u + 1, v + 1)
nx.draw(G, nx.get_node_attributes(G, 'pos'), with_labels=True, font_weight='bold')
This is the result of the code above:
Here I've used the pos() function to generate each node position. These positions are saved as tuples (x, y) within each node as a label. And they're eventually used to draw the graph.
My function returns 2 different values which I want to utilise in 2 different graphs using Matplotlib. How can I achieve it?
def option_value_european_put(T, m, r, sigma, mu, E):
cost_value_at_initial_t_put = []
portfolio_payoff_put = []
for e in E:
delta_t = T / m
u = (1 + (sigma * math.sqrt(delta_t)) * (math.sqrt(1 + ((mu ** 2) * delta_t) / math.pow(sigma, 2))))
v = 2 - u
option_stock_price_matrix_put = np.zeros((m + 1, m + 1))
sum = 0
k = m
start = m
for i in range(m + 1):
option_stock_price_matrix_put[i][start] = max(
(e - stock_price_binomial_model(
mu, sigma, T, m,
S
)[i][start], 0)
)
for j in range(m - 1, -1, -1):
for i in range(0, j + 1):
v_plus = option_stock_price_matrix_put[i][j + 1]
v_minus = option_stock_price_matrix_put[i + 1][j + 1]
v_t = ((((v_plus - v_minus) / (u - v)) * (1 + r * delta_t)) + (u * v_minus - v * v_plus) / (u - v)) / (
1 + r * delta_t)
option_stock_price_matrix_put[i][j] = v_t
cost_value_at_initial_t_put.append(option_stock_price_matrix_put[0][0])
for i in range(0, m+1):
sum = sum + option_stock_price_matrix_put[k][i]
portfolio_return_average = math.average(sum)
portfolio_payoff_put.append(portfolio_return_average-option_stock_price_matrix_put[0][0] )
return cost_value_at_initial_t_put, portfolio_payoff_put
I want to use cost_value_at_initial_t_put in 1 Matplotlib plot and the other value in another plot. How can I use it?
Supposing that cost_value_at_initial_t_put and portfolio_payoff_cut are both lists you can create subplots:
import matplotlib.pyplot as plt
fig, (ax_cost, ax_payoff) = plt.subplots(nrows=2)
ax_cost.plot(cost_value_at_initial_t_put)
ax_payoff.plot(portfolio_payoff_cut)
def colorize(im, h, s, l_adjust):
result = Image.new('RGBA', im.size)
pixin = np.copy(im)
pixout = np.array(result)
>>>>>>>>>>>>>>>>> loop <<<<<<<<<<<<<<<<<
for y in range(pixout.shape[1]):
for x in range(pixout.shape[0]):
lum = currentRGB(pixin[x, y][0], pixin[x, y][1], pixin[x, y][2])
r, g, b = colorsys.hls_to_rgb(h, lum, s)
r, g, b = int(r * 255.99), int(g * 255.99), int(b * 255.99)
pixout[x, y] = (r, g, b, 255)
>>>>>>>>>>>>>>>>>>>>> Loop end <<<<<<<<<<<
return result
Trying to find the HSL per pixel value from a frame of input video but it's taking too much time about 1.5s but want to reduce the time to at least within 0.3s. Any faster way to do this without using these 2 loops? Looking for something like LUT(Look up table)/vectorize/something with NumPy shortcut to avoid those 2 loops. Thanks
OR
Part 2 ->>
If I break the custom currentRGB() into the for loops it looks like :
def colorize(im, h, s, l_adjust):
result = Image.new('RGBA', im.size)
pixin = np.copy(im)
pixout = np.array(result)
for y in range(pixout.shape[1]):
for x in range(pixout.shape[0]):
currentR, currentG, currentB = pixin[x, y][0]/255 , pixin[x, y][1]/255, pixin[x, y][2]/255
#luminance
lum = (currentR * 0.2126) + (currentG * 0.7152) + (currentB * 0.0722)
if l_adjust > 0:
lum = lum * (1 - l_adjust)
lum = lum + (1.0 - (1.0 - l_adjust))
else:
lum = lum * (l_adjust + 1)
l = lum
r, g, b = colorsys.hls_to_rgb(h, l, s)
r, g, b = int(r * 255.99), int(g * 255.99), int(b * 255.99)
pixout[x, y] = (r, g, b, 255)
return pixout
You can use Numba to drastically speed the computation up. Here is the implementation:
import numba as nb
#nb.njit('float32(float32,float32,float32)')
def hue_to_rgb(p, q, t):
if t < 0: t += 1
if t > 1: t -= 1
if t < 1./6: return p + (q - p) * 6 * t
if t < 1./2: return q
if t < 2./3: return p + (q - p) * (2./3 - t) * 6
return p
#nb.njit('UniTuple(uint8,3)(float32,float32,float32)')
def hls_to_rgb(h, l, s):
if s == 0:
# achromatic
r = g = b = l
else:
q = l * (1 + s) if l < 0.5 else l + s - l * s
p = 2 * l - q
r = hue_to_rgb(p, q, h + 1./3)
g = hue_to_rgb(p, q, h)
b = hue_to_rgb(p, q, h - 1./3)
return (int(r * 255.99), int(g * 255.99), int(b * 255.99))
#nb.njit('void(uint8[:,:,::1],uint8[:,:,::1],float32,float32,float32)', parallel=True)
def colorize_numba(pixin, pixout, h, s, l_adjust):
for x in nb.prange(pixout.shape[0]):
for y in range(pixout.shape[1]):
currentR, currentG, currentB = pixin[x, y, 0]/255 , pixin[x, y, 1]/255, pixin[x, y, 2]/255
#luminance
lum = (currentR * 0.2126) + (currentG * 0.7152) + (currentB * 0.0722)
if l_adjust > 0:
lum = lum * (1 - l_adjust)
lum = lum + (1.0 - (1.0 - l_adjust))
else:
lum = lum * (l_adjust + 1)
l = lum
r, g, b = hls_to_rgb(h, l, s)
pixout[x, y, 0] = r
pixout[x, y, 1] = g
pixout[x, y, 2] = b
pixout[x, y, 3] = 255
def colorize(im, h, s, l_adjust):
result = Image.new('RGBA', im.size)
pixin = np.copy(im)
pixout = np.array(result)
colorize_numba(pixin, pixout, h, s, l_adjust)
return pixout
This optimized parallel implementation is about 2000 times faster than the original code on my 6-core machine (on 800x600 images). The hls_to_rgb implementation is coming from this post. Note that the string in #nb.njit decorators are not mandatory but enable Numba to compile the function ahead of time instead of at the first call. For more information about the types, please read the Numba documentation.
I need to write the following pseudocode into Python code:
enter image description here
And here is my code:
import math
def f(x):
v = math.sin(math.pi/2*x)
return v
def zero_matrix(i, j, start_from=1):
matrix = [[-1] * (j+start_from)]
for x in range(i):
line = [-1] * start_from + [0] * j
matrix.append(line)
return matrix
def zero_vector(i, start_from=1):
return [-1] * start_from + [0] * i
def algo_12_3(l, T, alpha, m, N):
"""Crank-Nicolson Method.
Args:
l: endpoint
T: maximum time
alpha: constant
m:
N:
Return:
approximations wi,j to u(xi, tj) for each i=i,...,m-1 and j=1,...,N.
"""
w = zero_matrix(m, N)
z = zero_vector(m-1)
ll = zero_vector(m-1)
u = zero_vector(m-1)
h = l / m
k = T / N
lambda_ = alpha * alpha * k / (h * h)
for i in range(1, m):
w[i][0] = f(i * h)
ll[1] = 1 + lambda_
u[1] = -lambda_/(2*ll[1])
for i in range(2, m-1):
ll[i] = 1 + lambda_ + lambda_ * u[i-1]/2
u[i] = -lambda_/(2 * ll[i])
ll[m-1] = 1 + lambda_ + lambda_ * u[m-2]/2
for j in range(1, N + 1):
t = j * k
z[1] = ((1 - lambda_) * w[1][j-1] + lambda_ / 2 * w[2][j-1]) / ll[1]
for i in range(2, m):
z[i] = ((1 - lambda_) * w[i][j-1] + lambda_ / 2 * (w[i+1][j-1] + w[i-1][j-1] + z[i-1])) / ll[i]
w[m-1][j] = z[m-1]
for i in range(m-2, 0, -1):
w[i][j] = z[i] - u[i] * w[i+1][j]
print('t={}: '.format(t))
for i in range(1, m):
print('({}, {})'.format(i*h, w[i][j]))
algo_12_3(2, 0.1, 1, 4, 2)
My outputs are:
t=0.05:
(0.5, 0.6282614874855517)
(1.0, 0.8822836003342388)
(1.5, 0.5449281541522184)
t=0.1:
(0.5, 0.55687611895338)
(1.0, 0.7741379272219071)
(1.5, 0.5013205633978245)
However, the correct outputs should be:
t=0.05:
(0.5, 0.62884835)
(1.0, 0.88932586)
(1.5, 0.62884835)
t=0.1:
(0.5, 0.59404972)
(1.0, 0.84011317)
(1.5, 0.59404972)
I don't know if it's with the range or with the initial matrix formation. Can anyone help me with it? Thanks a lot!! Appreciate it!
I'm trying to plot an airfoil from the formula as described on this wikipedia page.
This Jupyter notebook can be viewed on this github page.
%matplotlib inline
import math
import matplotlib.pyplot as pyplot
def frange( start, stop, step ):
yield start
while start <= stop:
start += step
yield start
#https://en.wikipedia.org/wiki/NACA_airfoil#Equation_for_a_cambered_4-digit_NACA_airfoil
def camber_line( x, m, p, c ):
if 0 <= x <= c * p:
yc = m * (x / math.pow(p,2)) * (2 * p - (x / c))
#elif p * c <= x <= c:
else:
yc = m * ((c - x) / math.pow(1-p,2)) * (1 + (x / c) - 2 * p )
return yc
def dyc_over_dx( x, m, p, c ):
if 0 <= x <= c * p:
dyc_dx = ((2 * m) / math.pow(p,2)) * (p - x / c)
#elif p * c <= x <= c:
else:
dyc_dx = ((2 * m ) / math.pow(1-p,2)) * (p - x / c )
return dyc_dx
def thickness( x, t, c ):
term1 = 0.2969 * (math.sqrt(x/c))
term2 = -0.1260 * (x/c)
term3 = -0.3516 * math.pow(x/c,2)
term4 = 0.2843 * math.pow(x/c,3)
term5 = -0.1015 * math.pow(x/c,4)
return 5 * t * c * (term1 + term2 + term3 + term4 + term5)
def naca4( m, p, t, c=1 ):
for x in frange( 0, 1.0, 0.01 ):
dyc_dx = dyc_over_dx( x, m, p, c )
th = math.atan( dyc_dx )
yt = thickness( x, t, c )
yc = camber_line( x, m, p, c )
xu = x - yt * math.sin(th)
xl = x + yt * math.sin(th)
yu = yc + yt * math.cos(th)
yl = yc - yt * math.cos(th)
yield (xu, yu), (xl, yl)
#naca2412
m = 0.02
p = 0.4
t = 12
naca4points = naca4( m, p, t )
for (xu,yu),(xl,yl) in naca4points:
pyplot.plot( xu, yu, 'r,')
pyplot.plot( xl, yl, 'r,')
pyplot.ylabel('y')
pyplot.xlabel('x')
pyplot.axis('equal')
figure = pyplot.gcf()
figure.set_size_inches(16,16,forward=True)
The result looks like .
I expected it to look more like .
Questions: Why is the line not completely smooth? There seems to be a discontinuity where the beginning and end meet. Why does it not look like the diagram on wikipedia? How do I remove the extra loop at the trailing edge? How do I fix the chord so that it runs from 0.0 to 1.0?
First, t should be 0.12 not 12. Second, to make a smoother plot, increase the sample points.
It is also a good idea to use vectorize method in numpy:
%matplotlib inline
import math
import matplotlib.pyplot as plt
import numpy as np
#https://en.wikipedia.org/wiki/NACA_airfoil#Equation_for_a_cambered_4-digit_NACA_airfoil
def camber_line( x, m, p, c ):
return np.where((x>=0)&(x<=(c*p)),
m * (x / np.power(p,2)) * (2.0 * p - (x / c)),
m * ((c - x) / np.power(1-p,2)) * (1.0 + (x / c) - 2.0 * p ))
def dyc_over_dx( x, m, p, c ):
return np.where((x>=0)&(x<=(c*p)),
((2.0 * m) / np.power(p,2)) * (p - x / c),
((2.0 * m ) / np.power(1-p,2)) * (p - x / c ))
def thickness( x, t, c ):
term1 = 0.2969 * (np.sqrt(x/c))
term2 = -0.1260 * (x/c)
term3 = -0.3516 * np.power(x/c,2)
term4 = 0.2843 * np.power(x/c,3)
term5 = -0.1015 * np.power(x/c,4)
return 5 * t * c * (term1 + term2 + term3 + term4 + term5)
def naca4(x, m, p, t, c=1):
dyc_dx = dyc_over_dx(x, m, p, c)
th = np.arctan(dyc_dx)
yt = thickness(x, t, c)
yc = camber_line(x, m, p, c)
return ((x - yt*np.sin(th), yc + yt*np.cos(th)),
(x + yt*np.sin(th), yc - yt*np.cos(th)))
#naca2412
m = 0.02
p = 0.4
t = 0.12
c = 1.0
x = np.linspace(0,1,200)
for item in naca4(x, m, p, t, c):
plt.plot(item[0], item[1], 'b')
plt.plot(x, camber_line(x, m, p, c), 'r')
plt.axis('equal')
plt.xlim((-0.05, 1.05))
# figure.set_size_inches(16,16,forward=True)
Thanks for the code.
I have modified the code for symmetrical airfoils:
def naca4s(x, t, c=1):
yt = thickness(x, t, c)
return ((x, yt),
(x, -yt))