# Path: nlpy/tools/utils.py
# def identical(a,b):
#     """
#     Check that two arrays or lists are identical. Must be cautious because
#     of Numpy's strange behavior:
#     >>> a = np.array([]) ; b = np.array([0])
#     >>> np.all(a==b)
#     True
#     """
#     if a.shape == b.shape:
#         return np.all(a==b)
#     return False
# 
# def where(cond):
#     "Bypass Numpy's annoyances. Gee does someone need to write a proper Numpy!"
#     return np.where(cond)[0]
# 
# Path: nlpy/tools/exceptions.py
# class InfeasibleError(Exception):
#     """
#     Error that can be raised to signal an infeasible iterate.
#     """
#     def __init__(self):
#         pass
# 
# class UserExitRequest(Exception):
#     """
#     Exception that the caller can use to request clean exit.
#     """
#     def __init__(self):
#         pass


from nlpy.krylov.pcg   import TruncatedCG
from nlpy.krylov.linop import SimpleLinearOperator
from nlpy.krylov.linop import SymmetricallyReducedLinearOperator as ReducedHessian
from nlpy.tools.utils import identical, where
from nlpy.tools.exceptions import InfeasibleError, UserExitRequest
    from nlpy.model import AmplModel
import numpy as np
import logging
import pdb
    import sys
                continue

            # Projected-gradient phase: determine next working set.
            (x, (lower,upper)) = self.projected_gradient(x, g=g, active_set=(lower,upper))
            g = qp.grad(x)
            qval = qp.obj(x)
            pg = self.pgrad(x, g=g, active_set=(lower,upper))
            pgNorm = np.linalg.norm(pg)
            print 'Main loop with iter=%d and pgNorm=%g' % (iter, pgNorm)

            if pgNorm <= stoptol:
                exitOptimal = True
                continue

            # Conjugate gradient phase: explore current face.

            # 1. Obtain indices of the free variables.
            fixed_vars = np.concatenate((lower,upper))
            free_vars = np.setdiff1d(np.arange(n, dtype=np.int), fixed_vars)

            # 2. Construct reduced QP.
            print 'Starting CG on current face.'
            ZHZ = ReducedHessian(self.H, free_vars)
            Zg  = g[free_vars]
            print 'Free vars: ', free_vars
            print 'ZHZ.shape: ', ZHZ.shape
            print 'len(Zg)  : ', len(Zg)
            cg = SufficientDecreaseCG(Zg, ZHZ)
            try:
                cg.Solve()

# Path: nlpy/tools/utils.py
# def identical(a,b):
#     """
#     Check that two arrays or lists are identical. Must be cautious because
#     of Numpy's strange behavior:
#     >>> a = np.array([]) ; b = np.array([0])
#     >>> np.all(a==b)
#     True
#     """
#     if a.shape == b.shape:
#         return np.all(a==b)
#     return False
# 
# def where(cond):
#     "Bypass Numpy's annoyances. Gee does someone need to write a proper Numpy!"
#     return np.where(cond)[0]
# 
# Path: nlpy/tools/exceptions.py
# class InfeasibleError(Exception):
#     """
#     Error that can be raised to signal an infeasible iterate.
#     """
#     def __init__(self):
#         pass
# 
# class UserExitRequest(Exception):
#     """
#     Exception that the caller can use to request clean exit.
#     """
#     def __init__(self):
#         pass


# Path: nlpy/optimize/solvers/bqp.py
from nlpy.krylov.pcg   import TruncatedCG
from nlpy.krylov.linop import SimpleLinearOperator
from nlpy.krylov.linop import SymmetricallyReducedLinearOperator as ReducedHessian
from nlpy.tools.utils import identical, where
from nlpy.tools.exceptions import InfeasibleError, UserExitRequest
    from nlpy.model import AmplModel
import numpy as np
import logging
import pdb
    import sys

                continue

            # Projected-gradient phase: determine next working set.
            (x, (lower,upper)) = self.projected_gradient(x, g=g, active_set=(lower,upper))
            g = qp.grad(x)
            qval = qp.obj(x)
            pg = self.pgrad(x, g=g, active_set=(lower,upper))
            pgNorm = np.linalg.norm(pg)
            print 'Main loop with iter=%d and pgNorm=%g' % (iter, pgNorm)

            if pgNorm <= stoptol:
                exitOptimal = True
                continue

            # Conjugate gradient phase: explore current face.

            # 1. Obtain indices of the free variables.
            fixed_vars = np.concatenate((lower,upper))
            free_vars = np.setdiff1d(np.arange(n, dtype=np.int), fixed_vars)

            # 2. Construct reduced QP.
            print 'Starting CG on current face.'
            ZHZ = ReducedHessian(self.H, free_vars)
            Zg  = g[free_vars]
            print 'Free vars: ', free_vars
            print 'ZHZ.shape: ', ZHZ.shape
            print 'len(Zg)  : ', len(Zg)
            cg = SufficientDecreaseCG(Zg, ZHZ)
            try:
                cg.Solve()

            except UserExitRequest: