%***************************************************************
% Function gls_complex_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_complex_t is a set of tests problems for the 
%  functions gls and regls where the omponents are 
%  complex-valued. 
%
% Source of the code: 
%  http://www.boundary-element-method.com/mfiles/gls_complex_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 3
% gls test on imaginary 2x2 system 
A=i*[4 -1; 3 1];
B=i*[1 1; 2 -3];
alpha=i*[0.5; -2];
beta=i*[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: imaginary 2x2 test passed' 
else 
    'gls: imaginary 2x2 test failed' 
end
% regls test on imaginary 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: imaginary 2x2 test passed' 
else 
    'regls: imaginary 2x2 test failed' 
end

% Test 4
% gls test on complex 2x2 system 
x_solution=[5+2i; 2-3i];
y_solution=[-1-3i; 4-i];
A=[1+2i 3-i; -2-i 5+3i];
B=[-1+i 2-i; 3+2i 2-i];

alpha=[2+i; 0.1-0.3i];
beta=[15-19i; 0.01+0.05i];
c=A*x_solution-B*y_solution;
f=alpha.*x_solution+beta.*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: complex 2x2 test passed' 
else 
    'gls: complex 2x2 test failed' 
end
% regls test on complex 2x2 system
x_solution=[-3+i; 0];
y_solution=[1-4i; 1-i];
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: complex 2x2 test passed' 
else 
    'regls: complex 2x2 test failed' 
end

% Test 5
% gls test on complex 5x5 system 
x_solution=[1+2i; 0; -1; 2-3i; i];
y_solution=[-2; 1-i; 2; 0; 3+j];
A=[ 1+2i  3-i  0   -1    1-i;
    5-i   0   -i   -2-i  3;
    2+i  -3+i  2    0   -2+4i;
    0     1+5i 3-i  8   -3-7i;
   -5+2i  0    6i   2-i  0     ];
B=[-1+3i  i   -2i  -1    1-i;
   -3+i   2-i  2+i  0   -5+i;
   -4+8i -3+i -3+7i 0    1+10i;
    1+3i  0    2-i  3-i  8;
    2i    0    6i   2-i  0 ];  
c=A*x_solution-B*y_solution;
alpha=[0.1+0.2i; 3;      0;  7i;   10+12i];
beta= [0;       0.1i; 0.01;  10+i;   2-i];
f=alpha.*x_solution+beta.*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) 
    'complex 5x5 test passed' 
else 
    'complex 5x5 test failed' 
end
x_solution=[1+i; 0; 0.1-0.2i; 6-i; 2];
y_solution=[24-3i; -0.3+2i; 0; 5-7i; -4i];
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: complex 5x5 test passed' 
else 
    'regls: complex 5x5 test failed' 
end
