Struct curve_sampling::Fun

source ·
pub struct Fun<F> { /* private fields */ }
Expand description

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

Implementations§

source§

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

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/speed.rs (line 38)
34
35
36
37
38
39
40
41
42
fn main() {
    let mut b = false;
    for n in 10 .. 5_000 {
        let s = Sampling::fun(|x| x.sin(), 0., 10.)
            .n(n).build();
        b &= s.is_empty();
    }
    println!("{b}")
}
More examples
Hide additional examples
examples/nice.rs (line 13)
7
8
9
10
11
12
13
14
15
16
fn main() -> Result<(), Box<dyn Error>> {
    let f = |t: f64| [t.cos(), (2. * t).sin()];
    let s = Sampling::param(f, 0., 2. * PI).build();
    s.write(&mut BufWriter::new(File::create("/tmp/nice1.dat")?))?;

    let f = |x: f64| (- x.powi(2)).exp();
    let s = Sampling::fun(f, -2.5, 2.5).n(53).build();
    s.write(&mut BufWriter::new(File::create("/tmp/nice2.dat")?))?;
    Ok(())
}
examples/sin_inv_x.rs (line 29)
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

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) -> Selfwhere 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<'a, I>(self, pts: I) -> Selfwhere I: IntoIterator<Item = &'a (f64, f64)>,

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> Fun<F>where F: FnMut(f64) -> f64,

source

pub fn build(self) -> Sampling

Return the sampling.

Examples found in repository?
examples/speed.rs (line 38)
34
35
36
37
38
39
40
41
42
fn main() {
    let mut b = false;
    for n in 10 .. 5_000 {
        let s = Sampling::fun(|x| x.sin(), 0., 10.)
            .n(n).build();
        b &= s.is_empty();
    }
    println!("{b}")
}
More examples
Hide additional examples
examples/nice.rs (line 13)
7
8
9
10
11
12
13
14
15
16
fn main() -> Result<(), Box<dyn Error>> {
    let f = |t: f64| [t.cos(), (2. * t).sin()];
    let s = Sampling::param(f, 0., 2. * PI).build();
    s.write(&mut BufWriter::new(File::create("/tmp/nice1.dat")?))?;

    let f = |x: f64| (- x.powi(2)).exp();
    let s = Sampling::fun(f, -2.5, 2.5).n(53).build();
    s.write(&mut BufWriter::new(File::create("/tmp/nice2.dat")?))?;
    Ok(())
}
examples/sin_inv_x.rs (line 29)
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(())
}

Auto Trait Implementations§

§

impl<F> RefUnwindSafe for Fun<F>where F: RefUnwindSafe,

§

impl<F> !Send for Fun<F>

§

impl<F> !Sync for Fun<F>

§

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

§

impl<F> UnwindSafe for Fun<F>where F: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.