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?
Related
I have two arrays R3_mod with shape (21,21) containing many zeros and P2 with shape (21,) containing many zeros. I am getting the inverse of R3_mod using np.linalg.pinv() and eventually multiplying it to P2 as shown below. Is there a more efficient way to invert such arrays and then multiply?
Since the arrays are too big, you can access it here: https://drive.google.com/drive/u/0/folders/1NjEiNoneMaCbmbmObEs2GCNIb08NFIy3
import numpy as np
X = np.linalg.pinv(R3_mod).dot(P2)
Assuming that the matrix R3_mod is indeed invertible, I think it's best to use np.linalg.inv instead of linalg.pinv.
inv computes the inverse of the matrix directly, where pinv (stands for pseudo-inverse, see https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse) computes the matrix A' that minimizes |AA'-I|. If the input matrix is invertible, pinv should return the same result as inv.
If I want to calculate the k smallest eigenvalues of the matrix multiplication AA' with A of size 300K by 512 and "'" is the transpose, then that would be infeasible to do it in traditional way. Matlab however provides a nice functionality by using a function argument that perform the product Afun = #(x) A*(A'*x)); to the eigs function. Then, to find the smallest 6 eigenvalues/eigenvectors we call d = eigs(Afun,300000,6,'smallestabs'), where the second input is the size of the matrix AA'. Is there a function in python that performs a similar thing?
To my knowledge, there is no such functionality in numpy. However, I don't see any limitations by using simply numpy.linalg.eigvals for retrieving an array of the matrix eigenvalues. Then simply find the N smallest with a sort:
import numpy as np
import numpy.linalg
A = np.array() # your matrix
eigvals = numpy.linalg.eigvals(A)
eigvals.sort()
smallest_6_eigvals = eigvals[:6]
I'm trying to generate a kernel function for GP using only Matrix operations (no loops).
Vectors where no problem taking advantage of broadcasting
def kernel(A,B):
return 1/np.exp(np.linalg.norm(A-B.T))**2
A and B are both [n,1] vectors, but with [n,m] shaped matrices It just doesn't work. (Also tried reshaping to [1,n,m])
I'm interested on computing an X Matrix where every ij-th element is defined by Ai-Bj.
Now I'm working on Numpy but my final objective is implement this on Tensorflow.
Thanks in Advance.
I'm trying to implement the idea I have suggested here, for Cauchy product of multivariate finite power series (i.e. polynomials) represented as NumPy ndarrays. numpy.convolve does the job for 1D arrays, respectively. But to my best knowledge there is no implementations of convolution for arbitrary dimensional arrays. In the above link, I have suggested the equation:
for convolution of two n dimensional arrays Phi of shape P=[p1,...,pn] and Psi of the shape Q=[q1,...,qn], where:
omegas are the elements of n dimensional array Omega of the shape O=P+Q-1
<A,B>_F is the generalization of Frobenius inner product for arbitrary dimensional arrays A and B of the same shape
A^F is A flipped in all n directions
{A}_[k1,...,kn] is a slice of A starting from [0,...,0] to [k1,...,kn]
Psi' is Psi extended with zeros to have the shape O as defined above
I tried implementing the above functions one by one:
import numpy as np
def crop(A,D1,D2):
return A[tuple(slice(D1[i], D2[i]) for i in range(D1.shape[0]))]
as was suggested here, slices/crops A from D1 to D2,
def sumall(A):
sum1=A
for k in range(A.ndim):
sum1 = np.sum(sum1,axis=0)
return sum1
is a generalization of numpy.sum for multidimensional ndarrays,
def flipall(A):
A1=A
for k in range(A.ndim):
A1=np.flip(A1,k)
return A1
flips A is all existing axises, and finally
def conv(A,B,K):
D0=np.zeros(K.shape,dtype=K.dtype)
return sumall(np.multiply(crop(A,np.maximum(D0,np.minimum(A.shape,K-B.shape)) \
,np.minimum(A.shape,K)), \
flipall(crop(B,np.maximum(D0,np.minimum(B.shape,K-A.shape)) \
,np.minimum(B.shape,K)))))
where K=[k1,...,kn] and for all 0<=kj<=oj, is a modified version of formula above which only calculate the non-zero multiplications to be more efficient. Now I'm trying to populate the Omega array using fromfunction or meshgrid in combination to vectorize as suggested here, but I have failed so far. Now my questions in prioritized order are:
how can I implement the final step and populate the final array in an efficient and pythonic way?
are there more efficient implementations of the functions above? Or how would you implement the formula?
is my equation correct? does this represent multiplication of multivariate finite power series?
haven't really others implemented this before in NumPy or am I reinventing the wheel here? I would appreciate if you could point me towards other solutions.
I would appreciate if you could help me with these questions. Thanks for your help in advance.
P.S.1 You may find some examples and other information in this GitHub Gist
P.S.2 Here in the AstroPy mailing list I was told that scipy.signal.convolve and/or scipy.ndimage.convolve do the job for higher dimensions as well. There is also a scipy.ndimage.filters.convolve. Here I have explained why they are not what I'm looking for.
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.