Interaction of sparse and dense matrices in scipy
Hi all, I need some advice on operations between dense and sparse matrix "objects". Which operations are possible in scipy ? Assume that A and B are sparse while C and D are dense matrices of appropriate dimension. dense sparse dot(C,D) A*B linalg.norm(C) linalg.norm(C.todense()) linalg.kron(C,D) ? identity(n), eye(n) How about interactions of sparse and dense matrices - multiplication of a sparse matrix A with a dense matrix C - Kronecker product of sparse matrices ? - Kronecker product of a dense and a sparse matrix ? - sparse matrix functions expm(A),... ? - sparse eigensolver - to be continued Can I use different sparse formats like csc and csr w.r.t. to multiplication, addition,... And what is the output of A = io.mmread("A.mtx") ? I mean sparse or dense. Any comments ? Nils
Nils Wagner wrote:
dense sparse
dot(C,D) A*B linalg.norm(C) linalg.norm(C.todense()) linalg.kron(C,D) ? identity(n), eye(n)
Hi Nils, I have just added speye, spidentity to the scipy.sparse (using spdiags). You can test them in svn. r.
Robert Cimrman wrote:
Nils Wagner wrote:
dense sparse
dot(C,D) A*B linalg.norm(C) linalg.norm(C.todense()) linalg.kron(C,D) ? identity(n), eye(n)
Hi Nils, I have just added speye, spidentity to the scipy.sparse (using spdiags). You can test them in svn.
r.
Hi Robert, Great ! Thank you very much ! :-) Nils
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.net http://www.scipy.net/mailman/listinfo/scipy-user
Nils Wagner wrote:
Robert Cimrman wrote:
Nils Wagner wrote:
dense sparse
dot(C,D) A*B linalg.norm(C) linalg.norm(C.todense()) linalg.kron(C,D) ? identity(n), eye(n)
Hi Nils, I have just added speye, spidentity to the scipy.sparse (using spdiags). You can test them in svn.
r.
Hi Robert,
Great ! Thank you very much ! :-)
Nils
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.net http://www.scipy.net/mailman/listinfo/scipy-user
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.net http://www.scipy.net/mailman/listinfo/scipy-user
Hi Robert, Is there a special reason why you have used the CSC format ? speye(n, m=None, k=0, dtype='d') speye( n, m ) returns a (n, m) matrix tored in CSC sparse matrix format, where the k-th diagonal is all ones, and everything else is zeros. It should be stored instead of tored ;-) spidentity(n, dtype='d') spidentity( n ) returns the identity matrix of shape (n, n) stored in CSC sparse matrix format. Thank you again. Nils
Hi Robert,
Is there a special reason why you have used the CSC format ?
yes, because these functions are simple wrappers of the 'spdiags' fortran function which returns the CSC matrix.
speye(n, m=None, k=0, dtype='d') speye( n, m ) returns a (n, m) matrix tored in CSC sparse matrix format, where the k-th diagonal is all ones, and everything else is zeros.
It should be stored instead of tored ;-)
fixed, thanks. r.
Nils Wagner wrote:
I need some advice on operations between dense and sparse matrix "objects". Which operations are possible in scipy ?
<snip>
- multiplication of a sparse matrix A with a dense matrix C - Kronecker product of sparse matrices ? - Kronecker product of a dense and a sparse matrix ? - sparse matrix functions expm(A),... ? - sparse eigensolver
I don't know about all of these, but currently multiplication works between arrays by sparse matrices (and should be efficient) and you can achieve exponentiation for integer powers by repeated multiplication. But currently multiplication a * b does NOT work if 'a' is a dense *matrix* and 'b' is sparse. I'd like to fix this, but it's not easy, and we'd need to discuss how.
Can I use different sparse formats like csc and csr w.r.t. to multiplication, addition,...
Yes, it should work for any two formats, but one matrix will usually be converted internally to the same format as the other, so it's more efficient to use the same format unless you know what you're doing.
And what is the output of A = io.mmread("A.mtx") ? I mean sparse or dense.
It should be the same format that it was stored in. -- Ed
Ed Schofield wrote:
Nils Wagner wrote:
I need some advice on operations between dense and sparse matrix "objects". Which operations are possible in scipy ?
<snip>
- multiplication of a sparse matrix A with a dense matrix C - Kronecker product of sparse matrices ? - Kronecker product of a dense and a sparse matrix ? - sparse matrix functions expm(A),... ? - sparse eigensolver
I don't know about all of these, but currently multiplication works between arrays by sparse matrices (and should be efficient) and you can achieve exponentiation for integer powers by repeated multiplication. But currently multiplication a * b does NOT work if 'a' is a dense *matrix* and 'b' is sparse. I'd like to fix this, but it's not easy, and we'd need to discuss how.
Can I use different sparse formats like csc and csr w.r.t. to multiplication, addition,...
Yes, it should work for any two formats, but one matrix will usually be converted internally to the same format as the other, so it's more efficient to use the same format unless you know what you're doing.
And what is the output of A = io.mmread("A.mtx") ? I mean sparse or dense.
It should be the same format that it was stored in.
-- Ed
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.net http://www.scipy.net/mailman/listinfo/scipy-user
Hi Ed, Thank you for your valuable comments. The multiplication a*b, where a is dense a b is sparse, is certainly of importance. For example Block Krylov methods make heavily use of such products. Anyway, it would be nice if scipy can handle such products :-) Any comments or suggestions ? Cheers, Nils Some References http://math.nist.gov/spblas/ Maybe we can benift from octave in some sense. http://octave.sourceforge.net/index/f/krylov.html Sparse Kronecker product http://octave.sourceforge.net/index/f/spkron.html Sparse random matrices http://octave.sourceforge.net/index/f/sprand.html .
Nils Wagner wrote:
Hi Ed,
Thank you for your valuable comments. The multiplication a*b, where a is dense a b is sparse, is certainly of importance. For example Block Krylov methods make heavily use of such products. Anyway, it would be nice if scipy can handle such products :-)
It does work now where 'a' is an *array*, but not when 'a' is a (dense) matrix. For now, just use a dense array :) The reason is that the matrix object overrides the * operator and calls dot(a, b). I'll think about this problem when I have time and make some proposals on scipy-dev and/or numpy-discussion for how to solve it... -- Ed
Ed Schofield wrote:
Nils Wagner wrote:
Hi Ed,
Thank you for your valuable comments. The multiplication a*b, where a is dense a b is sparse, is certainly of importance. For example Block Krylov methods make heavily use of such products. Anyway, it would be nice if scipy can handle such products :-)
It does work now where 'a' is an *array*, but not when 'a' is a (dense) matrix. For now, just use a dense array :)
The reason is that the matrix object overrides the * operator and calls dot(a, b). I'll think about this problem when I have time and make some proposals on scipy-dev and/or numpy-discussion for how to solve it...
-- Ed
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.net http://www.scipy.net/mailman/listinfo/scipy-user
Hi Ed, May I send you my (sparse/dense) codes for solving large Sylvester equations. The results of the dense version are o.k. while the sparse version doesn't work as expected. I hope you can drop some hints ;-) . Cheers, Nils
Hi Nils, On Tue, 21 Feb 2006, Nils Wagner wrote: [...]
May I send you my (sparse/dense) codes for solving large Sylvester equations. The results of the dense version are o.k. while the sparse version doesn't work as expected. I hope you can drop some hints ;-) .
It seems that you are becoming the sparse expert! Would it be possible that you collect the information on sparse matrices in the http://www.scipy.org/Cookbook? Something like http://www.scipy.org/Cookbook/SparseMatrices with (e.g.) topics like: - sparse matrix formats - operations with sparse matrices - operations on sparse matrics - solving sparse matrix eigenvalue problems - presumably best on a subpage http://www.scipy.org/Cookbook/SparseMatrices/EigenvalueProblems would be extremely helpful. Your code on solving large Sylvester equations would surely be interesting as well. Best, Arnd
participants (4)
-
Arnd Baecker -
Ed Schofield -
Nils Wagner -
Robert Cimrman