Ruby 中的有限矩阵

发布于 2024-08-06 04:32:04 字数 123 浏览 3 评论 0原文

为什么 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 技术交流群。

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

发布评论

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

评论(1

桃气十足 2024-08-13 04:32:04

Matrix 类的设计者一定是不可变数据结构和函数式编程的爱好者。是的,你是对的。

无论如何,都有一个简单的解决方案可以满足您的需求。使用Matrix来实现它的功能,然后,只需使用.to_a来获取一个真正的数组。

>> Matrix.identity(2).to_a
=> [[1, 0], [0, 1]]

另请参阅数值 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.

>> Matrix.identity(2).to_a
=> [[1, 0], [0, 1]]

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

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