2

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?

3
  • numpy.array(x, y) should raise TypeError: data type not understood. The code you've posted doesn't seem to match the code you actually ran. Can you post an MCVE? Commented Feb 19, 2016 at 3:52
  • The output you've posted seems to indicate that in your real code, you did something like temp1 = numpy.array([x, x]), mixing up x and y. Commented Feb 19, 2016 at 3:54
  • @user2357112 I've added a MCVE that should run now Commented Feb 19, 2016 at 4:16

1 Answer 1

1
np.array(x, y)

That's not how you create a two-element array. You need to pass a single array-like argument to array, rather than individual elements:

np.array([x, y])

Right now, you're actually passing y as the dtype argument. I'm not sure whether it's a bug that this doesn't raise a TypeError. In any case, you're actually getting a 0-dimensional array (yes, 0) whose only element is x, and the broadcasting rules mean that this:

temp1 = np.subtract(temp1, otherArr)

produces np.array([x-otherArr[0], x-otherArr[1]]).

Sign up to request clarification or add additional context in comments.

1 Comment

Wow that's so dumb. Given my beginner python skills and everything I assumed that if I was doing it wrong that the compiler would catch it but that's a bad assumption to make. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.