plato
plato

Reputation: 1

Multiplication and Division of elements in a numpy array gives integer results

import numpy as np
A = np.array([[2,1,-1,8],
         [-3,-1,2,-11],
         [-2,1,2,-3]])

B =  A[1]+A[0]* (-A[1][0]/A[0][0])
print(B) #B =[ 0.   0.5  0.5  1. ]
A[1] = A[1]+A[0]* (-A[1][0]/A[0][0])
print(A[1]) #A[1] = [0 0 0 1]

How does the above situation happen, and what can I do about it?

Upvotes: 0

Views: 234

Answers (1)

Transhuman
Transhuman

Reputation: 3547

Use dtype=float in main array. Your array is integer by default.

import numpy as np
A = np.array([[2,1,-1,8],
         [-3,-1,2,-11],
         [-2,1,2,-3]], dtype=float)
B =  A[1] + (A[0]*(-A[1,0]/A[0,0]))
print(B)
A[1] = A[1] + (A[0]*(-A[1,0]/A[0,0]))
print(A[1])
#Output:
#[ 0.   0.5  0.5  1. ]
#[ 0.   0.5  0.5  1. ]

Upvotes: 3

Related Questions