相关属性的MATLAB惰性评估 [英] MATLAB Lazy Evaluation in Dependent Property

查看:72
本文介绍了相关属性的MATLAB惰性评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些依赖属性的类,但是我真的只想计算一次.

I have a class with a few properties that are dependent, but that I'd really like to calculate only once.

我已经得出结论,在MATLAB中对依赖类的属性使用惰性求值是不可能的,也不是一个坏主意.最初的计划是为每个需要更新的(公共)属性都拥有一个私有逻辑标志,并让构造函数将其设置为true.然后,当调用属性访问器时,仅当需要时,它将检查该标志并计算值并将其存储(在另一个私有属性中).如果该标志为假,则仅返回缓存值的副本.

I've just about concluded that using lazy evaluation on a dependent class property in MATLAB is either impossible or a bad idea. The original plan was to have a private logical flag for each (public) property that needs updating and to have the constructor set it to true. Then when the property accessor was called, it would check that flag and calculate the value and store it (in another private property) only if required. If the flag were false, it would simply return a copy of the cached value.

我认为困难在于对属性访问者的约束,也就是说,他们不理会其他不相关的属性.换句话说,get.property(self)方法不能更改self对象的状态.有趣的是,这在我当前的课程中无声地失败了. (即,在get.方法中既未设置更新标志,也未设置缓存的计算结果,因此每次都运行昂贵的计算.)

I believe the difficulty lies in a constraint on property accessors, that is, that they leave other unrelated properties alone. In other words, a get.property(self) method can't change the state of the self object. Interestingly, this fails silently in my current class. (I.e., the neither the update flag nor the cached calculation results get set in the get. method, so the expensive calculation is run every time).

我的怀疑是将惰性属性从公共依赖属性更改为具有公共GetAccess但私有SetAccess的方法会起作用.但是,我不喜欢以这种方式欺骗属性约定.我希望只有一个惰性"属性属性可以为我完成所有这些工作.

My suspicion is that changing the lazy property from a public dependent property to a method with public GetAccess but private SetAccess would work. However, I don't like having to spoof the property convention in this way. I wish there were just a "lazy" property attribute that could do all this for me.

我缺少明显的东西吗?是否禁止在MATLAB中使用用于依赖类属性的访问器方法来更改类实例的状态?如果是这样,定义具有私有副作用的访问器是获得我想要的行为的最不邪恶的方法吗?

Am I missing something obvious? Are accessor methods for dependent class properties in MATLAB forbidden to change the class instance's state? If so, is defining what amounts to an accessor with a private side effect the least evil way of getting the behavior I want?

这是一个测试类...

here's a test class...

classdef LazyTest
  properties(Access = public)
    % num to take factorial of
    factoriand
  end

  properties(Access = public, Dependent)
    factorial
  end

  properties(Access = private)
    % logical flag
    do_update_factorial
    % old result
    cached_factorial
  end

  methods
    function self = LazyTest(factoriand)
      self.factoriand = factoriand;
      self.do_update_factorial = true;
    end
  end

  methods
    function result = get.factorial(self)
      if self.do_update_factorial
        self.cached_factorial = factorial(self.factoriand);
        % pretend this is expensive
        pause(0.5)
        self.do_update_factorial = false
      end
      result = self.cached_factorial;
    end
  end
end

使用

close all; clear classes; clc

t = LazyTest(3)
t.factorial

for num = 1:10
  tic
  t.factoriand = num
  t.factorial
  toc
end

handle继承后,时间大大减少了.

After inheriting from handle the time drops substantially.

推荐答案

我假设您正在使用值类.使用处理类(classdef myClass < handle),通过引用,您可以轻松地从get方法中修改类.例如,我使用您建议的内容来从文件(如果尚未加载)或私有的隐藏属性中加载数据.

I assume you're using a value class. With a handle class (classdef myClass < handle), which is passed by reference, you can easily modify the class from within a get-method. For example, I use what you propose in order to load data from file (if not yet loaded) or from a private, hidden property.

请注意,使用lazy依赖属性以某种方式提出的建议无法达到使用依赖属性的目的,即保证您的数据始终与所衍生属性的状态保持最新.每次更改其他属性时,您的惰性属性都会过时.

Note that using a lazy dependent property the way you propose somewhat defeats the purpose of using a dependent property, i.e. the guarantee that your data is always up-to-date with the state of the properties it is derived from. Each time you change the other properties, your lazy property gets outdated.

您可以(应该)向所有其他属性添加设置方法,以将私有属性设置为空(isempty(obj.myPrivateProperty)是您需要知道是否必须计算的逻辑标志").但是,如果这样做,为什么不只是让set-methods调用一些update方法来立即更新/重新计算所有依赖"属性呢?

You could (should) add a set-method to all other properties that sets the private property to empty (isempty(obj.myPrivateProperty) is the "logical flag" you need to know whether you have to calculate). But if you do that, why not just have the set-methods call some update method that updates/recalculates all "dependent" properties right away?

这篇关于相关属性的MATLAB惰性评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆