Reshape a boolean sparse matrix in Yale format to accomodate a larger number of non-zero elements or to trim unused non-zero elements after a guess allocation.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(yale_sparse), | intent(inout) | :: | m |
Matrix to be reshaped |
||
integer(kind=ip), | intent(in) | :: | nnz |
New shape for the matrix |
subroutine reallocate_mat(m, nnz)
!! Reshape a boolean sparse matrix in Yale format to accomodate a
!! larger number of non-zero elements or to trim unused non-zero
!! elements after a guess allocation.
implicit none
type(yale_sparse), intent(inout) :: m
!! Matrix to be reshaped
integer(ip), intent(in) :: nnz
!! New shape for the matrix
integer(ip), allocatable :: tmp(:)
if(nnz == 0) then
! The matrix is empty, just allocate empty ci
deallocate(m%ci)
allocate(m%ci(0))
else
allocate(tmp(nnz))
if(size(m%ci) > nnz) then
! We are shrinking the matrix
tmp = m%ci(1:nnz)
else
! We are enlarging the matrix
tmp(1:size(m%ci)) = m%ci
end if
deallocate(m%ci)
allocate(m%ci(nnz))
m%ci = tmp
deallocate(tmp)
end if
end subroutine reallocate_mat