mc

%% Make test AOs

nsecs = 10000;
fs    = 10;

pl = plist('nsecs', nsecs, 'fs', fs, 'tsfcn', 'sin(2*pi*7.433*t) + randn(size(t))');

a1 = ao(pl);
a1.setName('a1');
a1.setYunits('m');

% This should output N
iunits1 = unit('m');
ounits1 = unit('V');
iunits2 = unit('V');
ounits2 = unit('N');

lp = miir(plist('type', 'lowpass', 'fs', fs, 'fc', 0.1, 'order', 5,'iunits', iunits1, 'ounits', ounits1));
hp = miir(plist('type', 'highpass', 'fs', fs, 'fc', 1, 'order', 2,'iunits', iunits2, 'ounits', ounits2));

a1f = filter(a1, [lp hp], plist('bank', 'serial'));

%% Simplify

b = a1f.simplifyYunits()

%% Units with prefixes


a = ao(1:10);
a.setYunits('mm m^-1')

a.simplifyYunits


%% Units with prefixes


a = ao(1:10);
a.setYunits('mm m Hz um^-2')

a.simplifyYunits(plist('prefixes', true))

%% More tests on prefixes
N = 8;

test = true(N,1);

% Case 1
jj = 1;
% A unit is canceled out but there is a remaining prefix AND a remaining unit.
% Example: 'fN mN^-1 Hz'
a = ao([1:10]',[2:2:20]',plist('xunits','s'));
a.setYunits('fN mN^-1 Hz');

% The unit Newton will be canceled out and the remaining prefix (here 'p') will be added to the Hz
b = simplifyYunits(a, plist('prefixes', true));
c = simplifyYunits(a, plist('prefixes', false));

% Result: 'pHz'
if ne(b.yunits, unit('Hz')) || ne(c.yunits, unit('pHz'))
  test(jj) = false;
end

% Case 2
jj = 2;
%   A unit is canceled out but there is a remaining prefix AND NO remaining unit.
%   Example: 'fN mN^-1'
a = ao([1:10]',[2:2:20]',plist('xunits','s'));
a.setYunits('fN mN^-1');

%   In this case it is not possible to keep the remaining prefix
%   --> Don't simplify the unit (it is only possible if we compute the prefix to the values)
b = simplifyYunits(a, plist('prefixes', true));
c = simplifyYunits(a, plist('prefixes', false));

% Result:  'fN mN^-1'
if ne(b.yunits, unit('')) || ne(c.yunits, unit('fN mN^-1'))
  test(jj) = false;
end

% Case 3
jj = 3;
%   The 'normal' case
% Example: 'fN m^-2 mN^-2 Hz mm'
a = ao([1:10]',[2:2:20]',plist('xunits','s'));
a.setYunits('fN m^-2 mN^-2 Hz mm');

b = simplifyYunits(a, plist('prefixes', true));
c = simplifyYunits(a, plist('prefixes', false));

%   Result: 'GN^-1 km^-1 Hz'
if ne(b.yunits, unit('N^-1 m^-1 Hz')) || ne(c.yunits, unit('GN^-1 km^-1 Hz'))
  test(jj) = false;
end

% Case 4
jj = 4;
%   Power
% Example: ('mm')^2
a = ao([1:10]',[2:2:20]',plist('xunits','s'));
a.setYunits('mm');

b = simplifyYunits(a.^2, plist('prefixes', true));
c = simplifyYunits(a.^2, plist('prefixes', false));

%   Result: 'mm^2'
if ne(b.yunits, unit('m^2')) || ne(c.yunits, unit('mm^2'))
  test(jj) = false;
end

% Case 5
jj = 5;
%   Power
n = utils.math.randelement([-4:-1 1:4],1);
% Example: ('mm^-1 nm^-1')^n
a = ao([1:10]',[2:2:20]',plist('xunits','s'));
a.setYunits('mm^-1 nm^-1');

b = simplifyYunits(a.^n, plist('prefixes', true));
c = simplifyYunits(a.^n, plist('prefixes', false));

%   Result: 'um^-n'
if ne(b.yunits, unit(['m^' num2str(n*-2)])) || ne(c.yunits, unit(['um^' num2str(n*-2)]))
  test(jj) = false;
end

% Case 6
jj = 6;
%   A unit is canceled out but there is a remaining prefix AND a
%   remaining unit.
% Example: ('mm um ks^-1 s')
a = ao([1:10]',[2:2:20]',plist('xunits','s'));
a.setYunits('mm um ks^-1 s');

b = simplifyYunits(a, plist('prefixes', true))
c = simplifyYunits(a, plist('prefixes', false))

%   Result: 'um^2'
if ne(b.yunits, unit('m^2')) || ne(c.yunits, unit('um^2'))
  test(jj) = false;
end

% Case 7
jj = 7;
%   A unit is canceled out but there is a remaining prefix AND a
%   remaining unit.
% Example: ('mm um ks^-1 s')
a = ao([1:10]',[2:2:20]',plist('xunits','s'));
a.setYunits('mm um ks^-1 s');

b = simplifyYunits(sqrt(a), plist('prefixes', true))
c = simplifyYunits(sqrt(a), plist('prefixes', false))

%   Result: 'um^2'
if ne(b.yunits, unit('m')) || ne(c.yunits, unit('um'))
  test(jj) = false;
end

% Case 8
jj = 8;
%   Sqrt of Power
n = utils.math.randelement([-4:-2 2:4],1);
% Example: ('mm^-1 nm^-1')^2n
a = ao([1:10]',[2:2:20]',plist('xunits','s'));
a.setYunits('mm^-1 nm^-1');

b = simplifyYunits(sqrt(a.^(2*n)), plist('prefixes', true))
c = simplifyYunits(sqrt(a.^(2*n)), plist('prefixes', false))

%   Result: 'um^-n'
if ne(b.yunits, unit(['m^' num2str(n*-2)])) || ne(c.yunits, unit(['um^' num2str(n*-2)]))
  test(jj) = false;
end
