生成严格对角占优矩阵

发布于 2024-12-25 22:59:17 字数 256 浏览 3 评论 0原文

有没有办法在 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 技术交流群。

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

发布评论

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

评论(2

强辩 2025-01-01 22:59:17

您可以对每行的绝对值求和,并将相应对角线条目的符号乘以其行总和添加到该对角线条目。

In[457]:= SeedRandom[11111];
n = 5;

In[465]:= mat1 = RandomReal[{-100, 100}, {n, n}]

Out[465]= {{-47.2529, 53.4377, 28.6267, 
  69.098, -66.3035}, {71.5837, -38.9932, 66.885, -35.7296, 
  38.6584}, {-55.4822, -45.8442, 52.9929, 55.1683, 
  18.8236}, {12.2189, -47.5637, 36.1517, 88.7082, 
  95.101}, {-87.9987, -44.2326, -7.09374, -16.7852, 42.521}}

In[466]:= mat = 
 mat1 + DiagonalMatrix[(Total /@ Abs[mat1])*Sign[Diagonal[mat1]]]

Out[466]= {{-311.972, 53.4377, 28.6267, 
  69.098, -66.3035}, {71.5837, -290.843, 66.885, -35.7296, 
  38.6584}, {-55.4822, -45.8442, 281.304, 55.1683, 
  18.8236}, {12.2189, -47.5637, 36.1517, 368.452, 
  95.101}, {-87.9987, -44.2326, -7.09374, -16.7852, 241.152}}

这是否足以满足您的目的可能取决于您想要的“随机性”。

You could sum the absolute values of each row, and add sign of corresponding diagonal entry times its row sum to that diagonal entry.

In[457]:= SeedRandom[11111];
n = 5;

In[465]:= mat1 = RandomReal[{-100, 100}, {n, n}]

Out[465]= {{-47.2529, 53.4377, 28.6267, 
  69.098, -66.3035}, {71.5837, -38.9932, 66.885, -35.7296, 
  38.6584}, {-55.4822, -45.8442, 52.9929, 55.1683, 
  18.8236}, {12.2189, -47.5637, 36.1517, 88.7082, 
  95.101}, {-87.9987, -44.2326, -7.09374, -16.7852, 42.521}}

In[466]:= mat = 
 mat1 + DiagonalMatrix[(Total /@ Abs[mat1])*Sign[Diagonal[mat1]]]

Out[466]= {{-311.972, 53.4377, 28.6267, 
  69.098, -66.3035}, {71.5837, -290.843, 66.885, -35.7296, 
  38.6584}, {-55.4822, -45.8442, 281.304, 55.1683, 
  18.8236}, {12.2189, -47.5637, 36.1517, 368.452, 
  95.101}, {-87.9987, -44.2326, -7.09374, -16.7852, 241.152}}

Whether this suffices for your purposes perhaps depends on what you want in terms of "randomness".

无戏配角 2025-01-01 22:59:17

怎么样:

n = 5;
(a = Table[Random[], {n}, {n}]) // MatrixForm
Table[If[i == j, 
   a[[i, j]] = Total[Abs[a[[i, All]]]] - Abs[a[[i, j]]]], {i, 5}, {j, 
   5}];
a // MatrixForm

edit(1)

我在想,为了使上面的内容更加随机,我应该将对角线上生成的元素乘以另一个随机数 > 1. 否则,矩阵并不是真正随机的,因为可以通过对行上所有其他元素求和来计算对角线上的元素是什么。

所以,这是上面的版本 2

  n = 5;
  (a = Table[Random[], {n}, {n}]) // MatrixForm

  Do[
    Do[If[i == j, 
          a[[i, j]] = 
               RandomReal[{1, 10}]*(Total[Abs[a[[i, All]]]]-Abs[a[[i, j]]])
         ], 
      {i, 5}], 
    {j, 5}
   ];

 a // MatrixForm

矩阵仍然不是完全随机的,但至少比以前更加随机:)

edit(2)

喝完咖啡后,我想我应该做以上功能更多!所以我用我认为更 Mathematica/函数式的风格重写了上面的内容(没有明确的 Do 循环)。

因此

scale = 2;
A = Table[RandomReal[], {3}, {3}]
A = ReplacePart[
  A, {{i_, i_}}:> RandomReal[{1, scale}]*(Total@Abs@A[[i, All]]-Abs@A[[i, i]])]

,在 mat 之前,

 {{0.577887, 0.825449, 0.085029}, 
 {0.68226, 0.81484,0.903905}, 
 {0.289007, 0.642185, 0.598648}}

在 mat 成为之后,

 {{1.74871, 0.825449, 0.085029}, 
  {0.68226, 2.15998,0.903905}, 
  {0.289007, 0.642185, 1.58928}}

我真的开始喜欢这种函数式编程方式。它还似乎使代码更短,我认为这是一件好事。更少的代码意味着更少的错误机会。

how about:

n = 5;
(a = Table[Random[], {n}, {n}]) // MatrixForm
Table[If[i == j, 
   a[[i, j]] = Total[Abs[a[[i, All]]]] - Abs[a[[i, j]]]], {i, 5}, {j, 
   5}];
a // MatrixForm

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

  n = 5;
  (a = Table[Random[], {n}, {n}]) // MatrixForm

  Do[
    Do[If[i == j, 
          a[[i, j]] = 
               RandomReal[{1, 10}]*(Total[Abs[a[[i, All]]]]-Abs[a[[i, j]]])
         ], 
      {i, 5}], 
    {j, 5}
   ];

 a // MatrixForm

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

scale = 2;
A = Table[RandomReal[], {3}, {3}]
A = ReplacePart[
  A, {{i_, i_}}:> RandomReal[{1, scale}]*(Total@Abs@A[[i, All]]-Abs@A[[i, i]])]

hence, before mat was

 {{0.577887, 0.825449, 0.085029}, 
 {0.68226, 0.81484,0.903905}, 
 {0.289007, 0.642185, 0.598648}}

after mat becomes

 {{1.74871, 0.825449, 0.085029}, 
  {0.68226, 2.15998,0.903905}, 
  {0.289007, 0.642185, 1.58928}}

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.

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