function f = BSpline( m, k, l, t )
% BSpline gives the analytical form of k-th B-spline of order m on l-th subinterval of
% given partition t
% 
% Usage:    f = BSpline( m, k, l, t );
%           f = BSpline( m, k, l );
%
% Inputs:  
%   m       order of spline (m>=1), m=1 is a piecewise constant function 
%   k       specifies the index of spline, B-spline living on on I=[t(k),t(k+m)] is 
%           calculated if the sequence of knots t is not defined then 
%           I=t(k:k+m)=[k-1,k-1+m]     
%   l       specifies the interval [t(l),t(l+1)] on which the k-th B-spline is studied            
%   t       sequence of knots (optional) 
%           if t is a single number then the sequence is considered to be t:t+m  
%           if t not specified then t=k-1:k-1+m
%
% Output:
%   f       analytical form of k-th B-spline of order m on l-th subinterval of t
%  
% References: 
%   L.L. Schumaker, Spline Functions: Basic Theory, New York, Wiley, 1981.  
% 
% Remark:   It is not recommended to use this procedure by user. You can do
%           the same using SymSpline.   


% See http://www.ncrg.aston.ac.uk/Projects/BiOrthog/ for more details
% creating of t if not specified  
if nargin==3
  t(k:k+m)=k-1:k-1+m;
end; 
 
if length(t)==1 t=t:t+m;end

if l-k>m | l-k<0
  f=0;return;
end  
if (m==1) 
  if l-k==0 f=1;
  else f=0;
  end 
  return;
end 
f=0;
x=sym('x','real');
if t(k+m-1)~=t(k)      
  f=(x-t(k))/(t(k+m-1)-t(k))*BSpline(m-1,k,l,t);
end
if t(k+m)~=t(k+1) 
  f=f+((t(k+m)-x)/(t(k+m)-t(k+1)))*BSpline(m-1,k+1,l,t);
end


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