Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behavior under presence of variable with zero-only coefficients #413

Open
hdolfen opened this issue Jun 25, 2023 · 1 comment
Labels

Comments

@hdolfen
Copy link

hdolfen commented Jun 25, 2023

When plotting two-dimensional polynomials, I noticed that when a variable has only 0 coefficients it cannot be evaluated. This can result in an error or unexpected behavior.

To Reproduce
Running the example results in the same evaluation of the second and third polynomial in the array. These are polynomials of degree 1, and they should be oriented differently.

import chaospy
import numpy as np
import matplotlib.pyplot as plt

X = 1  # m
Y = 1  # degrees

# Generate distribution
distribution = chaospy.J(chaospy.Uniform(0, X), chaospy.Uniform(0, Y))

x, y = np.linspace((0, 0), (X, Y), 100).T
x_grid, y_grid = np.meshgrid(x, y)

polynomial = chaospy.generate_expansion(2, distribution)

q0, q1 = chaospy.variable(2)

# polynomial = polynomial + (q0 + q1) * 1e-100

print(polynomial)

for i in range(len(polynomial)):
    z = polynomial[i](x_grid, y_grid)
    fig = plt.figure()
    ax = fig.add_subplot(projection='3d')
    ax.plot_surface(x_grid, y_grid, z)
plt.show()

Expected behavior
The issue can be solved by adding the following.

polynomial = polynomial + (q0 + q1) * 1e-100

This feels like a hack though.

Desktop (please complete the following information):

  • OS: Linux CentOS
  • Python version: 3.9.12
  • Chaospy version 4.3.13
@hdolfen hdolfen added the bug label Jun 25, 2023
@jonathf
Copy link
Owner

jonathf commented Jul 3, 2023

Iterating over a polynomial array of 2 dimensions reduces to 1 dimensions when there the other variable isn't present. In other words: [1, x, y, xy] = [f1(), f2(x), f3(y), f4(x, y)]. You will notice the difference if you change p(x, y) with a more explicit p(q0=x, q1=y).

I agree that isn't the best option, and I will update the default in the next release. For now you can fix your problem by setting the following option at the top of you code:

chaospy.set_options(retain_names=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants