On Thu, Nov 21, 2024 at 03:09:37PM +0000, Ed . wrote: > Hi Luis, > > Thank you for both the code and the explanation! I was aware of the > limitations of diffover for this (including its name - just "diff" caused > clashes with Text::Diff in a downstream module). I'm proposing to rename > "diffover" to "numdiff" (for numerical differentiation) in the next version > of PDL, any objections?
However, it just takes the differences between consecutive values. I would call it firstdiff, as there are also second, third, etc. diffs. These are analogous, but not equal, to the corresponding (first, second, etc.) derivatives one find in calculus, and may be used to build approximate derivatives, as in the examples. > I considered using diff2 in my quickly-written prototype, but chose > diffover so as not to shrink the dim by 1. There are other PDL methods that allow the user to specify what to do with the boundaries. Maybe the same should be done here. In my solution the boundary could be kept if the function is periodic, but otherwise, it should be thrown away. Another alternative is to add values beyond the boundary: repeating the boundary value would correspond to a 0 value for the derivative at the boundary. One could also add f_{n+1}=2f_{n}-f_{n-1}, which would correspond to a null curvature at the boundary. Or the easiest, which is to trim down the derivative at the boundary by droping the last element for forward differences, the first for backward differences and both for centered differences. > I am also keen to capture the 3 functions you've made, probably with > "partial" going into Ufunc, and gaining an options param that has > "difference" of either "forward", "backward", or "centred", or > "shrink" using diff2. "div" and "curl" would then "just" be examples > in the docs. Does anyone see any difficulties with this? Boyd, I don't > feel this would clash with an Advent posting in any important way, but > I'm very pleased to hear your thoughts if you disagree :-) Would be useful. Notice that I assumed the function was defined on a regular grid. If not, more elaborate schemes would be needed to estimate the centered derivative. Another alternative for finding derivative, which may be useful in some cases, is to make a Fourier transform of the data, multiply it by i()*(wavevector) and then transform back. Best regards, Luis > > Best regards, > Ed > -- o W. Luis Mochán, | tel:(52)(777)329-1734 /<(*) Instituto de Ciencias Físicas, UNAM | fax:(52)(777)317-5388 `>/ /\ Av. Universidad s/n CP 62210 | (*)/\/ \ Cuernavaca, Morelos, México | moc...@fis.unam.mx /\_/\__/ GPG: 791EB9EB, C949 3F81 6D9B 1191 9A16 C2DF 5F0A C52B 791E B9EB _______________________________________________ pdl-devel mailing list pdl-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/pdl-devel