craig_solver Subroutine

private subroutine craig_solver(m, n, nnz, irow, icol, a, b, damp, atol, btol, itnlim, nout, x, istop, itn, rnorm, xnorm)

Sparse minimum-norm linear solver using CRAIG's method.

Computes the minimum-norm solution of the compatible system , where A is an m x n sparse matrix stored in coordinate (triplet) format. This is intended for the underdetermined case (n > m), but works for any compatible system. The dense matrix is never formed: the only operations on A are the products and , each costing O(nnz).

The method is based on the Golub-Kahan bidiagonalization , where is lower bidiagonal. The iterate is with . Since every , converges to the minimum-norm solution . It is mathematically equivalent to CG applied to , , but without forming , and it minimizes the error at each step. The residual is available for free: .

If damp > 0, the regularized problem is solved instead: subject to , whose solution is . This is done by applying the same method to the operator , which always has full row rank, so the iteration cannot break down. This is useful when A is rank deficient.

Status codes (istop)

  • 0 -- b = 0, so the solution is x = 0
  • 1 -- converged:
  • 2 -- breakdown: . b is not in the range of A (incompatible system).
  • 3 -- iteration limit itnlim reached

References

  • C. C. Paige and M. A. Saunders, "LSQR: An algorithm for sparse linear equations and sparse least squares", ACM TOMS 8(1), 1982.
  • M. A. Saunders, "Solution of sparse rectangular systems using LSQR and CRAIG", BIT 35, 1995.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: m

number of rows in A

integer, intent(in) :: n

number of columns in A

integer, intent(in) :: nnz

number of nonzero elements of A

integer, intent(in), dimension(nnz) :: irow

row indices of the nonzero elements of A

integer, intent(in), dimension(nnz) :: icol

column indices of the nonzero elements of A

real(kind=wp), intent(in), dimension(nnz) :: a

nonzero elements of A

real(kind=wp), intent(in), dimension(m) :: b

right hand side

real(kind=wp), intent(in) :: damp

damping (regularization) parameter

real(kind=wp), intent(in) :: atol

relative error in the definition of A (values less than machine epsilon are replaced by it)

real(kind=wp), intent(in) :: btol

relative error in the definition of b (values less than machine epsilon are replaced by it)

integer, intent(in) :: itnlim

maximum number of iterations

integer, intent(in) :: nout

output unit for printing iterations (none if <=0)

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

minimum-norm solution

integer, intent(out) :: istop

status code (see above)

integer, intent(out), optional :: itn

number of iterations performed

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

estimate of

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

estimate of


Called by

proc~~craig_solver~~CalledByGraph proc~craig_solver craig_solver proc~nlesolver_solver nlesolver_type%nlesolver_solver proc~nlesolver_solver->proc~craig_solver

Source Code

    subroutine craig_solver(m,n,nnz,irow,icol,a,b,damp,atol,btol,itnlim,nout,x,istop,itn,rnorm,xnorm)

    implicit none

    integer,intent(in)                   :: m      !! number of rows in `A`
    integer,intent(in)                   :: n      !! number of columns in `A`
    integer,intent(in)                   :: nnz    !! number of nonzero elements of `A`
    integer,dimension(nnz),intent(in)    :: irow   !! row indices of the nonzero elements of `A`
    integer,dimension(nnz),intent(in)    :: icol   !! column indices of the nonzero elements of `A`
    real(wp),dimension(nnz),intent(in)   :: a      !! nonzero elements of `A`
    real(wp),dimension(m),intent(in)     :: b      !! right hand side
    real(wp),intent(in)                  :: damp   !! damping (regularization) parameter \( \delta \ge 0 \)
    real(wp),intent(in)                  :: atol   !! relative error in the definition of `A`
                                                   !! (values less than machine epsilon are replaced by it)
    real(wp),intent(in)                  :: btol   !! relative error in the definition of `b`
                                                   !! (values less than machine epsilon are replaced by it)
    integer,intent(in)                   :: itnlim !! maximum number of iterations
    integer,intent(in)                   :: nout   !! output unit for printing iterations (none if `<=0`)
    real(wp),dimension(n),intent(out)    :: x      !! minimum-norm solution
    integer,intent(out)                  :: istop  !! status code (see above)
    integer,intent(out),optional         :: itn    !! number of iterations performed
    real(wp),intent(out),optional        :: rnorm  !! estimate of \( \| b - A x - \delta s \| \)
    real(wp),intent(out),optional        :: xnorm  !! estimate of \( \| [x; s] \| \)

    real(wp),dimension(m) :: u      !! left Lanczos vector
    real(wp),dimension(n) :: v      !! right Lanczos vector (`x` part)
    real(wp),dimension(:),allocatable :: w  !! right Lanczos vector (`s` part, only if `damp>0`)
    real(wp) :: alpha, beta, zeta   !! bidiagonalization and solution coefficients
    real(wp) :: anorm, bnorm        !! norms of `[A, damp*I]` (Frobenius) and `b`
    real(wp) :: xnrm2               !! \( \| [x; s] \|^2 \)
    real(wp) :: res                 !! residual norm
    real(wp) :: atol_, btol_        !! tolerances used
    logical  :: damped              !! if `damp>0`
    integer  :: k                   !! iteration counter

    x      = zero
    istop  = 0
    k      = 0
    res    = zero
    xnrm2  = zero
    damped = damp > zero
    atol_  = max(atol, eps)
    btol_  = max(btol, eps)

    ! beta_1 u_1 = b
    u     = b
    beta  = norm2(u)
    bnorm = beta
    if (beta > zero) then

        u = u / beta

        ! alpha_1 v_1 = A^T u_1
        call atx(u, v)
        if (damped) then
            w = damp * u
            alpha = sqrt(dot_product(v,v) + dot_product(w,w))
        else
            alpha = norm2(v)
        end if

        if (alpha <= zero) then
            istop = 2  ! A^T b = 0 : b is orthogonal to range(A)
            res   = bnorm
        else
            v = v / alpha
            if (damped) w = w / alpha

            anorm = sqrt(sum(a**2) + m*damp**2)  ! Frobenius norm of [A, damp*I]
            zeta  = beta / alpha                  ! first element of z: L_k z_k = beta_1 e_1

            if (nout > 0) write(nout,'(A)') '   itn       ||r||           ||x||'

            istop = 3
            do k = 1, itnlim

                ! update the solution: x_k = x_{k-1} + zeta_k v_k
                x     = x + zeta * v
                xnrm2 = xnrm2 + zeta**2

                ! continue the bidiagonalization:
                ! beta_{k+1} u_{k+1} = A v_k + damp w_k - alpha_k u_k
                call ax(v, u, -alpha)
                if (damped) u = u + damp * w
                beta = norm2(u)

                ! residual: r_k = -beta_{k+1} zeta_k u_{k+1}
                res = abs(beta * zeta)
                if (nout > 0) write(nout,'(I6,1P,2E16.6)') k, res, sqrt(xnrm2)

                if (res <= btol_*bnorm + atol_*anorm*sqrt(xnrm2)) then
                    istop = 1
                    exit
                end if

                u = u / beta

                ! alpha_{k+1} v_{k+1} = A^T u_{k+1} - beta_{k+1} v_k
                call atx(u, v, -beta)
                if (damped) then
                    w = damp * u - beta * w
                    alpha = sqrt(dot_product(v,v) + dot_product(w,w))
                else
                    alpha = norm2(v)
                end if

                if (alpha <= eps*anorm) then
                    istop = 2  ! breakdown: system is incompatible (rank deficient A)
                    exit
                end if

                v = v / alpha
                if (damped) w = w / alpha

                ! next element of z (forward substitution with L_k)
                zeta = -beta * zeta / alpha

            end do
            k = min(k, itnlim)

        end if
    end if

    if (present(itn))   itn   = k
    if (present(rnorm)) rnorm = res
    if (present(xnorm)) xnorm = sqrt(xnrm2)

    contains

        subroutine ax(xv, y, c)
            !! compute `y = A*xv + c*y` using the sparse triplet representation
            real(wp),dimension(n),intent(in)    :: xv
            real(wp),dimension(m),intent(inout) :: y
            real(wp),intent(in)                 :: c
            integer :: i
            y = c * y
            do i = 1, nnz
                y(irow(i)) = y(irow(i)) + a(i) * xv(icol(i))
            end do
        end subroutine ax

        subroutine atx(yv, xv, c)
            !! compute `xv = A^T*yv + c*xv` using the sparse triplet representation
            !! (if `c` is not present, `xv` is an output only)
            real(wp),dimension(m),intent(in)    :: yv
            real(wp),dimension(n),intent(inout) :: xv
            real(wp),intent(in),optional        :: c
            integer :: i
            if (present(c)) then
                xv = c * xv
            else
                xv = zero
            end if
            do i = 1, nnz
                xv(icol(i)) = xv(icol(i)) + a(i) * yv(irow(i))
            end do
        end subroutine atx

    end subroutine craig_solver