function [E,G] = errsokm(w, D, be)
% ERRSOKM error function for sparse second order kernel methods to 
% be used in Hyper-Elliptical Conjugate Gradient Descent HECGD
%    
%   E = errsokm( w, D, be) computes the regularized error function
%     E=w'*D*w+be*sum(abs(w))^2/sum(w^2)
%
%   [E,G] = errsokm( w, D, be) also returns the gradient wrt. w
%
% See also hecgd
    
% Copyright (C) 2006  TU-Berlin, Roland Vollgraf
% 
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
% 
% If you use this software, please cite 
% "R. Vollgraf and K. Obermayer, Sparse Optimization for Second Order Kernel
% Methods, in Proc. of 2006 IJCNN, Vancouver, 2006"
% in your publications

    norm1w = sum(abs(w));
    ww = w'*w;
    
    E = w'*D*w + be*norm1w^2/ww; 
    
    if nargout >1
        
        G = 2*D*w + 2*be*(norm1w*ww*sign(w) - norm1w^2*w)/ww^2 ;
        
    end
        
