numerical approximation 2D integral - numeric

I want to approximate numerically integral in the 2 D rectangular domain.
D = [0, 10] X [0, 10]
enter image description here
I know we can approximate integral with a double sum for x and y. We need to divide domain my subdomains.
x bar and y bar are the values in the center of each subdomain.
My function is represented numerically.
I know my value of function f(x, y) for every pair (x, y), but I don't know how to find function values in pair (x bar and y bar)
x1, x2 = np.meshgrid(np.linspace(0, 10, 100),np.linspace(0, 10, res))
xx = np.vstack([x1.ravel(), x2.ravel()]).T
f = KL._eigenfunctions[0](xx) #this is just the class KL that can find the value of function
This is how my function looks like in 2 D domain: enter image description here

Related

Numpy: Is there any simple way to solve equation in form Ax = b such that some x's takes fixed values

So basically I want to solve Ax = b but I want the value of x1 to always be equation to say 4.
For example, if A is 3x3 and x is 3x1 then the answer of the above equation should be in form x = [4, x2, x3]
if always x1=4, then x1 is no longer a unknown --> insert x1=4 in each place of the system and simplify the equations (algebraically = manually) --> you will get a system where A is 2x2 and x is 2x1.

In pandas, how to apply a function to every column that returns two columns

I would like to apply a function to every column of my grouped multiindex pandas dataframe.
If I had a function my_function() that returns a scalar, I would use
data_grouped = data.groupby(['type'])
data_transf = data_grouped.apply(lambda x: my_function(x))
However, consider another function my_function_array() takes an array (all n rows within one group) as an input and returns an n x 2 array as the output.
How can I apply this to every column of my grouped dataframe data_grouped? That is, I want to take every column of my grouped data of m rows and replace it by the n x 2 output of my_function_array().
Here's some sample data. There are other groups (types) but I only show one
type frame x y
F1675 1 77.369027 108.013249
2 107.784096 22.177883
3 22.385162 65.024619
4 65.152003 77.74970
def my_function_array(data_vec, D=2, T=2):
N = len(data_vec) - (D-1)*T # length of embedded signal
embed_data = np.zeros([N,D])
for di in range(-D//2,D//2):
embed_data[:,di] = data_vec[ np.arange((D//2+di)*T, N+(D//2+di)*T) ]
return embed_data
Appyling the function to the second column y
my_function_array(np.array([108.013249, 22.177883, 65.024619, 77.74970]))
I have
array([[ 65.024619, 108.013249],
[ 77.7497 , 22.177883]])
So, the expected output is
type frame x_1 x_2 y_1 y_2
F1675 1 22.385162 77.369027 65.024619 108.013249
2 65.152003 107.784096 77.7497 22.177883
where x_1 and x_2 are the two columns resulting from x (the naming is not important, can be anything). Note that the groups have become shorter and wider.
I think you need return pd.DataFrame:
def my_function_array(data_vec, D=2, T=2):
# print (data_vec.name)
N = len(data_vec) - (D-1)*T # length of embedded signal
embed_data = np.zeros([N,D])
for di in range(-D//2,D//2):
embed_data[:,di] = data_vec[ np.arange((D//2+di)*T, N+(D//2+di)*T) ]
return pd.DataFrame(embed_data).add_prefix(data_vec.name)
f = lambda x: pd.concat([my_function_array(x[y]) for y in x], axis=1)
data_transf = data.groupby(['type']).apply(f)
print (data_transf)
x0 x1 y0 y1
type
F1675 0 22.385162 77.369027 65.024619 108.013249
1 65.152003 107.784096 77.749700 22.177883

How to efficiently compute an L2 distance between rows of two array using only basic numpy operations? [duplicate]

I have 2 lists of points as numpy.ndarray, each row is the coordinate of a point, like:
a = np.array([[1,0,0],[0,1,0],[0,0,1]])
b = np.array([[1,1,0],[0,1,1],[1,0,1]])
Here I want to calculate the euclidean distance between all pairs of points in the 2 lists, for each point p_a in a, I want to calculate the distance between it and every point p_b in b. So the result is
d = np.array([[1,sqrt(3),1],[1,1,sqrt(3)],[sqrt(3),1,1]])
How to use matrix multiplication in numpy to compute the distance matrix?
Using direct numpy broadcasting, you can do this:
dist = np.sqrt(((a[:, None] - b[:, :, None]) ** 2).sum(0))
Alternatively, scipy has a routine that will compute this slightly more efficiently (particularly for large matrices)
from scipy.spatial.distance import cdist
dist = cdist(a, b)
I would avoid solutions that depend on factoring-out matrix products (of the form A^2 + B^2 - 2AB), because they can be numerically unstable due to floating point roundoff errors.
To compute the squared euclidean distance for each pair of elements off them - x and y, we need to find :
(Xik-Yjk)**2 = Xik**2 + Yjk**2 - 2*Xik*Yjk
and then sum along k to get the distance at coressponding point as dist(Xi,Yj).
Using associativity, it reduces to :
dist(Xi,Yj) = sum_k(Xik**2) + sum_k(Yjk**2) - 2*sum_k(Xik*Yjk)
Bringing in matrix-multiplication for the last part, we would have all the distances, like so -
dist = sum_rows(X^2), sum_rows(Y^2), -2*matrix_multiplication(X, Y.T)
Hence, putting into NumPy terms, we would end up with the euclidean distances for our case with a and b as the inputs, like so -
np.sqrt((a**2).sum(1)[:,None] + (b**2).sum(1) - 2*a.dot(b.T))
Leveraging np.einsum, we could replace the first two summation-reductions with -
np.einsum('ij,ij->i',a,a)[:,None] + np.einsum('ij,ij->i',b,b)
More info could be found on eucl_dist package's wiki page (disclaimer: I am its author).
If you have 2 each 1-dimensional arrays, x and y, you can convert the arrays into matrices with repeating columns, transpose, and apply the distance formula. This assumes that x and y are coordinated pairs. The result is a symmetrical distance matrix.
x = [1, 2, 3]
y = [4, 5, 6]
xx = np.repeat(x,3,axis = 0).reshape(3,3)
yy = np.repeat(y,3,axis = 0).reshape(3,3)
dist = np.sqrt((xx-xx.T)**2 + (yy-yy.T)**2)
dist
Out[135]:
array([[0. , 1.41421356, 2.82842712],
[1.41421356, 0. , 1.41421356],
[2.82842712, 1.41421356, 0. ]])
L2 distance = (a^2 + b^2 - 2ab)^0.5
a = np.random.randn(5, 3)
b = np.random.randn(2, 3)
a2 = np.sum(np.square(a), axis = 1)[..., None]
b2 = np.sum(np.square(b), axis = 1)[None, ...]
ab = -2*np.dot(a, b.T)
dist = np.sqrt(a2 + b2 + ab)

3D Density visualisation with matplotlib

I am trying to plot a 3D column with associated density.
Specifically, I have a .txt file with 4 separate columns x, y, z, density. The first 3 columns are the cartesian coordinates of the column, density a list of density values associated with each cross-section, at height z, of the column.
I can plot the column with a colormap as follows
x=np.linspace(-1, 1, 100)
z=np.linspace(-20, 5, 50)
Xc, Zc=np.meshgrid(x, z)
Yc = np.sqrt(1-Xc**2)
# Draw parameters
rstride = 1
cstride = 1
surf1 = ax.plot_surface(Xc, Yc, Zc, alpha=1., rstride=rstride, cstride=cstride,antialiased=False, cmap=cm.coolwarm,linewidth=0)
surf2 = ax.plot_surface(Xc, -Yc, Zc, alpha=1., rstride=rstride, cstride=cstride, antialiased=False, cmap=cm.coolwarm,linewidth=0)
and I can associate a colormap to z
fig.colorbar(surf1, shrink=0.5, aspect=5)
I would like to associate the colormap to the values in the fourth column, while maintaining the plotted dimensions of the cylinder constant.
I would appreciate any help on the matter.
Thanks.

numpy multiply vectors to form square matrix

suppose I have two numpy arrays x and y of shape N which I want to represent as size N x 1 each, and I want to multiply them as x y' to a get a matrix of size N x N. But if I try:
np.dot(x, y.T) or np.dot(x.T, y)
I always get a scalar (size 1 x 1).
Is it possible to specify to numpy to multiply two arrays along a particular dimension?
To clarify, suppose I have
x = [x1, x2]
y = [y1, y2]
I want
xy' = [[x1*y1, x1*y2], [x2*y1, x2*y2]]
but numpy always seems to return
xy' = x1*y1+x2*y2
You want np.outer(x, y). You can also do it with broadcasting:
x[:, None] * y
which is more flexible