MATLAB 从属属性中的惰性求值

发布于 2024-10-16 20:35:03 字数 1546 浏览 11 评论 0 原文

我有一个类,其中有一些依赖的属性,但我真的只想计算一次。

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

我认为困难在于对属性访问器的限制,即它们不理会其他不相关的属性。换句话说, get.property(self) 方法无法更改 self 对象的状态。有趣的是,这在我当前的课堂上悄然失败了。 (即,更新标志和缓存的计算结果都没有在 get. 方法中设置,因此每次都会运行昂贵的计算)。

我怀疑将惰性属性从公共依赖属性更改为具有公共 GetAccess 但私有 SetAccess 的方法会起作用。然而,我不喜欢以这种方式欺骗财产约定。我希望有一个“惰性”属性可以为我完成这一切。

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

编辑:这是一个测试类...

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

handle 继承后运行它,

close all; clear classes; clc

t = LazyTest(3)
t.factorial

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

时间大大减少。

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

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.

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).

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.

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?

Edit: 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

Run it with

close all; clear classes; clc

t = LazyTest(3)
t.factorial

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

After inheriting from handle the time drops substantially.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爺獨霸怡葒院 2024-10-23 20:35:03

我假设您使用的是值类。使用 句柄类 (classdef myClass ),它是通过引用传递的,您可以轻松地从 get 方法中修改该类。例如,我使用您建议的内容来从文件(如果尚未加载)或从私有​​隐藏属性加载数据。

请注意,使用您建议的惰性依赖属性的方式在某种程度上违背了使用依赖属性的目的,即保证您的数据始终与其派生的属性的状态保持同步从。每次更改其他属性时,您的惰性属性就会过时。

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

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.

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.

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文