Why is the output of my function in binary? - python

I wrote a differentiable Heaviside function and vectorised it. However, the output seems to be odd and binary. The code is as follows:
import numpy as np
import matplotlib.pyplot as plt
def heaviside(x, epis):
if (x>= epis):
y=1
elif (x< -epis):
y=0;
else:
y = 0.5 + x/(2*epis) + np.sin(np.pi*x/epis)/(2*np.pi);
print (x, y);
return y;
x1 = np.linspace(-1, 1, 100);
vheaviside = np.vectorize(heaviside);
y1 = vheaviside(x1, 0.2);
plt.plot(x1, y1, 'b', alpha=0.3)
plt.show()
The input is an array in [-1, 1], and the outputs are supposed to be a continuous Heaviside function. However, all of the output is 0 or 1. Why?

You need to specify when you vectorize the function that it should be using floats:
vheaviside = np.vectorize(heaviside, [float])
otherwise, per the documentation:
The output type is determined by evaluating the first element of the input
which in this case is an integer. Alternatively, make sure heaviside always returns a float, by replacing e.g. y = 1 with y = 1.0.

A simpler and more efficient way of defining a vectorized Heaviside function with Numpy would be using numpy.piecewise:
def heaviside(x, epis):
return np.piecewise(x, (x>=epis, x<-epis),
(1, 0, lambda x: 0.5 + x/(2*epis) + np.sin(np.pi*x/epis)/(2*np.pi) ) )

As far as I know there is only discontinuous one.Try this:
def h(x):
if x == 0:
return 0.5
return 0 if x < 0 else 1

Related

Outputting results of loop of a loop

I am iteratively solving this implicit equation
using fsolve within a for loop over a range of values of the independent variable, V.
I also want to vary I_L and run the for loop over each value and generate an individual text files.
I know how to use the open and write text files, what I'm struggling with is setting loops up correctly to output what I want.
I have coded a simpler example below to allow for ease of understanding since it's just the loops I'm stuck on.
import numpy as np
from scipy.optimize import fsolve
import scipy.constants as sc
x = np.linspace(-1, 1, 1001)
C_vary = [0, 1, 2, 3]
def equation(y, x, C):
return C - np.exp(x+y) - y
for C in C_vary:
y = []
Solve equation at each value of C_vary and output y values to new list of
results
I have introduced an initial guess for the function y(x), but you can check the details further. The output y is a list, with each element corresponding to a value of the C_vary parameters, for each x.
import numpy as np
from scipy.optimize import fsolve
import scipy.constants as sc
x = np.linspace(-1, 1, 1001)
C_vary = [0, 1, 2, 3]
def equation(y, x, C):
return C - np.exp(x+y) - y
y0 = np.exp( 0.5*x ) #initial guess
y = [ fsolve( equation, y0, (x,ci) ) for ci in C_vary ]
If you are after I as a function of V and other parameters, you can solve the equation by means of the Lambert W function.
It has the form
z = e^(a z + b)
where z is linear in I, and this is
- a z e^(- a z) = - a e^b
or
z = - W(-a e^b) / a.

Python ternary if on numpy array with condition on array cell values

I have a conditional function, say f(x), that takes domain values from a numpy.ndarray the_array and maps into another numpy.ndarray, result
f(x) = g(x) if x>0
h(x) otherwise
g(x) and h(x) here are some other functions.
Looks to me something like the following, but I don't know how to refer to the corresponding array entries in the ternary if:
result = g(the_array) if <??> else h(the_array)
If there's no problem valuating g(x) and h(x) for all of x, then
result = np.where( x>0, g(x), h(x))
If g can be evaluated only at x>0, we have to do more work. For example:
mask = x>0
result = h(x)
result[mask] = g(x[mask])
Some ufunc take where and out parameters that work like this. If g is a ufunc:
g(x, where=x>0, out = h(x))
result = the_array
for i in range(len(the_array)):
result[i] = g(the_array[i]) if the_array[i] > 0 else h(the_array[i])
you can use this array based operation assuming your g and h function can accept all values as input (meaning g does not through error/exception for non-positive values and h for positive values). The equation is quite self explanatory of the if statement in question:
result = g(x)*(x>0) + h(x)*(x<=0)
And if your g function only accepts positives and h function only accepts non-positives, you can mask array x and do operations and merge them like this:
idx_p = np.argwhere(x>0)
idx_np = np.argwhere(x<=0)
result = np.zeros_like(x)
result[idx_p] = g(x[x>0].reshape(-1, 1))
result[idx_np] = h(x[x<=0].reshape(-1, 1))
example code:
x = np.array([-1,1,-2,2])
def g(x):
return x**2
def h(x):
return x
result = g(x)*(x>0) + h(x)*(x<=0)
output:
[-1 1 -2 4]
As long as your function is quick to evaluate, you can use numpy.where:
import numpy as np
def g(x):
return x * 2
def h(x):
return x * 10
x = np.arange(-5, 5)
result = np.where(x > 0, g(x), h(x))
After this, result is
array([-50, -40, -30, -20, -10, 0, 2, 4, 6, 8])

finding gradient to specific x-value

I have a set of data in a numpy array - x-values, lets say between 0-100, and y-values. I need to get the gradient to a specific x-value ex. x=20 but I can only get the np.gradient function to give me the gradient at a certain index-value. right now I have:
g=np.gradient(y)
print(g[20])
but this of course gives me the gradient at i=20 and not x=20
I have both the x and y values in one 2D array and 2 x 1D arrays defined in my script
EDIT:
I actually came to solve it like this:
def grad(x, value):
def find_nearest(x, value):
x = np.asarray(Timeppmh)
idx = (np.abs(x - value)).argmin()
i = x.tolist().index(x[idx])
return i
g=np.gradient(yp,x)
find_nearest(x,value)
return g[find_nearest(x,value)]
If the value 20 is in x you could just do j[x == 20]. However, if that is not the case, you would need to approximate the gradient value. You can use for example linear interpolation.
import numpy as np
x = np.linspace(0, 100, 80)
print(20 in x) # 20 is not in x
# False
y = x * x + 3 * x + 2
# Pass x as second argument for value spacing
g = np.gradient(y, x)
print(np.interp(20, x, g)) # Should be 43
# 43.00000000000001

Plot a piecewise function in 3D

I am new to python and trying to 3d plot a piecewise function. I am trying to 3d plot the 'mainformula' function below on the z-axis as it varies with x and y ranging from 0 to 10, and constant = 1. But I can't quite seem to figure out the plotting method here.
from sympy import *
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
def mainformula(x,y,constant):
return Piecewise((subformula1(x,y,constant), y >= 0 and y < 3),(subformula2(x,y,constant),y>=3 and y <= 10))
def subformula1(x,y,constant):
return x + y + constant
def subformula2(x,y,constant):
return x - y - constant
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(0, 10, 0.25)
Y = np.arange(0, 10, 0.25)
constant = 1
X, Y = np.meshgrid(X, Y)
Z = mainformula(X,Y,constant)
surf = ax.plot_surface(X, Y, Z)
plt.show()
The error I get when I run that code is: "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"
You are working on arrays so it's never going to work to use array > 3 in a boolean context (for example with and) this will always give you the error you received. But you can always define your conditions as boolean masks and operate your formula on the appropriate elements:
def mainformula(x,y,constant):
z = np.zeros_like(x)
# Condition 1 indexes all elements where subformula 1 is valid
condition1 = np.logical_and(y >= 0, y < 3)
# condition1 = (y >= 0) & (y < 3) # is another way of writing it
z[condition1] = x[condition1] + y[condition1] + constant
# now do it in the range where subformula 2 is valid
condition2 = np.logical_and(y >= 3, y <= 10)
# condition1 = (y >= 3) & (y <= 10) # is another way of writing it
z[condition2] = x[condition2] - y[condition2] - constant
return z
This doesn't use sympy.Piecewise but works alright when only trying to plot. If you want seperate functions instead of doing it all in the main formula you need to change it a bit:
z[condition1] = subformula1(x[condition1], y[condition1], constant)
and similar for condition2.
The problem is you are trying to make logic assertions into arrays that do not have a direct one. It's difficult for Python to assert what this is:
y >= 0 and y < 3
So you'll have to change somewhat your code into something it can understand:
def mainformula(x,y,constant):
y2 = y[y>=0]
y2 = y2[y2<3]
y3 = y[y>=3]
y3 = y2[y2<=10]
return Piecewise((subformula1(x,y,constant), y2),(subformula2(x,y,constant),y3))
The problem is Piecewise function also does not seem to accept some arrays into one of the arguments you have. You'll have to rethink your problem, perhaps by building a loop for the Piecewise function in order to be launched at every element.

Correctly annotate a numba function using jit

I started with this code to calculate a simple matrix multiplication. It runs with %timeit in around 7.85s on my machine.
To try to speed this up I tried cython which reduced the time to 0.4s. I want to also try to use numba jit compiler to see if I can get similar speed ups (with less effort). But adding the #jit annotation appears to give exactly the same timings (~7.8s). I know it can't figure out the types of the calculate_z_numpy() call but I'm not sure what I can do to coerce it. Any ideas?
from numba import jit
import numpy as np
#jit('f8(c8[:],c8[:],uint)')
def calculate_z_numpy(q, z, maxiter):
"""use vector operations to update all zs and qs to create new output array"""
output = np.resize(np.array(0, dtype=np.int32), q.shape)
for iteration in range(maxiter):
z = z*z + q
done = np.greater(abs(z), 2.0)
q = np.where(done, 0+0j, q)
z = np.where(done, 0+0j, z)
output = np.where(done, iteration, output)
return output
def calc_test():
w = h = 1000
maxiter = 1000
# make a list of x and y values which will represent q
# xx and yy are the co-ordinates, for the default configuration they'll look like:
# if we have a 1000x1000 plot
# xx = [-2.13, -2.1242,-2.1184000000000003, ..., 0.7526000000000064, 0.7584000000000064, 0.7642000000000064]
# yy = [1.3, 1.2948, 1.2895999999999999, ..., -1.2844000000000058, -1.2896000000000059, -1.294800000000006]
x1, x2, y1, y2 = -2.13, 0.77, -1.3, 1.3
x_step = (float(x2 - x1) / float(w)) * 2
y_step = (float(y1 - y2) / float(h)) * 2
y = np.arange(y2,y1-y_step,y_step,dtype=np.complex)
x = np.arange(x1,x2,x_step)
q1 = np.empty(y.shape[0],dtype=np.complex)
q1.real = x
q1.imag = y
# Transpose y
x_y_square_matrix = x+y[:, np.newaxis] # it is np.complex128
# convert square matrix to a flatted vector using ravel
q2 = np.ravel(x_y_square_matrix)
# create z as a 0+0j array of the same length as q
# note that it defaults to reals (float64) unless told otherwise
z = np.zeros(q2.shape, np.complex128)
output = calculate_z_numpy(q2, z, maxiter)
print(output)
calc_test()
I figured out how to do this with some help from someone else.
#jit('i4[:](c16[:],c16[:],i4,i4[:])',nopython=True)
def calculate_z_numpy(q, z, maxiter,output):
"""use vector operations to update all zs and qs to create new output array"""
for iteration in range(maxiter):
for i in range(len(z)):
z[i] = z[i] + q[i]
if z[i] > 2:
output[i] = iteration
z[i] = 0+0j
q[i] = 0+0j
return output
What I learnt is that use numpy datastructures as inputs (for typing), but within use c like paradigms for looping.
This runs in 402ms which is a touch faster than cython code 0.45s so for fairly minimal work in rewriting the loop explicitly we have a python version faster than C(just).

Categories