Arleee
Arleee

Reputation: 1

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() when using Numpy and Scipy

I am trying to compute an integral in Python using numpy and scipy.

This is my current code:

import scipy
import numpy as N

d = 160
w = 1.8
np = 265

def f(x):
    return N.sin(N.power(x, 2))


inta = scipy.integrate.quad(f, 0, N.sqrt(N.divide(N.pi, 2)))
a = N.power((N.divide(N.multiply(N.multiply(2, N.sqrt(2)), inta), d)), 2)
s = N.sqrt(N.divide(N.pi, a))

def funx(u):
    return N.cos(N.multiply(N.divide(a, 2), N.square(u)))


x = scipy.integrate.quad(funx, 0, N.multiply(N.divide(1, np), s))
print(x)

However there is an error when computing the last integral

x = scipy.integrate.quad(funx, 0, N.multiply(N.divide(1, np), s))

"ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()". I am especially confused why the first integral ran fine but the second one gave an error.

How can I fix this error?


Edit:

Full error message:

Traceback (most recent call last):
  File "C:\Users\eeerm\PycharmProjects\GDSGenerator\GDSWriter\stackOverflowq.py", line 20, in <module>
    x = scipy.integrate.quad(funx, 0, N.multiply(N.divide(1, np), s))
  File "C:\Users\eeerm\PycharmProjects\GDSGenerator\venv\lib\site-packages\scipy\integrate\_quadpack_py.py", line 408, in quad
    flip, a, b = b < a, min(a, b), max(a, b)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Upvotes: 0

Views: 162

Answers (1)

Joao_PS
Joao_PS

Reputation: 670

By default, quad returns a tuple as: (value_of_integration, estimated_error).

So,

inta, inta_error = integrate.quad(f, 0, N.sqrt(N.divide(N.pi, 2)))

And

x, x_error = integrate.quad(funx, 0, N.multiply(N.divide(1, np), s))

Maybe You need type this, before all:

import scipy.integrate as integrate

Upvotes: 0

Related Questions