Commit 609df4b3 authored by Jakob Gabriel's avatar Jakob Gabriel
Browse files

quantity.Discrete.diff: previously, if diff("z", 0) was used, not the 0th...

quantity.Discrete.diff: previously, if diff("z", 0) was used, not the 0th derivative w.r.t. z was calculated but the 1st. This bug is now fixed.
parent 4b67d09e
......@@ -1326,8 +1326,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
bMat = flip(bMat, idx(it));
end
b = quantity.Discrete(bMat, obj(1).domain, ...
'name', "flip(" + obj(1).name + ")");
b = quantity.Discrete(bMat, obj(1).domain, "name", "flip(" + obj(1).name + ")");
end % flipDomain()
function newObj = changeDomain(obj, domain)
......@@ -2288,7 +2287,13 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
diffGridName = {diffGridName.name};
end
result = obj.diff_inner(k, diffGridName);
if k == 0
result = obj.copy();
elseif k > 0
result = obj.diff_inner(k, diffGridName);
else
error("only defined for derivatives of order k = 0, 1, 2, ...");
end
end % diff()
function I = int(obj, varargin)
......
......@@ -46,7 +46,7 @@ tc.verifyEqual(testMatrix ./ quan(1).at(0.5), at(testMatrix ./ quan(1), 0.5))
tc.verifyEqual(at(quan ./ quan, 0.5), ones([2, 2]));
end
function testiseye(tc)
function testIseye(tc)
z = quantity.Domain("z", linspace(0, 1, 11));
zeta = quantity.Domain("zeta", linspace(0, 1, 11));
I = quantity.Discrete.eye([2, 3], [z, zeta]);
......@@ -59,7 +59,7 @@ I = quantity.Discrete(1+z.grid/100, z);
tc.verifyFalse(I.iseye());
I = quantity.Discrete(1+0*z.grid, z);
tc.verifyTrue(I.iseye());
end % testEye()
end % testIseye()
function testSubsDomain(tc)
z = quantity.Domain("z", linspace(0, 1, 11));
......@@ -1021,31 +1021,34 @@ myMat = ones(2, 2) * z.Discrete();
tc.verifyEqual(myMat.diff.on(), ones(5, 2, 2));
end % testDiffMat()
function testDiffConstant2d(testCase)
function testDiffConstant2d(tc)
%% 2d
[zNdgrid, zetaNdgrid] = ndgrid(linspace(0, 1, 11), linspace(0, 1, 21));
myQuantity = quantity.Discrete(cat(3, 2*ones(11, 21), zNdgrid, zetaNdgrid), ...
[quantity.Domain("z", linspace(0, 1, 11)), quantity.Domain("zeta", linspace(0, 1, 21))], ...
'name', 'constant');
myQuantityDz = diff(myQuantity, 'z', 1);
myQuantityDzeta = diff(myQuantity, 'zeta', 1);
myQuantityDz = diff(myQuantity, "z", 1);
myQuantityDzeta = diff(myQuantity, "zeta", 1);
% verify zero-th derivative
tc.verifyEqual( myQuantity.diff("z", 0).on(), myQuantity.on() );
testCase.verifyError( @() diff(myQuantity), ?MException )
testCase.verifyError( @() diff(myQuantity, 1, {'z', 'zeta'}), ?MException )
testCase.verifyError( @() diff(myQuantity, {'z', 'zeta'}), ?MException )
tc.verifyError( @() diff(myQuantity), ?MException )
tc.verifyError( @() diff(myQuantity, 1, {'z', 'zeta'}), ?MException )
tc.verifyError( @() diff(myQuantity, {'z', 'zeta'}), ?MException )
% constant
testCase.verifyEqual(myQuantityDz(1).on(), zeros(11, 21));
testCase.verifyEqual(myQuantityDzeta(1).on(), zeros(11, 21));
tc.verifyEqual(myQuantityDz(1).on(), zeros(11, 21));
tc.verifyEqual(myQuantityDzeta(1).on(), zeros(11, 21));
% zNdgrid
testCase.verifyEqual(myQuantityDz(2).on(), ones(11, 21), "AbsTol", 10*eps);
testCase.verifyEqual(myQuantityDzeta(2).on(), zeros(11, 21), "AbsTol", 10*eps);
tc.verifyEqual(myQuantityDz(2).on(), ones(11, 21), "AbsTol", 10*eps);
tc.verifyEqual(myQuantityDzeta(2).on(), zeros(11, 21), "AbsTol", 10*eps);
% zetaNdgrid
testCase.verifyEqual(myQuantityDz(3).on(), zeros(11, 21), "AbsTol", 10*eps);
testCase.verifyEqual(myQuantityDzeta(3).on(), ones(11, 21), "AbsTol", 10*eps);
tc.verifyEqual(myQuantityDz(3).on(), zeros(11, 21), "AbsTol", 10*eps);
tc.verifyEqual(myQuantityDzeta(3).on(), ones(11, 21), "AbsTol", 10*eps);
end
......
Markdown is supported
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