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
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
% the quantity, ...
if obj.isConstant
......@@ -2210,15 +2210,17 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi
function P = zeros(valueSize, grid, varargin)
%ZEROS initializes an zero quantity.Discrete object
% P = zeros(gridSize, valueSize, grid) returns a
% quantity.Discrete object that has only zero entries on a
% grid with defined by the cell GRID and the value size
% defined by the size-vector VALUESIZE
% P = zeros(VALUESIZE, GRID) returns a quantity.Discrete
% object that has only zero entries on a grid which is
% defined by the cell GRID and the value size defined by the
% size-vector VALUESIZE
% P = quantity.Discrete([n,m], {linspace(0,1)',
% linspace(0,10)});
% creates an (n times m) zero quantity.Discrete on the
% 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)
grid = {grid};
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.
% todo:
......@@ -42,11 +42,10 @@ classdef Domain < matlab.mixin.CustomDisplay
obj.grid = myParser.Results.grid(:);
obj.name = myParser.Results.name;
myParser.unmatchedWarning();
else
obj = quantity.Domain.empty();
end
myParser.unmatchedWarning();
end
function i = get.ones(obj)
......@@ -82,45 +81,58 @@ classdef Domain < matlab.mixin.CustomDisplay
s = [obj.n];
end
function s = eq(obj, obj2)
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 s = eq(obj, obj2)
%
% 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)
l = ~(obj == obj2);
end
function [joinedDomain, index] = gridJoin(obj1, obj2)
function [joinedDomain, index] = join(domain1, domain2)
%% gridJoin combines the grid and gridName of two objects (obj1,
% obj2), such that every gridName only occurs once and that the
% 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;
% #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
% both
for i = 1 : numel(joinedGrid)
currentGridName = joinedGrid{i};
[index1, logicalIndex1] = obj1.gridIndex(currentGridName);
[index2, logicalIndex2] = obj2.gridIndex(currentGridName);
[index1, logicalIndex1] = domain1.gridIndex(currentGridName);
[index2, logicalIndex2] = domain2.gridIndex(currentGridName);
%
% if ~any(logicalIndex1)
......@@ -132,8 +144,8 @@ classdef Domain < matlab.mixin.CustomDisplay
% Check if a domain is in both domains:
% -> then take the finer one of both
if any(logicalIndex1) && any(logicalIndex2)
tempDomain1 = obj1(index1);
tempDomain2 = obj2(index2);
tempDomain1 = domain1(index1);
tempDomain2 = domain2(index2);
assert(tempDomain1.lower == tempDomain2.lower, '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
end
% If it is not in both, -> just take the normal grid
elseif any(logicalIndex1)
joinedDomain(i) = obj1(index1);
joinedDomain(i) = domain1(index1);
elseif any(logicalIndex2)
joinedDomain(i) = obj2(index2);
joinedDomain(i) = domain2(index2);
end
end
end
function [joinedDomain, index] = gridJoin(obj1, obj2)
[joinedDomain, index] = obj1.join(obj2);
end % gridJoin()
function [idx, logicalIdx] = gridIndex(obj, names)
%% 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
% "idx" and its logical index as "log"
......@@ -174,7 +190,11 @@ classdef Domain < matlab.mixin.CustomDisplay
log = strcmp({obj.name}, names{k});
logicalIdx = logicalIdx | 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
idx(k) = 0;
end
......
......@@ -81,7 +81,7 @@ classdef Function < quantity.Discrete
if nargin < 3
recalculate = false;
end
if ~recalculate && (myDomain == obj(1).domain)
if ~recalculate && isequal(myDomain, obj(1).domain)
value = obj2value@quantity.Discrete(obj);
else
% otherwise the function has to be evaluated on the new
......
......@@ -5,15 +5,18 @@ end
function setupOnce(testCase)
end
function testEq(testCase)
function testEquality(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 );
dd = d;
ee = e;
testCase.verifyNotEqual( d, e );
testCase.verifyEqual( dd, d );
testCase.verifyEqual( ee, e );
testCase.verifyNotEqual( e, d );
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