Struct curve_sampling::Sampling
source · 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) -> impl Iterator<Item = [f64; 2]> + '_
pub fn iter(&self) -> impl Iterator<Item = [f64; 2]> + '_
Iterate 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
,…
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?
6 7 8 9 10 11 12 13 14 15 16 17 18
fn main() -> Result<(), Box<dyn Error>> {
let s = cs::Sampling::from_iter(
[[0., -0.5], [1.5, 1.], [0.2, 0.5], [0.3, 1.5], [1., 0.6],
[NAN, NAN], [-0.5, 0.5], [-1., 0.], [0.5, 0.5]]);
s.write(&mut File::create("/tmp/clip0.dat")?)?;
s.latex().write(&mut File::create("/tmp/clip0.tex")?)?;
let s1 = s.clip(cs::BoundingBox { xmin: 0., xmax: 1.,
ymin: 0., ymax: 1. });
s1.write(&mut File::create("/tmp/clip1.dat")?)?;
s1.latex().write(&mut File::create("/tmp/clip1.tex")?)?;
Ok(())
}
source§impl Sampling
impl Sampling
sourcepub fn uniform<F>(f: F, a: f64, b: f64) -> Uniform<F>where
F: FnMut(f64) -> f64,
pub fn uniform<F>(f: F, a: f64, b: f64) -> Uniform<F>where
F: FnMut(f64) -> f64,
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?
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() -> Result<(), Box<dyn Error>> {
let path = "/tmp/latex_speed.tex";
let mut fh = File::create(path)?;
write!(fh, "\\documentclass[12pt,a4paper]{{article}}\n\
\\usepackage{{tikz}}\n\
\\begin{{document}}\n\
\\begin{{tikzpicture}}\n")?;
let n = 40_000;
println!("Run \"latex {}\" measure LaTeX speed with {} points.\n",
path, n);
let s = Sampling::uniform(f64::sin, -6., 6.).n(n).build();
s.latex().write(&mut fh)?;
write!(fh, "\\end{{tikzpicture}}\n\
\\end{{document}}")?;
Ok(())
}
More examples
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
fn main() -> Result<(), Box<dyn Error>> {
let s = Sampling::from_iter([[0., 0.], [1., 1.], [NAN, NAN],
[1., 1.], [3., -1.]]);
s.latex().arrow_pos(0.3).write(&mut File::create("/tmp/arrow0.tex")?)?;
let s = Sampling::uniform(|x| -0.7 * (x - 1.).powi(2), 0., 2.5).build();
s.latex().arrow_pos(0.6).write(&mut File::create("/tmp/arrow1.tex")?)?;
File::create("/tmp/arrows.tex")?.write_all(
r"\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[x=3cm, y=3cm]
\draw[->] (-1.2, 0) -- (3.2, 0);
\draw[->] (0, -1.2) -- (0, 1.7);
\foreach \x in {-1, -0.5, 0.5, 1, 1.5,..., 3}{
\draw (\x, 3pt) -- (\x, -3pt) node[below]{$\scriptstyle \x$};
}
\foreach \y in {-1, -0.5, 0.5, 1, 1.5}{
\draw (3pt, \y) -- (-3pt, \y) node[left]{$\scriptstyle \y$};
}
\begin{scope}[color=blue, line width=1pt]
\input{arrow0.tex}
\end{scope}
\begin{scope}[color=orange, line width=1pt]
\input{arrow1.tex}
\end{scope}
\end{tikzpicture}
\end{document}".as_bytes())?;
std::env::set_current_dir("/tmp")?;
Command::new("pdflatex")
.args(["-interaction=batchmode", "arrows.tex"]).output()?;
Ok(())
}
source§impl Sampling
impl Sampling
sourcepub fn fun<F>(f: F, a: f64, b: f64) -> Fun<F>where
F: FnMut(f64) -> f64,
pub fn fun<F>(f: F, a: f64, b: f64) -> Fun<F>where
F: FnMut(f64) -> f64,
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
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
fn main() -> R {
let mut fh = File::create("/tmp/sin_inv_x.gp")?;
write!(fh, "set terminal pngcairo\n\
set grid\n")?;
let mut d = 0;
let mut save = |s: &Sampling, n, title| -> R {
d += 1;
let fname = format!("/tmp/sin_inv_x{}.dat", d);
s.write(&mut File::create(&fname)?)?;
write!(fh, "set output \"sin_inv_x{}.png\"\n\
plot '{}' with l lt 1 lw 2 title \"{} ({} pts)\"\n",
d, &fname, title, n)?;
write!(fh, "set output \"sin_inv_x{}_p.png\"\n\
plot '{}' with l lt 5 lw 2 title \"{}\", \
'{}' with p lt 3 pt 5 ps 0.2 title \"points ({})\"\n",
d, &fname, title, &fname, n)?;
Ok(())
};
let f = |x: f64| x * (1. / x).sin();
let s = Sampling::fun(f, -0.4, 0.4).n(227).build();
save(&s, 227, "x sin(1/x)")?;
let s = Sampling::fun(f, -0.4, 0.4).n(389).build();
save(&s, 389, "x sin(1/x)")?;
let s = Sampling::fun(|x: f64| (1. / x).sin(), -0.4, 0.4).n(391).build();
save(&s, 391, "sin(1/x)")?;
Ok(())
}
source§impl Sampling
impl Sampling
sourcepub fn param<F>(f: F, a: f64, b: f64) -> Param<F>where
F: FnMut(f64) -> [f64; 2],
pub fn param<F>(f: F, a: f64, b: f64) -> Param<F>where
F: FnMut(f64) -> [f64; 2],
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
impl Sampling
sourcepub fn latex(&self) -> LaTeX<'_>
pub fn latex(&self) -> LaTeX<'_>
Write the sampling self
using PGF/TikZ commands.
Examples found in repository?
6 7 8 9 10 11 12 13 14 15 16 17 18
fn main() -> Result<(), Box<dyn Error>> {
let s = cs::Sampling::from_iter(
[[0., -0.5], [1.5, 1.], [0.2, 0.5], [0.3, 1.5], [1., 0.6],
[NAN, NAN], [-0.5, 0.5], [-1., 0.], [0.5, 0.5]]);
s.write(&mut File::create("/tmp/clip0.dat")?)?;
s.latex().write(&mut File::create("/tmp/clip0.tex")?)?;
let s1 = s.clip(cs::BoundingBox { xmin: 0., xmax: 1.,
ymin: 0., ymax: 1. });
s1.write(&mut File::create("/tmp/clip1.dat")?)?;
s1.latex().write(&mut File::create("/tmp/clip1.tex")?)?;
Ok(())
}
More examples
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() -> Result<(), Box<dyn Error>> {
let path = "/tmp/latex_speed.tex";
let mut fh = File::create(path)?;
write!(fh, "\\documentclass[12pt,a4paper]{{article}}\n\
\\usepackage{{tikz}}\n\
\\begin{{document}}\n\
\\begin{{tikzpicture}}\n")?;
let n = 40_000;
println!("Run \"latex {}\" measure LaTeX speed with {} points.\n",
path, n);
let s = Sampling::uniform(f64::sin, -6., 6.).n(n).build();
s.latex().write(&mut fh)?;
write!(fh, "\\end{{tikzpicture}}\n\
\\end{{document}}")?;
Ok(())
}
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
fn main() -> Result<(), Box<dyn Error>> {
let s = Sampling::from_iter([[0., 0.], [1., 1.], [NAN, NAN],
[1., 1.], [3., -1.]]);
s.latex().arrow_pos(0.3).write(&mut File::create("/tmp/arrow0.tex")?)?;
let s = Sampling::uniform(|x| -0.7 * (x - 1.).powi(2), 0., 2.5).build();
s.latex().arrow_pos(0.6).write(&mut File::create("/tmp/arrow1.tex")?)?;
File::create("/tmp/arrows.tex")?.write_all(
r"\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[x=3cm, y=3cm]
\draw[->] (-1.2, 0) -- (3.2, 0);
\draw[->] (0, -1.2) -- (0, 1.7);
\foreach \x in {-1, -0.5, 0.5, 1, 1.5,..., 3}{
\draw (\x, 3pt) -- (\x, -3pt) node[below]{$\scriptstyle \x$};
}
\foreach \y in {-1, -0.5, 0.5, 1, 1.5}{
\draw (3pt, \y) -- (-3pt, \y) node[left]{$\scriptstyle \y$};
}
\begin{scope}[color=blue, line width=1pt]
\input{arrow0.tex}
\end{scope}
\begin{scope}[color=orange, line width=1pt]
\input{arrow1.tex}
\end{scope}
\end{tikzpicture}
\end{document}".as_bytes())?;
std::env::set_current_dir("/tmp")?;
Command::new("pdflatex")
.args(["-interaction=batchmode", "arrows.tex"]).output()?;
Ok(())
}
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
6 7 8 9 10 11 12 13 14 15 16 17 18
fn main() -> Result<(), Box<dyn Error>> {
let s = cs::Sampling::from_iter(
[[0., -0.5], [1.5, 1.], [0.2, 0.5], [0.3, 1.5], [1., 0.6],
[NAN, NAN], [-0.5, 0.5], [-1., 0.], [0.5, 0.5]]);
s.write(&mut File::create("/tmp/clip0.dat")?)?;
s.latex().write(&mut File::create("/tmp/clip0.tex")?)?;
let s1 = s.clip(cs::BoundingBox { xmin: 0., xmax: 1.,
ymin: 0., ymax: 1. });
s1.write(&mut File::create("/tmp/clip1.dat")?)?;
s1.latex().write(&mut File::create("/tmp/clip1.tex")?)?;
Ok(())
}
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
fn main() -> R {
let mut fh = File::create("/tmp/sin_inv_x.gp")?;
write!(fh, "set terminal pngcairo\n\
set grid\n")?;
let mut d = 0;
let mut save = |s: &Sampling, n, title| -> R {
d += 1;
let fname = format!("/tmp/sin_inv_x{}.dat", d);
s.write(&mut File::create(&fname)?)?;
write!(fh, "set output \"sin_inv_x{}.png\"\n\
plot '{}' with l lt 1 lw 2 title \"{} ({} pts)\"\n",
d, &fname, title, n)?;
write!(fh, "set output \"sin_inv_x{}_p.png\"\n\
plot '{}' with l lt 5 lw 2 title \"{}\", \
'{}' with p lt 3 pt 5 ps 0.2 title \"points ({})\"\n",
d, &fname, title, &fname, n)?;
Ok(())
};
let f = |x: f64| x * (1. / x).sin();
let s = Sampling::fun(f, -0.4, 0.4).n(227).build();
save(&s, 227, "x sin(1/x)")?;
let s = Sampling::fun(f, -0.4, 0.4).n(389).build();
save(&s, 389, "x sin(1/x)")?;
let s = Sampling::fun(|x: f64| (1. / x).sin(), -0.4, 0.4).n(391).build();
save(&s, 391, "sin(1/x)")?;
Ok(())
}