44module test_linalg_least_squares
55 use testdrive, only: error_type, check, new_unittest, unittest_type
66 use stdlib_linalg_constants
7- use stdlib_linalg, only: lstsq
7+ use stdlib_linalg, only: lstsq,solve_lstsq
88 use stdlib_linalg_state, only: linalg_state_type
99
1010 implicit none (type,external)
@@ -20,6 +20,8 @@ module test_linalg_least_squares
2020 type(unittest_type), allocatable, intent(out) :: tests(:)
2121
2222 allocate(tests(0))
23+
24+ tests = [tests,new_unittest("issue_823",test_issue_823)]
2325
2426 #:for rk,rt,ri in REAL_KINDS_TYPES
2527 #:if rk!="xdp"
@@ -100,6 +102,39 @@ module test_linalg_least_squares
100102
101103 #:endif
102104 #:endfor
105+
106+ ! Test issue #823
107+ subroutine test_issue_823(error)
108+ type(error_type), allocatable, intent(out) :: error
109+
110+ ! Dimension of the problem.
111+ integer(ilp), parameter :: n = 42
112+ ! Data for the least-squares problem.
113+ complex(dp) :: A(n+1, n), b(n+1), x_true(n), x_lstsq(n+1)
114+ ! Internal variables.
115+ real(dp), allocatable :: tmp(:, :, :), tmp_vec(:, :)
116+ ! Error handler
117+ type(linalg_state_type) :: state
118+
119+ ! Zero-out data.
120+ A = 0.0_dp ; b = 0.0_dp ; x_true = 0.0_dp ; x_lstsq = 0.0_dp
121+ allocate(tmp(n+1, n, 2)) ; tmp = 0.0_dp
122+ allocate(tmp_vec(n, 2)) ; tmp_vec = 0.0_dp
123+
124+ ! Generate a random complex least-squares problem of size (n+1, n).
125+ call random_number(tmp) ; call random_number(tmp_vec)
126+ A = cmplx(tmp(:, :, 1), tmp(:, :, 2), kind=dp)
127+ x_true = cmplx(tmp_vec(:, 1), tmp_vec(:, 2), kind=dp)
128+ b = matmul(A, x_true)
129+
130+ ! Solve the lstsq problem.
131+ call solve_lstsq(A, b, x_lstsq, err=state)
132+
133+ ! Check that no segfault occurred
134+ call check(error,state%ok(),'issue 823 returned '//state%print())
135+ if (allocated(error)) return
136+
137+ end subroutine test_issue_823
103138
104139end module test_linalg_least_squares
105140
0 commit comments