Dot product between transpose of sparse matrix and vector in python scipy - scipy

I know the function spmat.dot(vec) that returns the standard matrix vector 'dot' product between a sparse matrix spmat and a numpy array vec in scipy.
I need to compute transpose(spmat).dot(vec). Is there a built in function that does exactly this ?
During my research, I stumbled upon scipy.sparse.linalg.LinearOperator.rmatvec, but it does not seem to be available in my scipy version.
Thanks,

Related

Getting a sparse vector from sparse matrix by sparse vector dot product in Swift

Swift's library "Accelerate" has sparse matrix types and several classes of functions for sparse matrix multiplication with different argument types, and BLAS-like functions with sparse matrices and vectors.
Interestingly, there are no functions that produce a sparse vector from a sparse matrix dot product with a sparse vector. (Or at least I did not see any in
Accelerate's documentation
.)
It looks like the workflow using
SparseVector_double for d = S . v
could be:
Convert the sparse vector v into a dense vector (or matrix)
Use the function SparseMultiply
Make the dense result d sparse
Alternative workflows with the BLAS functions are possible, say, using the function
sparse_matrix_product_sparse_double, but, again, the result is dense and has to be converted into a sparse vector/matrix.
I have several questions:
Is my conjecture that there is no direct way of getting a sparse vector from a dot product correct?
What is the fastest and easiest way to convert the dense vector/matrix results from the dot product functions into a sparse vector/matrix?
I should just scan the 0's with a loop, or there are relevant library functions?
What are the reasons none of these functions produce sparse structures?

Matrices and diagonalization in Swift 3.x

I am looking for 2 things: a way to define a matrix in Swift and a way to diagonlize said matrix.
So far, I've found a way to make something that resembles a matrix using this:
var NumColumns = 2
var NumRows = 4
var array = Array<Array<Double>>()
for column in 0...NumColumns {
array.append(Array(repeating:Double(), count:NumRows))
}
print(array)
But someone told me that this will not do because after I have the matrix I will need to use a diagonalization algorithm specifically on a matrix and not something similar to a matrix.
Any ideas?
The common way of defining matrices in different languages (including Swift) is row-major order. So, essentially your matrix is stored as contiguous array of rows. This will allow you to do most of linear algebra operations efficiently using Apple Accelerate framework. For your particular case, Apple has something already, however it won't be nicely documented neither will the API be "good-looking". Apple provides Swift bindings to LAPACK (Fortran based linear algebra library). To diagonalise your matrix you will need to find eigenvectors for it using dsyevd_ routine (http://www.netlib.org/lapack/explore-html/d2/d8a/group__double_s_yeigen_ga694ddc6e5527b6223748e3462013d867.html#ga694ddc6e5527b6223748e3462013d867). As an output of the function you will receive a matrix (represented in column-major order) of eigenvectors and a vector of eigenvalues (an array). If you transpose the matrix using another API function vDSP_mtransD (https://developer.apple.com/reference/accelerate/1450422-vdsp_mtransd) and create a diagonal matrix from eigenvalues, you will get matrices V and D for which the equation A = VDV' is satisfied. This is, as far as I understand, exactly what you're looking for.

Calculating large number of generalized eigenvalues with Matlab on GPU

I have a lot of small matrices already stored on GPU card and now I want to calculate their generalized eigenvalues with another matrix.
Current code:
cov=gpuArray(cov) %7x7 matrix;
p=1:numel(ix1); %numel(ix1)... number of stored matrices
p=gpuArray(p);
%covs... 7x7xn matrix already on GPU, n is large (>100000)
g = arrayfun(#(x) eig(covs(:,:,x)/cov), p);
If I tried to run code I get error that function eig is unsupported, but I read that eig is supported on GPU.
So my question is what I did wrong (my first attempt with arrayfun) and if there is better way to calculate generalized eigenvalues.

Finding eigenvalues in MATLAB without using eig function

I'm trying to find eigenvalues of a matrix without using eig function (my homework says so). In Matlab, I define the matrix and identity matrix. But I cannot set up this equation:
A - x*I
x here is lambda, A is the matrix that I should find eigenvalues of and I is the identity matrix. If you know how to find eigenvalues, you supposed to understand this. How can I go through?
you can get some inspiration here: http://en.wikipedia.org/wiki/Eigenvalue_algorithm
if the matrix is fixed size, you can easily do the det(A-lambda*eye)=0 solving by yourself and use that.
With power iteration you can already find the dominant eigenvalue, and I knew there was an extension to this algorithm to also find the other eigenvalues, but cannot recall how that works :(

How can I index the diagonals of a 3-D matrix in MATLAB?

I have an M-by-M-by-N matrix, which is a concatenation of N M-by-M matrices. I want to reduce this matrix to an M-by-N matrix by taking the diagonals of each M-by-M submatrix and concatenating them together. How can I do this in a simple vectorized way?
You can do it by getting the linear indices of the diagonals and using it to form a new matrix
[M,~,N]=size(A);%# A is your matrix
indx=cumsum([1:(M+1):M^2; M^2.*ones(N-1,M)]);%#diagonal indices
B=A(indx');%'# transpose to get MxN
In the above, I've used ~ to disregard that output from the function. However, this works only if you're using MATLAB R2009b and above. If your version is older than that, use a dummy variable instead.