Skip to content

Commit 095219e

Browse files
authored
linalg: hermitian (#896)
2 parents fcb5a50 + 5efba9b commit 095219e

File tree

5 files changed

+118
-1
lines changed

5 files changed

+118
-1
lines changed

doc/specs/stdlib_linalg.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,37 @@ Returns a `logical` scalar that is `.true.` if the input matrix is skew-symmetri
506506
{!example/linalg/example_is_skew_symmetric.f90!}
507507
```
508508

509+
## `hermitian` - Compute the Hermitian version of a rank-2 matrix
510+
511+
### Status
512+
513+
Experimental
514+
515+
### Description
516+
517+
Compute the Hermitian version of a rank-2 matrix.
518+
For `complex` matrices, the function returns the conjugate transpose (`conjg(transpose(a))`).
519+
For `real` or `integer` matrices, the function returns the transpose (`transpose(a)`).
520+
521+
### Syntax
522+
523+
`h = ` [[stdlib_linalg(module):hermitian(interface)]] `(a)`
524+
525+
### Arguments
526+
527+
`a`: Shall be a rank-2 array of type `integer`, `real`, or `complex`. The input matrix `a` is not modified.
528+
529+
### Return value
530+
531+
Returns a rank-2 array of the same shape and type as `a`. If `a` is of type `complex`, the Hermitian matrix is computed as `conjg(transpose(a))`.
532+
For `real` or `integer` types, it is equivalent to the intrinsic `transpose(a)`.
533+
534+
### Example
535+
536+
```fortran
537+
{!example/linalg/example_hermitian.f90!}
538+
```
539+
509540
## `is_hermitian` - Checks if a matrix is Hermitian
510541

511542
### Status

example/linalg/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ADD_EXAMPLE(diag5)
66
ADD_EXAMPLE(eye1)
77
ADD_EXAMPLE(eye2)
88
ADD_EXAMPLE(is_diagonal)
9+
ADD_EXAMPLE(hermitian)
910
ADD_EXAMPLE(is_hermitian)
1011
ADD_EXAMPLE(is_hessenberg)
1112
ADD_EXAMPLE(is_skew_symmetric)

example/linalg/example_hermitian.f90

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
! Example program demonstrating the usage of hermitian
2+
program example_hermitian
3+
use stdlib_linalg, only: hermitian
4+
implicit none
5+
6+
complex, dimension(2, 2) :: A, AT
7+
8+
! Define input matrices
9+
A = reshape([(1,2),(3,4),(5,6),(7,8)],[2,2])
10+
11+
! Compute Hermitian matrices
12+
AT = hermitian(A)
13+
14+
print *, "Original Complex Matrix:"
15+
print *, A
16+
print *, "Hermitian Complex Matrix:"
17+
print *, AT
18+
19+
end program example_hermitian

src/stdlib_linalg.fypp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ module stdlib_linalg
4949
public :: is_diagonal
5050
public :: is_symmetric
5151
public :: is_skew_symmetric
52+
public :: hermitian
5253
public :: is_hermitian
5354
public :: is_triangular
5455
public :: is_hessenberg
@@ -309,6 +310,31 @@ module stdlib_linalg
309310
#:endfor
310311
end interface is_hermitian
311312

313+
interface hermitian
314+
!! version: experimental
315+
!!
316+
!! Computes the Hermitian version of a rank-2 matrix.
317+
!! For complex matrices, this returns `conjg(transpose(a))`.
318+
!! For real or integer matrices, this returns `transpose(a)`.
319+
!!
320+
!! Usage:
321+
!! ```
322+
!! A = reshape([(1, 2), (3, 4), (5, 6), (7, 8)], [2, 2])
323+
!! AH = hermitian(A)
324+
!! ```
325+
!!
326+
!! [Specification](../page/specs/stdlib_linalg.html#hermitian-compute-the-hermitian-version-of-a-rank-2-matrix)
327+
!!
328+
329+
#:for k1, t1 in RCI_KINDS_TYPES
330+
pure module function hermitian_${t1[0]}$${k1}$(a) result(ah)
331+
${t1}$, intent(in) :: a(:,:)
332+
${t1}$ :: ah(size(a, 2), size(a, 1))
333+
end function hermitian_${t1[0]}$${k1}$
334+
#:endfor
335+
336+
end interface hermitian
337+
312338

313339
! Check for triangularity
314340
interface is_triangular
@@ -1568,6 +1594,17 @@ contains
15681594
end function is_hermitian_${t1[0]}$${k1}$
15691595
#:endfor
15701596

1597+
#:for k1, t1 in RCI_KINDS_TYPES
1598+
pure module function hermitian_${t1[0]}$${k1}$(a) result(ah)
1599+
${t1}$, intent(in) :: a(:,:)
1600+
${t1}$ :: ah(size(a, 2), size(a, 1))
1601+
#:if t1.startswith('complex')
1602+
ah = conjg(transpose(a))
1603+
#:else
1604+
ah = transpose(a)
1605+
#:endif
1606+
end function hermitian_${t1[0]}$${k1}$
1607+
#:endfor
15711608

15721609
#:for k1, t1 in RCI_KINDS_TYPES
15731610
function is_triangular_${t1[0]}$${k1}$(A,uplo) result(res)

test/linalg/test_linalg.fypp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
module test_linalg
55
use testdrive, only : new_unittest, unittest_type, error_type, check, skip_test
66
use stdlib_kinds, only: sp, dp, xdp, qp, int8, int16, int32, int64
7-
use stdlib_linalg, only: diag, eye, trace, outer_product, cross_product, kronecker_product
7+
use stdlib_linalg, only: diag, eye, trace, outer_product, cross_product, kronecker_product, hermitian
88
use stdlib_linalg_state, only: linalg_state_type, LINALG_SUCCESS, linalg_error_handling
99

1010
implicit none
@@ -52,6 +52,9 @@ contains
5252
new_unittest("trace_int64", test_trace_int64), &
5353
#:for k1, t1 in RCI_KINDS_TYPES
5454
new_unittest("kronecker_product_${t1[0]}$${k1}$", test_kronecker_product_${t1[0]}$${k1}$), &
55+
#:endfor
56+
#:for k1, t1 in CMPLX_KINDS_TYPES
57+
new_unittest("hermitian_${t1[0]}$${k1}$", test_hermitian_${t1[0]}$${k1}$), &
5558
#:endfor
5659
new_unittest("outer_product_rsp", test_outer_product_rsp), &
5760
new_unittest("outer_product_rdp", test_outer_product_rdp), &
@@ -597,6 +600,32 @@ contains
597600
end subroutine test_kronecker_product_${t1[0]}$${k1}$
598601
#:endfor
599602

603+
#:for k1, t1 in CMPLX_KINDS_TYPES
604+
subroutine test_hermitian_${t1[0]}$${k1}$(error)
605+
!> Error handling
606+
type(error_type), allocatable, intent(out) :: error
607+
integer, parameter :: m = 2, n = 3
608+
${t1}$, dimension(m,n) :: A
609+
${t1}$, dimension(n,m) :: AT, expected, diff
610+
real(${k1}$), parameter :: tol = 1.e-6_${k1}$
611+
612+
integer :: i,j
613+
614+
do concurrent (i=1:m,j=1:n)
615+
A (i,j) = cmplx(i,-j,kind=${k1}$)
616+
expected(j,i) = cmplx(i,+j,kind=${k1}$)
617+
end do
618+
619+
620+
AT = hermitian(A)
621+
622+
diff = AT - expected
623+
624+
call check(error, all(abs(diff) < abs(tol)), "hermitian: all(abs(diff) < abs(tol)) failed")
625+
626+
end subroutine test_hermitian_${t1[0]}$${k1}$
627+
#:endfor
628+
600629
subroutine test_outer_product_rsp(error)
601630
!> Error handling
602631
type(error_type), allocatable, intent(out) :: error

0 commit comments

Comments
 (0)