To use Symbols in limits of Definite integral - numpy

I want to use the limits (Ef-e*V) and Ef as lower and upper limits in my Definite integral. where Ef is given 10 electron volts , e is the electronic charge and I want the ans in terms of V
How would i DO THAT????
PLEASE HELP
from scipy.integrate import quad
import sympy as sp
import math
e = 1.6 * (10 ** -19)
L = 10 ** -9
h = 6.626 * (10 ** -34)
h_cut = 1.05 * (10 ** -34)
m = 9.11 * (10 ** -31)
V0 = 4.0*e # in J
EF = 10.0*e # in J
E = sp.Symbol('E')
V = sp.Symbol('V')
def f(E):
j = (4 * E * (V0 - E)) / (4 * E * (V0 - E) + V0 ** 2 * ((2 * m * (V0 - E)) * ((L / h_cut) ** 2)))
return j
i, err = quad(f, EF - e * V, EF)
print('i= ', i)
I = (2 * e * i) / h
print(I)
```
THE error is as follows:
Traceback (most recent call last):
File "C:/Users/Subham/Desktop/Integration/integration.py", line 22, in <module>
i, err = quad(f, EF - e * V, EF)
File "C:\Users\Subham\AppData\Local\Programs\Python\Python37-32\lib\site-packages\scipy\integrate\quadpack.py", line 337, in quad
flip, a, b = b < a, min(a, b), max(a, b)
File "C:\Users\Subham\Desktop\Integration\venv\lib\site-packages\sympy\core\relational.py", line 304, in __nonzero__
raise TypeError("cannot determine truth value of Relational")
TypeError: cannot determine truth value of Relational

Since you are using SymPy, you could do the integral there. It works best if you work with Rational numbers, however, so we do this
>>> gx = nsimplify(f(x), rational=True)
Then compute the integral
>>> i = integrate(gx, (x,EF-e*V,EF))
And display it at whatever precision you desire. Here is the result to 2 sigfigs on each number:
>>> nfloat(i, 2)
1.6e-19*V + 1.7e-17*log(1.9e-17 - 1.6e-19*V) + 6.5e-16

Related

how to solve rsa related message attack with exponent of 65537?

c1 = ([message] + b'good')^e mod N
c2 = ([message] + b'hello')^e mod N
we got c1,c2 and e, N.how to get message recover
e.g:
from Crypto.Util.number import *
N = getPrime(1024) * getPrime(1024)
e = 65537
m = b'asdasdasdasdasdasdasdad'
print(N)
print(pow(bytes_to_long(m) + bytes_to_long(b'hello'), e, N))
print(pow(bytes_to_long(m) + bytes_to_long(b'good'), e, N))
# 13824259202707707704698945750555650188923092989268878762501795757989658824989810874884266999337165342953213211887061514825958553609144117703703258701326022555786135220477315399712884125614387173381827614381256300829440707734494820382614289703908717491613414182027679499244735432109060499149532381212541977764000110184519399366525605760733146179772941429335256079914560952785133589850388762981579475642697758871309468524199989272654149503691316057495226800070232198670430646266325131194854572267338487885035101469575839387407242537184807389659155489522800266906762308283506648079055576871122888512049475856330890001343
# 8276659109712793815299578561719138578174632968651765806041202217454912208960034861689360987329311934883429921704719276734783502306194357901541231165498953390016674194748506249298549911771734899753148678798552104754999924151283524844665822574622839869787064551474267788166961708626565174688777456346982873550222301030207711832604295851347969452338879329997396351375967501075989830884016503399111439211560006966836981816154790123070674190106461105668194452404913979853606956887928831700424160777167891676755674297360337554607504753418365643045588301072415879765662558357415118404821285358454161314770144838910923623504
# 12062529732270479369398408885987121015035665116730823919415100374357168027361875295561106591018597251449600246188613002097103212665616638658219288950827495971784194429969249895507132940672081924143164246070195738418425526032150020095433941736897502046253164728866258960451123211291798117834562763926759386287252564794804678055732450470110455829664228632583208321124476416193489413745132190993800532396985948065271318259684439260205668490905514290180662532835602885190966567491432319734167361123863401995933808462898545858708457817527307705396477763226807528727397866105985550723953556595591642168706497014266952344649

Division by Zero error in calculating series

I am trying to compute a series, and I am running into an issue that I don't know why is occurring.
"RuntimeWarning: divide by zero encountered in double_scalars"
When I checked the code, it didn't seem to have any singularities, so I am confused. Here is the code currently(log stands for natural logarithm)(edit: extending code if that helps):
from numpy import pi, log
#Create functions to calculate the sums
def phi(z: int):
k = 0
phi = 0
#Loop through 1000 times to try to approximate the series value as if it went to infinity
while k <= 100:
phi += ((1/(k+1)) - (1/(k+(2*z))))
k += 1
return phi
def psi(z: int):
psi = 0
k = 1
while k <= 101:
psi += ((log(k))/( k**(2*z)))
k += 1
return psi
def sig(z: int):
sig = 0
k = 1
while k <= 101:
sig += ((log(k))**2)/(k^(2*z))
k += 1
return sig
def beta(z: int):
beta = 0
k = 1
while k <= 101:
beta += (1/(((2*z)+k)^2))
k += 1
return beta
#Create the formula to approximate the value. For higher accuracy, either calculate more derivatives of Bernoulli numbers or increase the boundry of k.
def Bern(z :int):
#Define Euler–Mascheroni constant
c = 0.577215664901532860606512
#Begin computations (only approximation)
B = (pi/6) * (phi(1) - c - 2 * log(2 * pi) - 1) - z * ((pi/6) * ((phi(1)- c - (2 * log(2 * pi)) - 1) * (phi(1) - c) + beta(1) - 2 * psi(1)) - 2 * (psi(1) * (phi(1) - c) + sig(1) + 2 * psi(1) * log(2 * pi)))
#output
return B
A = int(input("Choose any value: "))
print("The answer is", Bern(A + 1))
Any help would be much appreciated.
are you sure you need a ^ bitwise exclusive or operator instead of **? I've tried to run your code with input parameter z = 1. And on a second iteration the result of k^(2*z) was equal to 0, so where is from zero division error come from (2^2*1 = 0).

How to vectorise an integration function?

I'm a beginner in numpy and I want to vectorise this function:
I don't quite understand what I need to do but this is what I've come up with:
n = 1000000
h = 1/n
x = np.arange(1,n,1)
def f(x):
return x ** 3
def rec(x):
result = np.zeros_like(x)
result[x < n] = f((x[x < n])*h)
return result
integral = 0.5*h + h*rec(x)
print integral
I end up with an array of 0's. Could someone please point me in the right direction?
Try:
def trap(f, a, b, n):
xs = np.linspace(a, b, n + 1)
ys = f(xs)
return (0.5 * ys[0] + 0.5 * ys[-1] + np.sum(ys[1:-1])) * (b - a) / n

Multiplying multidimensional array in python

I have two arrays:
L, M, N = 6, 31, 500
A = np.random.random((L, M, N))
B = np.random.random((L, L))
I am trying to get an array C such that:
C = B * A
C has dimension [L, M, N]
I tried answer posted at this link but it hasn't given me the desired output.
A for loop version of above code is:
L, M, N = 6, 31, 500
A = np.random.random((L, M, N))
B = np.random.random((L, L))
z1 = []
for j in range(M):
a = np.squeeze(A[:, j, :])
z1.append(np.dot(B, a))
z2 = np.stack(z1)
I think you are looking for numpy.tensordot() where you can specify along which axes to sum:
np.tensordot(B,A,axes=(1,0))

Gamma distribution, incomplete beta function

I need to calculate Gamma cumulative distribution, and it seems this is fairly equivalent to calculating the incomplete beta function.
Excel does have an inculded calculator, but I found no trace of the used algorithm.
Do any of you know an accurate way to calculate this function?
I tried the following, translated into VB.NET from a website, but it gives stupid results:
Function IncompleteBetaFunc(x As Double, a As Double, b As Double) As Double
If x <= 0 Or x >= 1 Then Return 0
Dim bt As Double
bt = Math.Exp(GammaLn(a + b) - GammaLn(a) - GammaLn(b) + a * Math.Log(x) + b * Math.Log(1.0 - x))
If x < (a + 1.0) / (a + b + 2.0) Then
Return bt * betacf(a, b, x) / a
Else
Return 1.0 - bt * betacf(b, a, 1.0 - x) / b
End If
End Function
Function betacf(x As Double, a As Double, b As Double) As Double
Const MAXIT As Integer = 100
Const EPS As Double = 0.0000003
Const FPMIN As Double = 1.0E-30
Dim aa, c, d, del, h, qab, qam, qap As Double
Dim m, m2 As Integer
qab = a + b
qap = a + 1.0
qam = a - 1.0
c = 1.0
d = 1.0 - qab * x / qap
If (Math.Abs(d) < FPMIN) Then d = FPMIN
d = 1.0 / d
h = d
For m = 1 To MAXIT
m2 = 2 * m
aa = m * (b - m) * x / ((qam + m2) * (a + m2))
d = 1.0 + aa * d
If (Math.Abs(d) < FPMIN) Then d = FPMIN
c = 1.0 + aa / c
If (Math.Abs(c) < FPMIN) Then c = FPMIN
d = 1.0 / d
h *= d * c
aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2))
d = 1.0 + aa * d
If (Math.Abs(d) < FPMIN) Then d = FPMIN
c = 1.0 + aa / c
If (Math.Abs(c) < FPMIN) Then c = FPMIN
d = 1.0 / d
del = d * c
h *= del
If (Math.Abs(del - 1.0) < EPS) Then Exit For
Next
Return h
End Function
Thanks!
Meta.Numerics includes well-tested and performant code for this any many other special functions. Its incomplete Beta function is documented here. The underlying code can be studied here. It also has a full-on Gamma distribution object, which will give moments, generate random variates, and do other distribution-related stuff in addition to computing the CDF. The package available via NuGet; just search for Meta.Numerics in the VS NuGet interface.