@@ -410,6 +410,29 @@ def test_lsqr_pylops_scipy(par):
410410 assert_array_almost_equal (xinv_sp , x , decimal = 4 )
411411
412412
413+ @pytest .mark .skipif (
414+ int (os .environ .get ("TEST_CUPY_PYLOPS" , 0 )) == 1 , reason = "Not CuPy enabled"
415+ )
416+ @pytest .mark .parametrize ("par" , [(par3 ), (par4 )])
417+ def test_lsqr_calc_var (par ):
418+ """Compare PyLops and scipy LSQR variance computation for the diagonal of
419+ (A^H A)^-1 (issue #639)."""
420+ np .random .seed (10 )
421+
422+ A = np .random .normal (0 , 1 , (par ["ny" ], par ["nx" ]))
423+ Aop = MatrixMult (A , dtype = par ["dtype" ])
424+ y = Aop * (np .ones (par ["nx" ]))
425+
426+ # niter is only a ceiling (atol/btol stop earlier); use scipy's default of
427+ # 2 * nx so both implementations run for the same number of iterations.
428+ niter = 2 * par ["nx" ]
429+ var = lsqr (Aop , y , x0 = None , niter = niter , atol = 1e-8 , btol = 1e-8 )[9 ]
430+ var_sp = sp_lsqr (Aop , y , iter_lim = niter , atol = 1e-8 , btol = 1e-8 , calc_var = True )[9 ]
431+
432+ assert not np .allclose (var , var [0 ])
433+ assert_array_almost_equal (var , var_sp , decimal = 6 )
434+
435+
413436@pytest .mark .parametrize (
414437 "par" , [(par1 ), (par2 ), (par3 ), (par4 ), (par1j ), (par2j ), (par3j ), (par3j )]
415438)
0 commit comments