Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ENH Add N-D fitting and evaluation methods to the chebyshev.py and polynomial.py library #6071

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

Closed
wants to merge 1 commit into from

Conversation

pbrod
Copy link
Contributor

@pbrod pbrod commented Jul 13, 2015

Currently there are no functions in chebyshev.py/ polynomial.py that do

  • N-dimensional fitting for N>1
  • N-dimensional evaluation for N>3
  • Construct Vandermonde matrix for N>3

This submission is a first implementation to fill the above mentioned gap.
Functions added to chebyshev.py and polynomial.py are:
chebvalnd, chebgridnd, chebfitnd, chebfitfunc, chebvandernd
polyvalnd, polygridnd, polyfitnd, polyfitfunc, polyvandernd
and supporting tests

Examples on use

>>> import numpy as np
>>> import numpy.polynomial.chebyshev as cheb
>>> import matplotlib.pyplot as plt
>>> n = 51
>>> xorder, yorder = n-1, n-1
>>> x = cheb.chebpts1(n)
>>> xgrid, ygrid = np.meshgrid(x, x)
>>> def f(x, y): return np.exp(-x**2-6*y**2)
>>> zgrid = f(xgrid, ygrid)
>>> dcoeff2 = cheb.chebfitnd((xgrid, ygrid), zgrid, [xorder,yorder])
>>> dcoeff = cheb.chebfitfunc(f, n=(xorder+1,yorder+1))
>>> np.allclose(dcoeff2, dcoeff)
True

Visualize the significant coefficients

>>> plt.figure()
>>> plt.spy(np.abs(dcoeff)>1e-13)
>>> plt.title('Coefficients > 10^-13')

figure_1

Evaluate on finer grid

>>> xi = np.linspace(-1, 1, 151)
>>> Xi,Yi = np.meshgrid(xi, xi)
>>> Zi = f(Xi, Yi)

Compute residuals

>>> zzi = cheb.chebvalnd(dcoeff, Xi, Yi)
>>> devi = Zi - zzi

>>> zz = cheb.chebvalnd(dcoeff, xgrid, ygrid)
>>> dev = zgrid - zz

and plot them

>>> plt.figure() 
>>> plt.contourf(xgrid, ygrid, np.abs(dev))
>>> plt.colorbar()
>>> plt.title('Error at the chebfitnd-points')

>>> plt.figure()
>>> plt.contourf(Xi, Yi, np.abs(devi))
>>> plt.colorbar()
>>> plt.title('Error on a fine grid')
>>> plt.show('hold')

figure_2
figure_3

@charris
Copy link
Member

charris commented Jul 18, 2015

You should squash the commits and follow the guidelines in doc/source/dev/gitwash/development_workflow.rst for the messages. Also, doc/source/reference/routines.polynomials.chebyshev.rst will need an update before this is merged, and perhaps the doc/release/1.10.0-notes.rst also if this is merged before the 1.10.0 branch.

@@ -1411,6 +1412,111 @@ def chebgrid3d(x, y, z, c):
return c

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add the new functions to __all__.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also in the module documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. This is done. I have updated the files to according to your comments.

@charris
Copy link
Member

charris commented Jul 18, 2015

I like this in general. The tests are a bit skimpy and some of my style comments apply more generally. Once this is settled, it should be extended to the remaining polynomials, although power series will be a bit of an exception.

I've thought a bit about making classes for some of this similar to the Polynomial etc. classes but with fewer features. Two other generalization that might be useful in practice are vector valued functions and gradients and Jacobians. If you have any thoughts along those lines I'd like to hear them.

@charris
Copy link
Member

charris commented Jul 20, 2015

I'm curious if you actually have a use case for the > 3d case?

@charris
Copy link
Member

charris commented Oct 1, 2015

The second commit should be a bit more specific and begin with DOC:, or you could just squash it into the first...

Now begins the drudgery. We try to keep all 6 polynomials in sync, so functions added to one also need to be added to the remaining 5 and tested. Yep, it's a PITA ;)

@charris
Copy link
Member

charris commented Oct 1, 2015

If this goes in, it will also need a mention in doc/release/1.11.0-notes.rst.

If `c` has fewer than N dimensions, ones are implicitly appended to its
shape to make it N-D. The shape of the result will be c.shape[3:] +
x1.shape.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs .. versionadded:: 1.11.0 here, followed by a blank line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grep for other examples.

@pbrod pbrod force-pushed the ENH_chebyshev branch 4 times, most recently from 2caf7d5 to 0bf9fb3 Compare October 1, 2015 09:37
@pbrod
Copy link
Contributor Author

pbrod commented Oct 1, 2015

Added .. versionadded:: 1.11.0. and updated doc/release/1.11.0-notes.rst

@seberg
Copy link
Member

seberg commented Oct 1, 2015

@pbrod, I think you have to update your master and rebase, it seems to me you created a new 1.11 release notes file. This already exists.

@pbrod pbrod force-pushed the ENH_chebyshev branch 2 times, most recently from f7b3b43 to a4ad7b5 Compare October 1, 2015 14:29
@pbrod
Copy link
Contributor Author

pbrod commented Oct 1, 2015

Ok. Thanks.

Added:
chebvalnd, chebgridnd, chebfitnd, chebfitfunc, chebvandernd
polvalnd, polygridnd, polyfitnd, polyfitfunc, polyvandernd

Also added supporting tests
@pbrod pbrod changed the title ENH Add N-D fitting and evaluation methods to the chebyshev.py library ENH Add N-D fitting and evaluation methods to the chebyshev.py and polynomial.py library Oct 1, 2015
@homu
Copy link
Contributor

homu commented Nov 16, 2015

☔ The latest upstream changes (presumably #6688) made this pull request unmergeable. Please resolve the merge conflicts.

@homu
Copy link
Contributor

homu commented Jun 12, 2016

☔ The latest upstream changes (presumably #7735) made this pull request unmergeable. Please resolve the merge conflicts.

@woodscn
Copy link

woodscn commented Sep 22, 2016

For what it's worth, I was just thinking about trying to implement something like this. I'm also interested in evaluation for N>3, i.e. x,y,z,t; or state spaces, etc.

What is the current status of this PR?

@charris
Copy link
Member

charris commented Sep 22, 2016

IIRC, it was in decent shape. I was thinking of taking a further look at it in the next few days.

@woodscn
Copy link

woodscn commented Sep 22, 2016

Looking at this more closely, I'm not sure if it does what I'm looking for. Essentially, I'm after something like a MultiVariatePolynomial helper class, with things like derivatives, integrals, +, *, etc.

@charris
Copy link
Member

charris commented Sep 25, 2018

@pbrod This is old but I still think this is useful. Want to pursue this?

@charris
Copy link
Member

charris commented Sep 25, 2018

@woodscn I agree that that would be useful. If you are motivated in that direction you may want to put together a PR.

@Qiyu8
Copy link
Member

Qiyu8 commented Jun 8, 2020

@pbrod Are you still working on this? looks valuable if you can solve the confict 😄

@rossbar
Copy link
Contributor

rossbar commented Jun 8, 2020

There is another attempt at expanding polynomial fitting to ND in #14151

Base automatically changed from master to main March 4, 2021 02:03
@seberg seberg requested a review from charris September 8, 2021 16:36
@scratchmex
Copy link
Contributor

scratchmex commented Dec 2, 2021

ping @pbrod. I think this is very valuable. Do you have any opinion or comments about the difference between this and #14151 as mentioned by rossbar? To not repeat efforts

@pbrod
Copy link
Contributor Author

pbrod commented Dec 3, 2021

I am closing this as #14151 does the same and more.

@pbrod pbrod closed this Dec 3, 2021
pbrod added a commit to pbrod/numpy that referenced this pull request Dec 7, 2021
Currently there are no functions in chebyshev.py that do N-dimensional fitting for N>1
This submission is a attempt to fill the above mentioned gap and complements the efforts in numpy#14151.

The multidimensional interpolation method implemented here uses the 1D Discrete Cosine Transform (DCT) instead of the vandermonde method. This is a reimplementation of the chebfitfunc function found in numpy#6071. The accuracy of the new interpolation method is better than the old one especially for polynomials of high degrees.

Also added domain as input to chebinterpolate in order to interpolate the function over any input domain.
Added test_2d_approximation for testing interpolation of a 2D function.
Replaced all references to the missing chebfromfunction with chebinterpolate.
pbrod added a commit to pbrod/numpy that referenced this pull request Dec 7, 2021
Currently there are no functions in chebyshev.py that do N-dimensional fitting for N>1
This submission is a attempt to fill the above mentioned gap and complements the efforts in numpy#14151.

The multidimensional interpolation method implemented here uses the 1D Discrete Cosine Transform (DCT) instead of the vandermonde method. This is a reimplementation of the chebfitfunc function found in numpy#6071. The accuracy of the new interpolation method is better than the old one especially for polynomials of high degrees.

Also added domain as input to chebinterpolate in order to interpolate the function over any input domain.
Added test_2d_approximation for testing interpolation of a 2D function.
Replaced all references to the missing chebfromfunction with chebinterpolate.
pbrod added a commit to pbrod/numpy that referenced this pull request Feb 14, 2022
Currently there are no functions in chebyshev.py that do N-dimensional fitting for N>1
This submission is a attempt to fill the above mentioned gap and complements the efforts in numpy#14151.

The multidimensional interpolation method implemented here uses the 1D Discrete Cosine Transform (DCT) instead of the vandermonde method. This is a reimplementation of the chebfitfunc function found in numpy#6071. The accuracy of the new interpolation method is better than the old one especially for polynomials of high degrees.

Also added domain as input to chebinterpolate in order to interpolate the function over any input domain.
Added test_2d_approximation for testing interpolation of a 2D function.
Replaced all references to the missing chebfromfunction with chebinterpolate.
pbrod added a commit to pbrod/numpy that referenced this pull request Feb 14, 2022
Currently there are no functions in chebyshev.py that do N-dimensional fitting for N>1
This submission is a attempt to fill the above mentioned gap and complements the efforts in numpy#14151.

The multidimensional interpolation method implemented here uses the 1D Discrete Cosine Transform (DCT) instead of the vandermonde method. This is a reimplementation of the chebfitfunc function found in numpy#6071. The accuracy of the new interpolation method is better than the old one especially for polynomials of high degrees.

Also added domain as input to chebinterpolate in order to interpolate the function over any input domain.
Added test_2d_approximation for testing interpolation of a 2D function.
Replaced all references to the missing chebfromfunction with chebinterpolate.
Made doctest of chebinterpolate more robust.
pbrod added a commit to pbrod/numpy that referenced this pull request Feb 14, 2022
Currently there are no functions in chebyshev.py that do N-dimensional fitting for N>1
This submission is a attempt to fill the above mentioned gap and complements the efforts in numpy#14151.

The multidimensional interpolation method implemented here uses the 1D Discrete Cosine Transform (DCT) instead of the vandermonde method. This is a reimplementation of the chebfitfunc function found in numpy#6071. The accuracy of the new interpolation method is better than the old one especially for polynomials of high degrees.

Also added domain as input to chebinterpolate in order to interpolate the function over any input domain.
Added test_2d_approximation for testing interpolation of a 2D function.
Replaced all references to the missing chebfromfunction with chebinterpolate.
Made doctest of chebinterpolate more robust.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.