フィルターのクリア

how to connect different classes?

3 ビュー (過去 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 Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by