Here are the two cases under consideration. (a)Scalars and (b) Numpy matrices. My query is about initialization and assignment.
1 Perfect
y = x = 0;
x = 7
print(x,y)
y = 8
print(x,y)
x=10
print( x,y)
output is perfectly fine. Effect of y = x = 0 is not seen.
7 0
7 8
10 8
2 Perfect
import numpy as np
n=3
#d11 = d =np.zeros(n)#.reshape(-1,1) ;
d11 = np.zeros(n); d =np.zeros(n)
for j in np.arange(0, n, 1):
d11[j] = j**3
d[j] = j**2
print (d11, d )
output is perfectly fine, as d11 and d are initialized separately.
[ 0. 1. 8.] [ 0. 1. 4.]
3 Ambiguous
import numpy as np
n=3
d11 = d =np.zeros(n)#.reshape(-1,1) ;
#d11 = np.zeros(n); d =np.zeros(n)
for j in np.arange(0, n, 1):
d11[j] = j**3
d[j] = j**2
print (d11, d )
output is not okay, as d11 and d are initialized and equated. Though I update d11 and d separately, there are being equated to eachother with the more recent update. Output goes like this:
[ 0. 1. 4.] [ 0. 1. 4.]
Queries:
Initializing multiple variables with a single line is working fine on scalars (y = x = 0) , but not on matrices (d11 = d =np.zeros(n))?
Is there something wrong with my coding or understanding ? This is not the case with Matlab. Why d11 = d is taking effect in case 3, though they are being updated at different stages/lines.
Case 3 can be considered as bad programming skill ?. I dont see any use of this kind of functionality from any programming language ?
Please enlighten me. I just moved from matlab to python. Hence I got this fundamental question.
Thanks in advance for your attention.