This project has moved and is read-only. For the latest updates, please go here.

Using MathNet efficiently questions

Jul 2, 2012 at 6:59 AM

Hi there I have to question for creating an app (where speed is critical)

What the faster way to do this :


1_ Operation result gives me a n lenght vector , I need to replace this vector with a n+1 lenght copy where vector[n] = 1.0;


2_ Fastest way to create a n*m matrix filled with random numbers (distribution ?)


thank you very much

Jul 14, 2012 at 9:24 AM

That depends very much on what "speed is critical" means in practice. In any case you'd need to benchmark your specific scenario, but some pointers:

  1.  Assuming a dense double vector, the fastest way will likely be along the lines of:

    var raw = (double[])vector;
    var raw2 = new double[raw.Length + 1];
    Buffer.BlockCopy(raw, 0, raw2, 0, raw.Length * Constants.SizeOfDouble);
    raw2[raw2.Length-1] = 1d; var vector2 = (DenseVector)raw2;
  2. Slow: DoRandom() method. Faster:

    const rows = 100, cols = 200;
    var dist = new Normal();
    var matrix = new DenseMatrix(rows, cols, dist.Samples().Take(rows * cols).ToArray());