linspace

Evenly spaced numbers over a specified interval.

@optmath
linspace
(
T
size_t N
)
(
size_t[N] lengths
,
T[2][N] intervals...
)
if (
N &&
(
isFloatingPoint!T ||
isComplex!T
)
)

Parameters

T

floating point or complex numbers type

lengths size_t[N]

list of dimension lengths. Each length must be greater then 1.

intervals T[2][N]

list of [start, end] pairs.

Return Value

Type: auto

n-dimensional grid of evenly spaced numbers over specified intervals.

Examples

1D

auto s = linspace!double([5], [1.0, 2.0]);
assert(s == [1.0, 1.25, 1.5, 1.75, 2.0]);

// reverse order
assert(linspace!double([5], [2.0, 1.0]) == s.retro);

// remove endpoint
s.popBack;
assert(s == [1.0, 1.25, 1.5, 1.75]);

2D

import mir.functional: refTuple;

auto s = linspace!double([5, 3], [1.0, 2.0], [0.0, 1.0]);

assert(s == [
    [refTuple(1.00, 0.00), refTuple(1.00, 0.5), refTuple(1.00, 1.0)],
    [refTuple(1.25, 0.00), refTuple(1.25, 0.5), refTuple(1.25, 1.0)],
    [refTuple(1.50, 0.00), refTuple(1.50, 0.5), refTuple(1.50, 1.0)],
    [refTuple(1.75, 0.00), refTuple(1.75, 0.5), refTuple(1.75, 1.0)],
    [refTuple(2.00, 0.00), refTuple(2.00, 0.5), refTuple(2.00, 1.0)],
    ]);

assert(s.map!"a * b" == [
    [0.0, 0.500, 1.00],
    [0.0, 0.625, 1.25],
    [0.0, 0.750, 1.50],
    [0.0, 0.875, 1.75],
    [0.0, 1.000, 2.00],
    ]);

Complex numbers

auto s = linspace!cdouble([3], [1.0 + 0i, 2.0 + 4i]);
assert(s == [1.0 + 0i, 1.5 + 2i, 2.0 + 4i]);

See Also

Meta