-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Gleich
committed
May 16, 2009
1 parent
9bccc10
commit f181379
Showing
5 changed files
with
102 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
function [nzi,nzj,nzv] = csr_to_sparse(rp,ci,ai,ncols) | ||
% CSR_TO_SPARSE Convert from compressed row arrays to a sparse matrix | ||
% | ||
% A = csr_to_sparse(rp,ci,ai) returns the sparse matrix represented by the | ||
% compressed sparse row representation rp, ci, and ai. The number of | ||
% columns of the output sparse matrix is max(max(ci),nrows). See the call | ||
% below. | ||
% | ||
% A = csr_to_sparse(rp,ci,ai,ncol) While we can infer the number of rows | ||
% in the matrix from this expression, you may want a | ||
% different number of | ||
% | ||
% [nzi,nzj,nzv] = csr_to_sparse(...) returns the arrays that feed the | ||
% sparse call in matlab. You can use this to avoid the sparse call and | ||
% customize the behavior. | ||
% | ||
% This command "inverts" the behavior of sparse_to_csr. | ||
% Repeated entries in the matrix are summed, just like sparse does. | ||
% | ||
% See also SPARSE SPARSE_TO_CSR | ||
% | ||
% Example: | ||
% A=sparse(6,6); A(1,1)=5; A(1,5)=2; A(2,3)=-1; A(4,1)=1; A(5,6)=1; | ||
% [rp ci ai]=sparse_to_csr(A); | ||
% A2 = csr_to_sparse(rp,ci,ai) | ||
|
||
% David F. Gleich | ||
% Copyright, Stanford University, 2008-2009 | ||
|
||
% History | ||
% 2009-05-01: Initial version | ||
% 2009-05-16: Documentation and example | ||
|
||
nrows = length(rp)-1; | ||
nzi = zeros(length(ci),1); | ||
for i=1:nrows | ||
for j=rp(i):rp(i+1)-1 | ||
nzi(j) = i; | ||
end | ||
end | ||
|
||
if nargout<2, | ||
if nargin>3, | ||
nzi = sparse(nzi,ci,ai,nrows,ncols); | ||
else | ||
% we make the matrix square unless there are more columns | ||
ncols = max(max(ci),nrows); | ||
if isempty(ncols), ncols=0; end | ||
nzi = sparse(nzi,ci,ai,nrows,ncols); | ||
end | ||
else | ||
nzj = ci; | ||
nzv = ai; | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
function test_csr_to_sparse | ||
%% empty arguments | ||
A = csr_to_sparse(1,[],[]); | ||
if ~isequal(A,sparse([])) | ||
error('gaimc:csr_to_sparse','failed to convert empty matrix'); | ||
end | ||
|
||
A = csr_to_sparse(1,[],[],50); | ||
if size(A,2)~=50 | ||
error('gaimc:csr_to_sparse','incorrect empty size output'); | ||
end | ||
|
||
%% exact test | ||
% clique to sparse | ||
rp = 1:5:26; | ||
ci = reshape(repmat(1:5,5,1)',25,1); | ||
ai = ones(25,1); | ||
A=csr_to_sparse(rp,ci,ai); | ||
if ~isequal(A,sparse(full(ones(5)))) | ||
error('gaimc:csr_to_sparse','failed to convert clique'); | ||
end | ||
|
||
%% 100 random trials of round trips between sparse_to_csr and csr_to_sparse | ||
for t=1:100 | ||
A = sprand(100,80,0.01); | ||
[rp ci ai]=sparse_to_csr(A); | ||
A2 = csr_to_sparse(rp,ci,ai,80); | ||
if ~isequal(A,A2) | ||
error('gaimc:csr_to_sparse','random sparse test failed'); | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
function test_main | ||
% TODO Check the directory | ||
test_examples | ||
test_sparse_to_csr | ||
test_csr_to_sparse % should always come after sparse_to_csr | ||
test_dfs | ||
test_load_gaimc_graph | ||
test_largest_component | ||
test_examples | ||
|
||
end | ||
end |