Commit e4e28691 authored by Ferdinand Fischer's avatar Ferdinand Fischer
Browse files

Unittests are now working with the domain object

parent e805bffc
......@@ -723,17 +723,18 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi
function solution = subs(obj, gridName2Replace, values)
% SUBS substitute variables of a quantity
% solution = SUBS(obj, newDomain), replaces the original domain
% of the object with the new domain specified by newDomain.
% NewDomain must have the same grid name as the original
% domain.
%
% solution = SUBS(obj, GRIDNAMES2REPLACE, VALUES)
% replaces the domains which are specified by
% GRIDNAMES2REPLACE by VALUES. GRIDNAMES2REPLACE must be a
% cell-array with the names of the domains which should be
% replaced by VALUES. VALUES must be a cell-array of the new
% values or new grid names.
% solution = SUBS(obj, NEWDOMAIN), replaces the original
% domain of the object with the new domain specified by
% NEWDOMAIN. NEWDOMAIN must have the same grid name as the
% original domain.
%
% solution = SUBS(obj, GRIDNAMES2REPLACE, VALUES) replaces
% the domains which are specified by GRIDNAMES2REPLACE by
% VALUES. GRIDNAMES2REPLACE must be a cell-array with the
% names of the domains or an object-array with
% quantity.Domain objects which should be replaced by VALUES.
% VALUES must be a cell-array of the new values or new grid
% names.
%
% Example:
% q = q.subs('z', 't')
......@@ -756,7 +757,6 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi
values = {gridName2Replace.grid};
gridName2Replace = {gridName2Replace.name};
elseif nargin == 3
gridName2Replace = misc.ensureIsCell(gridName2Replace);
......@@ -813,7 +813,6 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi
newGridName = {values{1}, ...
obj(1).gridName{1:1:numel(obj(1).gridName) ~= gridIndices(1) ...
& 1:1:numel(obj(1).gridName) ~= gridIndices(2)}};
else
% this is the default case. just grid name is
% changed.
......@@ -1150,6 +1149,10 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi
% gridName in the obj properties remains unchanged, only the
% data points are exchanged.
%
% newObj = CHANGEGRID(obj, domain) changes the domain of the
% object specified by the name of DOMAIN into the
% corresponding domain from DOMAIN.
%
% example:
% q.changeGrid( linspace(0,1)', 't')
% will change the grid with the name 't' to the new grid linspace(0,1)'
......@@ -1158,8 +1161,13 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi
return;
end
gridNew = misc.ensureIsCell(gridNew);
gridNameNew = misc.ensureIsCell(gridNameNew);
if isa(gridNew, 'quantity.Domain')
gridNameNew = {gridNew.name};
gridNew = {gridNew.grid};
else
gridNew = misc.ensureIsCell(gridNew);
gridNameNew = misc.ensureIsCell(gridNameNew);
end
[gridIndexNew, logIdx] = obj(1).domain.gridIndex(gridNameNew);
newDomain = obj(1).domain;
......
......@@ -33,13 +33,14 @@ classdef Symbolic < quantity.Function
variableParser.addParameter('variable', quantity.Symbolic.getVariable(valueContinuous));
variableParser.addParameter('symbolicEvaluation', false);
variableParser.addParameter('gridName', '');
variableParser.addParameter('domain', []);
variableParser.parse(varargin{:});
if ~variableParser.isDefault('variable')
warning('Do not set the variable property. It will be ignored.')
end
if variableParser.isDefault('gridName')
if variableParser.isDefault('gridName') && variableParser.isDefault('domain')
warning('Grid names are generated from the symbolic variable. Please set the grid names in a quantity.Domain object explicitly')
variableNames = cellstr(string(variableParser.Results.variable));
varargin = [varargin(:)', {'gridName'}, {variableNames}];
......@@ -272,23 +273,53 @@ classdef Symbolic < quantity.Function
function solution = subs(obj, gridName, values)
%% SUBS substitues gridName and values
% solution = subs(obj, gridName, values) TODO
% This function substitutes the variables specified with
% gradName with values. It can be used to rename grids or to
% evaluate (maybe just some) grids.
% GridName is cell-array of char-arrays
% chosen from obj.gridName-property. Values is a cell-array of
% the size of gridName. Each cell can contain arrays themself,
% but those arrays must be of same size. Values can be
% char-arrays standing for new gridName or or numerics.
% In contrast to on() or at(), only some, but not necessarily
% all variables are evaluated.
if ~iscell(gridName)
gridName = {gridName};
% solution = subs(obj, DOMAIN) can be used to change the grid
% of this quantity. The domains with the same domain name as
% DOMAIN will be replaced with the corresponding grid in
% DOMAIN.
%
% solution = subs(obj, DOMAINNAME, GRID) can be used to
% change the grid of this quantity. The grid of the domains with the
% DOMAINNAME will be replaced by the values specified in
% GRID. DOMAINNAME must be a cell-array containing the names
% and GRID must be a cell-array containing the new grids.
%
% solution = sub(obj, DOMAINNAME, NEWDOMAIN) can be used to
% change the grid and/or the name of a domain. DOMAINNAME
% must be a cell-array of domain names, or a oboject-array of
% quantity.Domain objects. The NEWDOMAIN must be an
% object-array of quantity.Domain.
if isa(gridName, 'quantity.Domain')
if nargin == 2
gridName = {gridName.name};
values = {gridName.grid};
else
gridName = {gridName.name};
end
else
gridName = misc.ensureIsCell(gridName);
end
if ~iscell(values)
values = {values};
if nargin == 3 && isa(values, 'quantity.Domain')
% replacement of the grid AND the gridName
% 1) replace the grid
solution = obj.changeGrid( {values.grid}, gridName );
% 2) replace the name
solution = solution.subs( gridName, {values.name} );
return
else
values = misc.ensureIsCell(values);
end
% % ensure that domains which should be replaced are known by the
% % object
% for k = 1:length(values)
% if ischar(values{k})
% assert( any( strcmp( obj(1).gridName, values{k} ) ), 'The domain name to be replaced must be a domain name of the object' );
% end
% end
isNumericValue = cellfun(@isnumeric, values);
if any((cellfun(@(v) numel(v(:)), values)>1) & isNumericValue)
error('only implemented for one value per grid');
......@@ -347,7 +378,7 @@ classdef Symbolic < quantity.Function
end
end
solution = quantity.Symbolic(double(symbolicSolution), ...
'grid', newGrid, 'variable', newGridName, 'name', obj(1).name);
'grid', newGrid, 'gridName', newGridName, 'name', obj(1).name);
else
% before creating a new quantity, it is checked that
% newGridName is unique. If there are non-unique
......@@ -356,7 +387,7 @@ classdef Symbolic < quantity.Function
uniqueGridName = unique(newGridName, 'stable');
if numel(newGridName) == numel(uniqueGridName)
solution = quantity.Symbolic(symbolicSolution, ...
'grid', newGrid, 'variable', newGridName, 'name', obj(1).name);
'grid', newGrid, 'gridName', newGridName, 'name', obj(1).name);
else
uniqueGrid = cell(1, numel(uniqueGridName));
for it = 1 : numel(uniqueGrid)
......@@ -375,10 +406,9 @@ classdef Symbolic < quantity.Function
end
end
solution = quantity.Symbolic(symbolicSolution, ...
'grid', uniqueGrid, 'variable', uniqueGridName, 'name', obj(1).name);
'grid', uniqueGrid, 'gridName', uniqueGridName, 'name', obj(1).name);
end
end
end
end % subs()
......
......@@ -778,14 +778,16 @@ Ff1 = [1, 1^2; 1+1, 1];
Ffz = quantity.Symbolic([z, z^2; z+z, 1], 'domain', Z);
Fyx = quantity.Symbolic([w, x^2; x+w, 1], 'domain', [X W]);
% test 1: replace all domains by numerical values
fOf1 = f.subs({'x', 'y'}, {1, 1});
% TODO: discuss this cases with Jacob:
% the old variable 'x' and 'y' will be replaced by new ones. At this
% point it is not clear on which domain this variable is defined!
%
fOfz = f.subs({'x', 'y'}, {'z', 'z'});
fOfyx = f.subs({'x', 'y'}, {'bla', 'x'});
% test 2: replace one domain by a numerical value
fOf2 = f.subs({'x'}, {1});
Z0 = quantity.Domain('grid', 0 , 'name', 'z');
fOfz = f.subs([X, Y], [Z Z]);
fOfyx = f.subs({'x', 'y'}, {'w', 'x'});
testCase.verifyEqual(Ff1, fOf1);
testCase.verifyEqual(Ffz.sym(), fOfz.sym());
......@@ -794,19 +796,19 @@ testCase.verifyEqual(Fyx.sym(), fOfyx.sym());
%%
syms z zeta
assume(z>0 & z<1); assume(zeta>0 & zeta<1);
F = quantity.Symbolic(zeros(1), 'grid', {linspace(0,1), linspace(0,1)}, 'variable', {z, zeta});
F = quantity.Symbolic(zeros(1), 'grid', {linspace(0,1), linspace(0,1)}, 'gridName', {'z', 'zeta'});
Feta = F.subs('z', 'eta');
testCase.verifyEqual(Feta(1).gridName, {'eta', 'zeta'});
%%
fMessy = quantity.Symbolic(x^1*y^2*z^4*w^5, 'domain', [X, Y, Z, W]);
fMessyA = fMessy.subs({'x', 'y', 'z', 'bla'}, {'a', 'a', 'a', 'a'});
fMessyYY = fMessy.subs({'bla', 'x', 'z'}, {'bli', 'y', 'y'});
fMessyA = fMessy.subs({'x', 'y', 'z', 'w'}, {'a', 'a', 'a', 'a'});
fMessyYY = fMessy.subs({'w', 'x', 'z'}, {'xi', 'y', 'y'});
testCase.verifyEqual(fMessyA.gridName, {'a'});
testCase.verifyEqual(fMessyA.grid, {linspace(0, 1, 21)});
testCase.verifyEqual(fMessyYY.gridName, {'y', 'bli'});
testCase.verifyEqual(fMessyYY.grid, {linspace(0, 1, 15), linspace(0, 1, 21)});
testCase.verifyEqual(fMessyA.grid, {linspace(0, 1, 11)'});
testCase.verifyEqual(fMessyYY.gridName, {'y', 'xi'});
testCase.verifyEqual(fMessyYY.grid, {linspace(0, 1, 11)', linspace(0, 1, 3)'});
% % sub multiple numerics -> not implemented yet
% f11 = f.subs('x', [1; 2]);
% f1 = f.subs('x', 1);
......
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