I have a function which I want to integrate. I have two numpy arrays, one with the x-values and one with the function f(x). I am looking for a function F(x) which is the antiderivative of f(x), defined on the same grid x. This grid x is non-uniform.
Is there some numpy or scipy function giving me the array F(x)?
You are likely looking for scipy.integrate.cumtrapz.
Related
I have a function foo(x,y) that takes two scalars (or lists of scalars) and returns a scalar output (or list of scalars computed pairwise from the input). I want to be able to evaluate this function over 2 orthogonal arrays such that the output is a matrix ij of foo(x[i], y[j]).
I have a for-loop version that solves this problem as below:
import numpy as np
x = np.range(50) # Could be linspaces, whatever the axis in the vector space is
y = np.range(50)
mat = np.zeros(len(x), len(y)) # To hold the result for plotting
for i in range(len(x)):
for j in range(len(y)):
mat[i][j] = foo(x[i], y[j])
where my result is stored in mat. However, this is dreadfully slow, and looks to me as if it could easily be vectorized. I'm not aware of how Python solves this problem however, as this doesn't appear to be something like zip or map. Is there another such function or concept (beyond trivially making extremely long arrays of the same array rotated by a value and passing them that way) that could vectorize this successfully? Or is the nature of the foo function limiting the ability to vectorize this?
In this case, itertools.product is the tool you want. It generates an iterable sequence of elements from the Cartesian product of N inputs, which you can use to discretely map a vector space. You can then evaluate foo on these. This isn't vectorization per se, but does reduce the nested for loop.
See docs at https://docs.python.org/3/library/itertools.html#itertools.product
I want to use the minimization function from scipy scipy.optimize.minimize.
I have a function def f(x,a,b,c) whose arguments are three scalars. I have 3 numpy matrices A B C and i want to calculate a matrix whose (i,j) component is the minimum of f(x,A[i,j],B[i,j],C[i,j]) over all posible x. Using just scipy.optimize.minimize(f,1,args=(A,B,C)) do not work. Any idea of how can I do it efficiently?
I have a simple problem.
I have two matrices A and B,
and I want to find a transformation AX that makes AX closest to B in the least squares sense.
i.e. find X such that X = argmin ||AX -B|| under 2-norm.
How can I solve this problem using numpy or scipy?
I tried to search but the only method that is there is lstsq (https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.lstsq.html) but it finds a linear equation solution involving vector x.
How to solve my problem, i.e. find a transformation X for matrix A?
Are there any non-linear methods that do this? How is this problem solved in optimization?
I have a 2D pressure field and I would like to interpolate the value at
particular set of singular points or locations. I'm suspect downscaling is a solution but before I try and write the functions for python, I was wondering if there is a way/code already in existence. Perhaps scipy has a function but I'm unaware and cannot find any.
Any help is appreciated
Scipy has a 2-dimensional interpolation function:
scipy.interpolate.interp2d
Information on how to use this function can be found on http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.interpolate.interp2d.html
scipy.interpolate.interp2d(x, y, z)
in which x and y are 1-dimensional arrays of coordinates corresponding to the two dimensional array z which is your pressurefield.
x corresponds to the second axis of the z-matrix.
y corresponds to the first axis of the z-matrix
I've noticed that when applying certain operations on meshgrids like the one below I get an error because the operations may not be compatible with numpy. Sometimes there might be a numpy function alternative for sin, cos but not for all functions like functions in scipy.
Say, I have a function called MATHOPERATION(x,y) which takes two numbers, x and y, and outputs another number. Where x and y are numbers in X and Y that occupy the same position in the meshgrid. So the the output for MATHOPERATION(X,Y) would be a meshgrid of the same size as X and Y
So my question is how do I get around this problem when the function MATHOPERATION isn't compatible with numpy?
If I understand your question correctly, you may want to use
import numpy as np
map(MATHOPERATION , np.ravel(X) , np.ravel(Y))
which should make your meshgrid a sequence-like object.