ChristopherChudzicki / math3d-react

A user-friendly 3d graphing calculator for the web. Create, share, and animate 3d mathematical scenes.
https://www.math3d.org
Mozilla Public License 2.0
253 stars 35 forks source link

Nth derivatives? #373

Open DownLoude opened 1 year ago

DownLoude commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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.

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

DownLoude commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 just merged a PR https://github.com/ChristopherChudzicki/math3d-react/pull/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.