function [A] = transinplace0(A); % % Input: A in csc format % % Output: A is overwritten with A' % Warning: The result does not have ordered columns % % computes the transpose of a CSC matrix A % (almost) in place % n = A.n; m = A.m; nz = A.nz; % create one int array J of length nnz J = A.i; % I - indices assigned to J array does transpose % % reconstruct column indices j so that % [A.i, j, A.x] is in [i,j,Aij] format % On completion i <-> j have been swapped % for j = 1:n, for (p = A.p(j) : A.p(j+1)-1), A.i(p) = j; end end c = zeros (1,n) ; for k = 1:nz j = J (k) ; c (j) = c (j) + 1 ; end A.p = cumsum ([1 c]) ; w = A.p ; for col = 1:n for k = (A.p (col)) : (A.p (col+1)-1) while (col ~= J(k)) % the kth triplet is out of place ik = A.i (k) ; jk = J (k) ; xk = A.x (k) ; % find out where it should go in the final result p = w (jk) ; w (jk) = w (jk) + 1 ; % swap A.i (k) = A.i (p) ; J (k) = J (p) ; A.x (k) = A.x (p) ; A.i (p) = ik ; J (p) = jk ; A.x (p) = xk ; % now the kth triplet might still be out of place, but the % pth triplet is in its final position. The pth triplet will % not be considered again as a "p"th triplet, since it is % inside the sorted range for its column. Thus, we must % eventually place the kth triplet in its final place. % The algorithm will progress to the final solution: QED. end end end