Is there a particularly elegant way to eval
a local variable? My use case would be this function:
function A_dipole(which, x, y, z; A₀=19.8517061694#=ų MHz=#)
i, j = (Symbol(_) for _ in String(which))
r = √(x^2 + y^2 + z^2)
return A₀ * (1/r^3 - 3*eval(i)*eval(j)/r^5)
end
where for, e.g. A_dipole(:xy, x, y, z)
, the eval(i)
would evaluate to the value of x
and eval(j)
would evaluate to the value of y
. I can of course define r_i
and r_j
values like
function A_dipole(which, x, y, z; A₀=19.8517061694#=ų MHz=#)
i, j = (Symbol(_) for _ in String(which))
r_i = (i == :x) ? x : (i == :y) ? y : (i == :z) ? z : 0.0
r_j = (j == :x) ? x : (j == :y) ? y : (j == :z) ? z : 0.0
r = √(x^2 + y^2 + z^2)
return A₀ * (1/r^3 - 3*r_i*r_j/r^5)
end
but I was wondering if Julia provided any neat way that I’m not aware of to evaluate local variables more directly.