Ruby 中的有限矩阵
为什么 Matrix 类没有方法来编辑其向量和分量?似乎矩阵内的所有内容都可以读取但不能写入。我错了吗?是否有一些第三方优雅的类似矩阵的类允许我删除行并有意编辑它们?
如果没有这样的课程,请通知我 - 我将停止搜索。
How come the Matrix
class has no methods to edit its vectors and components? It seems like everything inside a matrix can be read but not written. Am I mistaken? Is there some third-party elegant Matrix
-like class that would allow me to delete rows and intentionally edit them?
Please, notify me if there is no such class – I will stop searching.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Matrix 类的设计者一定是不可变数据结构和函数式编程的爱好者。是的,你是对的。
无论如何,都有一个简单的解决方案可以满足您的需求。使用
Matrix
来实现它的功能,然后,只需使用.to_a
来获取一个真正的数组。另请参阅数值 Ruby Narray。您还可以对类进行猴子补丁以添加更多行为。如果您这样做,请修补 Matrix 的子类。 (有些 Ruby 库项目希望从必需的类中获得更多行为,因此他们直接修改它们,从而使他们的新文件有些有毒。他们可以很容易地只修补子类或单例类。)
哦,khelll (:-) 可能会想让我说,你很可能有一种方法可以做到这一点你想要函数式风格。也就是说,通过创建新对象而不是修改旧对象。
The designer of class
Matrix
must have been a fan of immutable data structures and functional programming. Yes, you are correct.In any case, there is a simple solution for what you want. Use
Matrix
for what it can do, then, just use.to_a
to get a real array.See also Numerical Ruby Narray. You could also monkeypatch the class to add more behavior. If you do this, please patch a subclass of Matrix. (There are Ruby library projects out there that want more behavior from
require
d classes so they directly modify them, making their new files somewhat toxic. They could have so easily just patched a subclass or singleton class.)Oh, and khelll (:-) would probably like me to say that there is quite possibly a way for you to do what you want in a functional style. That is, by creating new objects rather than by modifying the old ones.