From bb7de89465ac6488e1d65cc05f1ba43bada63401 Mon Sep 17 00:00:00 2001
From: Ferdinand Fischer <ferdinand.fischer@fau.de>
Date: Thu, 20 Feb 2020 16:06:55 +0100
Subject: [PATCH] Fixed bug in quantity.Domain/eq

---
 +quantity/Discrete.m                | 40 +++++++++++++++++++++--------
 +quantity/Domain.m                  | 16 ++++++++++--
 +unittests/+quantity/testDiscrete.m | 13 ++++++++--
 +unittests/+quantity/testDomain.m   | 12 +++++++++
 +unittests/+quantity/testSymbolic.m |  3 ++-
 5 files changed, 68 insertions(+), 16 deletions(-)

diff --git a/+quantity/Discrete.m b/+quantity/Discrete.m
index ecffc93..6a94e59 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 90729b7..e4aeab6 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 f875e9b..bb938e8 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 ea22a6d..c6b9738 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 618167b..b053269 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];
-- 
GitLab