Home > database >  C# Mathnet Numerics Polynomial Evaluate across an vector/array, do I have to loop?
C# Mathnet Numerics Polynomial Evaluate across an vector/array, do I have to loop?

Time:11-10

Ok, so I'm attempting to fumble my way through a Matlab source code conversion into C#. Thus far I've been able to do everything without needing to implement any loops.

data = vector of data Fourier.Forward(data.AsArray()) takes in data and overwrites the original vector with the results. Vector.ABS(data) calculates the absolute value across the vector Polynomial.Fit(x.AsArray(), data.Array()) calculates and returns the coefficients for the polynomial

but now I need to plot and find the Y value based on the X. The only function I can find is

Polynomial.Evaluate, but it takes in a single X value, and returns the Y. Is there a function that will take in my entire vector or as an array, that will return a single vector/array of results?

CodePudding user response:

You can use Linq's Select.

If you have a collection of X in xCol, and Polynomial.Evaluate evaluates an X into a Y, you can do the following:

var yCol = xCol.Select(x => Polynomial.Evaluate(x)).

It's effectively a loop, but it looks and acts like a vector function

The way it works is that the lamba you specified

x => Polynomial.Evaluate(x)

Is applied to each member of xCol, resulting in an output enumerable collection.

You can read that lambda as "given an x, return Polynomial.Evaluate(x)"

  • Related