Commit 8518e01c authored by Jakob Gabriel's avatar Jakob Gabriel
Browse files

mpower for quantity.Symbolic + updated unittests (also for quantity.Discrete)

parent 8f42a9bc
......@@ -1609,7 +1609,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
end % sqrtm()
function P = power(obj, p)
% a.^p implemented by multiplication
% a.^p elementwise power implemented by multiplication
% #todo unittest required
P = quantity.Discrete.zeros(size(obj), obj(1).domain);
......
......@@ -496,6 +496,19 @@ classdef Symbolic < quantity.Function
P = prod(quantity.Discrete(obj), dim);
end
end % prod()
function P = power(obj, p)
% a.^p elementwise power
P = quantity.Symbolic(power(obj.sym(), p), obj(1).domain, ...
"name", obj(1).name + ".^{" + num2str(p) + "}");
end % power()
function P = mpower(obj, p)
% Matrix power a^p is matrix or scalar a to the power p.
P = quantity.Symbolic(mpower(obj.sym(), p), obj(1).domain, ...
"name", obj(1).name + "^{" + num2str(p) + "}");
end % mpower()
function d = det(obj)
% det(X) returns the the determinant of the squre matrix X
if isempty(obj)
......
......@@ -1253,21 +1253,28 @@ verifyTrue(testCase, numeric.near(CA.on(), ca, 1e-12));
end
function testMPower(testCase)
%%
z = linspace(0,1,7).';
a = sin(z * pi);
A = quantity.Discrete({a}, quantity.Domain("z", z));
aa = sin(z * pi) .* sin(z * pi);
function testMPower(tc)
%% scalar case
z = quantity.Domain("z", linspace(0, 1, 11));
A = quantity.Discrete(sin(z.grid * pi), z);
aa = sin(z.grid * pi) .* sin(z.grid * pi);
AA = A^2;
testCase.verifyEqual( aa, AA.on())
testCase.verifyEqual( on(A^0), ones(size(z)))
end
tc.verifyEqual( aa, AA.on());
tc.verifyEqual( on(A^0), ones(z.n, 1));
tc.verifyEqual( on(A^3), on(AA * A));
%% matrix case
zeta = quantity.Domain("zeta", linspace(0, 1, 21));
B = [0*z.Discrete + zeta.Discrete, A + 0*zeta.Discrete; ...
z.Discrete+zeta.Discrete, A * subs(A, "z", "zeta")];
BBB = B^3;
BBB_alt = B * B * B;
tc.verifyEqual(BBB.on(), BBB_alt.on(), 'AbsTol', 1e-14);
tc.verifyEqual(BBB.at({0.5, 0.6}), (B.at({0.5, 0.6}))^3, 'AbsTol', 1e-15);
tc.verifyEqual(on(B^0), permute(repmat(eye(2), [1, 1, B(1).domain(1).n, B(1).domain(2).n]), [3, 4, 1, 2]), ...
'AbsTol', 1e-15);
end % testMPower()
function testPlus(testCase)
%%
......
......@@ -4,12 +4,53 @@ function [tests ] = testSymbolic()
tests = functiontests(localfunctions());
end
function testValueContinuousBug(tc)
z = quantity.Domain("z", linspace(0, 1, 7));
zeta = quantity.Domain("zeta", linspace(0, 1, 5));
A = quantity.Symbolic(sin(sym("z") * pi), z);
B = [0*z.Symbolic + zeta.Symbolic, A + 0*zeta.Symbolic; ...
z.Symbolic+zeta.Symbolic, A * subs(A, "z", "zeta")];
B2 = [zeta.Symbolic + 0*z.Symbolic, A + 0*zeta.Symbolic; ...
z.Symbolic+zeta.Symbolic, A * subs(A, "z", "zeta")];
tc.verifyEqual(B.on([z, zeta]), B2.on([z, zeta]));
% TODO: fix this error!
% this problem is maybe caused as input arguments of B2(1).valueContinuous is somehow ordered
% differently compared to B2(2:4).valueContinuous or all B(:).valueContinuous,
% i.e. B2(1).valueContinuous is (zeta, z) instead of (z, zeta).
end % testValueContinuousBug()
function testMPower(tc)
%% scalar case
z = quantity.Domain("z", linspace(0, 1, 11));
A = quantity.Symbolic(sin(sym("z") * pi), z);
aa = sin(z.grid * pi) .* sin(z.grid * pi);
AA = A^2;
tc.verifyEqual( aa, AA.on());
tc.verifyEqual( on(A^0), ones(z.n, 1));
tc.verifyEqual( on(A^3), on(AA * A));
%% matrix case
zeta = quantity.Domain("zeta", linspace(0, 1, 21));
B = [0*z.Symbolic + zeta.Symbolic, A + 0*zeta.Symbolic; ...
z.Symbolic+zeta.Symbolic, A * subs(A, "z", "zeta")];
B_discrete = quantity.Discrete(B);
BBB = B^3;
BBB_alt = B * B * B;
BBB_discrete = B_discrete^3;
tc.verifyEqual(BBB.on(), BBB_alt.on(), 'AbsTol', 1e-14);
tc.verifyEqual(BBB.at({0.5, 0.6}), (B.at({0.5, 0.6}))^3, 'AbsTol', 1e-15);
tc.verifyEqual(BBB_discrete.at({0.5, 0.6}), (B.at({0.5, 0.6}))^3, 'AbsTol', 1e-15);
tc.verifyEqual(on(B^0), permute(repmat(eye(2), [1, 1, B(1).domain(1).n, B(1).domain(2).n]), [3, 4, 1, 2]), ...
'AbsTol', 1e-15);
end % testMPower()
function testNorm(tc)
z = quantity.Domain("z", linspace(0, 1, 11));
quan = Symbolic(z) * [-1; 2; 3];
tc.verifyEqual(quan.norm.on(), on(z.Discrete*sqrt(1+4+9)), 'AbsTol', 1e-15);
tc.verifyEqual(quan.norm.on(), on(quan.norm(2)), 'AbsTol', 1e-15);
end
end % testNorm()
function testLog(tc)
z = quantity.Domain("z", linspace(0, 1, 11));
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment