Skip to main content

Fun

Struct Fun 

Source
pub struct Fun<F, D> { /* private fields */ }
Expand description

Options for sampling graphs of functions ℝ → ℝ and ℝ → ℝ². See Sampling::fun.

Implementations§

Source§

impl<F, Y, D> Fun<F, D>
where F: FnMut(f64) -> Y, Y: Img<D>,

Source

pub fn n(self, n: usize) -> Self

Set the maximum number of evaluations of the function to build the sampling. Panic if n < 2.

Examples found in repository?
examples/nice.rs (line 13)
7fn main() -> Result<(), Box<dyn Error>> {
8    let f = |t: f64| [t.cos(), (2. * t).sin()];
9    let s = Sampling::fun(f, 0., 2. * PI).build();
10    s.write(&mut BufWriter::new(File::create("/tmp/nice1.dat")?))?;
11
12    let f = |x: f64| (- x.powi(2)).exp();
13    let s = Sampling::fun(f, -2.5, 2.5).n(53).build();
14    s.write(&mut BufWriter::new(File::create("/tmp/nice2.dat")?))?;
15    Ok(())
16}
More examples
Hide additional examples
examples/sin_inv_x.rs (line 29)
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

pub fn viewport(self, vp: BoundingBox) -> Self

Set the zone of interest for the sampling. Segments that end up outside this box will not be refined.

Source

pub fn init<'a, I>(self, ts: I) -> Self
where I: IntoIterator<Item = &'a f64>,

Add initial values of t such that f(t) (see [Sampling :: fun]) must be included into the sampling in addition to the n evaluations. Only the values between a and b are taken into account (other values are ignored).

Source

pub fn init_pt<I>(self, pts: I) -> Self
where I: IntoIterator<Item = (f64, Y)>,

Add initial points (t, f(t)) to include into the sampling in addition to the n evaluations. This allows you to use previous evaluations of f. Only the couples with first coordinate t between a and b (see [Sampling :: fun]) are considered (other values are ignored).

Source§

impl<F, Y, D> Fun<F, D>
where F: FnMut(f64) -> Y, Y: Img<D>,

Source

pub fn build(self) -> Sampling<D>

Return the sampling.

Examples found in repository?
examples/nice.rs (line 9)
7fn main() -> Result<(), Box<dyn Error>> {
8    let f = |t: f64| [t.cos(), (2. * t).sin()];
9    let s = Sampling::fun(f, 0., 2. * PI).build();
10    s.write(&mut BufWriter::new(File::create("/tmp/nice1.dat")?))?;
11
12    let f = |x: f64| (- x.powi(2)).exp();
13    let s = Sampling::fun(f, -2.5, 2.5).n(53).build();
14    s.write(&mut BufWriter::new(File::create("/tmp/nice2.dat")?))?;
15    Ok(())
16}
More examples
Hide additional examples
examples/sin_inv_x.rs (line 29)
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}

Auto Trait Implementations§

§

impl<F, D> Freeze for Fun<F, D>
where F: Freeze,

§

impl<F, D> !RefUnwindSafe for Fun<F, D>

§

impl<F, D> !Send for Fun<F, D>

§

impl<F, D> !Sync for Fun<F, D>

§

impl<F, D> Unpin for Fun<F, D>
where F: Unpin, D: Unpin,

§

impl<F, D> !UnwindSafe for Fun<F, D>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.