Commit 4ec292a2 authored by Ferdinand Fischer's avatar Ferdinand Fischer
Browse files

close #6: Implemented a sort function to sort the grids before the concatenation

parent 2abce96a
...@@ -427,7 +427,44 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi ...@@ -427,7 +427,44 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi
end end
end end
end % gridJoin() 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) function c = horzcat(a, varargin)
%HORZCAT Horizontal concatenation. %HORZCAT Horizontal concatenation.
% [A B] is the horizontal concatenation of objects A and B % [A B] is the horizontal concatenation of objects A and B
...@@ -520,7 +557,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi ...@@ -520,7 +557,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi
% this function has the very special thing that it a does % this function has the very special thing that it a does
% not have to be an quantity.Discrete object. So it has to % not have to be an quantity.Discrete object. So it has to
% be checked which of the input arguments is an % 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 % the basic values for the initialization of new
% quantity.Discrete values % quantity.Discrete values
isAquantityDiscrete = cellfun(@(o) isa(o, 'quantity.Discrete'), objCell); isAquantityDiscrete = cellfun(@(o) isa(o, 'quantity.Discrete'), objCell);
...@@ -568,10 +605,14 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi ...@@ -568,10 +605,14 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi
end 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}); [fineGrid, fineGridName] = getFinestGrid(objCell{~isEmpty});
for it = 1 : (numel(varargin) + 1) % +1 because the first entry is a for it = 1 : (numel(varargin) + 1) % +1 because the first entry is a
assert(all(strcmp(fineGridName, objCell{it}(1).gridName)), ... % change the grid to the finest
'gridNames of objects that are concatenated must be equal');
objCell{it} = objCell{it}.changeGrid(fineGrid, fineGridName); objCell{it} = objCell{it}.changeGrid(fineGrid, fineGridName);
end end
assertSameGrid(objCell{:}); assertSameGrid(objCell{:});
......
...@@ -155,7 +155,7 @@ end ...@@ -155,7 +155,7 @@ end
function testConcatenate(testCase) function testConcatenate(testCase)
t = linspace(0, pi)'; t = linspace(0, pi, 7)';
A = quantity.Discrete({sin(t); cos(t)}, 'grid', {t}, 'gridName', 't'); A = quantity.Discrete({sin(t); cos(t)}, 'grid', {t}, 'gridName', 't');
B = quantity.Discrete({tan(t); exp(t)}, 'grid', {t}, 'gridName', 't'); B = quantity.Discrete({tan(t); exp(t)}, 'grid', {t}, 'gridName', 't');
...@@ -183,6 +183,18 @@ O_vert = [O; O]; ...@@ -183,6 +183,18 @@ O_vert = [O; O];
testCase.verifyEqual(size(O_horz, 1), 5); testCase.verifyEqual(size(O_horz, 1), 5);
testCase.verifyEqual(size(O_vert, 1), 10); 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 end
function testExp(testCase) function testExp(testCase)
......
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