.= Julia 中的运算符
.= operator in Julia
代码取自:https://tutorials.sciml.ai/html/models/01-classical_physics.html
如下所示:
# Simple Harmonic Oscillator Problem
using OrdinaryDiffEq, Plots
# Parameters
ω = 1
# Initial Conditions
x₀ = [0.0]
dx₀ = [π/2]
tspan = (0.0, 2π)
ϕ = atan((dx₀[1]/ω)/x₀[1])
A = √(x₀[1]^2 + dx₀[1]^2)
# Define the problem
function harmonicoscillator(ddu,du,u,ω,t)
ddu .= -ω^2 * u
end
# Pass to solvers
prob = SecondOrderODEProblem(harmonicoscillator, dx₀, x₀, tspan, ω)
sol = solve(prob, DPRKN6())
# Plot
plot(sol, vars=[2,1], linewidth=2, title ="Simple Harmonic Oscillator", xaxis = "Time", yaxis = "Elongation", label = ["x" "dx"])
plot!(t->A*cos(ω*t-ϕ), lw=3, ls=:dash, label="Analytical Solution x")
plot!(t->-A*ω*sin(ω*t-ϕ), lw=3, ls=:dash, label="Analytical Solution dx")
我不明白 .= 运算符在谐波振荡器函数中的用法。使用 = 给我错误的答案。所以,我想知道 .=
与 =
有何不同?它没有向量化 ddu
因为 RHS 都是标量。
I don't understand the usage of .= operator in the function
harmonicoscillator. [...] It is not vectorizing ddu because RHS is all scalar.
是; u
、du
和 ddu
不是标量,它们是长度为 1 的向量。
你可以问 Julia .=
语法是什么意思:
julia> Meta.@lower a .= b
:($(Expr(:thunk, CodeInfo(
@ none within `top-level scope'
1 ─ %1 = Base.broadcasted(Base.identity, b)
│ %2 = Base.materialize!(a, %1)
└── return %2
))))
看起来有点复杂,但本质上是一个广播作业,类似于
for i in eachindex(a, b)
a[i] = b[i]
end
Using = gives me the wrong answer.
是的,因为 DiffEq 库需要函数 harmonicoscillator
来修改输入。如果你只使用 =
你创建一个新的函数局部变量而不是修改输入向量,并且从外部是不可见的。
代码取自:https://tutorials.sciml.ai/html/models/01-classical_physics.html 如下所示:
# Simple Harmonic Oscillator Problem
using OrdinaryDiffEq, Plots
# Parameters
ω = 1
# Initial Conditions
x₀ = [0.0]
dx₀ = [π/2]
tspan = (0.0, 2π)
ϕ = atan((dx₀[1]/ω)/x₀[1])
A = √(x₀[1]^2 + dx₀[1]^2)
# Define the problem
function harmonicoscillator(ddu,du,u,ω,t)
ddu .= -ω^2 * u
end
# Pass to solvers
prob = SecondOrderODEProblem(harmonicoscillator, dx₀, x₀, tspan, ω)
sol = solve(prob, DPRKN6())
# Plot
plot(sol, vars=[2,1], linewidth=2, title ="Simple Harmonic Oscillator", xaxis = "Time", yaxis = "Elongation", label = ["x" "dx"])
plot!(t->A*cos(ω*t-ϕ), lw=3, ls=:dash, label="Analytical Solution x")
plot!(t->-A*ω*sin(ω*t-ϕ), lw=3, ls=:dash, label="Analytical Solution dx")
我不明白 .= 运算符在谐波振荡器函数中的用法。使用 = 给我错误的答案。所以,我想知道 .=
与 =
有何不同?它没有向量化 ddu
因为 RHS 都是标量。
I don't understand the usage of .= operator in the function harmonicoscillator. [...] It is not vectorizing ddu because RHS is all scalar.
是; u
、du
和 ddu
不是标量,它们是长度为 1 的向量。
你可以问 Julia .=
语法是什么意思:
julia> Meta.@lower a .= b
:($(Expr(:thunk, CodeInfo(
@ none within `top-level scope'
1 ─ %1 = Base.broadcasted(Base.identity, b)
│ %2 = Base.materialize!(a, %1)
└── return %2
))))
看起来有点复杂,但本质上是一个广播作业,类似于
for i in eachindex(a, b)
a[i] = b[i]
end
Using = gives me the wrong answer.
是的,因为 DiffEq 库需要函数 harmonicoscillator
来修改输入。如果你只使用 =
你创建一个新的函数局部变量而不是修改输入向量,并且从外部是不可见的。