Commit 51a42e8e authored by Ferdinand Fischer's avatar Ferdinand Fischer
Browse files

Fixed equality comparison for quantity.Domain.

It uses the isequal comparison now to check if the objects have the same entries and not the == as before.
Changed type of quantity.Domain to handle
parent 22d1a5f9
...@@ -2158,7 +2158,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi ...@@ -2158,7 +2158,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi
value = reshape(v, [obj(1).gridSize(), size(obj)]); value = reshape(v, [obj(1).gridSize(), size(obj)]);
if nargin == 2 && myDomain ~= obj(1).domain if nargin == 2 && ~isequal( myDomain, obj(1).domain )
% if a new domain is specified for the evaluation of % if a new domain is specified for the evaluation of
% the quantity, ... % the quantity, ...
if obj.isConstant if obj.isConstant
...@@ -2210,15 +2210,17 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi ...@@ -2210,15 +2210,17 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi
function P = zeros(valueSize, grid, varargin) function P = zeros(valueSize, grid, varargin)
%ZEROS initializes an zero quantity.Discrete object %ZEROS initializes an zero quantity.Discrete object
% P = zeros(gridSize, valueSize, grid) returns a % P = zeros(VALUESIZE, GRID) returns a quantity.Discrete
% quantity.Discrete object that has only zero entries on a % object that has only zero entries on a grid which is
% grid with defined by the cell GRID and the value size % defined by the cell GRID and the value size defined by the
% defined by the size-vector VALUESIZE % size-vector VALUESIZE
% P = quantity.Discrete([n,m], {linspace(0,1)', % P = quantity.Discrete([n,m], {linspace(0,1)',
% linspace(0,10)}); % linspace(0,10)});
% creates an (n times m) zero quantity.Discrete on the % creates an (n times m) zero quantity.Discrete on the
% grid (0,1) x (0,10) % grid (0,1) x (0,10)
% %
% P = zeros(VALUESIZE, DOMAIN) creates a matrix of size
% VALUESIZE on the DOMAIN with zero entries.
if ~iscell(grid) if ~iscell(grid)
grid = {grid}; grid = {grid};
end end
......
classdef Domain < matlab.mixin.CustomDisplay classdef Domain < handle & matlab.mixin.CustomDisplay
%DOMAIN class to describes a range of values on which a function can be defined. %DOMAIN class to describes a range of values on which a function can be defined.
% todo: % todo:
...@@ -42,11 +42,10 @@ classdef Domain < matlab.mixin.CustomDisplay ...@@ -42,11 +42,10 @@ classdef Domain < matlab.mixin.CustomDisplay
obj.grid = myParser.Results.grid(:); obj.grid = myParser.Results.grid(:);
obj.name = myParser.Results.name; obj.name = myParser.Results.name;
myParser.unmatchedWarning();
else else
obj = quantity.Domain.empty(); obj = quantity.Domain.empty();
end end
myParser.unmatchedWarning();
end end
function i = get.ones(obj) function i = get.ones(obj)
...@@ -82,45 +81,58 @@ classdef Domain < matlab.mixin.CustomDisplay ...@@ -82,45 +81,58 @@ classdef Domain < matlab.mixin.CustomDisplay
s = [obj.n]; s = [obj.n];
end end
function s = eq(obj, obj2) % function s = eq(obj, obj2)
%
if isempty(obj) && isempty(obj2) % if isempty(obj) && isempty(obj2)
% if both are empty -> they are equal % % if both are empty -> they are equal
s = true; % s = true;
elseif isempty(obj) || isempty(obj2) % elseif isempty(obj) || isempty(obj2)
% if only one domain is empty -> they are not equal % % if only one domain is empty -> they are not equal
s = false; % s = false;
else % else
% if both are not empty: check if the grids and the % % if both are not empty: check if the grids and the
% gridNames are equal % % gridNames are equal
s = all(obj.gridLength == obj2.gridLength); % s = all(obj.gridLength == obj2.gridLength);
%
for k = 1:numel(obj) % for k = 1:numel(obj)
s = s && all(obj(k).grid == obj2(k).grid); % s = s && all(obj(k).grid == obj2(k).grid);
s = s && strcmp(obj(k).name, obj2(k).name); % s = s && strcmp(obj(k).name, obj2(k).name);
end % end
end % end
end % end
%
function l = ne(obj, obj2) function l = ne(obj, obj2)
l = ~(obj == obj2); l = ~(obj == obj2);
end end
function [joinedDomain, index] = gridJoin(obj1, obj2) function [joinedDomain, index] = join(domain1, domain2)
%% gridJoin combines the grid and gridName of two objects (obj1, %% gridJoin combines the grid and gridName of two objects (obj1,
% obj2), such that every gridName only occurs once and that the % obj2), such that every gridName only occurs once and that the
% finer grid of both is used. % finer grid of both is used.
arguments
domain1 quantity.Domain = quantity.Domain.empty
domain2 quantity.Domain = quantity.Domain.empty
end
if nargin == 1 && length(domain1) == 1
joinedDomain = domain1;
index = 1;
return;
end
[joinedGrid, index] = unique({obj1.name, obj2.name}, 'stable'); [joinedGrid, index] = unique({domain1.name, domain2.name}, 'stable');
joinedDomain = quantity.Domain.empty; joinedDomain = quantity.Domain.empty;
% #todo@domain: the gird comparison seems to be very
% complicated
% check for each grid if it is in the domain of obj1 or obj2 or % check for each grid if it is in the domain of obj1 or obj2 or
% both % both
for i = 1 : numel(joinedGrid) for i = 1 : numel(joinedGrid)
currentGridName = joinedGrid{i}; currentGridName = joinedGrid{i};
[index1, logicalIndex1] = obj1.gridIndex(currentGridName); [index1, logicalIndex1] = domain1.gridIndex(currentGridName);
[index2, logicalIndex2] = obj2.gridIndex(currentGridName); [index2, logicalIndex2] = domain2.gridIndex(currentGridName);
% %
% if ~any(logicalIndex1) % if ~any(logicalIndex1)
...@@ -132,8 +144,8 @@ classdef Domain < matlab.mixin.CustomDisplay ...@@ -132,8 +144,8 @@ classdef Domain < matlab.mixin.CustomDisplay
% Check if a domain is in both domains: % Check if a domain is in both domains:
% -> then take the finer one of both % -> then take the finer one of both
if any(logicalIndex1) && any(logicalIndex2) if any(logicalIndex1) && any(logicalIndex2)
tempDomain1 = obj1(index1); tempDomain1 = domain1(index1);
tempDomain2 = obj2(index2); tempDomain2 = domain2(index2);
assert(tempDomain1.lower == tempDomain2.lower, 'Grids must have same domain for gridJoin') assert(tempDomain1.lower == tempDomain2.lower, 'Grids must have same domain for gridJoin')
assert(tempDomain1.upper == tempDomain2.upper, 'Grids must have same domain for gridJoin') assert(tempDomain1.upper == tempDomain2.upper, 'Grids must have same domain for gridJoin')
...@@ -145,17 +157,21 @@ classdef Domain < matlab.mixin.CustomDisplay ...@@ -145,17 +157,21 @@ classdef Domain < matlab.mixin.CustomDisplay
end end
% If it is not in both, -> just take the normal grid % If it is not in both, -> just take the normal grid
elseif any(logicalIndex1) elseif any(logicalIndex1)
joinedDomain(i) = obj1(index1); joinedDomain(i) = domain1(index1);
elseif any(logicalIndex2) elseif any(logicalIndex2)
joinedDomain(i) = obj2(index2); joinedDomain(i) = domain2(index2);
end end
end end
end
function [joinedDomain, index] = gridJoin(obj1, obj2)
[joinedDomain, index] = obj1.join(obj2);
end % gridJoin() end % gridJoin()
function [idx, logicalIdx] = gridIndex(obj, names) function [idx, logicalIdx] = gridIndex(obj, names)
%% GRIDINDEX returns the index of the grid %% GRIDINDEX returns the index of the grid
% [idx, log] = gridIndex(obj, names) searches in the gridName % [idx, log] = gridIndex(obj, names) searches in the name
% properties of obj for the "names" and returns its index as % properties of obj for the "names" and returns its index as
% "idx" and its logical index as "log" % "idx" and its logical index as "log"
...@@ -174,7 +190,11 @@ classdef Domain < matlab.mixin.CustomDisplay ...@@ -174,7 +190,11 @@ classdef Domain < matlab.mixin.CustomDisplay
log = strcmp({obj.name}, names{k}); log = strcmp({obj.name}, names{k});
logicalIdx = logicalIdx | log; logicalIdx = logicalIdx | log;
if any(log) if any(log)
idx(k) = nArgIdx(log); % #todo@domain: if the index for a grid name is searched in a
% list with multiple grids but same names, only the first
% occurrence is returned. this should be changed...
tmpFirst = nArgIdx(log);
idx(k) = tmpFirst(1);
else else
idx(k) = 0; idx(k) = 0;
end end
......
...@@ -81,7 +81,7 @@ classdef Function < quantity.Discrete ...@@ -81,7 +81,7 @@ classdef Function < quantity.Discrete
if nargin < 3 if nargin < 3
recalculate = false; recalculate = false;
end end
if ~recalculate && (myDomain == obj(1).domain) if ~recalculate && isequal(myDomain, obj(1).domain)
value = obj2value@quantity.Discrete(obj); value = obj2value@quantity.Discrete(obj);
else else
% otherwise the function has to be evaluated on the new % otherwise the function has to be evaluated on the new
......
...@@ -5,15 +5,18 @@ end ...@@ -5,15 +5,18 @@ end
function setupOnce(testCase) function setupOnce(testCase)
end end
function testEq(testCase) function testEquality(testCase)
d = quantity.Domain('grid', 0, 'name', 'd'); d = quantity.Domain('grid', 0, 'name', 'd');
e = quantity.Domain.empty(); e = quantity.Domain.empty();
testCase.verifyFalse( d == e ); dd = d;
testCase.verifyTrue( d == d ); ee = e;
testCase.verifyTrue( e == e );
testCase.verifyFalse( e == d ); testCase.verifyNotEqual( d, e );
testCase.verifyEqual( dd, d );
testCase.verifyEqual( ee, e );
testCase.verifyNotEqual( e, d );
end end
......
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