How to find index value from meshgrid in numpy - python

I have the following problem: I have two surface equations, and I am looking at what point they are zero. So I have the following:
b = np.arange(0,2,0.1)
k = np.arange(0,50,1)
b,k = np.meshgrid(b,k)
with these I produce z1 and z2, massive formulas, but they both use b and k:
z1 = ((0.5*rho*k**2 * Vd**2 * c)*(Cl * 0.1516*b**3 +
Cd*(((b*np.sqrt(b**2 * k**2 +1))/(2*k**2)) -
((np.log(np.sqrt(b**2 * k**2 + 1) + b*k))/(2*k**3)))) - F)
z2 = ((Cl * 0.1516 * b**3 * k**(-1)) -
((Cd/(8*k**4))*((3*np.log(np.sqrt(b**2 * k**2 + 1) + b*k)) +
(b*np.sqrt(b**2 * k**2 +1)*(2*b**2 * k**2 -3)*k))))
Now I know how to find the closest point at which z1 and z2 are zero. Just like below:
print min(z1[(-0.1<z1)&(z1<0.1)]), min(z2[(-0.1<z2)&(z2<0.1)])
but with these I only get the z-value which gives me a close value to zero. What I need is to find which b and k values correspond to that given result of either z1 or z2.
I tried to index it, but I seem not to do it correctly.

In this case, your "is close to zero" expression (-0.1<z1)&(z1<0.1) is an array of booleans. To find the indices of the True items you simply need to use nonzero().
(-0.1<z1) & (z1<0.1).nonzero()
For example:
>>> np.array([False, False, True, False, True, True, False]).nonzero()
(array([2, 4, 5]),)

Why not using argmin on the absolute value of z1 and z2?
Example for how to use argmin:
>>> t = np.random.rand(4,3)
>>> t
array([[ 0.55809884, 0.34630428, 0.78713403],
[ 0.15125035, 0.07231524, 0.06975387],
[ 0.63233302, 0.48026985, 0.21420412],
[ 0.32713745, 0.95168682, 0.34994332]])
>>> np.min(t)
0.069753866909461171
>>> np.argmin(t)
5
>>> t[5//3, 5%3] # 5 is the argmin, 3 is my number of columns
0.069753866909461171

Related

{sin,cos}(arctan(x)) vs. x / (x^2 + y^2)

I have to compute {sin, cos}(arctan2(x)) on a regular basis. These expressions have much cheaper equivalents, namely
import numpy as np
np.random.seed(0)
a, b = np.random.rand(2)
print(np.cos(np.arctan2(b, a)))
print(a / np.sqrt(a ** 2 + b ** 2))
# or a / np.hypot(a, b)
print()
print(np.sin(np.arctan2(b, a)))
print(b / np.hypot(a, b))
0.6087819565465009
0.6087819565465009
0.7933375885355579
0.793337588535558
Unfortunately, for the important border case a == b == 0.0, the alternative yields and error and gives nan. I'd like to avoid both.
Is there a specialized function that computes {sin, cos}(arctan2(x)) or perhaps another more appropriate expression? Like the above, it needs to work for scalar and vector inputs.
One possible trick to get around this issue is to just add a small epsilon to the values when you are dividing by zero.
import numpy as np
np.random.seed(0)
a = np.random.rand(10)
b = np.random.rand(10)
a[0] = 0
b[0] = 0
eps = 1e-9
p1 = np.cos(np.arctan2(b, a))
p2 = (a+eps) / ((np.sqrt(a ** 2 + b ** 2))+eps)
print(np.allclose(p1, p2))
p1 = np.sin(np.arctan2(b, a))
p2 = b / (np.hypot(a, b)+eps)
print(np.allclose(p1, p2))

Using a list of floats for a loop

I'm trying to run a Runge-Kutta algorithm to approximate a differential equation. I want to go through a list of values for a constant variable, A, in the function and have the algorithm loop go through for each item in the list and produce a graph. I keep getting an error saying "list indices must be integers or slices but not a float". I tried to convert the numbers in the list to being integer fractions of each other but that didn't work either. I'm mostly unsure on how to circumvent this error as some fixes I found didn't work, here is my code:
import numpy as np
import matplotlib.pyplot as plt
from math import pi
from numpy import arange
from matplotlib.pyplot import plot,show
wo = 1
w = 2 #defining wo, w, g1, Amplitude and steps
h = 0.001
g1 = 0.2
A = [0.1,0.25,0.5,0.7,0.75,0.85,0.95,1.00,1.02,1.031,1.033,1.035,1.05]
for item in list(A): #Converting list items into Float values
[float(i) for i in A]
xpoints = arange(0,100,h)
tpoints = []
zpoints = []
t=0
x = 0
z = pi/2
for i in A: #Calls for items in Amplitude list to run algorighm
def F(t, z, x): #Defining the differential equation
return -g1 * z - (wo ** 2 + 2 * A[i] * np.cos(w * t)) * np.sin(x)
for x in xpoints:
tpoints.append(t)
zpoints.append(z)
m1 = z*h
k1 = h*F(t,z,x) #setting up the runge-kutta algorithm
m2 = h*(z+(k1/2))
k2 = h*F(t+0.5*m1,z+0.5*m1,x+0.5*h)
m3 = h*(z+0.5*k2)
k3 = h*F(t+0.5*m2,z+0.5*m2,x+0.5*h)
m4 = h*(z+0.5*k3)
k4 = h*F(t+0.5*m3,z+0.5*m3,x+0.5*h)
t += (m1+2*m2+2*m3+m4)/6
z += (k1+2*k2+2*k3+k4)/6
A += 1
plot(xpoints,zpoints)
The problem isn't that the numbers themselves need to be converted. Note how you iterate with for i in A:. This means that i is the actual value and not the index. So where you use A[i], you're trying to go to the 0.1 index of A. Instead, just replace A[i] with i in the line at the bottom of this snippet.
A = [0.1,0.25,0.5,0.7,0.75,0.85,0.95,1.00,1.02,1.031,1.033,1.035,1.05]
...
for i in A:
def F(t, z, x):
return -g1 * z - (wo ** 2 + 2 * A[i] * np.cos(w * t)) * np.sin(x)
Because the value of i is an element of A. If you want to loop index by index in A list:
for i in range(len(A))
this works.
This time, you get an error in A + = 1. I think this place will be i + = 1.

Python SymPy 1.3 simplify symbolic matrix calculation

I have the following code for a symbolic matrix calculation
from sympy import *
A = MatrixSymbol( 'A', 3, 3 )
B = MatrixSymbol( 'B', 3, 3 )
C = MatrixSymbol( 'C', 3, 3 )
Z = A * ( A + B ) * C.inverse() * ( A + B ).transpose()
Z.expand()
This gives me the following result
A( A + B ) C^-1 ( A^T + B^T )
But I want the expanded result
A * A * C^-1 * A^T + A * B C^-1 * A^T + A * A C^-1 * B^T + A * B C^-1 * B^T
Could I ask you guys that how to do it ? Thank you very much
Currently, there is no way to expand matrix operations in sympy. The expression from the result you have above is the only way to view the result. Expansion functions available in sympy include expand(), expand_trig(), expand_power_exp(), expand_power_base(), expand_log(), expand_func() and hyperexpand(), none of which seem to work with your matrix expression.
The simplify function doesn't seem to work either if we wanted to evaluate your desired output to compare to the sympy output of Z
simplify(A*A*C.inverse()*A.transpose() + A*A*C.inverse()*B.transpose() + A*B*C.inverse()*A.transpose() + A*B*C.inverse()*B.transpose())
>>> A*A*C^-1*A.T + A*A*C^-1*B.T + A*B*C^-1*A.T + A*B*C^-1*B.T
which clearly isn't in the same form as the sympy expression for Z.
Even if you were to just test your desired output against the output of sympy, you still get a False result
Z == A * A * C.inverse() * A.transpose() + A * B*C.inverse() * A.transpose() + A * A*C.inverse() * B.transpose() + A * B*C.inverse() * B.transpose()
>>> False
We cannot define matrices A, B and C with known values for each index and then evaluate the expressions you have above to compare their outputs because MutableDenseMatrix objects do not have an inverse attribute.
If you want to actually compare the two expressions to see if they are equal, you could use numpy and compare both expressions with known matrix values. This is quite convoluted and wont work for very complex expressions, but it does work for simple expressions.
e.g.
import numpy as np
# define 3 numpy arrays
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
B = np.array([[0,0.1,-4],[19,0.67,6],[-1,99,5]])
C = np.array([[1,2,-1],[4,5,6],[-2,2,0]])
# sympy expression, compute iteratively
Z1 = np.dot(A, A + B)
Z1 = np.dot(Z1, np.linalg.inv(C))
Z1 = np.dot(Z1, A.T + B.T)
print(Z1)
>>>[[ 143.22088889 -672.5076 -100.24 ]
[-239.47685185 4558.45416667 3597.5 ]
[ 65.1457037 -94.81393333 519.56 ]]
# desired expression
p1 = np.dot(A, A)
p1 = np.dot(p1, np.linalg.inv(C))
p1 = np.dot(p1, A.T)
p2 = np.dot(A, B)
p2 = np.dot(p2, np.linalg.inv(C))
p2 = np.dot(p2, A.T)
p3 = np.dot(A, A)
p3 = np.dot(p3, np.linalg.inv(C))
p3 = np.dot(p3, B.T)
p4 = np.dot(A, B)
p4 = np.dot(p4, np.linalg.inv(C))
p4 = np.dot(p4, B.T)
Z2 = p1 + p2 + p3 + p4
print(Z2)
>>>[[ 143.22088889 -672.5076 -100.24 ]
[-239.47685185 4558.45416667 3597.5 ]
[ 65.1457037 -94.81393333 519.56 ]]
np.isclose(Z1,Z2)
>>>[[ True, True, True],
[ True, True, True],
[ True, True, True]
So we can see that the sympy expression Z1 and your desired expression Z2 are in fact the same, given the matrices above.

Is there a faster way of repeating a chunk of code x times and taking an average?

Starting with:
a,b=np.ogrid[0:n+1:1,0:n+1:1]
B=np.exp(1j*(np.pi/3)*np.abs(a-b))
B[z,b] = np.exp(1j * (np.pi/3) * np.abs(z - b +x))
B[a,z] = np.exp(1j * (np.pi/3) * np.abs(a - z +x))
B[diag,diag]=1-1j/np.sqrt(3)
this produces an n*n grid that acts as a matrix.
n is just a number chosen to represent the indices, i.e. an a*b matrix where a and b both go up to n.
Where z is a constant I choose to replace a row and column with the B[z,b] and B[a,z] formulas. (Essentially the same formula but with a small number added to the np.abs(a-b))
The diagonal of the matrix is given by the bottom line:
B[diag,diag]=1-1j/np.sqrt(3)
where,
diag=np.arange(n+1)
I would like to repeat this code 50 times where the only thing that changes is x so I will end up with 50 versions of the B np.ogrid. x is a randomly generated number between -0.8 and 0.8 each time.
x=np.random.uniform(-0.8,0.8)
I want to generate 50 versions of B with random values of x each time and take a geometric average of the 50 versions of B using the definition:
def geo_mean(y):
y = np.asarray(y)
return np.prod(y ** (1.0 / y.shape[0]), axis=-1)
I have tried to set B as a function of some index and then use a for _ in range(): loop, this doesn't work. Aside from copy and pasting the block 50 times and denoting each one as B1, B2, B3 etc; I can't think of another way of working this out.
EDIT:
I'm now using part of a given solution in order to show clearly what I am looking for:
#A matrix with 50 random values between -0.8 and 0.8 to be used in the loop
X=np.random.uniform(-0.8,0.8, (50,1))
#constructing the base array before modification by random x values in position z
a,b = np.ogrid[0:n+1:1,0:n+1:1]
B = np.exp(1j * ( np.pi / 3) * np.abs( a - b ))
B[diag,diag] = 1 - 1j / np.sqrt(3)
#list to store all modified arrays
randomarrays = []
for i in range( 0,50 ):
#copy array and modify it
Bnew = np.copy( B )
Bnew[z, b] = np.exp( 1j * ( np.pi / 3 ) * np.abs(z - b + X[i]))
Bnew[a, z] = np.exp( 1j * ( np.pi / 3 ) * np.abs(a - z + X[i]))
randomarrays.append(Bnew)
Bstack = np.dstack(randomarrays)
#calculate the geometric mean value along the axis that was the row in 2D arrays
B0 = geo_mean(Bstack)
From this example, every iteration of i uses the same value of X, I can't seem to get a way to get each new loop of i to use the next value in the matrix X. I am unsure of the ++ action in python, I know it does not work in python, I just don't know how to use the python equivalent. I want a loop to use a value of X, then the next loop to use the next value and so on and so forth so I can dstack all the matrices at the end and find a geo_mean for each element in the stacked matrices.
One pedestrian way would be to use a list comprehension or generator expression:
>>> def f(n, z, x):
... diag = np.arange(n+1)
... a,b=np.ogrid[0:n+1:1,0:n+1:1]
... B=np.exp(1j*(np.pi/3)*np.abs(a-b))
... B[z,b] = np.exp(1j * (np.pi/3) * np.abs(z - b +x))
... B[a,z] = np.exp(1j * (np.pi/3) * np.abs(a - z +x))
... B[diag,diag]=1-1j/np.sqrt(3)
... return B
...
>>> X = np.random.uniform(-0.8, 0.8, (10,))
>>> np.prod((*map(np.power, map(f, 10*(4,), 10*(2,), X), 10 * (1/10,)),), axis=0)
But in your concrete example we can do much better than that;
using the identity exp(a) x exp(b) = exp(a + b) we can convert the geometric mean after exponentiation to an arithmetic mean before exponentition. A bit of care is required because of the multivaluedness of the complex n-th root which occurs in the geometric mean. In the code below we normalize the angles occurring to range -pi, pi so as to always hit the same branch as the n-th root.
Please also note that the geo_mean function you provide is definitely wrong. It fails the basic sanity check that taking the average of copies of the same thing should return the same thing. I've provided a better version. It is still not perfect, but I think there actually is no perfect solution, because of the nonuniqueness of the complex root.
Because of this I recommend taking the average before exponentiating. As long as your random spread is less than pi this allows a well-defined averaging procedure with an average that is actually close to the samples
import numpy as np
def f(n, z, X, do_it_pps_way=True):
X = np.asanyarray(X)
diag = np.arange(n+1)
a,b=np.ogrid[0:n+1:1,0:n+1:1]
B=np.exp(1j*(np.pi/3)*np.abs(a-b))
X = X.reshape(-1,1,1)
if do_it_pps_way:
zbx = np.mean(np.abs(z-b+X), axis=0)
azx = np.mean(np.abs(a-z+X), axis=0)
else:
zbx = np.mean((np.abs(z-b+X)+3) % 6 - 3, axis=0)
azx = np.mean((np.abs(a-z+X)+3) % 6 - 3, axis=0)
B[z,b] = np.exp(1j * (np.pi/3) * zbx)
B[a,z] = np.exp(1j * (np.pi/3) * azx)
B[diag,diag]=1-1j/np.sqrt(3)
return B
def geo_mean(y):
y = np.asarray(y)
dim = len(y.shape)
y = np.atleast_2d(y)
v = np.prod(y, axis=0) ** (1.0 / y.shape[0])
return v[0] if dim == 1 else v
def geo_mean_correct(y):
y = np.asarray(y)
return np.prod(y ** (1.0 / y.shape[0]), axis=0)
# demo that orig geo_mean is wrong
B = np.exp(1j * np.random.random((5, 5)))
# the mean of four times the same thing should be the same thing:
if not np.allclose(B, geo_mean([B, B, B, B])):
print('geo_mean failed')
if np.allclose(B, geo_mean_correct([B, B, B, B])):
print('but geo_mean_correct works')
n, z, m = 10, 3, 50
X = np.random.uniform(-0.8, 0.8, (m,))
B0 = f(n, z, X, do_it_pps_way=False)
B1 = np.prod((*map(np.power, map(f, m*(n,), m*(z,), X), m * (1/m,)),), axis=0)
B2 = geo_mean_correct([f(n, z, x) for x in X])
# This is the recommended way:
B_recommended = f(n, z, X, do_it_pps_way=True)
print()
print(np.allclose(B1, B0))
print(np.allclose(B2, B1))
I think you should rely more on numpy functionality, when approaching your problem. Not a numpy expert myself, so there is surely room for improvement:
from scipy.stats import gmean
n = 2
z = 1
a = np.arange(n + 1).reshape(1, n + 1)
#constructing the base array before modification by random x values in position z
B = np.exp(1j * (np.pi / 3) * np.abs(a - a.T))
B[a, a] = 1 - 1j / np.sqrt(3)
#list to store all modified arrays
random_arrays = []
for _ in range(50):
#generate random x value
x=np.random.uniform(-0.8, 0.8)
#copy array and modify it
B_new = np.copy(B)
B_new[z, a] = np.exp(1j * (np.pi / 3) * np.abs(z - a + x))
B_new[a, z] = np.exp(1j * (np.pi / 3) * np.abs(a - z + x))
random_arrays.append(B_new)
#store all B arrays as a 3D array
B_stack = np.stack(random_arrays)
#calculate the geometric mean value along the axis that was the row in 2D arrays
geom_mean_for_rows = gmean(B_stack, axis = 2)
It uses the geometric mean function from scipy.stats module to have a vectorised approach for this calculation.

splitting an array in Python

I have an array that is (219812,2) but I need to split to 2 (219812).
I keep getting the error ValueError: operands could not be broadcast together with shapes (219812,2) (219812)
How can I accomplish?
As you can see, I need to take the two separate solutions from u = odeint and multiple them.
def deriv(u, t):
return array([ u[1], u[0] - np.sqrt(u[0]) ])
time = np.arange(0.01, 7 * np.pi, 0.0001)
uinit = array([ 1.49907, 0])
u = odeint(deriv, uinit, time)
x = 1 / u * np.cos(time)
y = 1 / u * np.sin(time)
plot(x, y)
plt.show()
To extract the ith column of a 2D array, use arr[:, i].
You could also unpack the array (it works row wise, so you need to transpose u so that it has shape (2, n)), using u1, u2 = u.T.
By the way, star imports aren't great (except maybe in the terminal for interactive use), so I added a couple of np. and plt. to your code, which becomes:
def deriv(u, t):
return np.array([ u[1], u[0] - np.sqrt(u[0]) ])
time = np.arange(0.01, 7 * np.pi, 0.0001)
uinit = np.array([ 1.49907, 0])
u = odeint(deriv, uinit, time)
x = 1 / u[:, 0] * np.cos(time)
y = 1 / u[:, 1] * np.sin(time)
plt.plot(x, y)
plt.show()
It also seems like a logarithmic plot looks nicer.
It sounds like you want to index into the tuple:
foo = (123, 456)
bar = foo[0] # sets bar to 123
baz = foo[1] # sets baz to 456
So in your case, it sounds like what you want to do might be...
u = odeint(deriv, uinit, time)
x = 1 / u[0] * np.cos(time)
y = 1 / u[1] * np.sin(time)
u1,u2 = odeint(deriv, uinit, time)
maybe ?

Categories

Resources