英文:
Trouble with matrix multiplication in Julia
问题
我在Julia中获得相同矩阵乘积的相同答案方面遇到了问题。
我在Jupyter Lab中编写了以下代码:
using LinearAlgebra
function U1(α, M)
return exp(α * M)
end
function U2(β, M)
return exp(β * M)
end
test_list = [1 2]
α_list = test_list[1]
β_list = test_list[2]
M1 = [1 3 3; 3 2 2; 2 2 1]
M2 = [3 2 1; 3 0 3; 2 1 1]
init_vec = [1 ; 1 ; 1]
for _ in 1:2
α = α_list[1]
β = β_list[1]
final_vec = U1(α, M1) * U2(β, M2) * init_vec
println(final_vec)
end
我随机设置了test_list
、init_vec
、M1
和M2
的值。如示所示,我得到了看似随机的答案。
可能是什么原因导致这种情况?我应该如何修改代码?
英文:
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:
using LinearAlgebra
function U1(α, M)
return exp(α * M)
end
function U2(β, M)
return exp(β * M)
end
test_list=[1 2]
α_list = test_list[1]
β_list = test_list[2]
M1=[1 3 3; 3 2 2; 2 2 1]
M2=[3 2 1; 3 0 3; 2 1 1]
init_vec=[1 ; 1 ; 1]
for _ in 1:2
α=α_list[1]
β=β_list[1]
final_vec = U1(α,M1) * U2(β,M2) * init_vec
println(final_vec)
end
#[3.2498540882389616e7, 3.362948386386128e7, 2.2549887512082998e7]
#[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:
[3.3668507705110244e7, 3.4777346320714206e7, 2.548964322110533e7]
[3.3668507705110244e7, 3.4777346320714206e7, 2.548964322110533e7]
When I replace test_list
, M1
, M2
, and init_vec
with appropriately sized random inputs I get:
[1.6282115006640232, 0.9234175545808518, 3.517098945783593]
[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:
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:
[3.3668507705110244e7, 3.4777346320714206e7, 2.548964322110533e7]
[3.3668507705110244e7, 3.4777346320714206e7, 2.548964322110533e7]
When I replace test_list
, M1
, M2
, and init_vec
with appropriately sized random inputs I get:
[1.6282115006640232, 0.9234175545808518, 3.517098945783593]
[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:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论