%***************************************************************
%  Function gls_real_t.m by www.boundary-element-method.com    *
%***************************************************************
% Matlab/Freemat/Octave codes
% gls.m Version 1, July 2015
% http://www.researchgate.net/profile/Stephen_Kirkup
% School of Computing, Engineering and Physical Sciences
%  University of Central Lancashire
% Issued under the GNU General Public License 2007, see 
%  gpl.txt
%
% Function gls_t is a set of tests problems for the functions
%  gls and regls. 
%
% Source of the code: 
%  http://www.boundary-element-method.com/mfiles/gls_real_t.m
% Source of the test function codes: 
%  http://www.boundary-element-method.com/mfiles/gls.m
%  http://www.boundary-element-method.com/mfiles/regls.m
% Source of the user-guide:  
%  http://www.boundary-element-method.com/mfiles/gls_t_m.pdf
%
% Licence: This is 'open source'; the software may be used and 
%  applied within other systems as long as its provenance is 
%  appropriately acknowledged. See the GNU Licence  
%  http://www.gnu.org/licenses/lgpl.txt
%  for more information or contact 
%  webmaster@boundary-element-method.com

function gls_t()
Tiny=0.000001;
    
% Test 1. 
%gls test on real 2x2 system 
A=[4 -1; 3 1];
B=[1 1; 2 -3];
alpha=[0.5; -2];
beta=[0.1; 5];
x_solution=[-1; 7];
y_solution=[1; 3];
f=alpha.*x_solution+beta.*y_solution;
c=A*x_solution-B*y_solution;
[x, y, xory, L, U, B_gls, P, lfail ] = gls(A, B, c, 2, alpha, beta, f);
if ((norm(x-x_solution)+norm(y-y_solution))<Tiny) 
    'gls: real 2x2 test passed' 
else 
    'gls: real 2x2 test failed' 
end

% regls test on real 2x2 system
x_solution=[5; -3];
y_solution=[2; -7];
f_new=alpha.*x_solution+beta.*y_solution;
c_new=A*x_solution-B*y_solution;
[x,y]=regls(2, xory, L, U, B_gls, P, alpha, beta, c_new, f_new);
if ((norm(x-x_solution)+norm(y-y_solution))<Tiny) 
    'regls: real 2x2 test passed' 
else 
    'regls: real 2x2 test failed' 
end

% Test 2
% gls test on real 5x5 system 
A=[ 19 -32 -92  62 -82; 
    93  91 -17  42  26; 
    76  98  -5 -92 -66;
   -31 -91 -94  13 -80;
   -91  11 -89  -4 -37];
B=[-0.89  0.16  0.92 -0.79 -0.96;
    0.32  0.83 -0.41 -0.47  0.55;
    0.67  0.87  0.94 -0.37  0.58;
   -0.53  0.42 -0.73 -0.46 -0.12;
   -0.57  0.96 -0.14  0.82  0.90];
alpha=[8;66;0;-53;45];
beta=[2.5;-0.3;-0.96;-0.74;0];
x_solution=[-0.06; -0.04; 0.08; -0.03; -0.02];
y_solution=[6; 3; -9; 2; -7];
f=alpha.*x_solution+beta.*y_solution;
c=A*x_solution-B*y_solution;
[x, y, xory, L, U, B_gls, P, lfail ] = gls(A, B, c, 5, alpha, beta, f);
if (norm(x-x_solution)+norm(y-y_solution)<Tiny)
    'gls: real 5x5 test passed' 
else 
    'gls: real 5x5 test failed' 
end

% regls test on real 5x5 system
x_solution=[0.3; -1.3; 0.4; -0.5; 2];
y_solution=[24; -33; 0; 57; -18];
f_new=alpha.*x_solution+beta.*y_solution;
c_new=A*x_solution-B*y_solution;
[x,y]=regls(5, xory, L, U, B_gls, P, alpha, beta, c_new, f_new);
if (norm(x-x_solution)+norm(y-y_solution)<Tiny)
    'regls: real 5x5 test passed' 
else 
    'regls: real 5x5 test failed' 
end

