Commit 5466f8ca authored by Ferdinand Fischer's avatar Ferdinand Fischer
Browse files

Changed quantity.Discrete/changeGrid to be protected. Furhter fixes are required: see #34

parent 04f4467a
function value = ensureIsCell(value) function value = ensureIsCell(value, optArg)
%ENSUREISCELL ensures that the value is a cell. %ENSUREISCELL ensures that the value is a cell.
% c = ensureIsCell(value) checks if value is a cell. If it is not a cell, % c = ensureIsCell(value) checks if value is a cell. If it is not a cell,
% it is converted as a cell. % it is converted as a cell.
arguments
value
optArg.stringArray2cell (1,1) logical = false;
end
if ~iscell(value) if ~iscell(value)
value = {value}; if optArg.stringArray2cell
end if isstring(value)
value = num2cell(value);
else
value = misc.ensureIsCell(value);
end
else
value = {value};
end
end
end end
...@@ -958,7 +958,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ... ...@@ -958,7 +958,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
gridName2Replace = {gridName2Replace.name}; gridName2Replace = {gridName2Replace.name};
elseif nargin == 3 elseif nargin == 3
gridName2Replace = misc.ensureIsCell(gridName2Replace); gridName2Replace = misc.ensureIsCell(gridName2Replace, "stringArray2cell", true);
for k = 1:numel( gridName2Replace ) for k = 1:numel( gridName2Replace )
if isa(gridName2Replace{k}, 'quantity.Domain') if isa(gridName2Replace{k}, 'quantity.Domain')
gridName2Replace{k} = gridName2Replace{k}.name; gridName2Replace{k} = gridName2Replace{k}.name;
...@@ -1370,68 +1370,6 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ... ...@@ -1370,68 +1370,6 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
'name', "flip(" + a(1).name + ")"); 'name', "flip(" + a(1).name + ")");
end % flipGrid() end % flipGrid()
function newObj = changeGrid(obj, gridNew, gridNameNew)
% CHANGEGRID change the grid of the quantity.
% newObj = CHANGEGRID(obj, gridNew, gridNameNew)
% change the grid of the obj quantity. The order of grid and
% 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)'
if isempty(obj)
newObj = obj.copy();
return;
end
if isa(gridNew, 'quantity.Domain')
gridNameNew = [gridNew.name];
gridNew = {gridNew.grid};
else
gridNameNew = misc.ensureString(gridNameNew);
gridNew = misc.ensureIsCell(gridNew);
for it = 1:numel(gridNew)
assert( isnumeric( [gridNew{it}] ), "The gridNew parameter must be a cell array of numeric arrays." )
end
end
if obj(1).isConstant
newDomain(1:length( gridNew )) = quantity.Domain();
for it = 1 : length(gridNew)
newDomain(it) = ...
quantity.Domain(gridNameNew(it), gridNew{it});
end
else
gridIndexNew = obj(1).domain.gridIndex(gridNameNew);
% initialization of the newDomain array as quantity.Domain
% array. This is required in order to handle also
% quantity.EquidistantDomains:
newDomain(1:obj(1).nargin) = quantity.Domain();
newDomain(:) = obj(1).domain;
for it = 1 : length(gridIndexNew)
newDomain(gridIndexNew(it)) = ...
quantity.Domain(gridNameNew(it), gridNew{it});
end
assert(isequal([newDomain.name], obj(1).gridName), ...
'rearranging grids failed');
end
newObj = obj.copy();
[newObj.domain] = deal(newDomain);
for it = 1 : numel(obj)
newObj(it).valueDiscrete = obj(it).on(newDomain);
end
end % changeGrid()
function newObj = replaceGrid(obj, myNewDomain, optArgs) function newObj = replaceGrid(obj, myNewDomain, optArgs)
% CHANGEGRID change the grid of the quantity. % CHANGEGRID change the grid of the quantity.
% newObj = REPLACEGRID(obj, MYNEWDOMAIN, "gridName", NEWGRIDNAME) % newObj = REPLACEGRID(obj, MYNEWDOMAIN, "gridName", NEWGRIDNAME)
...@@ -2584,7 +2522,67 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ... ...@@ -2584,7 +2522,67 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
end %% (Static) end %% (Static)
methods(Access = protected) methods(Access = protected)
function newObj = changeGrid(obj, gridNew, gridNameNew)
% CHANGEGRID change the grid of the quantity.
% newObj = CHANGEGRID(obj, gridNew, gridNameNew)
% change the grid of the obj quantity. The order of grid and
% 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)'
if isempty(obj)
newObj = obj.copy();
return;
end
if isa(gridNew, 'quantity.Domain')
gridNameNew = [gridNew.name];
gridNew = {gridNew.grid};
else
gridNameNew = misc.ensureString(gridNameNew);
gridNew = misc.ensureIsCell(gridNew);
for it = 1:numel(gridNew)
assert( isnumeric( [gridNew{it}] ), "The gridNew parameter must be a cell array of numeric arrays." )
end
end
if obj(1).isConstant
newDomain(1:length( gridNew )) = quantity.Domain();
for it = 1 : length(gridNew)
newDomain(it) = ...
quantity.Domain(gridNameNew(it), gridNew{it});
end
else
gridIndexNew = obj(1).domain.gridIndex(gridNameNew);
% initialization of the newDomain array as quantity.Domain
% array. This is required in order to handle also
% quantity.EquidistantDomains:
newDomain(1:obj(1).nargin) = quantity.Domain();
newDomain(:) = obj(1).domain;
for it = 1 : length(gridIndexNew)
newDomain(gridIndexNew(it)) = ...
quantity.Domain(gridNameNew(it), gridNew{it});
end
assert(isequal([newDomain.name], obj(1).gridName), ...
'rearranging grids failed');
end
newObj = obj.copy();
[newObj.domain] = deal(newDomain);
for it = 1 : numel(obj)
newObj(it).valueDiscrete = obj(it).on(newDomain);
end
end % changeGrid()
function [valDiscrete] = expandValueDiscrete(obj, newDomain) function [valDiscrete] = expandValueDiscrete(obj, newDomain)
% EXPANDVALUEDISCRETE expand the discrete value on the % EXPANDVALUEDISCRETE expand the discrete value on the
......
...@@ -275,8 +275,8 @@ classdef Symbolic < quantity.Function ...@@ -275,8 +275,8 @@ classdef Symbolic < quantity.Function
if isa(gridName2Replace, 'quantity.Domain') if isa(gridName2Replace, 'quantity.Domain')
if nargin == 2 if nargin == 2
gridName2Replace = {gridName2Replace.name};
values = {gridName2Replace.grid}; values = {gridName2Replace.grid};
gridName2Replace = {gridName2Replace.name};
else else
gridName2Replace = {gridName2Replace.name}; gridName2Replace = {gridName2Replace.name};
end end
...@@ -293,7 +293,7 @@ classdef Symbolic < quantity.Function ...@@ -293,7 +293,7 @@ classdef Symbolic < quantity.Function
return return
end end
values = misc.ensureIsCell(values); values = misc.ensureIsCell(values, "stringArray2cell", true);
isNumericValue = false(numel(values), 1); isNumericValue = false(numel(values), 1);
for it = 1 : numel(values) for it = 1 : numel(values)
% check which values are substitutions with numerical values in the domain % check which values are substitutions with numerical values in the domain
......
...@@ -1465,38 +1465,38 @@ testCase.verifyEmpty( e ); ...@@ -1465,38 +1465,38 @@ testCase.verifyEmpty( e );
end end
function testChangeGrid(testCase) % function testChangeGrid(testCase)
%
z = quantity.Domain("z", linspace(0, 1, 21).'); % z = quantity.Domain("z", linspace(0, 1, 21).');
t = quantity.Domain("t", linspace(0, 1, 31)); % t = quantity.Domain("t", linspace(0, 1, 31));
%
quan = quantity.Discrete({sin(z.grid * t.grid.' * pi); cos(z.grid * t.grid.' * pi)}, [z, t]); % quan = quantity.Discrete({sin(z.grid * t.grid.' * pi); cos(z.grid * t.grid.' * pi)}, [z, t]);
gridSampled = {linspace(0, 1, 11), linspace(0, 1, 21)}; % gridSampled = {linspace(0, 1, 11), linspace(0, 1, 21)};
quanCopy = copy(quan); % quanCopy = copy(quan);
%
% test the change of two grids: % % test the change of two grids:
quanSampled = quanCopy.changeGrid(gridSampled, {'z', 't'}); % quanSampled = quanCopy.changeGrid(gridSampled, {'z', 't'});
testCase.verifyEqual(quanSampled.on(), quan.on(gridSampled)) % testCase.verifyEqual(quanSampled.on(), quan.on(gridSampled))
%
% test the change of only one grid: % % test the change of only one grid:
newZ = linspace(0,1,5); % newZ = linspace(0,1,5);
qNewZ = quanCopy.changeGrid( newZ, 'z'); % qNewZ = quanCopy.changeGrid( newZ, 'z');
testCase.verifyEqual(qNewZ.on(), quan.on({newZ, t.grid})); % testCase.verifyEqual(qNewZ.on(), quan.on({newZ, t.grid}));
%
% test the change of grids in wrong order % % test the change of grids in wrong order
quanCopy2 = copy(quan); % quanCopy2 = copy(quan);
quanSampled2 = quanCopy2.changeGrid(gridSampled, {'t', 'z'}); % quanSampled2 = quanCopy2.changeGrid(gridSampled, {'t', 'z'});
testCase.verifyEqual(quanSampled2.on(), permute(quan.on(gridSampled, {'t', 'z'}), [2, 1, 3])); % testCase.verifyEqual(quanSampled2.on(), permute(quan.on(gridSampled, {'t', 'z'}), [2, 1, 3]));
%
% test the change of a quantity.EquidistantDomain with a quantity.Domain % % test the change of a quantity.EquidistantDomain with a quantity.Domain
e = quantity.EquidistantDomain( 'z', 0, 1); % e = quantity.EquidistantDomain( 'z', 0, 1);
d = quantity.Domain('z', linspace(0,1, 3)); % d = quantity.Domain('z', linspace(0,1, 3));
%
E = quantity.Discrete( sin(e.grid), e); % E = quantity.Discrete( sin(e.grid), e);
E_ = E.changeGrid(d); % E_ = E.changeGrid(d);
testCase.verifyEqual(E_.on(), E.on(d)) % testCase.verifyEqual(E_.on(), E.on(d))
%
end % end
function testSubs(testCase) function testSubs(testCase)
%% %%
......
...@@ -46,11 +46,11 @@ function testSubs(testCase) ...@@ -46,11 +46,11 @@ function testSubs(testCase)
f = quantity.Discrete((1:5)', quantity.EquidistantDomain("t", 0, 1, "stepNumber", 5)); f = quantity.Discrete((1:5)', quantity.EquidistantDomain("t", 0, 1, "stepNumber", 5));
testCase.verifyEqual( f.subs("t", 1), f.on(1)) testCase.verifyEqual( f.subs("t", 1), f.on(1))
testCase.verifyEqual( f.changeGrid(quantity.Domain("t", linspace(0,1))).on(), linspace(1,5)', 'AbsTol', 1e-15); testCase.verifyEqual( f.subs(quantity.Domain("t", linspace(0,1))).on(), linspace(1,5)', 'AbsTol', 1e-15);
f = quantity.Symbolic(sym("t"), quantity.EquidistantDomain("t", 0, 1, "stepNumber", 5)); f = quantity.Symbolic(sym("t"), quantity.EquidistantDomain("t", 0, 1, "stepNumber", 5));
testCase.verifyEqual( f.subs("t", 1), f.on(1)) testCase.verifyEqual( f.subs("t", 1), f.on(1))
testCase.verifyEqual( f.changeGrid(quantity.Domain("t", linspace(0,1))).on(), linspace(0,1)', 'AbsTol', 1e-15); testCase.verifyEqual( f.subs(quantity.Domain("t", linspace(0,1))).on(), linspace(0,1)', 'AbsTol', 1e-15);
end end
\ No newline at end of file
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