2

I'm trying to divide numpy array by numpy float64 type scalar. Following is my code.

pose_q = np.array(pose_q)
expectecd_q = np.array(expectecd_q)

pose_q = np.squeeze(pose_q)
expectecd_q = np.squeeze(expectecd_q)

q1 = expectecd_q / np.linalg.norm(expectecd_q)
q2 = pose_q / np.linalg.norm(pose_q)

d = abs(np.sum(np.multiply(q1, q2)))

However I'm getting the following error pointing towards expectecd_q / np.linalg.norm(expectecd_q)

TypeError: ufunc 'true_divide' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
5
  • what is the dtype of pose_q? Commented Aug 28, 2020 at 20:15
  • Please print out expectecd_q.dtype Commented Aug 28, 2020 at 20:29
  • @Miguel the type of pose_q is <class 'numpy.float64'> Commented Aug 28, 2020 at 20:36
  • @Ehsan the type of expected_q is <class 'numpy.ndarray'> Commented Aug 28, 2020 at 20:37
  • @Mr.RandyTom I think you are printing type(expected_q), I meant expected_q.dtype. Commented Aug 28, 2020 at 21:35

1 Answer 1

4

As you didn't provide your data, I created both arrays as:

a = np.array([12.0, 15.2, 19.3])  # Dividend
b = np.array(3.0)                 # Divider (a Numpy scalar)

If you want to divide a by b run just (no surprise) a / b. The result is:

array([4.        , 5.06666667, 6.43333333])

In your case, maybe you should identify what particular values you have as operands.

I looked in the Web for your error message. I found a suggestion that the cause can be that the array in question has text values (not float). It can happen when you read the array from a database. Check dtype of this array. class 'numpy.ndarray' says only that this is a Numpy array. But what is the type of its elements?

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

4 Comments

the type of np.linalg.norm(pose_q) is <class 'numpy.float64'> :(
@Mr.RandyTom error is not in pose_q line, it is in expected_q. Please print type of that.
and the expected_q is <class 'numpy.ndarray'>
You wrote the name of your variable as expected_q. But in your post there is expectecd_q (note additional "c"). Maybe this is a typo?

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.