function [ Q, beta ] = BioFor( D, Q, beta, k )
% BioFor enlargers the dual/biorthogonal basis enlarger by one vector
%  
% It modifies the biorthogonal basis  and updates the set of orthonormal vectors spanning
% the same space as the the enlaged basis. 
%  
% Usage:     [ Q, beta ] = BioFor( D, Q, beta, k );
%  
% Inputs:
%   D       dictionary, rearranged in such way that D(:,1:k-1) are the basis vectors and
%           D(:,k) is the atom to add into the basis  
%   Q       Q(:,1:k-1)= orthonormal basis for the span of D(:,1:k-1)
%   beta    biorthogonal basis to D(:,1:k-1)
%   k       k-th element of D to incorporate in the basis it implies that the first k-1
%           vectors are already in the basis 
%  
% Outputs: 
%   Q       Q(:,1:k)= orthonormal basis  for span of  D(:,1:k)
%   beta    updated biorthogonal basis 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/

%orthogonalization of D(:,k) w.r.t Q(:,1),..., Q(:,k-1)
if k==1 Q(:,k)=D(:,k);end
for p=1:k-1
  Q(:,k)=D(:,k)-(Q(:,p)'*D(:,k))*Q(:,p);
end
%re-orthogonalization of D(:,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
  
%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.
