将 Ruby 1.9.2 与 RubyMine 和 Matrix 结合使用

发布于 12-02 02:39 字数 548 浏览 1 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(2

酒绊2024-12-09 02:39:06

广告 1)我知道文档说 []= 是一个公共实例方法,1.9.2 中的现实似乎与此不符:

matrix.private_methods.grep(/\[\]/) #=> [:[]=]

我看到了两种解决方法。第一个是使用 send 绕过 private

matrix.send(:[]=, 0, 0, 5) #=> 5

第二个是遍历数组:

m = *matrix
m[0][0] = 5
matrix = Matrix[*m]

如果您确实想要,您可以更改方法的可见性:

matrix.class.class_eval { public :[]= }

请注意,我不这样做不鼓励任何这些,类的实现方式强烈暗示作者认为矩阵是不可变的对象。

广告 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:

matrix.private_methods.grep(/\[\]/) #=> [:[]=]

I see two ways around this. The first is using send to bypass private:

matrix.send(:[]=, 0, 0, 5) #=> 5

The second is going through an array:

m = *matrix
m[0][0] = 5
matrix = Matrix[*m]

If you really wanted to, you could change the visibility of the method:

matrix.class.class_eval { public :[]= }

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.

鱼忆七猫命九2024-12-09 02:39:06

只是想补充迈克尔的答案:

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.

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