Skip to content

sigurd4/linspace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status (nightly) Build Status (nightly, all features)

Build Status (stable) Build Status (stable, all features)

Test Status Lint Status

Latest Version License:MIT Documentation Coverage Status

linspace

Turns a range into a linearly spaced sequence of values.

  • Linspace::linspace returns an iterator.

  • Linspace::linspace_array returns an array.

Only works on bounded ranges like Range and RangeInclusive.

Examples

Both of these will print [0, 25, 50, 75].

use linspace::*;

let x: Vec<u32> = (0..100).linspace(4).collect();
assert_eq!(x, [0, 25, 50, 75]);
println!("{:?}", x);

let y: [u32; 4] = (0..100).linspace_array();
assert_eq!(y, [0, 25, 50, 75]);
println!("{:?}", y);

assert_eq!(x, y);

Both inclusive and exclusive ranges can be used. And these will print [0, 25, 50, 75, 100].

use linspace::*;

let x: Vec<u32> = (0..=100).linspace(5).collect();
assert_eq!(x, [0, 25, 50, 75, 100]);
println!("{:?}", x);

let y: [u32; 5] = (0..=100).linspace_array();
assert_eq!(y, [0, 25, 50, 75, 100]);
println!("{:?}", y);

assert_eq!(x, y);

Want a non-linear range? That's also possible. After all, Linspace::linspace just returns an Iterator.

use linspace::*;

let x: Vec<f32> = (0.0..1.0)
    .linspace(10)
    .map(|z| 10.0f32.powf(z))
    .collect();
println!("{:?}", x);

Very convenient!

About

Turns a range into a linearly spaced sequence of values.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages