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

Optimize sample-fn #44

Open
sky126 opened this issue Sep 20, 2024 · 3 comments
Open

Optimize sample-fn #44

sky126 opened this issue Sep 20, 2024 · 3 comments
Assignees
Labels
🎁 feature New feature or request

Comments

@sky126
Copy link

sky126 commented Sep 20, 2024

Optimize sample-fn to make the curve smoother
sample-fn

#let sample-fn3(fn, domain, samples, sample-at: ()) = {
  assert(samples + sample-at.len() >= 2,
    message: "You must at least sample 2 values")
  assert(type(domain) == array and domain.len() == 2,
    message: "Domain must be a tuple")

  let (lo, hi) = domain

  let y0 = (fn)(lo)
  let is-vector = type(y0) == array
  if not is-vector {
    y0 = ((lo, y0), )
  } else {
    y0 = (y0, )
  }
  
  let pts = (lo,)
  let currentX = lo
  let lastX = lo
  let currentY = (fn)(lo)
  let lastY = (fn)(lo)
  let i = 0
  while i < 1e4 {
    i = i + 1
    if currentX - lastX > 1e-2 or  currentY - lastY > 1e-2 {
      pts.push(currentX)
      lastX = currentX
      lastY = currentY
    }
    currentX = currentX + 1e-4 * (hi - lo)
    currentY = (fn)(currentX)
  }
  pts.push(hi)
  
  return pts.map(x => {
    if is-vector {
      (fn)(x)
    } else {
      (x, (fn)(x))
    }
  })
}
@johannes-wolf johannes-wolf self-assigned this Sep 20, 2024
@johannes-wolf
Copy link
Member

Thank you!

@johannes-wolf johannes-wolf added the 🎁 feature New feature or request label Sep 25, 2024
@jamesrswift
Copy link
Collaborator

10k iterations?

@johannes-wolf
Copy link
Member

We could allow passing a custom sample-fn to plot.add & friends, or set a default one using set-style.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎁 feature New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants