Ruby中有限的矩阵 [英] Limited matrices in Ruby
问题描述
为什么Matrix
类没有方法来编辑其向量和成分?矩阵中的所有内容似乎都可以读取但不能写入.我错了吗?是否有一些类似于第三方的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.
推荐答案
类Matrix
的设计者必须是不变数据结构和函数式编程的爱好者.是的,您是正确的.
The designer of class Matrix
must have been a fan of immutable data structures and functional programming. Yes, you are correct.
在任何情况下,都有您想要的简单解决方案.使用Matrix
可以执行的操作,然后只需使用.to_a
即可获取真实数组.
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.
>> Matrix.identity(2).to_a
=> [[1, 0], [0, 1]]
另请参见数字Ruby Narray .您也可以在该类上添加猴子补丁以添加更多行为.如果这样做,请修补Matrix的子类. (那里的Ruby库项目希望从require
d类获得更多的行为,因此它们可以直接对其进行修改,从而使它们的新文件有毒.可以很容易地修补子类或单例类.)
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.)
哦,和 khelll (:-)可能想让我说,您很有可能找到一种方法以功能样式完成您想要的事情.也就是说,通过创建新对象而不是通过修改旧对象.
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.
这篇关于Ruby中有限的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!