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

Nth derivatives? #373

Open
DownLoude opened this issue Apr 27, 2023 · 5 comments
Open

Nth derivatives? #373

DownLoude opened this issue Apr 27, 2023 · 5 comments

Comments

@DownLoude
Copy link

Is there any way to take the nth derivative of a function (without making a function for each individual derivative). i.e.

$$f(x) = x^3$$ $$f_n(n, x) = \frac{d^n}{{dx}^n} ( f(x) )$$ $$f(0, x) = f(x) = x^3$$ $$f(1, x) = f^\prime(x) = 3x^2$$ $$f(2, x) = f^{\prime\prime}(x) = 6x$$ $$f(3, x) = f^{\prime\prime\prime}(x) = 6$$

This is not what I want to have to do, and also returns an error anyway e.apply is not a function.

$$d_2f(x) = \mathrm{diff}(\mathrm{diff}(f, x), x)$$

This is arguably even worse, but its what I'm using right now.

$$df(x) = \mathrm{diff}(f, x)$$ $$d_2f(x) = \mathrm{diff}(df, x)$$ $$d_3f(x) = \mathrm{diff}(d_2f, x)$$

This would be very nice for doing the Associated Legendre polynomials $P^m_\ell(x)$ which contains $\frac{d^m}{{dx}^m}(P_\ell(x))$

@DownLoude
Copy link
Author

DownLoude commented Apr 27, 2023

A function like $\mathrm{ndiff}(f, n, \mathrm{var})$ would be very nice
and maybe also a $\mathrm{npdiff}$? Don't worry if that isn't possible though.

@ChristopherChudzicki
Copy link
Owner

ChristopherChudzicki commented Apr 27, 2023

Thanks for the context that this is for associated Legendre polynomials—that's helpful. I imagine working with $p_{\ell}(x)$ is not particularly convenient at the moment, either.

Unfortunately... There's not a great way to do this right now. Your current workaround with $df(x)$, $d_2f(x)$, d_3f(x)$ is reasonable given current limitations. For what it's worth, diff(diff(f),x) would also get you the second derivative. (Explanation: diff wants a function like f not a value like f(x). And diff(f) is a function, whereas diff(f,x) is a value. I'm absolutely not saying this is clear or that you should have been expected to discover that from the UI. 1 Math3d definitely needs better documentation around functions in general, and derivatives especially.

Footnotes

  1. I also don't really think this is good behavior. It's very unintuitive that diff(x^3, x) does not work.

@DownLoude
Copy link
Author

My current implementation is this:
https://www.math3d.org/eH3Z4cvhT

It works fine (though it is a little on the slow side). Unfortunately I'm not entirely sure how to implement $\frac{d^\ell}{{dx}^\ell}(x^2-1)^\ell$ without making $\ell$ a non-function variable instead of doing something like $P_l(\ell, x)$, since I cannot do $P_l(\ell, x)=\mathrm{diff}\bigl((x^2-1)^\ell, \ell, x\bigl)$. (For consistency's sake I also make $m$ not a function variable of $Pm_l$)

Also $n!$ doesn't work? I implemented it with arrays, and it works fine. I might be missing a function that I don't know how to write or something, and I tried doing it with $prod$ but I don't know what its arguments are.

It works, though if I want to add higher values of $m$ or $\ell$ I have to add to the array. The graph also freaks out at higher values of $m$? Not sure if that's the graph or the function though.

@DownLoude
Copy link
Author

DownLoude commented Apr 29, 2023

Here are some spherical harmonics I made with this https://www.math3d.org/7peENsvPR

Doing it with surfaces instead of colourmaps is possible, just that I need to put it at really low res, otherwise it crashes.

@ChristopherChudzicki
Copy link
Owner

ChristopherChudzicki commented Apr 29, 2023

That's very nice!

Re performance: Everything in math3d is evaluated numerically. This makes Nth derivatives particularly costly when expressed in terms of 1st derivatives.

Setting aside nth derivatives for a moment:

  • I do think math3d would benefit from providing special polynomials. These show up enough in physics/chem visualizations especially that they seem useful.
  • I am trying to focus my sideproject programming time on https://github.com/ChristopherChudzicki/math3d-next, a (backward-compatible) re-write of math3d to facilitate some longer term goals like user login

I just merged a PR #374 that adds a function LegendreP(x, l, m) with m optional.

Warning
The LegendreP(x, l, m) implementation is mostly a placeholder right now: P_l(x) for l = 0, 1, 2, 3 and P_l^m(x) for l = 0, 1, 2 (and corresponding m).

I'm not sure your interest in programming. If you'd like to contribute to flush out the definition of LegendreP, that contribution to this repo would be welcome. (And it would be a pretty isolated change: Probably just that file needs to be edited.)

The current math3d implementation has some issues around typesetting:

Screenshot 2023-04-29 at 5 11 45 PM

Those issues probably won't be addressed until math3d-next (a release of which is still several months away).

I'd also invite you to make an issue in https://github.com/ChristopherChudzicki/math3d-next re special polynomials. (I should, but I have to run in a few minutes.)

PS: I have looked a bit at JS special polynomial libraries in the past. There's not too much that I could find, but https://www.npmjs.com/package/ortho-poly seemed potentially promising.

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