Is there a linspace() like method in Math.Net
Asked Answered
S

3

5

Is there function in Math.Net like (MatLab/Octave/numpy)'s linspace() which takes 3 parameters (min, max, length) and creates an vector/array of evenly spaced values between min and max? It is not hard to implement but if there was a function already I would prefer to use that.

Stilted answered 11/6, 2013 at 14:13 Comment(0)
E
6

There is none exactly like linspace, but the signal generator comes quite close and creates an array:

SignalGenerator.EquidistantInterval(x => x, min, max, len)

I'm not fresh on the VB.net syntax, but I guess it's very close to C#.

In case you need a vector:

new DenseVector(SignalGenerator.EquidistantInterval(x => x, min, max, len))

Or you could implement it e.g. using the static Create function (in practice you may want to precompute the step):

DenseVector.Create(len, i => min + i*(max-min)/(len - 1.0))

Update 2013-12-14:

Since v3.0.0-alpha7 this is covered by two new functions:

  • Generate.LinearSpaced(length, a, b) -> MATLAB linspace(a, b, length)
  • Generate.LinearRange(a, [step], b) -> MATLAB a:step:b
Erythropoiesis answered 11/6, 2013 at 17:47 Comment(0)
I
1

I used this C# code to replicate the functionality of linspace (how numpy does it), feel free to use it.

public static float[] linspace(float startval, float endval, int steps)
{
    float interval = (endval / MathF.Abs(endval)) * MathF.Abs(endval - startval) / (steps - 1);
    return (from val in Enumerable.Range(0,steps)
            select startval + (val * interval)).ToArray(); 
}

Here is the VB Translation I made.

Public Function linspace(startval As Single, endval As Single, Steps As Integer) As Single()

    Dim interval As Single = (endval / Math.Abs(endval)) *(Math.Abs(endval - startval)) / (Steps - 1)
    Return (From val In Enumerable.Range(0, Steps) Select startval + (val * interval)).ToArray()

End Function

Use examples;

C#

float[] arr = linspace(-4,4,5)

VB

Dim arr as Single() = linspace(-4,4,5)

Result:

-4,-2,0,2,4
Isabellaisabelle answered 16/4, 2021 at 19:18 Comment(2)
Would you be able to do VB implementation for the OP as well?Cantatrice
I've just updated the original post with a translation of that code, for VBIsabellaisabelle
J
0

I checked the result from the code shown below and MATLAB linspace, it exactly matches. I myself use it for my research work in Monte Carlo implementations.

Below is the code image and the actual code.

static double[] LINSPACE(double StartValue, double EndValue, int numberofpoints)
    {

        double[] parameterVals = new double[numberofpoints];
        double increment = Math.Abs(StartValue - EndValue) / Convert.ToDouble(numberofpoints - 1);
        int j = 0; //will keep a track of the numbers 
        double nextValue = StartValue;
        for (int i = 0; i < numberofpoints; i++)
        {


            parameterVals.SetValue(nextValue, j);
            j++;
            if (j > numberofpoints)
            {
                throw new IndexOutOfRangeException();
            }
            nextValue = nextValue + increment;
        }
        return parameterVals;



    }

Code for creating a linspace function in C#

Joettejoey answered 14/4, 2020 at 5:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.