Fastest way to compute J' * J, where J is sparse

1 次查看(过去 30 天)
I have a sparse rectangular matrix, J, for which I want to compute:
>> H = J' * J;
It's a bit slow (transpose is taking 5s and the matrix multiplication 9s), and given this is a special and very common case of a transpose and multiply, I was wondering if MATLAB had a faster way, e.g. one which avoids an explicit transpose.
  15 个评论
Matt J
Matt J 2015-11-20
I don't know the complexity. It will have to be tested. I imagine this could be advantageous only for certain kinds of sparsity structure.
Oliver Woodford
Oliver Woodford 2015-11-20
For my size of matrix, qr(J,0) is vastly slower (i.e. several orders of magnitude; actually it crashed MATLAB after a minute or so) than a mex file that computes J'*J (a second or so). The mex file is a bit slower than standard MATLAB for smaller sparse matrices, but at least works for very tall matrices (which MATLAB produces an "Matrix too large" error on when transposing).

请先登录,再进行评论。

回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2014-11-3
编辑:Azzi Abdelmalek 2014-11-3
c=sparse(J);
H=full(c*c');
  2 个评论
Oliver Woodford
Oliver Woodford 2014-11-3
The matrix is already stored as a sparse matrix.
John D'Errico
John D'Errico 2014-11-3
编辑:John D'Errico 2014-11-3
Um, J is already assumed to be in sparse form, and one would definitely not want to compute a full result when working with sparse matrices. Finally, you put the transpose on the wrong term, computing J*J', not J'*J.

请先登录,再进行评论。


Matt J
Matt J 2014-11-4
编辑:Matt J 2014-11-5
I don't think there's anything available to accelerate an exact calculation of J'*J for general J. However, if you know in advance that J'*J happens to be banded to diagonals -k:k for small k (or if it can be approximated as such), then it might help to compute the 2*k+1 non-trivial diagonals individually. You can do so without transposition as below.
[m,n]=size(J);
k=2;
kc=k+1;
tic;
B=zeros(n);
B(:,kc)=sum(J.^2);
for i=1:k
tmp=sum(J(:,1:end-i).*J(:,i+1:end));
B(1:end-i,kc-i)=tmp;
B(i+1:end,kc+i)=tmp;
end
result=spdiags(B,-k:k,n,n);
toc;
Whether this is actually faster will probably depend on the specifics of J. If nothing else, it spares you the large memory consumption of holding wide sparse matrices such as J' in RAM
>> J=sparse(m,n); Jt=J'; whos J Jt
Name Size Bytes Class Attributes
J 3192027x3225 25824 double sparse
Jt 3225x3192027 25536240 double sparse
Replacing J'*J by a banded approximation is something I haven't tried myself with Gauss-Newton specifically, but the role of J'*J is already as an approximation there, so I think it could work. Other minimization algorithms tend to be robust to small errors in the derivatives.

类别

Help CenterFile Exchange 中查找有关 Sparse Matrices 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by