I have the following code, but it shows error:
IntegrationWarning: The maximum number of subdivisions (50) has been achieved.
If increasing the limit yields no improvement it is advised to analyze
the integrand in order to determine the difficulties. If the position of a
local difficulty can be determined (singularity, discontinuity) one will
probably gain from splitting up the interval and calling the integrator
on the subranges. Perhaps a special-purpose integrator should be used.
potC=sc.integrate.quad(lambda r: Psi(r,n2)*(-1/r)Psi(r,n1)(r**2),0,np.inf)
How to fix it?
import scipy as sc
import numpy as np
def Psi(r,n):
return 2*np.exp(-r/n)*np.sqrt(n)*sc.special.hyp1f1(1-n, 2, 2*r/n)/n**2
def p(n1,n2):
potC=sc.integrate.quad(lambda r: Psi(r,n2)*(-1/r)*Psi(r,n1)*(r**2),0,np.inf)
pot=potC[0]
return pot
print(p(15,15))
The error literally says what your problem is. Your function is not "well behaved" in some regions.
For example with n = 15 and r = 50 your special.hyp1f1(-14, 2, 2*50/15) result in NaN. I am not familiar with this function, so I do not know if this is the expected behaviour, but this is what happens.
You can try to isolate these points and exclude them from the integration (if you know lower and upper bounds of the function's value (if it is defined) you can also update the expected error of your integration) and just integrate in the well behaved regions.
If it is a bug in scipy, then please report it to them.
Ps.: Tested with scipy 1.8.0
Ps2.: With some reading I found, that you can get the values correctly, if you do your calculations with complex number, so the following code gives you a value:
import scipy as sc
from scipy import integrate
from scipy import special
import numpy as np
def Psi(r,n):
r = np.array(r,dtype=complex)
return 2*np.exp(-r/n)*np.sqrt(n)*special.hyp1f1(1-n, 2, 2*r/n)/n**2
def p(n1,n2):
potC=integrate.quad(lambda r: Psi(r,n2)*(-1/r)*Psi(r,n1)*(r**2),0,np.inf)
pot=potC[0]
return pot
print(p(15,15))
Related
I need to solve a large number of small convex optimisation problems with the same constraints, so I am trying to use cvxpy's DPP feature to cache/speedup compilation. It doesn't seem to work for my problem containing a single complex matrix parameter L.
import numpy as np
import cvxpy as cp
A = np.eye(4)+np.eye(4)*1j
L = cp.Parameter((4,4),complex=True)
X = cp.Variable((4,4),hermitian=True)
obj = cp.Minimize(cp.norm(X-L,'fro'))
prob = cp.Problem(obj)
L.value = A
assert prob.is_dcp(dpp=True)
prob.solve(solver=cp.SCS,verbose=True)
If I change the definitions of A and L to A=np.eye(4) and L = cp.Parameter((4,4)), then I do see the (Subsequent compilations of this problem, using the same arguments, should take less time.) message in the verbose print out.
I am using cvxpy version 1.2.1.
Does anyone know what's going on? Many thanks!
I'm using audiomath under Python 3.7 on Windows 10.
I have been using audiomath to standardize my audio files for EEG analyses, it has been very useful for all parameters except this one, I keep stuck when trying to make it create fade-ins, fade-outs or HannWindows.
I've run the same code on other machines with other versions of Python and Numpy and I still get the same error.
from audiomath import Sound, MakeRise
import numpy
sound01 = Sound('mySample.wav')
soundFadedIn = sound01.MakeHannWindow(5)
soundFadedIn.Play()
Log Error
As pointed out by #WarrenWeckesser, this was a bug in audiomath, which has been fixed in audiomath version 1.16.0+
Note that MakeHannWindow only returns the Hann weighting itself (with duration and sampling-frequency matched to sound01). It does not return the sound multiplied by the weighting as you seem to have assumed. What you seem to be trying to do may be better accomplished using the .Fade() method (which was also affected by the same bug).
With a little modification, the way you did it is one way to do it (it always gives you a symmetric fade-in and -out, optionally specifying the duration of the plateau in the middle, in seconds):
from audiomath import Sound
sound01 = Sound('mySample.wav')
soundFadedInAndOut = sound01 * sound01.MakeHannWindow(5) # note the multiplication
Or here's another, where you specify the duration of the rising and falling sections explicitly and separately, instead (it doesn't have to be symmetric, and either of the two durations can be 0):
from audiomath import Sound
sound01 = Sound('mySample.wav')
soundFadedInAndOut = sound01.Copy().Fade(risetime=0.5, falltime=0.5, hann=True)
Finally, if for some reason you're unable or unwilling to upgrade audiomath to 1.16, a workaround for the bug you're reporting might be to use the Shoulder() function from audiomath.Signal to generate your windowing function:
import audiomath as am, numpy as np
x = am.Sound('mySample.wav')
endFadeIn, startFadeOut = 0.5, x.duration-0.5
t = np.linspace(0, x.duration, x.nSamples) # in seconds
window = am.Signal.Shoulder(t, [0, endFadeIn, startFadeOut, x.duration]) # it's a numpy array, not a Sound
faded = x * window # but you can still multiply a Sound by it
faded.Play()
I have been trying to solve the following system of delay differential equations using JiTCDDE:
Model equations
And this is my code:
from jitcdde import y, t
from jitcdde import jitcdde
import matplotlib.pyplot as plt
import numpy as np
from pylab import *
N1=30290000
a1=0.98*N1
eps=1/5
b1=0.000024246
eta=0.3
B1=0.7
m=0.0005
chi=0.071
k1=0.185
alpha1=0.1155
delta=0.0225
phi1=0.26
omega1=0.26
d=3
model=[b1-((y(0)*B1*(y(2)+y(3)+(eta*y(5))))/a1)-b1*y(0)-m*y(0,t-d),
((y(0)*B1*(y(2)+y(3)+(eta*y(5))))/a1)-(k1+eps)*y(1)-m*y(1,t-d),
k1*eps*y(1)-(alpha1+chi)*y(2)-m*y(2,t-d),
(1-k1)*eps*y(1)-(phi1+omega1)*y(3)-m*y(3,t-d),
k1*y(1)+alpha1*y(2)-chi*y(4),
(phi1+omega1)*y(3)-(chi+delta)*y(5),
chi*(y(4)+y(5))-b1*y(6)-m*y(6,t-d),
delta*y(5)]
I=jitcdde(model)
I.constant_past([(0.98*N1-13),0,5,7,0,1,0,0], time=0.0)
I.step_on_discontinuities()
e=[]
for i in range(50):
e.append(I.integrate(i)[1])
print(e)
The problem is, for the second array of the solution (which I am trying to access), the first few values are negative values when I have specified that for t<0, the value is is 0. I have tried out this same model using ddeint and it gives a monotonically increasing curve with positive values, which is what I expect.
I want jitcdde to work though, since this model should run even when there is no delay term.
The first array seems fine and I have checked my model to see if I had made any typos but everything looks good to me. I have also tried using adjust_diff and integrate_blindly, but the issue remains.
import math
X=(math.asin(10*math.sin(math.radians(15)))/10*(180/math.pi))
print(X)
Whilst experimenting with using the sine rule it comes up with a ValueError: math domain error, however the code below does not bring up this issue.
import math
X=(math.asin(math.sin(math.radians(15)))*180/math.pi)
print(X)
I am aware that by multiplying by 10 then dividing by 10 equals 1 so the effects cancel out but I am unsure why an issue is being raised.
to debug the issue I always recommend to split the equation to smaller parts
Using following code you will see that issue coming in asin function because your argument is ~2.5 which is out of -1;1 range
import math
sin_val = math.sin(math.radians(15))
asin_val = math.asin(10*sin_val)
X = asin_val/10*(180/math.pi)
How does one choose the seed for the noise module in python?
I have this bit of code:
from noise import snoise2
terrainTiles[varX][varY].set_elevation(snoise2(x=varX/20,y=varY/20,octaves=1))
And it does create proper noise; however, i am unable to change the seed. I have been searching for hours and have yet to find a solution. Thanks!
Simplier example of the function:
from noise inport snoise2
print(snoise2(10,10))
SOLUTION
I found a solution separate of noise.py. I used this script I found on github: https://gist.github.com/eevee/26f547457522755cb1fb8739d0ea89a1
This also does not have a seed function, BUT, it has an unbias function so that far out coordinates still have proper noise. I used a 3 dimensional noise function where the 3rd dimension value is essentially the seed. Code shown here:
#generate world seed
worldSeed = random.randint(0, 100000000)
#generate noise objects. I hate this but im ghettoing it so that the 3rd dimension value is essentially the seed, I hate this but it works
elevationNoise = noise.PerlinNoiseFactory(dimension=3, octaves=1, unbias=True)
and it being applied to a value:
terrainTiles[varX][varY].set_elevation(elevationNoise(varX/20,varY/20,worldSeed)*1.15)
there is no seed parameter so to say, however there is a base parameter which you can modify which specifies an offset for the noise coordinates. For example:
import random
from noise import snoise2
seed = random.random()
print snoise2(10, 10, base=seed)
where base requires a float
so for your first example you should just be able to add base=seed to your snoise2(..):
terrainTiles[varX][varY].set_elevation(snoise2(x=varX/20,y=varY/20,octaves=1, base=seed))