生成严格对角占优矩阵
有没有办法在 Mathematica 中生成随机 n × n 严格对角占优? 我使用以下代码生成随机方阵:
A = RandomReal[{-100, 100}, {1000, 1000}]
编辑:我只需要一种方法来生成大型严格对角占优矩阵,行随机性并不重要。
Is there a way to generate a random n by n strictly diagonally dominant in Mathematica?
I use the following code to generate a random square matrix:
A = RandomReal[{-100, 100}, {1000, 1000}]
EDIT: I just need a way to generate a large strictly diagonally dominant matrix, row randomness is not crucial.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以对每行的绝对值求和,并将相应对角线条目的符号乘以其行总和添加到该对角线条目。
这是否足以满足您的目的可能取决于您想要的“随机性”。
You could sum the absolute values of each row, and add sign of corresponding diagonal entry times its row sum to that diagonal entry.
Whether this suffices for your purposes perhaps depends on what you want in terms of "randomness".
怎么样:
edit(1)
我在想,为了使上面的内容更加随机,我应该将对角线上生成的元素乘以另一个随机数 > 1. 否则,矩阵并不是真正随机的,因为可以通过对行上所有其他元素求和来计算对角线上的元素是什么。
所以,这是上面的版本 2
矩阵仍然不是完全随机的,但至少比以前更加随机:)
edit(2)
喝完咖啡后,我想我应该做以上功能更多!所以我用我认为更 Mathematica/函数式的风格重写了上面的内容(没有明确的 Do 循环)。
因此
,在 mat 之前,
在 mat 成为之后,
我真的开始喜欢这种函数式编程方式。它还似乎使代码更短,我认为这是一件好事。更少的代码意味着更少的错误机会。
how about:
edit(1)
I was thinking to myself that to make the above more random, I should multiply the generated elements on the diagonal by another random number > 1. Else, the way it is, the matrix is not really random, as one can figure what the element on the diagonal is by summing all the other elements on the row.
So, here is version 2 of the above
The matrix is still not exactly random, but at least a little more random than before :)
edit(2)
after having coffee, I thought that I should make the above more functional ! So I rewrote the above in what I think is a more Mathematica/functional style (no explicit Do loops).
here it is
hence, before mat was
after mat becomes
I am really starting to like this functional programming way. It also seems to make the code shorter, which is a good thing I think. Less code means less chance of a bug.