Skip to content

Commit

Permalink
Changed Functor into its standalone class.
Browse files Browse the repository at this point in the history
  • Loading branch information
ssun30 authored and speth committed May 11, 2023
1 parent 9e64da1 commit 3c822e4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
28 changes: 18 additions & 10 deletions interfaces/matlab_experimental/Func/Func.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@
% helper function to pass the correct parameters to the C
% library
if itype < 20
[m, n] = size(p);
lenp = m * n;
[msize, nsize] = size(p);
lenp = msize * nsize;
nn = callct('func_new', itype, n, lenp, p);
elseif itype < 45
m = n;
m = p;
nn = callct('func_new', itype, n, m, 0);
else
nn = callct('func_new', itype, n, 0, p);
Expand Down Expand Up @@ -164,6 +164,12 @@ function display(f)
% :return:
% Returns the value of the function evaluated at ``s``
%

if length(s) > 1
aa = eval(['a.', s(1).subs]);
b = subsref(aa, s(2:end));
return
end
if strcmp(s.type, '()')
ind = s.subs{:};
b = zeros(1, length(ind));
Expand All @@ -172,12 +178,14 @@ function display(f)
b(k) = callct('func_value', a.id, ind(k));
end

elseif strcmp(s.type, '.')
b = eval(['a.', s.subs]);
else error('Specify value for x as p(x)');
end

end

function s = get.char(f)
function s = char(f)
% Get the formatted string to display the function.
%
% s = f.char
Expand All @@ -188,17 +196,17 @@ function display(f)
% Formatted string displaying the function
%
if strcmp(f.typ, 'sum')
s = ['(' char(f.f1) ') + (' char(f.f2) ')'];
s = ['(' (f.f1.char) ') + (' f.f2.char ')'];
elseif strcmp(f.typ, 'diff')
s = ['(' char(f.f1) ') - (' char(f.f2) ')'];
s = ['(' f.f1.char ') - (' f.f2.char ')'];
elseif strcmp(f.typ, 'prod')
s = ['(' char(f.f1) ') * (' char(f.f2) ')'];
s = ['(' f.f1.char ') * (' f.f2.char ')'];
elseif strcmp(f.typ, 'ratio')
s = ['(' char(f.f1) ') / (' char(f.f2) ')'];
elseif all(p.coeffs == 0)
s = ['(' f.f1.char ') / (' f.f2.char ')'];
elseif all(f.coeffs == 0)
s = '0';
elseif strcmp(f.typ, 'polynomial')
d = length(p.coeffs) - 1;
d = length(f.coeffs) - 1;
s = [];
nn = 0;

Expand Down
14 changes: 10 additions & 4 deletions interfaces/matlab_experimental/Func/gaussian.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function g = gaussian(peak, center, width)
% Create a Gaussian :mat:func:`Func` instance.
classdef gaussian < Func
% Gaussian - Create a Gaussian :mat:func:`Func` instance.
%
% g = gaussian(peak, center, width)
% f = gaussian(peak, center, width)
%
% :param peak:
% The peak value
Expand All @@ -12,6 +12,12 @@
% function at center +/- (width)/2 is one-half
% the peak value.
%
methods

function f = gaussian(peak, center, width)
f = f@Func('gaussian', 0, [peak, center, width]);
end

end

g = Func('gaussian', 0, [peak, center, width]);
end
2 changes: 1 addition & 1 deletion samples/matlab_experimental/test_examples.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
isentropic();
reactor1();
reactor2();
surfreactor;
surf_reactor;
periodic_cstr;
plug_flow_reactor;
lithium_ion_battery
Expand Down

0 comments on commit 3c822e4

Please sign in to comment.