Commit 7484da00 authored by Ferdinand Fischer's avatar Ferdinand Fischer
Browse files

Pending commit in the fix of quantity.Function handling.

parent 49d24186
......@@ -200,8 +200,10 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
args = obj(1).optArgList;
for k = 1:numel(obj)
F = griddedInterpolant(obj(k).grid{:}', obj(k).on());
o(k) = quantity.Function(@(varargin) F(varargin{:}), obj(1).domain, args{:});
F = griddedInterpolant(obj(k).domain.ndgrid{:}, obj(k).on());
functionArguments = strjoin([obj(1).domain.name], ",");
functionHandle = eval("@(" + functionArguments + ") F(" + functionArguments + ")");
o(k) = quantity.Function(functionHandle, obj(1).domain, args{:});
end
o = reshape(o, size(obj));
......@@ -1389,6 +1391,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete ...
end
if isa(gridNew, 'quantity.Domain')
assert(nargin == 2, "quantity:Discrete:changeGrid", "If a domain is specified in the first argument, the gridNameNew argument can not be set")
gridNameNew = [gridNew.name];
gridNew = {gridNew.grid};
else
......
......@@ -38,6 +38,9 @@ classdef Function < quantity.Discrete
% verify that all arguments of each 'fun' are consitent with the domain names
for k = 1:numel(fun)
names = misc.functionArguments(fun{k});
% #fixme numel(myDomain.find(names)) does not work on empty domains, since find
% throws an error if no domain is found. So behaviour of quantity.Domain/find
% needs to be discussed.
assert( numel(myDomain) == numel(names) & ...
numel(myDomain) == numel(myDomain.find(names)), ...
'quantity:Function:domain', ...
......@@ -97,8 +100,23 @@ classdef Function < quantity.Discrete
% domain
value = cell(size(obj));
ndGrd = myDomain.ndgrid;
sortedNdGrid = cell(numel(myDomain), 1);
domainNames = [myDomain.name];
for k = 1:numel(obj)
tmp = obj(k).evaluateFunction( ndGrd{:} );
% verify that the order of the grid is the same as of the function arguments:
functionArguments = misc.functionArguments( obj(k).valueContinuous );
logIdx = cell(numel(functionArguments), 1);
for it = 1:numel(functionArguments)
logIdx{it} = contains(domainNames, functionArguments(it));
sortedNdGrid{it} = ndGrd{ logIdx{it} };
end
tmp = obj(k).evaluateFunction( sortedNdGrid{:} );
value{k} = tmp(:);
end
value = reshape( cell2mat(value), [ gridLength(myDomain), size(obj)]);
......
......@@ -848,13 +848,19 @@ classdef Symbolic < quantity.Function
end
end % getVariable()
function [f, argList] = setValueContinuous(f, symVar)
function [f, functionArguments] = setValueContinuous(f, symVar)
% converts symVar into a matlabFunction.
argList = string(symVar);
if isa(f, 'sym')
if isempty(symvar(f))
fDouble = double(f);
f = eval("@(" + argList + ") fDouble + quantity.Function.zero(" + argList + ")");
% convert the name of the symbolic variables into a comma separated list and
% then create a function handle by building the function string and evaluate it
% in matlab. That the function returns the right dimension is ensured by the
% "zero" function.
functionArguments = strjoin( string(symVar), ",");
f = eval("@(" + functionArguments + ") " + ...
"double(f) + quantity.Function.zero(" + functionArguments + ")");
else
if iscell( symVar )
symVar = [symVar{:}];
......
......@@ -310,10 +310,14 @@ end
function testCastDiscrete2Function(testCase)
z = quantity.Domain("z", linspace(0, 2*pi)');
d = quantity.Discrete({sin(z.grid); cos(z.grid)}, z);
t = quantity.Domain("t", 0:5);
dom = [z,t];
d = quantity.Discrete({sin(dom.ndgrid{1}); cos(dom.ndgrid{1})}, dom);
f = quantity.Function(d);
testCase.verifyTrue(all(size(f) == size(d)));
testCase.verifyEqual(f.on(), d.on());
end
......@@ -335,9 +339,7 @@ q = quantity.Discrete(sin(t) .* cos(t'), [domA, domB]);
q1V = q.valueDiscrete;
q.sort('descend');
q2V = q.valueDiscrete;
q2V = q.sort('descend').valueDiscrete;
tc.verifyEqual(q1V , q2V')
......
......@@ -6,7 +6,16 @@ end
function testChangeGrid(tc)
z = quantity.Domain("z", 0:5);
t = quantity.Domain("t", 0:4);
a = quantity.Function(@(z,t) z + t*0, [z, t]);
b = a.sort;
tc.verifyEqual( a.on([z,t]), b.on([z,t]));
c = a.changeGrid( linspace(0,5), "z");
%d = a.changeGrid( [t, z], {linspace(0,1), linspace(0,1)});
end
......@@ -156,6 +165,8 @@ t = quantity.Domain("t", 0:4);
a = quantity.Function(@(z,t) z + t, [z, t]);
b = quantity.Function(@(t,z) z + t, [z, t]);
testCase.verifyError(@() quantity.Function(@(z,t,x) z + t, [z,t]), 'quantity:Function:domain')
testCase.verifyError(@() quantity.Function({ @(z,t) z + t, @(z) z, @(z,t,x) z + t + x}, [z,t]), 'quantity:Function:domain')
......
......@@ -867,6 +867,17 @@ testCase.verifyTrue(q.isConstant());
p = quantity.Symbolic([0 0 0 0 z], myDomain);
testCase.verifyFalse(p.isConstant());
%%
myDomains = [myDomain, myDomain.rename("t")];
Q = quantity.Symbolic(sym(magic(2)), myDomains);
Qon = Q.on();
magic2 = magic(2);
for k = 1:numel(magic2)
testCase.verifyTrue( all( Qon(:,:,k) == magic2(k), 'all') )
end
end
function testIsConstant(testCase)
......
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