Commit ba93d876 authored by Jakob Gabriel's avatar Jakob Gabriel
Browse files

quantity.Discrete.subs: speedup by working with Domain internally

parent cfc05a42
......@@ -931,58 +931,57 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi
% f(zeta, zeta) -> the 2nd subs(f, zeta, z) will
% result in f(z, z) and not in f(zeta, z) as
% intended. This is solved, by an additonal
% substitution:
% substitution:
% f.subs(z,zetabackUp).subs(zeta,z).subs(zetabackUp,zeta)
values{end+1} = values{1};
gridName2Replace{end+1} = [gridName2Replace{1}, 'backUp'];
values{1} = [gridName2Replace{1}, 'backUp'];
values{1} = gridName2Replace{end};
end
if isequal(values{1}, gridName2Replace{1})
% replace with same variable... everything stay the
% replace with same variable... everything stays the
% same.
newGrid = obj(1).grid;
newGridName = obj(1).gridName;
% Do not use "return", since, later subs might need to be
% called recursively!
newValue = obj.on();
newDomain = obj(1).domain;
elseif any(strcmp(values{1}, obj(1).gridName))
% if for a quantity f(z, zeta) this method is
% called with subs(f, zeta, z), then g(z) = f(z, z)
% results, hence the dimensions z and zeta are
% merged.
gridIndices = [obj(1).domain.gridIndex(gridName2Replace{1}), ...
domainIndices = [obj(1).domain.gridIndex(gridName2Replace{1}), ...
obj(1).domain.gridIndex(values{1})];
newGridForOn = obj(1).grid;
if numel(obj(1).grid{gridIndices(1)}) > numel(obj(1).grid{gridIndices(2)})
newGridForOn{gridIndices(2)} = newGridForOn{gridIndices(1)};
else
newGridForOn{gridIndices(1)} = newGridForOn{gridIndices(2)};
newDomainForOn = obj(1).domain;
if obj(1).domain(domainIndices(1)).n > obj(1).domain(domainIndices(2)).n
newDomainForOn(domainIndices(2)) = quantity.Domain(...
newDomainForOn(domainIndices(2)).name, ...
newDomainForOn(domainIndices(1)).grid);
elseif obj(1).domain(domainIndices(1)).n < obj(1).domain(domainIndices(2)).n
newDomainForOn(domainIndices(1)) = quantity.Domain(...
newDomainForOn(domainIndices(1)).name, ...
newDomainForOn(domainIndices(2)).grid);
end
newValue = misc.diagNd(obj.on(newGridForOn), gridIndices);
newGrid = {newGridForOn{gridIndices(1)}, ...
newGridForOn{1:1:numel(newGridForOn) ~= gridIndices(1) ...
& 1:1:numel(newGridForOn) ~= gridIndices(2)}};
newGridName = {values{1}, ...
obj(1).gridName{1:1:numel(obj(1).gridName) ~= gridIndices(1) ...
& 1:1:numel(obj(1).gridName) ~= gridIndices(2)}};
newValue = misc.diagNd(obj.on(newDomainForOn), domainIndices);
newDomain = [newDomainForOn(domainIndices(2)), ...
newDomainForOn(all(1:1:numel(newDomainForOn) ~= domainIndices(:)))];
else
% this is the default case. just grid name is
% changed.
newGrid = obj(1).grid;
newGridName = obj(1).gridName;
newGridName{obj(1).domain.gridIndex(gridName2Replace{1})} ...
= values{1};
% this is the default case. just grid name is changed.
newDomain = obj(1).domain;
newDomain(obj(1).domain.gridIndex(gridName2Replace{1})) = ...
quantity.Domain(values{1}, ...
obj(1).domain(obj(1).domain.gridIndex(gridName2Replace{1})).grid);
newValue = obj.on();
end
elseif isnumeric(values{1}) && numel(values{1}) == 1
% if values{1} is a scalar, then obj is evaluated and
% the resulting quantity loses that spatial grid and
% the resulting quantity looses that spatial grid and
% gridName
newGridName = obj(1).gridName;
newGridName = newGridName(~strcmp(newGridName, gridName2Replace{1}));
newDomain = obj(1).domain;
newDomain = newDomain(~strcmp({newDomain.name}, gridName2Replace{1}));
% newGrid is the similar to the original grid, but the
% grid of gridName2Replace is removed.
newGrid = obj(1).grid;
newGrid = newGrid((1:1:numel(newGrid)) ~= obj(1).domain.gridIndex(gridName2Replace{1}));
newGridSize = cellfun(@(v) numel(v), newGrid);
newGridSize = newDomain.gridLength();
% newGridForOn is the similar to the original grid, but
% the grid of gridName2Replace is set to values{1} for
% evaluation of obj.on().
......@@ -993,18 +992,18 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi
elseif isnumeric(values{1}) && numel(values{1}) > 1
% if values{1} is a double vector, then the grid is
% replaced.
newGrid = obj(1).grid;
newGrid{obj(1).domain.gridIndex(gridName2Replace{1})} = values{1};
newGridName = obj(1).gridName;
newValue = obj.on(newGrid);
newDomain = obj(1).domain;
newDomain(obj(1).domain.gridIndex(gridName2Replace{1})) = ...
quantity.Domain(gridName2Replace{1}, values{1});
newValue = obj.on(newDomain);
else
error('value must specify a gridName or a gridPoint');
end
if isempty(newGridName)
if isempty(newDomain)
solution = newValue;
else
solution = quantity.Discrete(newValue, ...
'grid', newGrid, 'gridName', newGridName, ...
'domain', newDomain, ...
'name', obj(1).name);
end
if numel(gridName2Replace) > 1
......@@ -2173,7 +2172,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi
value = reshape(v, [obj(1).domain.gridLength(), size(obj)]);
if nargin == 2 && ~isequal( 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
......
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