1

I'm pretty new in C# and I would like to use it to write a software for the calibration of an AFM cantilever. Therefor, I would need to fit a pretty ugly function to the first peak of some none linear data. I thought the ALGLIB package might come in handy for this purpose, which seems relatively easy to apply. In the example of their homepage they use for their X-axis data:

double[,] x = new double[,]{{-1},{-0.8},{-0.6},{-0.4},{-0.2},{0},{0.2},{0.4},{0.6},{0.8},{1.0}};

I have my X-values in a list and would like to use them. How do I get them out of the list and into the double[,]? My last approach was something like this

List<double> xz = new List<double>();
...
double[,] x = new double[,]{xz.ToArray()};

It seems like it has to be in a double[,] array since it's mandatory for the function later

alglib.lsfitcreatef(x, y, c, diffstep, out state);

Could anybody help me with that? Or does anybody can recommend an easier approach for the non-linear fit?

thanks a lot in advance

matjes
  • 25
  • 3

2 Answers2

1

Here's a simple way to copy the values from your list to your array:

List<double> xz = // something
double[,] x = new double[xz.Count, 1];
for (int i = 0; i < xz.Count; i++)
{
    x[i, 0] = xz[i];
}

I couldn't find a better method than this manual copying. ToArray() creates an incompatible type (double[]), and Array.Copy expects the arrays to have the same rank (i.e. number of dimensions).

Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • Sometimes brute force is the only (or even best) way! – DonBoitnott Jun 06 '13 at 15:27
  • Yes it is. Sometimes brute force can look ugly, but actually be one of the fastest ways to do it! (e.g. most anything with LINQ: you could usually write faster code trivially, but it's uglier) – Tim S. Jun 06 '13 at 15:35
0

That example is a multi-dimensional array: http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

...but it doesn't look to be configured quite right.

Regardless, your list is one dimension.

Therefore, the equivalent would be:

List<Double> xz = new List<Double>();
Double[] myDblArray = xz.ToArray();
DonBoitnott
  • 10,787
  • 6
  • 49
  • 68
  • I know, I was also a bit confused why the ALGLIB want's to have a multi-dimensional array at this point. – matjes Jun 06 '13 at 15:26