solve Subroutine

private subroutine solve(me, b, x, istat, n_refine, rnorm)

Solve for the minimum-norm solution: , .

factor must be called first.

Iterative refinement is used to recover the accuracy lost by forming : the residual is computed with the original matrix, a correction is computed with the factorization, and . Note that x is accumulated directly (rather than computed as ): for an ill-conditioned A, y can be very large with cancellation in , while the corrections become small. The refinement stops when the residual no longer decreases. Each correction is in the range of , so the minimum-norm property is kept.

The refinement converges if (i.e., in double precision).

Type Bound

cholesky_minnorm_solver

Arguments

Type IntentOptional Attributes Name
class(cholesky_minnorm_solver), intent(inout) :: me
real(kind=wp), intent(in), dimension(:) :: b

right-hand side (size m)

real(kind=wp), intent(out), dimension(:) :: x

minimum-norm solution (size n)

integer, intent(out) :: istat

status code (CHOLESKY_SUCCESS if no errors)

integer, intent(in), optional :: n_refine

maximum number of iterative refinement steps (default is 2)

real(kind=wp), intent(out), optional :: rnorm

final residual norm


Calls

proc~~solve~~CallsGraph proc~solve cholesky_minnorm_solver%solve proc~lower_solve cholesky_minnorm_solver%lower_solve proc~solve->proc~lower_solve

Source Code

    subroutine solve(me, b, x, istat, n_refine, rnorm)

    class(cholesky_minnorm_solver),intent(inout) :: me
    real(wp),dimension(:),intent(in)  :: b         !! right-hand side (size `m`)
    real(wp),dimension(:),intent(out) :: x         !! minimum-norm solution (size `n`)
    integer,intent(out)               :: istat     !! status code (`CHOLESKY_SUCCESS` if no errors)
    integer,intent(in),optional       :: n_refine  !! maximum number of iterative refinement steps
                                                   !! (default is 2)
    real(wp),intent(out),optional     :: rnorm     !! final residual norm \( \| b - A x - \delta^2 y \| \)

    real(wp),dimension(:),allocatable :: y, dy, r, xbest
    real(wp) :: rn, rn_best
    integer :: it, nref, i, kk

    istat = CHOLESKY_SUCCESS
    if (.not. me%factored) then
        istat = CHOLESKY_ERR_NOT_READY; return
    end if
    if (size(b) /= me%m .or. size(x) /= me%n) then
        istat = CHOLESKY_ERR_INPUT; return
    end if

    nref = 2
    if (present(n_refine)) nref = max(0, n_refine)

    allocate(y(me%m), dy(me%m), r(me%m))
    y = zero
    x = zero
    r = b
    rn_best = huge(1.0_wp)

    do it = 0, nref

        call me%lower_solve(r, dy)
        y = y + dy

        ! x = x + A^T dy
        do i = 1, me%m
            do kk = me%rowptr(i), me%rowptr(i+1)-1
                x(me%colidx(kk)) = x(me%colidx(kk)) + me%val(kk) * dy(i)
            end do
        end do

        ! r = b - A x - damp^2 y
        do i = 1, me%m
            r(i) = b(i) - me%damp**2 * y(i)
            do kk = me%rowptr(i), me%rowptr(i+1)-1
                r(i) = r(i) - me%val(kk) * x(me%colidx(kk))
            end do
        end do
        rn = norm2(r)

        if (it == 0 .or. rn < rn_best) then
            rn_best = rn
            if (it < nref) xbest = x  ! save in case the next step is worse
        else
            ! no improvement: revert to the previous solution
            x = xbest
            exit
        end if
        if (rn == zero) exit

    end do

    if (present(rnorm)) rnorm = rn_best

    end subroutine solve