C语言将CSR存储模式转为coo,从COO格式转换为CSR稀疏矩阵格式时总计重复值

在我的代码,我使用这个子程序,我写道:

subroutine csr_sum_duplicates(Ap, Aj, Ax)

! Sum together duplicate column entries in each row of CSR matrix A

! The column indicies within each row must be in sorted order.

! Explicit zeros are retained.

! Ap, Aj, and Ax will be modified *inplace*

integer, intent(inout) :: Ap(:), Aj(:)

real(dp), intent(inout) :: Ax(:)

integer :: nnz, r1, r2, i, j, jj

real(dp) :: x

nnz = 1

r2 = 1

do i = 1, size(Ap) - 1

r1 = r2

r2 = Ap(i+1)

jj = r1

do while (jj < r2)

j = Aj(jj)

x = Ax(jj)

jj = jj + 1

do while (jj < r2)

if (Aj(jj) == j) then

x = x + Ax(jj)

jj = jj + 1

else

exit

end if

end do

Aj(nnz) = j

Ax(nnz) = x

nnz = nnz + 1

end do

Ap(i+1) = nnz

end do

end subroutine

,你可以使用这个子程序给指数排序:

subroutine csr_sort_indices(Ap, Aj, Ax)

! Sort CSR column indices inplace

integer, intent(inout) :: Ap(:), Aj(:)

real(dp), intent(inout) :: Ax(:)

integer :: i, r1, r2, l, idx(size(Aj))

do i = 1, size(Ap)-1

r1 = Ap(i)

r2 = Ap(i+1)-1

l = r2-r1+1

idx(:l) = argsort(Aj(r1:r2))

Aj(r1:r2) = Aj(r1+idx(:l)-1)

Ax(r1:r2) = Ax(r1+idx(:l)-1)

end do

end subroutine

其中argsort是

function iargsort(a) result(b)

! Returns the indices that would sort an array.

!

! Arguments

! ---------

!

integer, intent(in):: a(:) ! array of numbers

integer :: b(size(a)) ! indices into the array 'a' that sort it

!

! Example

! -------

!

! iargsort([10, 9, 8, 7, 6]) ! Returns [5, 4, 3, 2, 1]

integer :: N ! number of numbers/vectors

integer :: i,imin,relimin(1) ! indices: i, i of smallest, relative imin

integer :: temp ! temporary

integer :: a2(size(a))

a2 = a

N=size(a)

do i = 1, N

b(i) = i

end do

do i = 1, N-1

! find ith smallest in 'a'

relimin = minloc(a2(i:))

imin = relimin(1) + i - 1

! swap to position i in 'a' and 'b', if not already there

if (imin /= i) then

temp = a2(i); a2(i) = a2(imin); a2(imin) = temp

temp = b(i); b(i) = b(imin); b(imin) = temp

end if

end do

end function

这应该做你想做的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值