jonathf / chaospy

Chaospy - Toolbox for performing uncertainty quantification.
https://chaospy.readthedocs.io/
MIT License
445 stars 87 forks source link

"AttributeError" from creating a quadrature nodes/weights with default rule throws (numpy.broadcast_arrays() issue?) #431

Closed damar-wicaksono closed 1 month ago

damar-wicaksono commented 1 month ago

Describe the bug

Creating a set of quadrature nodes-weights via chaospy.generate_quadrature() with the default method (i.e., rule=clenshaw_curtis) throws an AttributeError exception

>>> cp.generate_quadrature(2, j, rule="clenshaw_curtis")
Traceback (most recent call last):
...
AttributeError: 'tuple' object has no attribute 'pop'

To Reproduce

>>> import chaospy as cp
>>> x1 = cp.Uniform(-1, 1)
>>> x2 = cp.Uniform(-1, 1)
>>> j = cp.J(x1, x2)
>>> cp.generate_quadrature(2, j, rule="clenshaw_curtis")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
  File "site-packages/chaospy/quadrature/frontend.py", line 171, in generate_quadrature
    abscissas, weights = _generate_quadrature(
  File "site-packages/chaospy/quadrature/frontend.py", line 287, in _generate_quadrature
    abscissas, weights = quad_function(order, dist, **parameters)
  File "site-packages/chaospy/quadrature/clenshaw_curtis.py", line 67, in clenshaw_curtis
    return hypercube_quadrature(
  File "site-packages/chaospy/quadrature/hypercube.py", line 69, in hypercube_quadrature
    order, domain, segments = align_arguments(order, domain, segments)
  File "site-packages/chaospy/quadrature/hypercube.py", line 133, in align_arguments
    output = [args.pop(0)]
AttributeError: 'tuple' object has no attribute 'pop'

Curiously, another rule seems to work just fine:

>>> cp.generate_quadrature(2, j, rule="gaussian")
(array([[-7.74596669e-01, -7.74596669e-01, -7.74596669e-01,
        -4.51028104e-17, -4.51028104e-17, -4.51028104e-17,
         7.74596669e-01,  7.74596669e-01,  7.74596669e-01],
       [-7.74596669e-01, -4.51028104e-17,  7.74596669e-01,
        -7.74596669e-01, -4.51028104e-17,  7.74596669e-01,
        -7.74596669e-01, -4.51028104e-17,  7.74596669e-01]]), array([0.07716049, 0.12345679, 0.07716049, 0.12345679, 0.19753086,
       0.12345679, 0.07716049, 0.12345679, 0.07716049]))
>>> 

Expected behavior

I expect the output to be the quadrature nodes and weights based on the Clenshaw-Curtis rule:

>>> cp.generate_quadrature(2, j)
(array([[-1., -1., -1.,  0.,  0.,  0.,  1.,  1.,  1.],
       [-1.,  0.,  1., -1.,  0.,  1., -1.,  0.,  1.]]), array([0.02777778, 0.11111111, 0.02777778, 0.11111111, 0.44444444,
       0.11111111, 0.02777778, 0.11111111, 0.02777778]))

Desktop (please complete the following information):

Additional context

I suspect there's a change behavior of numpy.broadcast_arrays() from outputting a list to a tuple between versions; as such, this issue may be related to Issue #424.

jonathf commented 1 month ago

Thanks for the report. broadcast_arrays in Numpy v2 is the culprit, yes. I've made a fix in version 4.3.17 now.