Access variable values

Pardon my ignorance, but I have a very basic question.

Suppose I have a variable and I want to assign the value of that variable to
the value of another variable without structurally linking the two variables
(i.e., the assignment has only to do with numbers).

One way I was able to do this in Flux was when I used the ‘<variable_name>.data’ option, which referred to the data in <variable_name> but not the tracked details, etc.

What I am asking for is a different sort of assignment, the capability of setting a variable equal to the value of another variable but not to the variable itself.

I think it depends what exactly the variable is. If it’s an array, then you can use copyto! or broadcasted assignment:

julia> A = rand(4,4)
4×4 Array{Float64,2}:
 0.492154  0.497869   0.351142  0.7143
 0.519419  0.825479   0.393257  0.436978
 0.613145  0.0596596  0.944314  0.762933
 0.378408  0.312915   0.147394  0.00624961

julia> B = zeros(4,4)
4×4 Array{Float64,2}:
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0

julia> B .= A
4×4 Array{Float64,2}:
 0.492154  0.497869   0.351142  0.7143
 0.519419  0.825479   0.393257  0.436978
 0.613145  0.0596596  0.944314  0.762933
 0.378408  0.312915   0.147394  0.00624961

julia> A[1] = 2.0
2.0

julia> B
4×4 Array{Float64,2}:
 0.492154  0.497869   0.351142  0.7143
 0.519419  0.825479   0.393257  0.436978
 0.613145  0.0596596  0.944314  0.762933
 0.378408  0.312915   0.147394  0.00624961

julia> copyto!(B, A)
4×4 Array{Float64,2}:
 2.0       0.497869   0.351142  0.7143
 0.519419  0.825479   0.393257  0.436978
 0.613145  0.0596596  0.944314  0.762933
 0.378408  0.312915   0.147394  0.00624961

julia> B
4×4 Array{Float64,2}:
 2.0       0.497869   0.351142  0.7143
 0.519419  0.825479   0.393257  0.436978
 0.613145  0.0596596  0.944314  0.762933
 0.378408  0.312915   0.147394  0.00624961

julia> A[1] = 3.0
3.0

julia> B
4×4 Array{Float64,2}:
 2.0       0.497869   0.351142  0.7143
 0.519419  0.825479   0.393257  0.436978
 0.613145  0.0596596  0.944314  0.762933
 0.378408  0.312915   0.147394  0.00624961

Assignment never assign the variable, it always assign the value. In another word, if you do a = b; b = c; b will have the value of c while a won’t. (assuming c isn’t b to begin with).

What you are probably asking is an assignment that makes a copy. That is not an assignment, it’s a copy. It’s not a fundamental part of the language and supported mutable types should implement copy to do that so that you can do a = copy(b). Immutable types generally don’t allow that.

2 Likes

Thanks very much, that is exactly what I was looking for.

1 Like

Thanks for that. I will try some experiments.

What about deepcooy?

It’s something else that’s completely unrelated to assignment and pretty much unrelated to copy. It’s usually not what you want.