Skip to content

Commit 589d94d

Browse files
committed
test real eigenvalue option
1 parent daf7a5e commit 589d94d

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

test/linalg/test_linalg_schur.fypp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ module test_linalg_schur
2323

2424
#:for rk,rt,ri in RC_KINDS_TYPES
2525
tests = [tests,new_unittest("schur_api_${ri}$",test_schur_api_${ri}$), &
26-
new_unittest("schur_random_${ri}$",test_schur_random_${ri}$)]
26+
new_unittest("schur_random_${ri}$",test_schur_random_${ri}$), &
27+
new_unittest("schur_symmetric_${ri}$",test_schur_symmetric_${ri}$)]
2728
#:endfor
2829

2930
end subroutine test_schur_decomposition
@@ -162,6 +163,50 @@ module test_linalg_schur
162163

163164
end subroutine test_schur_random_${ri}$
164165

166+
!> Test symmetric matrix (real eigenvalues)
167+
subroutine test_schur_symmetric_${ri}$(error)
168+
type(error_type), allocatable, intent(out) :: error
169+
170+
integer(ilp), parameter :: n = 3_ilp
171+
real(${rk}$), parameter :: rtol = 1.0e-4_${rk}$
172+
real(${rk}$), parameter :: eps = sqrt(epsilon(0.0_${rk}$))
173+
real(${rk}$) :: reigs(n)
174+
${rt}$, dimension(n,n) :: a, t, z
175+
type(linalg_state_type) :: state
176+
177+
! Define a symmetric 3x3 matrix with real eigenvalues
178+
a = reshape([ 3, 1, 0, &
179+
1, 3, 1, &
180+
0, 1, 3], shape=[n, n])
181+
182+
! Return real eigenvalues (Should trigger an error if they have an imaginary part)
183+
call schur(a, t, z, eigvals=reigs, err=state)
184+
185+
! Check return code
186+
call check(error, state%ok(), state%print())
187+
if (allocated(error)) return
188+
189+
! Check solution
190+
call check(error, all(schur_error(a, z, t) <= max(rtol * abs(a), eps)), &
191+
'converged solution (real symmetric, real eigs)')
192+
if (allocated(error)) return
193+
194+
contains
195+
196+
pure function schur_error(a,z,t) result(err)
197+
${rt}$, intent(in), dimension(:,:) :: a,z,t
198+
real(${rk}$), dimension(size(a,1),size(a,2)) :: err
199+
200+
#:if rt.startswith('real')
201+
err = abs(matmul(matmul(z,t),transpose(z)) - a)
202+
#:else
203+
err = abs(matmul(matmul(z,t),conjg(transpose(z))) - a)
204+
#:endif
205+
end function schur_error
206+
207+
end subroutine test_schur_symmetric_${ri}$
208+
209+
165210
#:endfor
166211

167212
end module test_linalg_schur

0 commit comments

Comments
 (0)