Using Matrix types in VB

Feb 11, 2013 at 5:14 PM

I have a bit of a problem understanding how best to use some of the matrix methods in VB. For example:
Option Explicit On
Option Strict On
Option Infer On

Imports MathNet.Numerics.LinearAlgebra.Double

Public Class myClass

Private Sub GetDiagonalFromQR()

            Dim tempVectorList As New List(Of DenseVector)

            'Code to populate list with DenseVectors here...

            Dim tempXMatrix As DenseMatrix = DenseMatrix.CreateFromColumns(tempVectorList)

            Dim diagonal() As Double = tempXMatrix.GramSchmidt.R.Diagonal.ToArray

        End Sub
End Class
This gives me an error when I have Option Strict On because the CreateFromColumns method is looking for a
System.Collections.Generic.IList(Of MathNet.Numerics.LinearAlgebra.Generic.Vector(Of Double))
Is there a way of getting round this? If I use Option Strict Off then the conversions between types are implicit but obviously this is bad practice.

Thanks for your help,
Feb 11, 2013 at 8:56 PM
Yes, CreateFromColumns would be more flexible if instead of IList it used IEnumerable, which is covariant. We may want to change that in the future.

For now the following options come to mind that should all work (C#, I'm not fluent in but it should work the same way):
  • Declare your vector list with the base type instead (i.e. what the compiler suggests):
var vs = new List<Vector<double>>();
  • Convert the list to an array using ToArray, leveraging a small (although a bit nasty) trick around array covariance:
var m = Matrix.CreateFromColumns(vs.ToArray());
  • Cast properly, using Linq:
var m = Matrix.CreateFromColumns(vs.Cast<Vector<double>>().ToList());
  • Convert the list:
var m = Matrix.CreateFromColumns(vs.ConvertAll(x => (Vector<double>)x));
Marked as answer by cdrnet on 10/3/2013 at 5:49 PM
Feb 12, 2013 at 11:37 AM
Thanks for your help - I'll have a go!