function [ Q, beta ] = BioDictIns( D, Q, beta, k )
% BioDictIns enlarges a basis selected from a given dictionary  by  one vector
%  
% It appropriately modifies the biorthogonal functions and unselected dictionary atoms 
% subtracted by their components in the selected basis. It also updates the set of 
% orthonormal functions spanning the same space as chosen atoms.
%  
% Usage:     [ Q, beta ] = BioDictIns( D, Q, beta, k );
%  
% Inputs:
%   D       dictionary, rearranged in such way that D(:,1:k-1) are the selected atoms
%           D(:,k) is the atom to add into the basis   
%   Q       Q(:,1:k-1)= orthonormal basis to D(:,1:k-1) Q(:,k:end)=D(:,k:end) without
%           component from D(:,1:k-1)    
%   beta    biorthogonal functions to D(:,1:k-1)  
%   k       k-th atom of D to incorporate it says that first k-1 atoms are already in
%           basis 
%  
% Outputs: 
%  Q        Q(:,1:k)= orthonormal basis to D(:,1:k) Q(:,k+1:end)=D(:,k+1:end) without
%           component from D(:,1:k) 
%  beta     updated biorthogonal functions to D(:,1:k)
%  
% References:
%   L. Rebollo-Neira, "Recursive bi-orthogonalisation approach and orthogonal projectors",
%   math-ph/0209026 (2002).  
%  
% See also NBioDictIns, BioInsert, NBioInsert.

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

nork=norm(Q(:,k)); 
Q(:,k)=Q(:,k)/nork; %normalization of Q(:,k) 

%updating of biorthogonal functions
if k>1
  beta=beta-Q(:,k)*(D(:,k)'*beta)/nork; 
end	
beta(:,k)=Q(:,k)/nork;   % k-th biorthogonal function
  
%orthogonalization of Q(:,k+1:n) wrt Q(:,k)    
for p=k+1:size(D,2)
  %orthogonalization of Q(:,p) wrt Q(:,k)
  Q(:,p)=Q(:,p)-(Q(:,k)'*Q(:,p))*Q(:,k);	
end

%Last modifiaciton 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.
