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
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
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 VB.net 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());
var m = Matrix.CreateFromColumns(vs.ConvertAll(x => (Vector<double>)x));
Thanks for your help - I'll have a go!