pub struct Sampling { /* private fields */ }
Expand description
A 2D sampling. This can be thought as a path, with possible “cuts” because of discontinuities or leaving the domain of the (parametric) function describing the path.
Implementations§
Source§impl Sampling
impl Sampling
Sourcepub fn iter(&self) -> SamplingIter<'_> ⓘ
pub fn iter(&self) -> SamplingIter<'_> ⓘ
Returns an iterator on the points (and cuts) of the path.
More precisely, a path is made of continuous segments whose
points are given by contiguous values [x,y]
with both x
and y
not NaN, interspaced by “cuts” [f64::NAN; 2]
. Two
cuts never follow each other. Isolated points p
are given
by … [f64::NAN; 2]
, p
, None
,…
Sourcepub fn iter_mut(&mut self) -> SamplingIterMut<'_> ⓘ
pub fn iter_mut(&mut self) -> SamplingIterMut<'_> ⓘ
Returns an iterator that allows to modify the points and cuts
of the path. Unlike [iter
], this iterates on all the nodes
even if several cuts (i.e., node with a non finite coordinate)
follow each other.
Sourcepub fn x(&self) -> Vec<f64>
pub fn x(&self) -> Vec<f64>
Iterator on the x-coordinates of the sampling.
See Self::iter
for more information.
Sourcepub fn y(&self) -> Vec<f64>
pub fn y(&self) -> Vec<f64>
Iterator on the y-coordinates of the sampling.
See Self::iter
for more information.
Source§impl Sampling
impl Sampling
Sourcepub fn bounding_box(&self) -> BoundingBox
pub fn bounding_box(&self) -> BoundingBox
Return the smallest rectangle enclosing all the points of the
sampling self
. If the path is empty, the “min” fields of
the bounding box are set to +∞ and “max” fields to -∞.
Sourcepub fn transpose(&mut self) -> &mut Self
pub fn transpose(&mut self) -> &mut Self
Transpose in place the x and y coordinates of the sampling.
Sourcepub fn clip(&self, bb: BoundingBox) -> Self
pub fn clip(&self, bb: BoundingBox) -> Self
Returns the sampling self
but clipped to the 2D box bb
. A
path that crosses the boundary will get additional nodes at
the points of crossing and the part outside the bounding box
will be dropped. (Thus a path entirely out of the bounding
box will be removed.)
Examples found in repository?
6fn main() -> Result<(), Box<dyn Error>> {
7 let s = cs::Sampling::from_iter(
8 [[0., -0.5], [1.5, 1.], [0.2, 0.5], [0.3, 1.5], [1., 0.6],
9 [NAN, NAN], [-0.5, 0.5], [-1., 0.], [0.5, 0.5]]);
10 s.write(&mut File::create("/tmp/clip0.dat")?)?;
11 s.latex().write(&mut File::create("/tmp/clip0.tex")?)?;
12 let s1 = s.clip(cs::BoundingBox { xmin: 0., xmax: 1.,
13 ymin: 0., ymax: 1. });
14 s1.write(&mut File::create("/tmp/clip1.dat")?)?;
15 s1.latex().write(&mut File::create("/tmp/clip1.tex")?)?;
16
17 Ok(())
18}
Source§impl Sampling
impl Sampling
Sourcepub fn uniform<F>(f: F, a: f64, b: f64) -> Uniform<F>
pub fn uniform<F>(f: F, a: f64, b: f64) -> Uniform<F>
Create a sampling for the graph of f
on the interval
[a
, b
] with evenly spaced values of the argument.
Panics if a
or b
is not finite.
§Example
use std::{fs::File, io::BufWriter};
use curve_sampling::Sampling;
let s = Sampling::uniform(|x| x.sin(), 0., 4.).build();
s.write(&mut BufWriter::new(File::create("target/uniform.dat")?))?;
Examples found in repository?
6fn main() -> Result<(), Box<dyn Error>> {
7 let path = "/tmp/latex_speed.tex";
8 let mut fh = File::create(path)?;
9 write!(fh, "\\documentclass[12pt,a4paper]{{article}}\n\
10 \\usepackage{{tikz}}\n\
11 \\begin{{document}}\n\
12 \\begin{{tikzpicture}}\n")?;
13 let n = 40_000;
14 println!("Run \"latex {}\" measure LaTeX speed with {} points.\n",
15 path, n);
16 let s = Sampling::uniform(f64::sin, -6., 6.).n(n).build();
17 s.latex().write(&mut fh)?;
18 write!(fh, "\\end{{tikzpicture}}\n\
19 \\end{{document}}")?;
20 Ok(())
21}
More examples
8fn main() -> Result<(), Box<dyn Error>> {
9 let s = Sampling::from_iter([[0., 0.], [1., 1.], [NAN, NAN],
10 [1., 1.], [3., -1.]]);
11 s.latex().arrow_pos(0.3).write(&mut File::create("/tmp/arrow0.tex")?)?;
12
13 let s = Sampling::uniform(|x| -0.7 * (x - 1.).powi(2), 0., 2.5).build();
14 s.latex().arrow_pos(0.6).write(&mut File::create("/tmp/arrow1.tex")?)?;
15
16
17 File::create("/tmp/arrows.tex")?.write_all(
18 r"\documentclass{article}
19\usepackage{tikz}
20\begin{document}
21\begin{tikzpicture}[x=3cm, y=3cm]
22 \draw[->] (-1.2, 0) -- (3.2, 0);
23 \draw[->] (0, -1.2) -- (0, 1.7);
24 \foreach \x in {-1, -0.5, 0.5, 1, 1.5,..., 3}{
25 \draw (\x, 3pt) -- (\x, -3pt) node[below]{$\scriptstyle \x$};
26 }
27 \foreach \y in {-1, -0.5, 0.5, 1, 1.5}{
28 \draw (3pt, \y) -- (-3pt, \y) node[left]{$\scriptstyle \y$};
29 }
30 \begin{scope}[color=blue, line width=1pt]
31 \input{arrow0.tex}
32 \end{scope}
33 \begin{scope}[color=orange, line width=1pt]
34 \input{arrow1.tex}
35 \end{scope}
36\end{tikzpicture}
37\end{document}".as_bytes())?;
38
39 std::env::set_current_dir("/tmp")?;
40 Command::new("pdflatex")
41 .args(["-interaction=batchmode", "arrows.tex"]).output()?;
42 Ok(())
43}
Source§impl Sampling
impl Sampling
Sourcepub fn fun<F>(f: F, a: f64, b: f64) -> Fun<F>
pub fn fun<F>(f: F, a: f64, b: f64) -> Fun<F>
Create a sampling of the graph of f
on the interval
[a
, b
] by evaluating f
at n
points.
Panics if a
or b
is not finite.
§Example
use std::{fs::File, io::BufWriter};
use curve_sampling::Sampling;
let s = Sampling::fun(|x| x.sin(), 0., 4.).build();
s.write(&mut BufWriter::new(File::create("target/fun.dat")?))?;
Examples found in repository?
More examples
9fn main() -> R {
10 let mut fh = File::create("/tmp/sin_inv_x.gp")?;
11 write!(fh, "set terminal pngcairo\n\
12 set grid\n")?;
13 let mut d = 0;
14 let mut save = |s: &Sampling, n, title| -> R {
15 d += 1;
16 let fname = format!("/tmp/sin_inv_x{}.dat", d);
17 s.write(&mut File::create(&fname)?)?;
18 write!(fh, "set output \"sin_inv_x{}.png\"\n\
19 plot '{}' with l lt 1 lw 2 title \"{} ({} pts)\"\n",
20 d, &fname, title, n)?;
21 write!(fh, "set output \"sin_inv_x{}_p.png\"\n\
22 plot '{}' with l lt 5 lw 2 title \"{}\", \
23 '{}' with p lt 3 pt 5 ps 0.2 title \"points ({})\"\n",
24 d, &fname, title, &fname, n)?;
25 Ok(())
26 };
27
28 let f = |x: f64| x * (1. / x).sin();
29 let s = Sampling::fun(f, -0.4, 0.4).n(227).build();
30 save(&s, 227, "x sin(1/x)")?;
31 let s = Sampling::fun(f, -0.4, 0.4).n(389).build();
32 save(&s, 389, "x sin(1/x)")?;
33
34 let s = Sampling::fun(|x: f64| (1. / x).sin(), -0.4, 0.4).n(391).build();
35 save(&s, 391, "sin(1/x)")?;
36
37 Ok(())
38}
Source§impl Sampling
impl Sampling
Sourcepub fn param<F>(f: F, a: f64, b: f64) -> Param<F>
pub fn param<F>(f: F, a: f64, b: f64) -> Param<F>
Create a sampling of the image of f
on the interval
[a
, b
] by evaluating f
at n
points.
Panics if a
or b
is not finite.
§Example
use std::{fs::File, io::BufWriter};
use curve_sampling::Sampling;
let s = Sampling::param(|t| [t.sin(), t.cos()], 0., 4.).build();
s.write(&mut BufWriter::new(File::create("target/param.dat")?))?;
Source§impl Sampling
§Output
impl Sampling
§Output
Sourcepub fn latex(&self) -> LaTeX<'_>
pub fn latex(&self) -> LaTeX<'_>
Write the sampling self
using PGF/TikZ commands.
Examples found in repository?
6fn main() -> Result<(), Box<dyn Error>> {
7 let s = cs::Sampling::from_iter(
8 [[0., -0.5], [1.5, 1.], [0.2, 0.5], [0.3, 1.5], [1., 0.6],
9 [NAN, NAN], [-0.5, 0.5], [-1., 0.], [0.5, 0.5]]);
10 s.write(&mut File::create("/tmp/clip0.dat")?)?;
11 s.latex().write(&mut File::create("/tmp/clip0.tex")?)?;
12 let s1 = s.clip(cs::BoundingBox { xmin: 0., xmax: 1.,
13 ymin: 0., ymax: 1. });
14 s1.write(&mut File::create("/tmp/clip1.dat")?)?;
15 s1.latex().write(&mut File::create("/tmp/clip1.tex")?)?;
16
17 Ok(())
18}
More examples
6fn main() -> Result<(), Box<dyn Error>> {
7 let path = "/tmp/latex_speed.tex";
8 let mut fh = File::create(path)?;
9 write!(fh, "\\documentclass[12pt,a4paper]{{article}}\n\
10 \\usepackage{{tikz}}\n\
11 \\begin{{document}}\n\
12 \\begin{{tikzpicture}}\n")?;
13 let n = 40_000;
14 println!("Run \"latex {}\" measure LaTeX speed with {} points.\n",
15 path, n);
16 let s = Sampling::uniform(f64::sin, -6., 6.).n(n).build();
17 s.latex().write(&mut fh)?;
18 write!(fh, "\\end{{tikzpicture}}\n\
19 \\end{{document}}")?;
20 Ok(())
21}
8fn main() -> Result<(), Box<dyn Error>> {
9 let s = Sampling::from_iter([[0., 0.], [1., 1.], [NAN, NAN],
10 [1., 1.], [3., -1.]]);
11 s.latex().arrow_pos(0.3).write(&mut File::create("/tmp/arrow0.tex")?)?;
12
13 let s = Sampling::uniform(|x| -0.7 * (x - 1.).powi(2), 0., 2.5).build();
14 s.latex().arrow_pos(0.6).write(&mut File::create("/tmp/arrow1.tex")?)?;
15
16
17 File::create("/tmp/arrows.tex")?.write_all(
18 r"\documentclass{article}
19\usepackage{tikz}
20\begin{document}
21\begin{tikzpicture}[x=3cm, y=3cm]
22 \draw[->] (-1.2, 0) -- (3.2, 0);
23 \draw[->] (0, -1.2) -- (0, 1.7);
24 \foreach \x in {-1, -0.5, 0.5, 1, 1.5,..., 3}{
25 \draw (\x, 3pt) -- (\x, -3pt) node[below]{$\scriptstyle \x$};
26 }
27 \foreach \y in {-1, -0.5, 0.5, 1, 1.5}{
28 \draw (3pt, \y) -- (-3pt, \y) node[left]{$\scriptstyle \y$};
29 }
30 \begin{scope}[color=blue, line width=1pt]
31 \input{arrow0.tex}
32 \end{scope}
33 \begin{scope}[color=orange, line width=1pt]
34 \input{arrow1.tex}
35 \end{scope}
36\end{tikzpicture}
37\end{document}".as_bytes())?;
38
39 std::env::set_current_dir("/tmp")?;
40 Command::new("pdflatex")
41 .args(["-interaction=batchmode", "arrows.tex"]).output()?;
42 Ok(())
43}
Sourcepub fn write(&self, f: &mut impl Write) -> Result<(), Error>
pub fn write(&self, f: &mut impl Write) -> Result<(), Error>
Write the sampling to f
in a tabular form: each point is
written as “x y” on a single line (in scientific notation).
If the path is interrupted, a blank line is printed. This
format is compatible with Gnuplot.
Examples found in repository?
More examples
6fn main() -> Result<(), Box<dyn Error>> {
7 let s = cs::Sampling::from_iter(
8 [[0., -0.5], [1.5, 1.], [0.2, 0.5], [0.3, 1.5], [1., 0.6],
9 [NAN, NAN], [-0.5, 0.5], [-1., 0.], [0.5, 0.5]]);
10 s.write(&mut File::create("/tmp/clip0.dat")?)?;
11 s.latex().write(&mut File::create("/tmp/clip0.tex")?)?;
12 let s1 = s.clip(cs::BoundingBox { xmin: 0., xmax: 1.,
13 ymin: 0., ymax: 1. });
14 s1.write(&mut File::create("/tmp/clip1.dat")?)?;
15 s1.latex().write(&mut File::create("/tmp/clip1.tex")?)?;
16
17 Ok(())
18}
9fn main() -> R {
10 let mut fh = File::create("/tmp/sin_inv_x.gp")?;
11 write!(fh, "set terminal pngcairo\n\
12 set grid\n")?;
13 let mut d = 0;
14 let mut save = |s: &Sampling, n, title| -> R {
15 d += 1;
16 let fname = format!("/tmp/sin_inv_x{}.dat", d);
17 s.write(&mut File::create(&fname)?)?;
18 write!(fh, "set output \"sin_inv_x{}.png\"\n\
19 plot '{}' with l lt 1 lw 2 title \"{} ({} pts)\"\n",
20 d, &fname, title, n)?;
21 write!(fh, "set output \"sin_inv_x{}_p.png\"\n\
22 plot '{}' with l lt 5 lw 2 title \"{}\", \
23 '{}' with p lt 3 pt 5 ps 0.2 title \"points ({})\"\n",
24 d, &fname, title, &fname, n)?;
25 Ok(())
26 };
27
28 let f = |x: f64| x * (1. / x).sin();
29 let s = Sampling::fun(f, -0.4, 0.4).n(227).build();
30 save(&s, 227, "x sin(1/x)")?;
31 let s = Sampling::fun(f, -0.4, 0.4).n(389).build();
32 save(&s, 389, "x sin(1/x)")?;
33
34 let s = Sampling::fun(|x: f64| (1. / x).sin(), -0.4, 0.4).n(391).build();
35 save(&s, 391, "sin(1/x)")?;
36
37 Ok(())
38}