function [value, gradient] = lagr_a(x,flag)
% LAGR_A evaluates an augmented Lagrange function
%
%  x  is the design vector
% flag is a character  string 
%     - the first character is used to control the evaluation
%       flag(1) = 'f' - evaluate the function (only)
%                        ='g' - evaluate the gradient (only)
%                        ='a' - evaluate both the function and its gradient
%
     global PAR
% PAR is a data structure that provides
% 
%  PAR.cost  is the name of the cost functional to be minimized
%  PAR.constr is the name of the (equality) constraint function
%  PAR.lambda is the current estimate of the Lagrange multiplier
%  PAR.penalty is a scalar (vector) of positive constants for the
%         quadratic penalty term in the augmented Lagragian
%
%  
      PAR.flag = flag(1);
      PAR.flag = 'a';
%
       [value    , nabla      ]   = feval(PAR.cost,x) ;
       [constr , jacobian]   = feval(PAR.constr,x);
%
%  if    flag(1) == 'f'    | flag(1) == 'a'
           value    = feval(PAR.cost,x) - PAR.lambda(:)'*constr(:)  ...
	                   + sum (PAR.penalty(:).*constr(:).*constr(:))/2;
	gradient =[];
% end
%
     if flag(1) == 'g' | flag(1) == 'a'
	gradient  = nabla(:) - jacobian'*PAR.lambda(:)' ;
	tmp             = PAR.lambda(:)'.*constr(:)';
	tmp2          = sum(tmp*jacobian);
	gradient  = gradient + tmp2(:);
    end				   
%
