function lambda = NBioDictIns( D, lambda, k )
% NBioDictIns enlarges a basis selected from a given dictionary by one vector. It modifies 
% the biorthogonal  basis for the same space and  gives the 
% projection of the remainding atoms onto the orthogonal complementary space
%  
% Usage:    lambda = NBioDictIns( D, lambda, k );
%  
% Inputs:
%   D       dictionary, D(:,1-k-1) is the selected basis  
%   lambda  lambda(:,1:k-1)=biorthogonal functions to D(:,1:k-1)
%           lambda(:,k:end)=D(:,k:end) without component from D(:,1:k-1)    
%   k       k-th atom of D to incorporate it says that first k-1 atoms are already in
%           basis
%  
% Outputs: 
%   lambda  lambda(:,1:k)=biorthogonal functions to D(:,1:k)
%           lambda(:,k+1:end)=D(:,k+1:end) without component from D(:,1:k) 
%  
% References:
%   L. Rebollo-Neira, "Recursive bi-orthogonalisation approach and orthogonal projectors", 
%   math-ph/0209026 (2002).  
%  
% Note: the difference between this routine and BioDictIns is that in this routine the
% orthonormal basis spanning D(:,1:k) is not available  
%
% See also BioDictIns, BioInsert, NBioInsert.

% See   http://www.ncrg.aston.ac.uk/Projects/HNLApprox/
%       http://www.nonlinear-approx.info/
  
name='NBioDictIns';  
lambdak=lambda(:,k);
for z=1:1
  %re-orthogonalization of lambda(:,k)  w.r.t D(:,1:k-1)  
  for p=1:k-1
    lambda(:,k)=lambda(:,k)-(D(:,p)'*lambdak)*lambda(:,p);
  end  
end 

nork2=lambda(:,k)'*lambda(:,k); 
Qk=lambda(:,k)/sqrt(nork2);
lambda(:,k)=lambda(:,k)/(nork2); %%kth biorthogonal function
%computing of biorthogonal functions from 1 to k-1
if k>1
  lambda(:,1:k-1)=lambda(:,1:k-1)-lambda(:,k)*(D(:,k)'*lambda(:,1:k-1)); 
end	
 for zz=1:2
for p=k+1:size(D,2);
  %orthogonalization of lambda(:,p) wrt lambda(:,k) 
  lambda(:,p)=lambda(:,p)-(Qk'*lambda(:,p))*Qk;
end
 end

%Last modifiation  Laura REBOLLO-NEIRA (2009)
%
%Copyright (C) 2006 Miroslav ANDRLE and Laura REBOLLO-NEIRA
%
%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.
