diff --git a/+quantity/Discrete.m b/+quantity/Discrete.m index ecffc9314dad891ae1337e24de80f5e83e2d717f..6a94e592842d81f7143c856b31823701bc0da49e 100644 --- a/+quantity/Discrete.m +++ b/+quantity/Discrete.m @@ -256,8 +256,15 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi assert(all(cellfun(@(v)isvector(v), myDomain)), 'The cell entries for a new grid have to be vectors') newGrid = myDomain; myDomain = quantity.Domain.empty(); + + if obj(1).isConstant() + gridNames = repmat({''}, length(newGrid)); + else + gridNames = {obj(1).domain.name}; + end + for k = 1:length(newGrid) - myDomain(k) = quantity.Domain('grid', newGrid{k}, 'name', obj(1).domain(k).name); + myDomain(k) = quantity.Domain('grid', newGrid{k}, 'name', gridNames{k}); end end elseif nargin == 3 @@ -276,9 +283,12 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi end % verify the domain - assert(numel(myDomain) == numel(obj(1).domain), ['Wrong grid for the evaluation of the object']); - [myDomain, gridPermuteIdx] = obj(1).domain.permute(myDomain); - + if obj(1).isConstant + gridPermuteIdx = 1:length(myDomain); + else + assert(numel(myDomain) == numel(obj(1).domain), ['Wrong grid for the evaluation of the object']); + [myDomain, gridPermuteIdx] = obj(1).domain.permute(myDomain); + end % get the valueDiscrete data for this object. Apply the % permuted myDomain. Then the obj2value will be evaluated % in the order of the original domain. The permuatation to @@ -1169,14 +1179,22 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi gridNameNew = misc.ensureIsCell(gridNameNew); end - [gridIndexNew, logIdx] = obj(1).domain.gridIndex(gridNameNew); - newDomain = obj(1).domain; - - for i = 1 : length(gridIndexNew) - newDomain(gridIndexNew(i)) = quantity.Domain(... - 'grid', gridNew{i}, 'name', gridNameNew{i}); + if obj(1).isConstant + for i = 1 : length(gridNew) + newDomain(i) = quantity.Domain(... + 'grid', gridNew{i}, 'name', gridNameNew{i}); + end + else + [gridIndexNew, logIdx] = obj(1).domain.gridIndex(gridNameNew); + newDomain = obj(1).domain; + + for i = 1 : length(gridIndexNew) + newDomain(gridIndexNew(i)) = quantity.Domain(... + 'grid', gridNew{i}, 'name', gridNameNew{i}); + end + assert(isequal({newDomain.name}, obj(1).gridName), 'rearranging grids failed'); end - assert(isequal({newDomain.name}, obj(1).gridName), 'rearranging grids failed'); + newObj = obj.copy(); [newObj.domain] = deal(newDomain); diff --git a/+quantity/Domain.m b/+quantity/Domain.m index 90729b70d6582e2e4322ec70d901c4acbdfbb6e9..e4aeab633d415b2ef958062713c45062549161f1 100644 --- a/+quantity/Domain.m +++ b/+quantity/Domain.m @@ -81,11 +81,23 @@ classdef Domain < matlab.mixin.CustomDisplay end function s = eq(obj, obj2) - s = all(obj.gridLength == obj2.gridLength); - for k = 1:numel(obj) + + if isempty(obj) && isempty(obj2) + % if both are empty -> they are equal + s = true; + elseif isempty(obj) || isempty(obj2) + % if only one domain is empty -> they are not equal + s = false; + else + % if both are not empty: check if the grids and the + % gridNames are equal + s = all(obj.gridLength == obj2.gridLength); + + for k = 1:numel(obj) s = s && all(obj(k).grid == obj2(k).grid); s = s && strcmp(obj(k).name, obj2(k).name); end + end end function l = ne(obj, obj2) diff --git a/+unittests/+quantity/testDiscrete.m b/+unittests/+quantity/testDiscrete.m index f875e9b738083a466f00b0a176f45a4f763964d3..bb938e8243be18c16307e522f636a1e155f2a55b 100644 --- a/+unittests/+quantity/testDiscrete.m +++ b/+unittests/+quantity/testDiscrete.m @@ -223,6 +223,13 @@ testCase.verifyEqual(DE.on(), compareDE.on()) ED = [E, D]; compareED = quantity.Discrete({tan(t*z), sin(t * z); cos(t * z), cos(t*z)}, 'grid', {t, z}, 'gridName', {'t', 'z'}); testCase.verifyEqual(ED.on(), compareED.on()) + +% concatenation of a constant and a function: +const = quantity.Discrete([5; 2], 'domain', quantity.Domain.empty); +% +% [const, A] +% [A, const] + end function testExp(testCase) @@ -890,10 +897,12 @@ testCase.verifyTrue( numeric.near( squeeze(double(a * 42)), [sin(z * pi) * 42, c testCase.verifyTrue( numeric.near( squeeze(double(s * a')), [sin(z * pi) * 42, cos(z * pi) * 42])); testCase.verifyTrue( numeric.near( squeeze(double(a * s)), [sin(z * pi) * 42, cos(z * pi) * 42])); +end -%% test - +function testOnConstant(testCase) +const = quantity.Discrete([0 1; 2 3], 'domain', quantity.Domain.empty); +const.on([1:5]) end diff --git a/+unittests/+quantity/testDomain.m b/+unittests/+quantity/testDomain.m index ea22a6db4a81c48e3d6104522dd3e71535e4376d..c6b9738a8d0ad827eac392467d6054cd9c6ec302 100644 --- a/+unittests/+quantity/testDomain.m +++ b/+unittests/+quantity/testDomain.m @@ -5,6 +5,18 @@ end function setupOnce(testCase) end +function testEq(testCase) + +d = quantity.Domain('grid', 0, 'name', 'd'); +e = quantity.Domain.empty(); + +testCase.verifyFalse( d == e ); +testCase.verifyTrue( d == d ); +testCase.verifyTrue( e == e ); +testCase.verifyFalse( e == d ); + +end + function testDomainInit(testCase) Z = linspace(0,pi, 3); diff --git a/+unittests/+quantity/testSymbolic.m b/+unittests/+quantity/testSymbolic.m index 618167bb3a96d74e390d585b8b6fd69794ad97c7..b053269d0612ef4b5586ffa3bc802751d9e729a4 100644 --- a/+unittests/+quantity/testSymbolic.m +++ b/+unittests/+quantity/testSymbolic.m @@ -79,7 +79,8 @@ end % testFlipGrid(); function testCumInt(testCase) tGrid = linspace(pi, 1.1*pi, 51)'; sGrid = tGrid; -syms s t +s = sym('s'); +t = sym('t'); a = [ 1, s; t, 1]; b = [ s; 2*s];