diff --git a/+quantity/Discrete.m b/+quantity/Discrete.m index a7d8568a561dd9f7042515ca6673ec1dc3febee6..acabadd465b4f2c3d4d7bd4858d816a14f31b797 100644 --- a/+quantity/Discrete.m +++ b/+quantity/Discrete.m @@ -427,7 +427,44 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi end end end % gridJoin() - + function obj = sort(obj, varargin) + %SORT sorts the grid of the object in a desired order + % obj = sortGrid(obj) sorts the grid in alphabetical order. + % obj = sort(obj, 'descend') sorts the grid in descending + % alphabetical order. + + if nargin == 2 && strcmp(varargin{1}, 'descend') + descend = 1; + else + descend = 0; + end + + % only sort the grids if there is something to sort + if obj(1).nargin > 1 + gridNames = obj(1).gridName; + + % this is the default case for ascending alphabetical + % order + [sortedNames, I] = sort(gridNames); + + % if descending: flip the order of the entries + if descend + sortedNames = flip(sortedNames); + I = flip(I); + end + + % sort the grid entries + [obj.grid] = deal(obj(1).grid(I)); + + % assign the new grid names + [obj.gridName] = deal(sortedNames); + + % permute the value discrete + for k = 1:numel(obj) + obj(k).valueDiscrete = permute(obj(k).valueDiscrete, I); + end + end + end% sort() function c = horzcat(a, varargin) %HORZCAT Horizontal concatenation. % [A B] is the horizontal concatenation of objects A and B @@ -520,7 +557,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi % this function has the very special thing that it a does % not have to be an quantity.Discrete object. So it has to % be checked which of the input arguments is an - % quantity.Discrete object. This is considered to be give + % quantity.Discrete object. This is considered to give % the basic values for the initialization of new % quantity.Discrete values isAquantityDiscrete = cellfun(@(o) isa(o, 'quantity.Discrete'), objCell); @@ -568,10 +605,14 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi end + % sort the grid names of each quantity + for it = 1: (numel(varargin) + 1) + objCell{it} = objCell{it}.sort; + end + [fineGrid, fineGridName] = getFinestGrid(objCell{~isEmpty}); for it = 1 : (numel(varargin) + 1) % +1 because the first entry is a - assert(all(strcmp(fineGridName, objCell{it}(1).gridName)), ... - 'gridNames of objects that are concatenated must be equal'); + % change the grid to the finest objCell{it} = objCell{it}.changeGrid(fineGrid, fineGridName); end assertSameGrid(objCell{:}); diff --git a/+unittests/+quantity/testDiscrete.m b/+unittests/+quantity/testDiscrete.m index 28d04c565c9b809ac092bac224d2a0cf3ea05353..3d5de173cbe1185044929ab98711fe1d9533620a 100644 --- a/+unittests/+quantity/testDiscrete.m +++ b/+unittests/+quantity/testDiscrete.m @@ -155,7 +155,7 @@ end function testConcatenate(testCase) -t = linspace(0, pi)'; +t = linspace(0, pi, 7)'; A = quantity.Discrete({sin(t); cos(t)}, 'grid', {t}, 'gridName', 't'); B = quantity.Discrete({tan(t); exp(t)}, 'grid', {t}, 'gridName', 't'); @@ -183,6 +183,18 @@ O_vert = [O; O]; testCase.verifyEqual(size(O_horz, 1), 5); testCase.verifyEqual(size(O_vert, 1), 10); +z = linspace(0, 1, 5); +D = quantity.Discrete({sin(t * z); cos(t * z)}, 'grid', {t, z}, 'gridName', {'t', 'z'}); +E = quantity.Discrete({tan(z' * t'); cos(z' * t')}, 'grid', {z, t}, 'gridName', {'z', 't'}); + +DE = [D, E]; +compareDE = quantity.Discrete({sin(t * z), tan(t*z); cos(t * z), cos(t*z)}, 'grid', {t, z}, 'gridName', {'t', 'z'}); + +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()) end function testExp(testCase)