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

Lagrange polynomials #45

Closed
dlouk opened this issue May 4, 2017 · 6 comments
Closed

Lagrange polynomials #45

dlouk opened this issue May 4, 2017 · 6 comments

Comments

@dlouk
Copy link

dlouk commented May 4, 2017

Hi there!

I was trying to use the lagrange_polynomial function which is in chaospy/orthogonal.py. I assume that those are the Lagrange basis polynomials. I have encountered the following issues:

  1. Calling chaospy.orthogonal.lagrange_polynomial([0,1]) (or with any number combination, as long as there is a zero in there) results in the error "raise numpy.linalg.LinAlgError("invertable matrix") LinAlgError: invertable matrix".

  2. Calling chaospy.orthogonal.lagrange_polynomial([1, 2]) results in the polynomials [-q0^2+2.0q0, 0.5q0^2-0.5q0]. However, using the definition of Lagrange polynomials, e.g. https://en.wikipedia.org/wiki/Lagrange_polynomial, one should get [-q0+2.0, q0-1.0].

Therefore, I would like to ask what exactly am I missing in how chaospy constructs Lagrange polynomials?

Best regards,
Dimitris

@jonathf
Copy link
Owner

jonathf commented May 5, 2017

The Lagrange polynomials have been experimental so far, but should work fine. What you have found is a bug.

I've made a quickfix and I it seems to solve the problem. Please let me know if it works as it should now.

@dlouk
Copy link
Author

dlouk commented May 8, 2017

Hi again and sorry for the late reply,

I have run the following tests:

  1. lagrange_polynomial[1.] or any other test with just one point in the array fails. Error message "Arrays cannot be empty".
  2. Same to my first comment, given any array with a zero in it, the lagrange_polynomial function produces the error message "LinAlgError: invertible matrix"
  3. Again as before, when the function works, the polynomial order is always one higher than the one I expect, according to theory (e.g. given as input the array [1.0, 2.0]).

Best,
Dimitris

@jonathf
Copy link
Owner

jonathf commented May 8, 2017

Are you getting the following output?

[1]: print chaospy.orthogonal.lagrange_polynomial([0, 1])
[-q0+1.0, q0]

[2]: print chaospy.orthogonal.lagrange_polynomial([1, 2])
[-q0+2.0, q0-1.0]

[3]: print chaospy.orthogonal.lagrange_polynomial([-1, 0, 1])
[0.5q0^2-0.5q0, -q0^2+1.0, 0.5q0^2+0.5q0]

If not are you sure you have updated your verision of the repo. If not:

  • Download/update the repo
  • (re)install with python setup.py install
  • then try to run it again.

@jonathf
Copy link
Owner

jonathf commented May 8, 2017

Sorry about that. I pushed it directly to master, which I really shouldn't.

I merged the changes to the development branch now.
Do git pull and you should see the updates.

@dlouk
Copy link
Author

dlouk commented May 8, 2017

Hi again,

one small thing that I noticed: if one gives as input an array with just one point, then the result is not the expected one, e.g.:
print cp.orthogonal.lagrange_polynomial([1]) --> q0-1.0
print cp.orthogonal.lagrange_polynomial([0]) --> q0

I would have expected a constant, e.g. 1.0.

Thanks for your time and effort and best regards,
Dimitris

@jonathf
Copy link
Owner

jonathf commented May 8, 2017

Thank you. I made the fix for the single point as well now, and added it to development.

I consider the issue as close. If that is not the case, feel free to reopen.

@jonathf jonathf closed this as completed May 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants