Skip to content

Commit

Permalink
sample: Add "int" option to sampling domain
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-wolf committed Nov 23, 2024
1 parent 414eae7 commit c8c5803
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
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))
})
})

0 comments on commit c8c5803

Please sign in to comment.