|
| 1 | +module array_utils |
| 2 | + |
| 3 | + use numeric_kinds, only: dp |
| 4 | + use lapack_wrapper, only: lapack_generalized_eigensolver, lapack_matmul, lapack_matrix_vector, & |
| 5 | + lapack_qr, lapack_solver |
| 6 | + implicit none |
| 7 | + |
| 8 | + !> \private |
| 9 | + private |
| 10 | + !> \public |
| 11 | + public :: concatenate, diagonal,eye, generate_diagonal_dominant, norm |
| 12 | + |
| 13 | +contains |
| 14 | + |
| 15 | + pure function eye(m, n, alpha) |
| 16 | + !> Create a matrix with ones in the diagonal and zero everywhere else |
| 17 | + !> \param m: number of rows |
| 18 | + !> \param n: number of colums |
| 19 | + !> \param alpha: optional diagonal value |
| 20 | + !> \return matrix of size n x m |
| 21 | + integer, intent(in) :: n, m |
| 22 | + real(dp), dimension(m, n) :: eye |
| 23 | + real(dp), intent(in), optional :: alpha |
| 24 | + |
| 25 | + !local variable |
| 26 | + integer :: i, j |
| 27 | + real(dp) :: x |
| 28 | + |
| 29 | + ! check optional values |
| 30 | + x = 1.d0 |
| 31 | + if (present(alpha)) x = alpha |
| 32 | + |
| 33 | + do i=1, m |
| 34 | + do j=1, n |
| 35 | + if (i /= j) then |
| 36 | + eye(i, j) = 0 |
| 37 | + else |
| 38 | + eye(i, i) = x |
| 39 | + end if |
| 40 | + end do |
| 41 | + end do |
| 42 | + |
| 43 | + end function eye |
| 44 | + |
| 45 | + pure function norm(vector) |
| 46 | + !> compute the norm-2 of a vector |
| 47 | + real(dp), dimension(:), intent(in) :: vector |
| 48 | + real(dp) :: norm |
| 49 | + |
| 50 | + norm = sqrt(sum(vector ** 2.d0)) |
| 51 | + |
| 52 | + end function norm |
| 53 | + |
| 54 | + subroutine concatenate(arr, brr) |
| 55 | + |
| 56 | + !> Concatenate two matrices |
| 57 | + !> \param arr: first array |
| 58 | + !> \param brr: second array |
| 59 | + !> \return arr concatenate brr (overwrites arr) |
| 60 | + |
| 61 | + real(dp), dimension(:, :), intent(inout), allocatable :: arr |
| 62 | + real(dp), dimension(:, :), intent(in) :: brr |
| 63 | + real(dp), dimension(:, :), allocatable :: tmp_array |
| 64 | + integer :: new_dim, dim_cols, dim_rows |
| 65 | + |
| 66 | + ! dimension |
| 67 | + dim_rows = size(arr, 1) |
| 68 | + dim_cols = size(arr, 2) |
| 69 | + |
| 70 | + ! Number of columns of the new matrix |
| 71 | + new_dim = dim_cols + size(brr, 2) |
| 72 | + |
| 73 | + ! move to temporal array |
| 74 | + allocate(tmp_array(dim_rows, new_dim)) |
| 75 | + tmp_array(:, :dim_cols) = arr |
| 76 | + |
| 77 | + ! Move to new expanded matrix |
| 78 | + deallocate(arr) |
| 79 | + call move_alloc(tmp_array, arr) |
| 80 | + |
| 81 | + arr(:, dim_cols + 1:) = brr |
| 82 | + |
| 83 | + end subroutine concatenate |
| 84 | + |
| 85 | + function generate_diagonal_dominant(m, sparsity, diag_val) result(arr) |
| 86 | + !> Generate a diagonal dominant square matrix of dimension m |
| 87 | + !> \param m dimension of the matrix |
| 88 | + !> \param sparsity magnitude order of the off-diagonal values |
| 89 | + |
| 90 | + integer, intent(in) :: m ! size of the square matrix |
| 91 | + real(dp), optional :: diag_val |
| 92 | + integer :: i, j |
| 93 | + real(dp) :: sparsity |
| 94 | + real(dp), dimension(m, m) :: arr |
| 95 | + call random_number(arr) |
| 96 | + |
| 97 | + arr = arr * sparsity |
| 98 | + do j=1, m |
| 99 | + do i=1, m |
| 100 | + if (i > j) then |
| 101 | + arr(i, j) = arr(j, i) |
| 102 | + else if(i == j) then |
| 103 | + if (present(diag_val))then |
| 104 | + arr(i,i) = diag_val |
| 105 | + else |
| 106 | + arr(i, i) = i |
| 107 | + end if |
| 108 | + end if |
| 109 | + end do |
| 110 | + end do |
| 111 | + |
| 112 | + end function generate_diagonal_dominant |
| 113 | + |
| 114 | + function diagonal(matrix) |
| 115 | + !> return the diagonal of a matrix |
| 116 | + real(dp), dimension(:, :), intent(in) :: matrix |
| 117 | + real(dp), dimension(size(matrix, 1)) :: diagonal |
| 118 | + |
| 119 | + ! local variables |
| 120 | + integer :: i, j, m |
| 121 | + |
| 122 | + ! dimension of the matrix |
| 123 | + m = size(matrix, 1) |
| 124 | + |
| 125 | + do i=1,m |
| 126 | + do j=1,m |
| 127 | + if (i == j) then |
| 128 | + diagonal(i) = matrix(i, j) |
| 129 | + end if |
| 130 | + end do |
| 131 | + end do |
| 132 | + |
| 133 | + end function diagonal |
| 134 | + |
| 135 | +end module array_utils |
0 commit comments