将 Ruby 1.9.2 与 RubyMine 和 Matrix 结合使用
我正在使用 ruby 1.9.2-p290 和 RubyMine。我尝试使用矩阵(需要“矩阵”)。所以,我有几个问题。
- 我怎样才能改变矩阵的任何值?
例如:
require 'matrix'
matrix = Matrix[[1, -2, 3], [3, 4, -5], [2, 4, 1]]
matrix[0, 0] = 5
p matrix
接下来给出:
in `<top (required)>': private method `[]=' called for Matrix[[1, -2, 3], [3, 4, -5], [2, 4, 1]]:Matrix (NoMethodError)
from -e:1:in `load'
from -e:1:in `<main>'
- 是否可以在 RubyMine IDE 中通过代码补全向我展示矩阵的方法?
- 我应该使用什么库来存储矩阵?矩阵?马特恩?还有别的事吗?
I am using ruby 1.9.2-p290 and RubyMine. And i try to use Matrix (require 'matrix'). So, i have few questions.
- How can i change any value of matrix?
For example:
require 'matrix'
matrix = Matrix[[1, -2, 3], [3, 4, -5], [2, 4, 1]]
matrix[0, 0] = 5
p matrix
Gives next:
in `<top (required)>': private method `[]=' called for Matrix[[1, -2, 3], [3, 4, -5], [2, 4, 1]]:Matrix (NoMethodError)
from -e:1:in `load'
from -e:1:in `<main>'
- Is it possible to show me methods for matrix by code completion in RubyMine IDE?
- What library(s) should i use for matrices? Matrix? Mathn? Something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
广告 1)我知道文档说
[]=
是一个公共实例方法,1.9.2 中的现实似乎与此不符:我看到了两种解决方法。第一个是使用
send
绕过private
:第二个是遍历数组:
如果您确实想要,您可以更改方法的可见性:
请注意,我不这样做不鼓励任何这些,类的实现方式强烈暗示作者认为矩阵是不可变的对象。
广告 2) 不幸的是,我不知道 RubyMine,但是可以找到
Matrix
类的文档 此处。Ad 3) 我还没有在 Ruby 中广泛使用矩阵,但是对于我所需要的,
Matrix
类就足够了。Ad 1) I know the documentation says that
[]=
is a public instance method, reality in 1.9.2 does not seem to match that:I see two ways around this. The first is using
send
to bypassprivate
:The second is going through an array:
If you really wanted to, you could change the visibility of the method:
Note that I don't encourage any of these, the way the class is implemented is a strong hint that the authors consider matrices to be immutable objects.
Ad 2) I don't know RubyMine unfortunately, but the documentation for the
Matrix
class can be found here.Ad 3) I haven't had an extensive use for matrices in Ruby yet, but for what I needed them the
Matrix
class was good enough.只是想补充迈克尔的答案:
1)矩阵库的设计使得矩阵是不可变的,就像你不能设置复数的实部一样。
我是该库的维护者(但不是原作者)。不过,我承认如果它们是可变的,它可能会很有用。在 Ruby 1.9.3 中更改它已经太晚了,但我希望检查一下使它们可变的后果。
3) 另一种可能性是检查
NArray
库。Just wanted to supplement Michael's answer:
1) The Matrix library has been designed such that Matrices are immutable, the same way that you can't set the real part of Complex number.
I'm the maintainer of the library (but not the original author). I admit it would probably be useful if they were mutable, though. It's too late to change it for Ruby 1.9.3, but I hope to check about consequences for making them mutable.
3) Another possibility is to check the
NArray
library.