subroutine calculate_residual(this, basis_index)
type(DavidsonCalcType), intent(inout) :: this
integer, intent(in) :: basis_index
! This routine calculates the residual, r, corresponding to the new estimate of the
! ground state, stored in davidson_eigenvector. This is defined as
! r = Hv - Ev,
! where H is the Hamiltonian, v is the ground state vector estimate and E is the
! ground state energy estimate.
if (iProcIndex == root) then
! Calculate r = Hv - Ev:
! Note that, here, eigenvector_proj holds the components of v in the Krylov basis,
! and multiplied_basis_vectors holds the Krylov vectors multiplied by H, hence
! the matmul below does indeed retturn Hv.
this%residual = matmul(this%multiplied_basis_vectors(:, 1:basis_index), this%eigenvector_proj(1:basis_index))
this%residual = this%residual - this%davidson_eigenvalue * this%davidson_eigenvector
end if
end subroutine calculate_residual