Julia中矩阵乘法的问题

huangapple go评论80阅读模式
英文:

Trouble with matrix multiplication in Julia

问题

我在Julia中获得相同矩阵乘积的相同答案方面遇到了问题。

我在Jupyter Lab中编写了以下代码:

  1. using LinearAlgebra
  2. function U1(α, M)
  3. return exp * M)
  4. end
  5. function U2(β, M)
  6. return exp * M)
  7. end
  8. test_list = [1 2]
  9. α_list = test_list[1]
  10. β_list = test_list[2]
  11. M1 = [1 3 3; 3 2 2; 2 2 1]
  12. M2 = [3 2 1; 3 0 3; 2 1 1]
  13. init_vec = [1 ; 1 ; 1]
  14. for _ in 1:2
  15. α = α_list[1]
  16. β = β_list[1]
  17. final_vec = U1(α, M1) * U2(β, M2) * init_vec
  18. println(final_vec)
  19. end

我随机设置了test_listinit_vecM1M2的值。如示所示,我得到了看似随机的答案。

可能是什么原因导致这种情况?我应该如何修改代码?

英文:

I'm having trouble with getting the same answer for the same matrix product in Julia.

I'm using Jupyter Lab and have written the following code:

  1. using LinearAlgebra
  2. function U1(α, M)
  3. return exp * M)
  4. end
  5. function U2(β, M)
  6. return exp * M)
  7. end
  8. test_list=[1 2]
  9. α_list = test_list[1]
  10. β_list = test_list[2]
  11. M1=[1 3 3; 3 2 2; 2 2 1]
  12. M2=[3 2 1; 3 0 3; 2 1 1]
  13. init_vec=[1 ; 1 ; 1]
  14. for _ in 1:2
  15. α=α_list[1]
  16. β=β_list[1]
  17. final_vec = U1(α,M1) * U2(β,M2) * init_vec
  18. println(final_vec)
  19. end
  20. #[3.2498540882389616e7, 3.362948386386128e7, 2.2549887512082998e7]
  21. #[3.3668507705110244e7, 3.4777346320714206e7, 2.548964322110533e7]

I randomly set the values of test_list, init_vec, M1, and M2. As shown, I'm getting random-looking answers.

What could be causing this? How should I modify the code?

答案1

得分: 1

Running your code in the REPL I get:

  1. [3.3668507705110244e7, 3.4777346320714206e7, 2.548964322110533e7]
  2. [3.3668507705110244e7, 3.4777346320714206e7, 2.548964322110533e7]

When I replace test_list, M1, M2, and init_vec with appropriately sized random inputs I get:

  1. [1.6282115006640232, 0.9234175545808518, 3.517098945783593]
  2. [1.6282115006640232, 0.9234175545808518, 3.517098945783593]

In other words, I can't reproduce the behavior you are observing. Have you tried to run the exact code you posted in a fresh Julia session?

Edited to add: Based on the comments I also ran your code in an IJulia Jupyter notebook and am getting the following image:

Image description

Note that using LinearAlgebra is not required here; you are not actually using any functions that aren't in Base.

As Oscar says, it's not a good idea to install Julia from sources other than the official binaries, so maybe try that.

英文:

Running your code in the REPL I get:

  1. [3.3668507705110244e7, 3.4777346320714206e7, 2.548964322110533e7]
  2. [3.3668507705110244e7, 3.4777346320714206e7, 2.548964322110533e7]

When I replace test_list, M1, M2, and init_vec with appropriately sized random inputs I get:

  1. [1.6282115006640232, 0.9234175545808518, 3.517098945783593]
  2. [1.6282115006640232, 0.9234175545808518, 3.517098945783593]

In other words, I can't reproduce the behaviour you are observing. Have you tried to run the exact code you posted in a fresh Julia session?


Edited to add: Based on the comments I also ran your code in an IJulia Jupyter notebook and am getting:

Julia中矩阵乘法的问题

Note that using LinearAlgebra is not required here, you are not actually using any functions that aren't in Base.

As Oscar says, it's not a good idea to install Julia from sources other than the official binaries, so maybe try that.

huangapple
  • 本文由 发表于 2023年3月7日 22:16:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定