Python C Extensions Optimize For Speed (Linux) - python

I am trying to compile some Python C extensions for both Windows and Linux. The most time consuming part of my program seems to be a for loop with a significant amount of simple calculations. When running it on Linux, it is about 2x slower than on Windows. Adding -O2 or -O3 seems to help a little bit, but not much. Are there any other optimization methods that would get it to a comparable speed?
The compiler I'm using is GCC: gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Here is the python equivalent. The C code is using a loop instead of numpy operations:
def cf(sigma, kappa, theta, rho, s0, v0, r, ttm, n,
alpha = 1.0, delta = 0.01, lambda_ = 0.002, u = -0.5):
x = np.log(s0)
phi = np.arange(n) * delta - 1j*(alpha + 1)
qq = kappa - 1j * rho * sigma * phi
d = np.sqrt(qq ** 2 + sigma ** 2 * phi ** 2 + 1j * sigma ** 2 * phi)
c = (qq - d) / (qq + d)
cc = 1j * phi * r * ttm + ((kappa * theta) / sigma ** 2) * \
((qq - d) * ttm - 2 * np.log((-c * np.exp(-d * ttm) + 1) / (1 - c)))
dd = ((qq - d) / sigma ** 2) * (1 - np.exp(-d * ttm)) / (-c * np.exp(-d * ttm)+1)
f = np.exp(cc + dd * v0 + 1j * phi * x)
return f

Related

Solving a double integral with scipy dblquad

I am trying to solve the double integral of this complicated function. The function contains 3 symbolic variables which are defined with sympy.symbols. My goal is to integrate the function with respect to only two of the variables. sym.integrate run for 2 hours with no results to show. I tried numerical integration with scipy.integrate.dblquad. But I am having troubles which I suspect is due to the third symbolic variable. Is there a way to do this.
Problem summarized.
sym.symbols('x y z')
My_function(x,y,z)
Integrate My_function with respect to x and y (Both from 0 to inf i.e. definite integral).
Thank you in advance
h, t, w, r, q = sym.symbols('h t w r q') # Define symbols
wn = 2.0
alpha = 1.76
beta = 1.59
a0 = 0.7
a1 = 0.282
a2 = 0.167
b0 = 0.07
b1 = 0.3449
b2 = -0.2073
psi = 0.05
F_H = 1.0 - sym.exp(-(h / alpha) ** beta)
mu_h = a0 + a1 * h ** a2
sig_h = b0 + b1 * sym.exp(b2 * h)
F_TIH = (1 / 2) * (1 + sym.erf((sym.log(t) - mu_h) / (sig_h * sym.sqrt(2))))
f_h = sym.diff(F_H, h)
f_tzIhs = sym.diff(F_TIH, t)
f_S = f_h * f_tzIhs
H = (1.0 - (w / wn) ** 2.0 + 1.0j * 2.0 * psi * w / wn) ** (-1.0)
S_eta_h_t = h ** 2.0 * t / (8.0 * pi ** 2.0) * (w * t / (2.0 * pi)) ** (-5.0) * sym.exp(-1.0 / pi * (w * t / (2.0 * pi)) ** (-4.0))
S_RIS_hu_tu = abs(H) ** 2.0 * S_eta_h_t
m0_s = sym.integrate((w ** 0 * S_RIS_hu_tu), (w, 0, np.inf))
m0_s.doit()
m2_s = sym.integrate((w ** 2 * S_RIS_hu_tu), (w, 0, np.inf))
m2_s.doit()
v_rIs = 1 / (2 * pi) * sym.sqrt(m2_s / m0_s) * sym.exp(-r ** 2 / (2 * m0_s))
fun = v_rIs * f_S
# The integral I am trying to solve is a function of h,t and r.
integ_ht = sym.integrate(fun,(h,0,np.inf),(t,0,np.inf))

trying to solve differential equations simultaneously

I am trying to build a code for chemical reactor design which is able to solve for the pressure drop, conversion, and temperature of a reactor. All these parameters have differential equations, so i tried to define them inside a function to be able to integrate them using ODEINT. However it seems that the function i've built has an error which i can't figure out which held me back from integration it.
the error that i'm encountering:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-32-63c706e84be5> in <module>
1 X0=[0.05,1200,2]
----> 2 y=func(X0,0)
<ipython-input-27-6cfd4fef5ee2> in func(x, W)
8 kp=np.exp(((42311)/(R*T))-11.24)
9 deltah=-42471-1.563*(T-1260)+0.00136*(T**2 -1260**2)- 2,459*10e-7*(T**3-1260**3)
---> 10 ra=k*np.sqrt((1-X)/X)*((0.2-0.11*X)/(1-0.055*X)*(P/P0)-(x/(kp*(1-x)))**2)
11 summ = 57.23+0.014*T-1.94*10e-6*T**2
12 dcp=-1.5625+2.72*10e-3*T-7.38*10e-7*T**2
TypeError: unsupported operand type(s) for -: 'int' and 'list'
and here is the full code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
fi = 0.45
gas_density = 0.054 #density
Pres0 = 2 #pressure
visc = 0.09
U = 10
Ac = 0.0422
T0 = 1400 #and 1200 also
gc = 4.17 * 10**8
bed_density = 33.8
Ta = 1264.6 #wall temp
fa0 = 0.188
def func(x,W):
X = x[0]
T = x[1]
P = x[2]
P0 = 2
R = 0.7302413
k = np.exp((-176008 / T) - (110.1 * np.log(T) + 912.8))
kp = np.exp(((42311) / (R * T)) - 11.24)
deltah = -42471 - 1.563 * (T - 1260) + 0.00136 * (T**2 - 1260**2) - 2,459 * 10e-7 * (T**3 - 1260**3)
ra = k * np.sqrt((1 - X) / X) * ((0.2 - 0.11 * X) / (1 - 0.055 * X) * (P / P0) - (x / (kp * (1 - x)))**2)
summ = 57.23 + 0.014 * T - 1.94 * 10e-6 * T**2
dcp = -1.5625 + 2.72 * 10e-3 * T - 7.38 * 10e-7 * T**2
dxdw = 5.31 * k * np.sqrt((1 - X) / X) * ((0.2 - 0.11 * X) / (1 - 0.055 * X) * (P / P0) - (x / (kp * (1 - x)))**2)
dpdw = (((-1.12 * 10**-8) * (1 - 0.55 * X) * T) / P) * (5500 * visc + 2288)
dtdw = (5.11 * (Ta - T) + (-ra) * deltah) / (fa0 * (summ + x * dcp))
return [dxdw, dpdw, dtdw]
X0 = [0.05, 1200, 2]
y = func(X0, 0)
thanks in advance
Inside line
ra=k*np.sqrt((1-X)/X)*((0.2-0.11*X)/(1-0.055*X)*(P/P0)-(x/(kp*(1-x)))**2)
you probably want to use ...X/(kp*(1-X))... instead of ...x/(kp*(1-x))... (i.e. use upper X), lower x is list type.
If you want to use some list variable l as multiple values somewhere then convert it to numpy array la = np.array(l) and use la in numpy vectorized expression.

How do you iterate and update a numpy array to solve a system of linear equations of form Ax=B?

So I am looking to solve a system of equations in python 3.7 with numpy. However, I need to solve the system of equations at the end of each iteration. During the iterations, it will solve some equations that will make up the contents of A and B to find x in the form of Ax=B. Upon solving for x I need to save these values to then solve the underlying equations for the following iteration to be reimplemented in A and B.
I have tried a more linear approach to solving the problem but it is not good for my end goal of solving the equation attached in the image. What I have done so far has also been attached below:
i = 0
while (y[i] >= 0 ): #Object is above water
t[i+1] = t[i] + dt
vx[i+1] = vx[i] + dt * ax[i] #Update the velocities
vy[i+1] = vy[i] + dt * ay[i]
v_ax[i+1] = (vx[i]*np.sin(phi/180*np.pi)) - (vy[i]*np.cos(phi/180*np.pi))
v_nor[i+1] = (vx[i]*np.cos(phi/180*np.pi)) + (vy[i]*np.sin(phi/180*np.pi))
F_wnor[i+1] = (Cd_a * A_da * rho_air * (v_nor[i] - v_wind*np.sin(phi/180*np.pi)) * abs(v_nor[i] - v_wind*np.sin(phi/180*np.pi)))/2
F_wax[i+1] = (Cd_a * A_da * rho_air * (v_ax[i] - v_wind*np.sin(phi/180*np.pi)) * abs(v_ax[i] - v_wind*np.sin(phi/180*np.pi)))/2
F_wx[i+1] = (-F_wax[i] * np.sin(phi/180*np.pi)) - (F_wnor[i] * np.cos(phi/180*np.pi))
F_wy[i+1] = (F_wax[i] * np.cos(phi/180*np.pi)) - (F_wnor[i] * np.sin(phi/180*np.pi))
ax[i+1] = F_wx[i]/M
ay[i+1] = (F_wx[i]/M) - g
y[i+1] = (y[i]+dt*vy[i])
x[i+1] = (x[i]+dt*vx[i])
i = i + 1
j = i
#under water velocities
# if y(t)>0: M*z'' = M.g - Fb + Fd + Fm
while (y[j] <= 0 and y[j] > -10):
if (abs(y[j]/r)< 2):
theta_degree = 2 * np.arccos(1 - (abs(y[j])/r))
theta = theta_degree/180*np.pi
m = ((rho_water * r**2)/2) * (((2*(np.pi)**3*(1-np.cos(theta))) / ( 3 * (2*np.pi-theta)**2)) \
+ (np.pi * (1-np.cos(theta)*1/3)) + (np.sin(theta)) - (theta))
dm_dz = ((rho_water * r)/np.sin(theta/2)) * (((2 * (np.pi)**3 / 3) * ((np.sin(theta) / (2*np.pi - theta)**2) \
+ (2 * (1-np.cos(theta)) / (2*np.pi - theta )**3))) + (np.pi * np.sin(theta) / 3) + np.cos(theta) - 1)
A_i = (r**2)/2 * (theta - np.sin(theta))
F_m[j] = - m * ay[j] - dm_dz * np.max(vy)*vy[j]
F_uwater[j] = (M * g) - (rho_water * A_i * g) - (Cd_y * rho_water * r * vy[j] * abs(vy[j]))
else:
m = np.pi * rho_water * r**2
dm_dz = 0
A_i = np.pi * r**2
F_m[j] = - m * ay[j] - dm_dz * vy[j]**2
F_uwater[j] = (M * g) - (rho_water * A_i * g) - (Cd_y * rho_water * r * vy[j] * abs(vy[j]))
print("Fully submerged")
t[j+1] = t[j] + dt
vx[j+1] = vx[j] + dt * ax[j] #Update the velocities
vy[j+1] = vy[j] + dt * ay[j]
ax[j+1] = F_wx[j]/M
ay[j+1] = (F_uwater[j] + F_m[j]/M)
y[j+1] = (y[j]+dt*vy[j])
x[j+1] = (x[j]+dt*vx[j])
print(y[j])
j = j + 1
I do not know how to go about this and help for getting started would be greatly appreciated!.
The problem I am trying to solve can be seen more clearly in the picture attached. System of equations I am trying to solve

Sympy - Integration is slow when expression contains many symbols

Say I have the following expression which I would like to integrate over the variable z from 0 to L.
import sympy as sp
mdot, D, R, alpha, beta, xi, mu0, q, cp, Tin, L = sp.symbols("\dot{m}, D, R, alpha, beta, xi, mu_0, q, c_p, T_in, L", real=True, positive=True, constant=True)
z = sp.symbols("z", real=True, positive=True)
n = sp.Symbol("n", real=True)
firstexpr = 8 * mdot**2 * R / (sp.pi**2 * D**5) * (alpha + beta * (sp.pi * D * mu0 / (4 * mdot))**xi * (q * z / (mdot * cp) + Tin)**(n * xi)) * (q * z / (mdot * cp) + Tin)
res1 = sp.integrate(firstexpr, (z, 0, L), conds="none")
This will take forever: I had to stop the computation after 10 minutes on my pc without getting an answer.
Situation improves dramatically if I rewrite my expression so that it contains only the minimum number of constant symbols, integrating it, and finally substituting the original symbols:
a = 8 * mdot**2 * R / (sp.pi**2 * D**5)
b = beta * (sp.pi * D * mu0 / (4 * mdot))**xi
c = q / (mdot * cp)
_a, _b, _c = sp.symbols("a, b, c", real=True, positive=True, constant=True)
secondexpr = _a * (alpha + _b * (_c * z + Tin)**(n * xi)) * (_c * z + Tin)
res2 = sp.integrate(secondexpr, (z, 0, L), conds="none")
sp.simplify(res2.subs([(_a, a), (_b, b), (_c, c)]))
Why is sympy taking extremely long time in the first case? Did I miss some assumption in the creation of my symbols?

How to convert Normal/cdf() computation that was originally written using sympy.statistics to use sympy.stats?

I haven't kept up with the changes in SymPy. I was looking at the following Black Scholes formula at: https://aaronschlegel.me/black-scholes-formula-python.html. It seems there was some refactoring that was done in SymPy so this no longer works. How would I change the following so it works again:
import sympy as sy
import sympy.statistics as systats
def euro_put_sym(S, K, T, r, sigma):
#S: spot price
#K: strike price
#T: time to maturity
#r: interest rate
#sigma: volatility of underlying asset
N = systats.Normal(0.0, 1.0)
d1 = (sy.ln(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * sy.sqrt(T))
d2 = (sy.ln(S / K) + (r - 0.5 * sigma ** 2) * T) / (sigma * sy.sqrt(T))
put = (K * sy.exp(-r * T) * N.cdf(-d2) - S * N.cdf(-d1))
return put
The errors I get are:
No name 'statistics' in module 'sympy'
Unable to import 'sympy.statistics'
In particular, how would the Normal and cdf be done now?
The statistics module in Sympy is named stats.
Also, Normal takes 3 parameters. A simple usage example is shown below:
>>> from sympy import symbols
>>> from sympy.stats import Normal, density, cdf
>>> x, mu, sigma = symbols("x mu sigma")
>>> N = Normal("N", mu, sigma)
>>> density(N)(x)
sqrt(2)*exp(-(-mu + x)**2/(2*sigma**2))/(2*sqrt(pi)*sigma)
>>> cdf(N)(x)
erf(sqrt(2)*(-mu + x)/(2*sigma))/2 + 1/2
For further reference, see Sympy Stats docs and Normal Distribution docs.
Now for your case, let me explain what you should be doing.
import sympy as sy
import sympy.stats as systats
def euro_put_sym(S, K, T, r, sigma):
#S: spot price
#K: strike price
#T: time to maturity
#r: interest rate
#sigma: volatility of underlying asset
N = systats.Normal('N', 0.0, 1.0)
d1 = (sy.ln(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * sy.sqrt(T))
d2 = (sy.ln(S / K) + (r - 0.5 * sigma ** 2) * T) / (sigma * sy.sqrt(T))
put = (K * sy.exp(-r * T) * systats.cdf(N)(-d2) - S * systats.cdf(N)(-d1))
return put
Now,
S, K, T, r, sigma = sy.symbols("S K T r sigma")
sy.pprint(euro_put_sym(S, K, T, r, sigma), use_unicode=False)
gives output as
/ / ___ / / 2\ /S\\\\ / /
| |0.5*\/ 2 *|T*\r - 0.5*sigma / + log|-|||| | |0.5*\/
| | \ \K//|| | |
| erf|---------------------------------------|| | erf|------
| | ___ || | |
|1 \ \/ T *sigma /| -T*r |1 \
K*|- - --------------------------------------------|*e - S*|- - ----------
\2 2 / \2
___ / / 2\ /S\\\\
2 *|T*\r + 0.5*sigma / + log|-||||
\ \K//||
---------------------------------||
___ ||
\/ T *sigma /|
----------------------------------|
2 /
Is this the expected output?
I tested it through the example in the link you have provided and the results are matching.
>>> euro_put_sym(50, 100, 1, 0.05, 0.25)
-25*erf(1.22379436111989*sqrt(2)) + 22.5614712250357 + 47.5614712250357*erf(1.34879436111989*sqrt(2))
I think this is the correct conversion from the old statistics package to the new one. What do you guys think?
import sympy as sy
import sympy.stats as systats
def euro_put_sym(S, K, T, r, sigma):
#S: spot price
#K: strike price
#T: time to maturity
#r: interest rate
#sigma: volatility of underlying asset
N = systats.Normal('x', 0.0, 1.0)
d1 = (sy.ln(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * sy.sqrt(T))
d2 = (sy.ln(S / K) + (r - 0.5 * sigma ** 2) * T) / (sigma * sy.sqrt(T))
put = (K * sy.exp(-r * T) * systats.cdf(N)(-d2) - S * systats.cdf(N)(-d1))
return put

Categories