# Path: nlpy/tools/norms.py
# def norm1(x):
# def norm2(x):
# def normp(x,p):
# def norm_infty(x):
# def normest(A, tol=1.0e-6, maxits=100):
#         A = np.random.randn(n,m)
#         A = np.random.rand(n,n)
#         A = .5*(A.T + A)


from nlpy.model.amplpy import AmplModel
from nlpy.krylov.linop import SimpleLinearOperator
from nlpy.krylov.ppcg import ProjectedCG as Ppcg
from nlpy.tools import norms
import numpy
import sys
    nlp.close()
    sys.exit(1)

x0 = numpy.ones(nlp.n) #nlp.x0            # Initial point
g  = numpy.random.random(nlp.n) #nlp.grad(x0)      # Objective gradient
J  = nlp.jac(x0)       # Obtain constraints Jacobian
delta = 1000 #max(1.0, 100 * norms.norm_infty(g))

# You can test constraints with a nonzero rhs by uncommenting the following,
# adding the 'rhs' and removing the 'radius' keywords in Ppcg below.
# Also make sure to adjust the computation of 'feas' at the bottom of the file.
#e = numpy.ones(nlp.n)
#c = numpy.empty(nlp.m, 'd')
#J.matvec(e, c)

# Call projected CG to solve problem
#   min  <g,d> + 1/2 <d, Hd>
#   s.t  Jd = 0,  |d| <= delta
CG = Ppcg(g,
          SimpleLinearOperator(nlp.n, nlp.n,
                               lambda p: nlp.hprod(nlp.x0,nlp.pi0,p),
                               symmetric=True),
          A=J, radius=delta, debug=True)
#CG = Ppcg(g, A=J, rhs=-c, matvec=hprod, debug=True)
CG.Solve()

Jd = numpy.empty(nlp.m, 'd')
J.matvec(CG.x, Jd)
feas = norms.norm_infty(Jd)
#feas = norms.norm_infty( c + Jd )  # for nonzero rhs constraint

# Path: nlpy/tools/norms.py
# def norm1(x):
# def norm2(x):
# def normp(x,p):
# def norm_infty(x):
# def normest(A, tol=1.0e-6, maxits=100):
#         A = np.random.randn(n,m)
#         A = np.random.rand(n,n)
#         A = .5*(A.T + A)


# Path: examples/demo_ppcg.py
from nlpy.model.amplpy import AmplModel
from nlpy.krylov.linop import SimpleLinearOperator
from nlpy.krylov.ppcg import ProjectedCG as Ppcg
from nlpy.tools import norms
import numpy
import sys

    nlp.close()
    sys.exit(1)

x0 = numpy.ones(nlp.n) #nlp.x0            # Initial point
g  = numpy.random.random(nlp.n) #nlp.grad(x0)      # Objective gradient
J  = nlp.jac(x0)       # Obtain constraints Jacobian
delta = 1000 #max(1.0, 100 * norms.norm_infty(g))

# You can test constraints with a nonzero rhs by uncommenting the following,
# adding the 'rhs' and removing the 'radius' keywords in Ppcg below.
# Also make sure to adjust the computation of 'feas' at the bottom of the file.
#e = numpy.ones(nlp.n)
#c = numpy.empty(nlp.m, 'd')
#J.matvec(e, c)

# Call projected CG to solve problem
#   min  <g,d> + 1/2 <d, Hd>
#   s.t  Jd = 0,  |d| <= delta
CG = Ppcg(g,
          SimpleLinearOperator(nlp.n, nlp.n,
                               lambda p: nlp.hprod(nlp.x0,nlp.pi0,p),
                               symmetric=True),
          A=J, radius=delta, debug=True)
#CG = Ppcg(g, A=J, rhs=-c, matvec=hprod, debug=True)
CG.Solve()

Jd = numpy.empty(nlp.m, 'd')
J.matvec(CG.x, Jd)
feas = norms.norm_infty(Jd)
#feas = norms.norm_infty( c + Jd )  # for nonzero rhs constraint

snorm = norms.norm2(CG.x) # The trust-region is in the l2 norm