From 2876f079159d0511344cd2d0a7a19d9c4e8afce7 Mon Sep 17 00:00:00 2001 From: jalvesz Date: Sat, 14 Mar 2026 12:05:22 +0100 Subject: [PATCH 1/3] add sort_data optional to coo2csr --- doc/specs/stdlib_sparse.md | 4 +++- src/sparse/stdlib_sparse_conversion.fypp | 12 +++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/doc/specs/stdlib_sparse.md b/doc/specs/stdlib_sparse.md index 4d0b6df5b..5d2b8a4ff 100644 --- a/doc/specs/stdlib_sparse.md +++ b/doc/specs/stdlib_sparse.md @@ -296,7 +296,7 @@ If the `diagonal` array has not been previously allocated, the `diag` subroutine ### Syntax -`call ` [[stdlib_sparse_conversion(module):coo2csr(interface)]] `(coo,csr)` +`call ` [[stdlib_sparse_conversion(module):coo2csr(interface)]] `(coo,csr[,sort_data])` ### Arguments @@ -304,6 +304,8 @@ If the `diagonal` array has not been previously allocated, the `diag` subroutine `csr` : Shall be a `CSR` type of `real` or `complex` type. It is an `intent(out)` argument. +`sort_data`, `optional` : Shall be a `logical` argument to determine whether data in the COO graph should be sorted before obtaining the CSR representation, default `.false.`. It is an `intent(in)` argument. + ### Syntax `call ` [[stdlib_sparse_conversion(module):coo2csc(interface)]] `(coo,csc)` diff --git a/src/sparse/stdlib_sparse_conversion.fypp b/src/sparse/stdlib_sparse_conversion.fypp index 6ada279c2..d7959dcfd 100644 --- a/src/sparse/stdlib_sparse_conversion.fypp +++ b/src/sparse/stdlib_sparse_conversion.fypp @@ -6,6 +6,7 @@ !! ! This code was modified from https://github.com/jalvesz/FSPARSE by its author: Alves Jose module stdlib_sparse_conversion + use stdlib_optval, only: optval use stdlib_sorting, only: sort use stdlib_sparse_constants use stdlib_sparse_kinds @@ -215,11 +216,20 @@ contains #:endfor #:for k1, t1, s1 in (KINDS_TYPES) - subroutine coo2csr_${s1}$(COO,CSR) + subroutine coo2csr_${s1}$(COO, CSR, sort_data) type(COO_${s1}$_type), intent(in) :: COO type(CSR_${s1}$_type), intent(out) :: CSR + logical, intent(in), optional :: sort_data + logical :: sort_data_ integer(ilp) :: i + sort_data_ = optval(x=sort_data, default=.false.) + + if(sort_data_) then + call sort_coo_unique_${s1}$( COO%index, COO%data, COO%nnz, COO%nrows, COO%ncols ) + COO%is_sorted = .true. + end if + CSR%nnz = COO%nnz; CSR%nrows = COO%nrows; CSR%ncols = COO%ncols CSR%storage = COO%storage From d474b66ccf0022cc8c887077010a8012d0265d36 Mon Sep 17 00:00:00 2001 From: jalvesz Date: Sun, 15 Mar 2026 18:25:08 +0100 Subject: [PATCH 2/3] make COO inout to allow sorting --- src/sparse/stdlib_sparse_conversion.fypp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sparse/stdlib_sparse_conversion.fypp b/src/sparse/stdlib_sparse_conversion.fypp index d7959dcfd..141948c06 100644 --- a/src/sparse/stdlib_sparse_conversion.fypp +++ b/src/sparse/stdlib_sparse_conversion.fypp @@ -217,8 +217,8 @@ contains #:for k1, t1, s1 in (KINDS_TYPES) subroutine coo2csr_${s1}$(COO, CSR, sort_data) - type(COO_${s1}$_type), intent(in) :: COO - type(CSR_${s1}$_type), intent(out) :: CSR + type(COO_${s1}$_type), intent(inout) :: COO + type(CSR_${s1}$_type), intent(out) :: CSR logical, intent(in), optional :: sort_data logical :: sort_data_ integer(ilp) :: i From f1d82be2dc25a5e302825ceb58d8a8247ecc72fd Mon Sep 17 00:00:00 2001 From: Jose Alves Date: Fri, 20 Mar 2026 07:50:36 +0100 Subject: [PATCH 3/3] enrich doc for sort_data in coo2csr --- doc/specs/stdlib_sparse.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/specs/stdlib_sparse.md b/doc/specs/stdlib_sparse.md index 5d2b8a4ff..1b1cf253d 100644 --- a/doc/specs/stdlib_sparse.md +++ b/doc/specs/stdlib_sparse.md @@ -304,7 +304,7 @@ If the `diagonal` array has not been previously allocated, the `diag` subroutine `csr` : Shall be a `CSR` type of `real` or `complex` type. It is an `intent(out)` argument. -`sort_data`, `optional` : Shall be a `logical` argument to determine whether data in the COO graph should be sorted before obtaining the CSR representation, default `.false.`. It is an `intent(in)` argument. +`sort_data`, `optional` : Shall be a `logical` argument to determine whether data in the COO graph should be sorted before obtaining the CSR representation. The transformation from COO to CSR depends on the former being sorted in row-major order and not having duplicate pairs. Using this boolean will call a sorting routine at the cost of extra runtime, default `.false.`. It is an `intent(in)` argument. ### Syntax