public NRsparseMat transpose() {
    int i,j,k,index,m=nrows,n=ncols;
    NRsparseMat at = new NRsparseMat(n,m,nvals);
    int[] count = new int[m];
    for (i=0;i<n;i++)
      for (j=col_ptr[i];j<col_ptr[i+1];j++) {
        k=row_ind[j];
        count[k]++;
      }
    for (j=0;j<m;j++)
      at.col_ptr[j+1]=at.col_ptr[j]+count[j];
    for(j=0;j<m;j++)
      count[j]=0;
    for (i=0;i<n;i++)
      for (j=col_ptr[i];j<col_ptr[i+1];j++) {
        k=row_ind[j];
        index=at.col_ptr[k]+count[k];
        at.row_ind[index]=i;
        at.val[index]=val[j];
        count[k]++;
      }
    return at;
  }