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

sample: Add "int" option to sampling domain #89

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/plot/sample.typ
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@
/// - fn (function): Function to sample of the form `(x) => y` or `(t) => (x, y)`, where
/// `x` or `t` are `float` values within the domain specified by `domain`.
/// - domain (domain): Domain of `fn` used as bounding interval for the sampling points.
/// - samples (int): Number of samples in domain.
/// - samples (int,str): Number of samples in domain or "INT" to use $"diam"("domain")$
/// number of samples (passed as int).
/// - sample-at (array): List of x values the function gets sampled at in addition
/// to the `samples` number of samples. Values outsides the
/// specified domain are legal.
/// -> array: Array of (x, y) tuples
#let sample-fn(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
if samples in ("int", "INT") {
samples = hi - lo + 1
fn = n => { fn(int(n)) }
}

assert(samples + sample-at.len() >= 2,
message: "You must at least sample 2 values")

let y0 = (fn)(lo)
let is-vector = type(y0) == array
Expand All @@ -29,6 +35,7 @@
y0 = (y0, )
}


let pts = sample-at + range(0, samples).map(t => lo + t / (samples - 1) * (hi - lo))
pts = pts.sorted()

Expand Down
Binary file added tests/plot/sample/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions tests/plot/sample/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#set page(width: auto, height: auto)
#import "/src/cetz.typ": *
#import "/src/lib.typ": *
#import "/tests/helper.typ": *

#let f(n) = {
assert(type(n) == int)
range(1, n+1).map(n => calc.pow(1/3, n)).sum(default: 0)
}

// Sample integer values
#test-case({
plot.plot(size: (3, 3), x-tick-step: none, y-tick-step: none,
{
plot.add(domain: (0, 7), samples: "INT", f, mark: "x")
})
})

// Take samples at specific points
#test-case({
plot.plot(size: (3, 3), x-tick-step: none, y-tick-step: none,
{
plot.add(domain: (0, 1), samples: 2, x => x, mark: "x",
sample-at: (.1, .2, .3))
})
})
Loading