Products > Programming
The Julia thread
SiliconWizard:
--- Quote from: rstofer on January 11, 2022, 12:36:13 am ---Some of the differences from MATLAB are profound and likely will lead to much teeth gnashing. Consider:
--- Quote ---Julia arrays are not copied when assigned to another variable. After A = B, changing elements of B will modify A as well.
--- End quote ---
I REALLY need to digest these differences!
--- End quote ---
That's a bit like Lua tables.
You can use the copy() function to make a copy: A = copy(B)
(of course, probably not to be abused - in general there may be other ways to achieve the same than having copies of arrays, depends!)
Of course, you'll get a completely new vector if you add elements, for instance:
--- Code: ---A = [1, 2]
B = [A; 3] # one short way of adding an element to a vector, you get [1, 2, 3], while if using:
B = [A, 3] # you get [[1,2], 3] as expected
# in both cases, of course, A is unmodified if you further modify B
--- End code ---
magic:
--- Quote from: rstofer on January 11, 2022, 12:36:13 am ---Some of the differences from MATLAB are profound and likely will lead to much teeth gnashing. Consider:
--- Quote ---Julia arrays are not copied when assigned to another variable. After A = B, changing elements of B will modify A as well.
--- End quote ---
I REALLY need to digest these differences!
--- End quote ---
Yeah, that's not looking good for a dummy-friendly language.
Computer scientists will get it, but mathematicians and physicists not so fast :P
brucehoult:
--- Quote from: magic on January 11, 2022, 09:30:32 am ---
--- Quote from: rstofer on January 11, 2022, 12:36:13 am ---Some of the differences from MATLAB are profound and likely will lead to much teeth gnashing. Consider:
--- Quote ---Julia arrays are not copied when assigned to another variable. After A = B, changing elements of B will modify A as well.
--- End quote ---
I REALLY need to digest these differences!
--- End quote ---
Yeah, that's not looking good for a dummy-friendly language.
Computer scientists will get it, but mathematicians and physicists not so fast :P
--- End quote ---
It's no different to C. Or Java. Or Python.
--- Code: ---$ python -c 'a=[1,2,3,4];b=a;b[2]=42;print a'
[1, 2, 42, 4]
--- End code ---
In almost all languages "assigning" an object is actually copying a reference to the object, not the object itself. C++ is one of the few exceptions to this.
magic:
Clearly they are buggy languages, then. Why would assigning something to b have anything to do with my a?
--- Code: ---> a=[1,2,3,4]; b=a; b(2)=42; a
a =
1 2 3 4
--- End code ---
brucehoult:
--- Quote from: magic on January 11, 2022, 10:12:44 am ---Clearly they are buggy languages, then. Why would assigning something to b have anything to do with my a?
--- Code: ---> a=[1,2,3,4]; b=a; b(2)=42; a
a =
1 2 3 4
--- End code ---
--- End quote ---
Languages aren't buggy. Programs are.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version