how to connect different classes?

조회 수: 2 (최근 30일)
Patrick Mboma
Patrick Mboma 2014년 7월 26일
댓글: Adam 2014년 8월 18일
Dear all, Suppose I have the following function
function varargout=myfunc(f,varargin)
atmp=cell(500,4);
iter=0;
nf=numel(f);
fval=nan(nf,1);
for ii=1:nf
fval(ii)=f{ii}(varargin{:});
end
function c=plus_(a,b)
id=find(strcmp(atmp(:,1),'+') && [atmp{:,2}]==a && [atmp{:,3}]==b);
if isempty(id)
c=a+b;
iter=iter+1;
atmp(iter,:)={'+',a,b,c};
else
c=atmp{id,end};
end
end
end
This function would not work, but it serves to illustrate the question I would like to ask. The point with this function is that it has a nested function "plus_" and the variables created in the main function (atmp, iter, etc.) are also available to the nested function and the nested function can modify those variables.
In this simple example, "f" is just an array of function handles, e.g. f{1}=@(x,y)exp(x+2*y); the nested function merely adds a and b, but does the actual calculation if a+b has not been computed before. If a+b has been computed before, then the result is just fetched.
For this to work, it should be written in classes overloading operations +, -, *, /, etc. But then how to implement variables atmp and iter such that they are available to the various methods? One solution would be to define atmp and iter as global variables to each of the overloaded methods and initialize it outside the class. A better solution, perhaps, would use handles, notify, events, addlistener, etc. but I do not know how to implement them. Any ideas?
Thanks, P.
  댓글 수: 5
Patrick Mboma
Patrick Mboma 2014년 7월 28일
Per,
Thanks again for the links. I will look into them although I don't feel like learning python just yet.
Adam
Adam 2014년 8월 18일
I'm confused what the question is here. Your title and the actual part of your post with a ? talks about classes yet everything else seems to be related to nested functions.
If you have a class that overloads the + operator and any other operators then properties of the class are available to the overloaded operators in the nature of being a class.

댓글을 달려면 로그인하십시오.

답변 (1개)

Aurele Turnes
Aurele Turnes 2014년 8월 4일
편집: Aurele Turnes 2014년 8월 4일
Like per isakson pointed out, I think that you can achieve this by using memoization techniques. The following page on MATLAB Central gives a pretty good example of how to do this:
In order to keep the same types of variable that you are using, your function could look something like this:
function f = memoize()
%one-arg F, inputs testable with ==
%allow nonscalar input.
atmp = cell(500,4);
iter = 0;
f = @inner;
function out = inner(a,b,F)
% find which in's already computed in x
if iter>0
ind= strcmp(atmp(1:iter,1),'+') & (cell2mat(atmp(1:iter,2))==a) & (cell2mat(atmp(1:iter,3))==b);
if any(ind)
out = atmp{ind,4};
else
out = eval(['a' F 'b']);
iter = iter+1;
atmp(iter,:) = {F, a , b, out};
end
else
iter = iter+1;
out = eval(['a' F 'b']);
atmp(iter,:) = {F, a , b, out};
end
end
end
You can then call it and use it as follows:
f = memoize
f(3,4,'+')
f(3,5,'+')
f(3,4,'+')
f(3,4,'*')
Look at the example function memoize2 in the link above to vectorize this technique.
  댓글 수: 1
Patrick Mboma
Patrick Mboma 2014년 8월 18일
Dear Aurele, I think I fairly well understand the concept of memoization and I use it in other applications. But for this one, my problem remains and comes from the fact that the various inputs are objects of a certain class which overloads the operations +, -, *, /, ^, cos, log, exp, etc.
Thanks

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by