diff --git a/+quantity/Discrete.m b/+quantity/Discrete.m index 875b948a982665aee96a617dcc01cb1e212f4bc0..1ade316e709b9ae0c4c25adce383e4acd7473f71 100644 --- a/+quantity/Discrete.m +++ b/+quantity/Discrete.m @@ -159,10 +159,10 @@ classdef (InferiorClasses = {?quantity.Symbolic, ?quantity.Operator}) Discrete %--------------------------- % --- getter and setters --- %--------------------------- - function i = isConstant(obj) + function itIs = isConstant(obj) % the quantity is interpreted as constant if it has no grid or % it has a grid that is only one point. - i = isempty(obj.gridSize) || prod(obj.gridSize) == 1; + itIs = isempty(obj.gridSize) || prod(obj.gridSize) == 1; end function doNotCopy = get.doNotCopy(obj) doNotCopy = obj.doNotCopyPropertiesName(); @@ -1143,9 +1143,11 @@ classdef (InferiorClasses = {?quantity.Symbolic, ?quantity.Operator}) Discrete if isa(a, 'double') P = (b' * a')'; + % this recursion is safe, because isa(b, 'double') is considered in + % the if above. return end - if a.isConstant == 1 && b.isConstant == false + if a.isConstant() && ~b.isConstant() % If the first argument a is constant value, then bad % things will happen. To avoid this, we calculate % a * b = (b' * a')' @@ -1154,7 +1156,7 @@ classdef (InferiorClasses = {?quantity.Symbolic, ?quantity.Operator}) Discrete P = (b' * a')'; return end - if all(size(b) == 1) % b is a scalar value + if numel(b) == 1 % b is a scalar value for k = 1:numel(a) P(k) = innerMTimes(a(k), b); end @@ -1162,15 +1164,19 @@ classdef (InferiorClasses = {?quantity.Symbolic, ?quantity.Operator}) Discrete return end - if all(size(a) == 1) - % TODO test the case a is double and b is scalar and vice - % versa - P = (b' * a')'; - return + if numel(a) == 1 + % in the previous version this was implemented recursivley with + % P = (b' * a')'; but this was an ininite loop for numel(a) == 1 + % and b.isConstant() + for k = 1:numel(b) + P(k) = innerMTimes(a, b(k)); end + P = reshape(P, size(b)); + return + end P = innerMTimes(a, b); - end + end function P = innerMTimes(a, b) assert(size(a, 2) == size(b, 1), ['For multiplication the ', ... diff --git a/+quantity/Symbolic.m b/+quantity/Symbolic.m index 354eb7a13509e752d8ddb3ff2d42783ad1c4495a..9f9930a06ce11f3efed90f0420bf0611612dcdaf 100644 --- a/+quantity/Symbolic.m +++ b/+quantity/Symbolic.m @@ -93,11 +93,11 @@ classdef Symbolic < quantity.Function end end - function i = isConstant(obj) - i = true; + function itIs = isConstant(obj) + itIs = true; for k = 1:numel(obj) - i = i && isempty(symvar(obj(k).sym)); - if ~i + itIs = itIs && isempty(symvar(obj(k).sym)); + if ~itIs break end end