Commit 8f42a9bc authored by Jakob Gabriel's avatar Jakob Gabriel
Browse files

quantity.Discrete/norm - new method for vector norm + unittests

parent 033ec538
......@@ -273,8 +273,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
data = {[ obj.valueDiscrete ], obj(1).domain.name, ...
obj(1).domain.grid, obj(1).name};
h = misc.hash(data);
end
end % hash()
function d = compositionDomain(obj, domainName)
......@@ -1608,6 +1607,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
error('sqrtm() is only implemented for quadratic matrices');
end
end % sqrtm()
function P = power(obj, p)
% a.^p implemented by multiplication
% #todo unittest required
......@@ -1626,7 +1626,8 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
P(k).name = obj(k).name + ".^" + num2str(p);
end
end
end
end % power
function P = mpower(a, p)
% Matrix power a^p is matrix or scalar a to the power p.
if p == 0
......@@ -1647,6 +1648,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
end
end
end % mpower()
function s = num2str(obj)
s = obj.name;
end % num2str()
......@@ -1893,6 +1895,24 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
end
end % l2norm()
function xNorm = norm(obj, p)
% norm implements the vector norm, similar to builtin matlab function norm
% norm(V,P) returns the p-norm of V defined as SUM(ABS(V).^P)^(1/P).
%
% norm(X) is the same as norm(X,2). (euclidian norm)
arguments
obj;
p (1, 1) double = 2;
end
if p == 2
xNorm = sqrt(sum(abs(obj).^p));
else
xNorm = sum(abs(obj).^p).^(1/p);
end
end % norm(obj, p)
function xNorm = quadraticNorm(obj, varargin)
% calculates the quadratic norm, i.e,
% xNorm = sqrt(x.' * x).
......
......@@ -12,6 +12,13 @@ testCase.TestData.sym.t = t;
testCase.TestData.sym.sy = sy;
end
function testNorm(tc)
z = quantity.Domain("z", linspace(0, 1, 11));
quan = Discrete(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
function testLog(tc)
z = quantity.Domain("z", linspace(0, 1, 11));
quan = Discrete(z) * ones(2, 2) + [1, 1; 0, 0]+0.1*ones(2,2);
......
......@@ -4,6 +4,13 @@ function [tests ] = testSymbolic()
tests = functiontests(localfunctions());
end
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
function testLog(tc)
z = quantity.Domain("z", linspace(0, 1, 11));
quan = Symbolic(z) * ones(2, 2) + [1, 1; 0, 0]+0.1*ones(2,2);
......
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