Commit 8b5d9b87 authored by Ferdinand Fischer's avatar Ferdinand Fischer
Browse files

Added converter of quantity.Discrete to quantity.Function: a linear...

Added converter of quantity.Discrete to quantity.Function: a linear interpolation is used as valueContin$
parent b66c5afa
......@@ -222,7 +222,15 @@ classdef (InferiorClasses = {?quantity.Symbolic, ?quantity.SymbolicII}) Discret
function d = double(obj)
d = obj.on();
end
function o = quantity.Function(obj)
props = nameValuePair( obj(1) );
for k = 1:numel(obj)
F = griddedInterpolant(obj(k).grid{:}', obj(k).on());
o(k) = quantity.Function(@(varargin) F(varargin{:}), ...
props{:});
end
end
function o = quantity.Operator(obj)
A = cell(size(obj, 3), 1);
for k = 1:size(obj, 3)
......@@ -1051,7 +1059,7 @@ classdef (InferiorClasses = {?quantity.Symbolic, ?quantity.SymbolicII}) Discret
end % plot()
function s = nameValuePair(obj, varargin)
assert(numel(obj) == 1, 'nameValuePair must not be called for an array object');
assert(numel(obj) == 1, 'nameValuePair must NOT be called for an array object');
s = struct(obj);
if ~isempty(varargin)
s = rmfield(s, varargin{:});
......@@ -1213,6 +1221,7 @@ classdef (InferiorClasses = {?quantity.Symbolic, ?quantity.SymbolicII}) Discret
zeta = sym(gridName2, 'real');
f0 = expm(obj.atIndex(1)*(z - zeta));
F = quantity.Symbolic(f0, 'grid', {myGrid, myGrid});
elseif isa(obj, 'quantity.Symbolic')
f0 = misc.fundamentalMatrix.odeSolver_par(obj.function_handle, myGrid);
F = quantity.Discrete(f0, ...
......@@ -1628,10 +1637,11 @@ classdef (InferiorClasses = {?quantity.Symbolic, ?quantity.SymbolicII}) Discret
end
function result = diff(obj, k, diffGridName)
% diff applies the kth-derivative for the variable specified
% with the input gridName to the obj. If no gridName is
% specified, then diff applies the derivative w.r.t. to all
% gridNames.
% DIFF computation of the derivative
% result = DIFF(obj, k, diffGridName) applies the
% 'k'th-derivative for the variable specified with the input
% 'diffGridName' to the obj. If no 'diffGridName' is specified,
% then diff applies the derivative w.r.t. to all gridNames.
if nargin == 1 || isempty(k)
k = 1; % by default, only one derivatve per diffGridName is applied
else
......@@ -1734,8 +1744,11 @@ classdef (InferiorClasses = {?quantity.Symbolic, ?quantity.SymbolicII}) Discret
end
[idxGrid, isGrid] = obj.gridIndex(intGridName);
if nargin == 4
assert(all((varargin{2} == a(idxGrid)) & (varargin{3} == b(idxGrid))), ...
'only integration from beginning to end of domain is implemented');
I = cumInt( obj, varargin{:});
return;
%
% assert(all((varargin{2} == a(idxGrid)) & (varargin{3} == b(idxGrid))), ...
% 'only integration from beginning to end of domain is implemented');
end
%% do the integration over one dimension
......@@ -1828,7 +1841,11 @@ classdef (InferiorClasses = {?quantity.Symbolic, ?quantity.SymbolicII}) Discret
if ~isa(A, 'quantity.Discrete')
A = quantity.Discrete(A, 'name', 'c', 'gridName', B(1).gridName);
elseif ~isa(B, 'quantity.Discrete')
B = quantity.Discrete(B, 'name', 'c', 'gridName', A(1).gridName);
if isnumeric(B)
B = quantity.Discrete(B, 'name', 'c', 'gridName', {});
else
B = quantity.Discrete(B, 'name', 'c', 'gridName', A(1).gridName, 'grid', A.grid);
end
end
% combine both domains with finest grid
......
......@@ -60,11 +60,15 @@ classdef Function < quantity.Discrete
function f = function_handle(obj)
for k = 1:numel(obj)
F{k} = @(varargin) obj(k).valueContinuous(varargin{:});
if numel(obj) == 1
f = @obj.valueContinuous;
else
for k = 1:numel(obj)
F{k} = @(varargin) obj(k).valueContinuous(varargin{:});
end
f = @(varargin) reshape((cellfun( @(c) c(varargin{:}), F)), size(obj));
end
f = @(varargin) reshape((cellfun( @(c) c(varargin{:}), F)), size(obj));
end
%-----------------------------
......
......@@ -3,7 +3,13 @@ classdef SymbolicII < quantity.Symbolic
methods
function obj = SymbolicII(valueContinuous, varargin)
obj@quantity.Symbolic(valueContinuous, varargin{:});
if nargin == 0
parentArgin = {};
else
parentArgin = [{valueContinuous}, varargin(:)'];
end
obj@quantity.Symbolic(parentArgin{:});
end
end % methods
......
......@@ -594,9 +594,9 @@ end
function testCumInt(testCase)
t = linspace(pi, 1.1*pi, 51)';
s = t;
[T, S] = ndgrid(t, t);
tt = linspace(pi, 1.1*pi, 51)';
s = tt;
[T, S] = ndgrid(tt, tt);
syms sy tt
......@@ -618,23 +618,23 @@ end
%% int_0_t a(t,s) * b(s) ds
% compute symbolic version of the volterra integral
v = int(a*b, sy, t(1), tt);
V = quantity.Symbolic(v, 'grid', t, 'gridName', 'tt');
v = int(a*b, sy, tt(1), tt);
V = quantity.Symbolic(v, 'grid', tt, 'gridName', 'tt');
V = V.subs('tt', 't');
% compute the numeric version of the volterra integral
K = quantity.Discrete(A, 'size', size(a), 'grid', {t, s}, 'gridName', {'t', 's'});
k = quantity.Discrete(A, 'size', size(a), 'grid', {tt, s}, 'gridName', {'t', 's'});
x = quantity.Discrete(B, 'size', size(b), 'grid', {s}, 'gridName', 's');
f = cumInt(K * x, 's', s(1), 't');
f = cumInt(k * x, 's', s(1), 't');
testCase.verifyEqual(V.on(), f.on(), 'AbsTol', 1e-5);
%% int_s_t a(t,s) * b(s) ds
v = int(a*b, sy, sy, tt);
V = quantity.Symbolic(subs(v, {tt, sy}, {'t', 's'}), 'grid', {t, s});
V = quantity.Symbolic(subs(v, {tt, sy}, {'t', 's'}), 'grid', {tt, s});
f = cumInt(K * x, 's', 's', 't');
f = cumInt(k * x, 's', 's', 't');
testCase.verifyEqual( f.on(f(1).grid, f(1).gridName), V.on(f(1).grid, f(1).gridName), 'AbsTol', 1e-5 );
......@@ -644,18 +644,18 @@ C = zeros([size(T), size(c)]);
for i = 1:numel(c)
C(:,:,i) = double(subs(c(i), {tt sy}, {T S}));
end
y = quantity.Discrete(C, 'size', size(c), 'grid', {t s}, 'gridName', {'t' 's'});
y = quantity.Discrete(C, 'size', size(c), 'grid', {tt s}, 'gridName', {'t' 's'});
v = int(a*c, sy, sy, tt);
V = quantity.Symbolic(subs(v, {tt, sy}, {'t', 's'}), 'grid', {t, s});
f = cumInt( K * y, 's', 's', 't');
V = quantity.Symbolic(subs(v, {tt, sy}, {'t', 's'}), 'grid', {tt, s});
f = cumInt( k * y, 's', 's', 't');
testCase.verifyEqual( f.on(f(1).grid, f(1).gridName), V.on(f(1).grid, f(1).gridName), 'AbsTol', 1e-5 );
%% testCumIntWithLowerAndUpperBoundSpecified
t = linspace(pi, 1.1*pi, 51)';
s = t;
[T, S] = ndgrid(t, t);
tt = linspace(pi, 1.1*pi, 51)';
s = tt;
[T, S] = ndgrid(tt, tt);
syms sy tt
a = [ 1, sy; tt, 1];
......@@ -668,12 +668,30 @@ for i = 1:size(a,1)
end
% compute the numeric version of the volterra integral
K = quantity.Discrete(A, 'size', size(a), 'grid', {t, s}, 'gridName', {'t', 's'});
k = quantity.Discrete(A, 'size', size(a), 'grid', {tt, s}, 'gridName', {'t', 's'});
fCumInt2Bcs = cumInt(K, 's', 'zeta', 't');
fCumInt2Cum = cumInt(K, 's', t(1), 't') ...
- cumInt(K, 's', t(1), 'zeta');
fCumInt2Bcs = cumInt(k, 's', 'zeta', 't');
fCumInt2Cum = cumInt(k, 's', tt(1), 't') ...
- cumInt(k, 's', tt(1), 'zeta');
testCase.verifyEqual(fCumInt2Bcs.on(), fCumInt2Cum.on(), 'AbsTol', 10*eps);
%% testCumInt with one independent variable
tt = linspace(0, pi, 51)';
syms t
a = [ 1, sin(t); t, 1];
% compute the numeric version of the volterra integral
f = quantity.Symbolic(a, 'grid', {tt}, 'variable', {'t'});
f = quantity.Discrete(f);
intK = cumInt(f, 't', 0, 't');
K = quantity.Symbolic( int(a, 0, t), 'grid', {tt}, 'variable', {'t'});
testCase.verifyEqual(intK.on(), K.on(), 'AbsTol', 1e-3);
end
function testAtIndex(testCase)
......@@ -1000,7 +1018,7 @@ A = int(a);
Anum = [integral2(@(z,t) sin(z.*t), z(1), z(end), t(1), t(end)); ...
integral2(@(z,t) cos(z.*t), z(1), z(end), t(1), t(end))];
verifyTrue(testCase, numeric.near(A.on(), Anum, 1e-2));
verifyTrue(testCase, numeric.near(A, Anum, 1e-2));
end
......
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