Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

diff function for finding local extrema #227

Closed
balint42 opened this issue Nov 4, 2014 · 10 comments
Closed

diff function for finding local extrema #227

balint42 opened this issue Nov 4, 2014 · 10 comments

Comments

@balint42
Copy link

balint42 commented Nov 4, 2014

I went through your lib looking for this. Since I could not find it I implemented it myself - any interest including it to your lib?

I'm going to add two versions, the current one taking [values] and another one operating on an [x] and an [y] vector.

edit: done

@josdejong
Copy link
Owner

Thanks for your suggestion. I've read your readme but I don't raelly understand what the function does exactly (I don't understand the outcome of the given example for example). Can you explain this a little bit more?

@balint42
Copy link
Author

balint42 commented Nov 4, 2014

Ok so it basically simply finds local maximum and local minimum values in a 2D graph. Or more precisely it finds the x-values at which those y-values are to be found. You can see the term "local maximum" / "local minimum" explained on Wiki. The wiki image is a bit sloppy though: a "global maximum" / "global minimum" can also be a "local maximum" / "local minimum". Simply put, in the algorithm I implemented a local maximum is any "valley" or "peak" in a 2D graph.

To my understanding this is one of the most commonly needed calculations in math & science - I was surprised not to find a JS function for it.

edit: I would appreciate any hints how to improve the docs.

@josdejong
Copy link
Owner

Ah, get it. Yes this is indeed a common, useful function. I think we can best introduce this together with a set of related functions like numerical integration, differentiation and others. Do you have any suggestion on what other related functions we definitely need here?

As for the docs: I would include an API description. The ^local min and ^local max that you just added to the usage example helped me :). And maybe an image like the one from Wikipedia helps?

@balint42
Copy link
Author

balint42 commented Nov 5, 2014

As for what functions to provide: I think the list is very short if one plans to handle vectors / values only. Basically only differentiation to the n-th degree remains to be done and I will gladly do that since it's really trivial. One has to do:

X = [   X(1)      X(2)   ...    X(m-1)       X(m)     ]
=>
Y = [X(2)-X(1) X(3)-X(2) ... X(m)-X(m-1)]

Im not sure how much sense integration then makes as one can only calculate one special case (of infinite possible cases) but it could be done like this if I'm not mistaken:

Y = [X(2)-X(1) X(3)-X(2) ... X(m)-X(m-1)]
=>
X = [ ... -Y(m-1)-Y(m-2)-Y(m-3)  -Y(m-1)-Y(m-2)  -Y(m-1)]

@josdejong
Copy link
Owner

Ok thanks for your feedback. Let's keep this in mind when composing functionality in this area.

@balint42
Copy link
Author

balint42 commented Nov 6, 2014

Ok so I've implemented all of the above now. Take a look and if you decide that you want to have it, I can make a pull request.

@milahu
Copy link

milahu commented Mar 6, 2021

its been a while : D i pushed some commits to https://github.com/milahu/diff-js

now function extremaXY also detects plateaus at the start and end of the array

todo: detect rise-plateau-rise and fall-plateau-fall (delta y is near zero for multiple points in a row)

my use case: from a scatterplot, generate interpolations for x → y and for y → x
for the inverse function y → x, i must know the local extrema and plateaus

similar projects:

@josdejong
Copy link
Owner

Thanks for your inputs @milahu . Any idea on how we could integrate this nicely in mathjs? "Just" import these as new functions?

@milahu
Copy link

milahu commented Mar 8, 2021

i suggest adding a new category for "numerical analysis" or "finite element algebra" ...
im not a native speaker (and not a mathematician), maybe you know a better name

maybe "array analysis"? (more intuitive for programmers)
these functions always operate on arrays of data points

for the local extrema function, i would prefer a longish name like math.arrayLocalExtrema(data, options)
only arrayExtrema could be misleading, cos extrema can also mean the global extrema,
what the d3-array library calls extent

i only worked a bit on arrayLocalExtrema cos thats the only function i need
so arrayDifferentiate and arrayIntegrate will need some work
like simplify the API, support custom data formats, add tests, add comments, rename variables

@josdejong
Copy link
Owner

Makes sense to create a new category for this. Preferably a short name. How about "numerical"?

So this new category would basically contain numerical functions like diff (have to find an other name, diff already exists), integral, and extrema, right? That would be a great addition!

Repository owner locked and limited conversation to collaborators Aug 26, 2022
@josdejong josdejong converted this issue into discussion #2691 Aug 26, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

4 participants