I have this code, that given some x and y float, I have to subtract them to another float and put them into the first two indices of a numpy array. The float I'm subtracting to is given in a numpy array already. However, I'm running into a strange problem where subtracting the indices individually yields a different answer than subtracting the numpy arrays. Here's the code:
import numpy as np
def calcFunc(x, y):
array = np.zeros(2)
print ("X is", x, "otherArr[0] is", otherArr[0])
print ("Y is", y, "otherArr[1] is", otherArr[1])
array[0] = x - otherArr[0]
array[1] = y - otherArr[1]
temp1 = np.array(x, y)
temp1 = np.subtract(temp1, otherArr)
print("temp1 is" , temp1)
print("sub is", array)
x = np.linspace(-3, 3, 50)
y = np.linspace(-3, 3, 50)
otherArr = np.random.rand(2) * 0.25
for i in range(len(x)):
for j in range(len(y)):
calcFunc(x[i], y[j])
where x and y are some floats that I get elsewhere and pass into this function that does this subtraction, so it changes each iteration. Then the code output is:
X is -3.0 otherArr[0] is 0.129294357724
Y is -3.0 otherArr[1] is 0.0308568538399
temp1 is [-3.12929436 -3.03085685]
array is [-3.12929436 -3.03085685]
X is -3.0 otherArr[0] is 0.129294357724
Y is -2.87755102041 otherArr[1] is 0.0308568538399
temp1 is [-3.12929436 -3.03085685]
array is [-3.12929436 -2.90840787]
X is -3.0 otherArr[0] is 0.129294357724
Y is -2.75510204082 otherArr[1] is 0.0308568538399
temp1 is [-3.12929436 -3.03085685]
array is [-3.12929436 -2.78595889]
X is -3.0 otherArr[0] is 0.129294357724
Y is -2.63265306122 otherArr[1] is 0.0308568538399
temp1 is [-3.12929436 -3.03085685]
array is [-3.12929436 -2.66350992]
X is -3.0 otherArr[0] is 0.129294357724
Y is -2.51020408163 otherArr[1] is 0.0308568538399
temp1 is [-3.12929436 -3.03085685]
array is [-3.12929436 -2.54106094]
X is -3.0 otherArr[0] is 0.129294357724
Y is -2.38775510204 otherArr[1] is 0.0308568538399
temp1 is [-3.12929436 -3.03085685]
array is [-3.12929436 -2.41861196]
I'm assuming this has to do with Y having more decimals after the first iteration and there appears some sort of rounding error. However why does the result differ from simply subtracting the indices instead? Isn't that what the array subtraction does in the first place?
numpy.array(x, y)should raiseTypeError: data type not understood. The code you've posted doesn't seem to match the code you actually ran. Can you post an MCVE?temp1 = numpy.array([x, x]), mixing upxandy.