Skip to content

Commit 91e619a

Browse files
committed
add example with conversion and spmv
1 parent 6e679f5 commit 91e619a

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

doc/specs/stdlib_sparse.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,9 @@ This module provides facility functions for converting between storage formats.
225225

226226
`sellc`, `intent(inout)`: Shall be a `SELLC` type of `real` or `complex` type.
227227

228-
`chunk`, `intent(in)`, `optional`: chunk size for the Sliced ELLPACK format.
228+
`chunk`, `intent(in)`, `optional`: chunk size for the Sliced ELLPACK format.
229+
230+
## Example
231+
```fortran
232+
{!example/strings/example_sparse_spmv.f90!}
233+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
program example_sparse_spmv
2+
use stdlib_linalg_constants, only: dp
3+
use stdlib_sparse
4+
implicit none
5+
6+
integer, parameter :: m = 4, n = 2
7+
real(dp) :: A(m,n), x(n)
8+
real(dp) :: y_dense(m), y_coo(m), y_csr(m)
9+
real(dp) :: alpha, beta
10+
type(COO_dp) :: COO
11+
type(CSR_dp) :: CSR
12+
13+
call random_number(A)
14+
! Convert from dense to COO and CSR matrices
15+
call dense2coo( A , COO )
16+
call coo2csr( COO , CSR )
17+
18+
! Initialize vectors
19+
x = 1._dp
20+
y_dense = 2._dp
21+
y_coo = y_dense
22+
y_csr = y_dense
23+
24+
! Perform matrix-vector product
25+
alpha = 3._dp; beta = 2._dp
26+
y_dense = alpha * matmul(A,x) + beta * y_dense
27+
call spmv( COO , x , y_coo , alpha = alpha, beta = beta )
28+
call spmv( CSR , x , y_csr , alpha = alpha, beta = beta )
29+
30+
print *, 'dense :', y_dense
31+
print *, 'coo :', y_coo
32+
print *, 'csr :', y_csr
33+
34+
end program example_sparse_spmv
35+
36+

0 commit comments

Comments
 (0)