This project has moved. For the latest updates, please go here.

Getting C# examples to compile in VS2012

Jun 22, 2014 at 9:36 AM
As a beginner I have worked out how to get the Math.net examples to compile and run in VS2012 here Stackoverflow
For an implementation of an Echo State Network I need to calculate the spectral radius of a matrix. I think this can be achieved by taking the AbsoluteMaximum() of the EigenValues as a complex vector.
To see if this indeed works, I want use the Math.net example on factorization and eigenvalues calculation Evd.cs So I pasted the body of the example into a new console application as described above and added the required namespace.
Yet this does not compile fully. There appears to be an issue with the Eigenvectors. I get following error (multiple times): error CS0119: 'MathNet.Numerics.LinearAlgebra.Generic.Factorization.Evd<T>.EigenVectors()' is a 'method', which is not valid in the given context
This error pops up in the call: Console.WriteLine(evd.EigenVectors.ToString("#0.00\t", formatProvider)); and every other time that evd.EigenVectors is called.

What do I need to add / change to get this example to compile?

Following is a snippet of the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization; // for formatprovider
using MathNet.Numerics.LinearAlgebra.Double;

namespace MathNet_eigenvalue_demo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Format matrix output to console
            var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
            formatProvider.TextInfo.ListSeparator = " ";

            // Create square symmetric matrix
            var matrix = DenseMatrix.OfArray(new[,] { { 1.0, 2.0, 3.0 }, { 2.0, 1.0, 4.0 }, { 3.0, 4.0, 1.0 } });
            Console.WriteLine(@"Initial square symmetric matrix");
            Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // Perform eigenvalue decomposition of symmetric matrix
            var evd = matrix.Evd();
            Console.WriteLine(@"Perform eigenvalue decomposition of symmetric matrix");

            // 1. Eigen vectors
            Console.WriteLine(@"1. Eigen vectors");
            Console.WriteLine(evd.EigenVectors.ToString("#0.00\t", formatProvider));
            Console.WriteLine();
        }
    }
}


     
Coordinator
Jun 24, 2014 at 6:06 PM
Ah, the samples have already been updated to v3 (except probability distributions, but I've just fixed them in master), but it seems you try to use them against v2. Can you upgrade your references to Math.NET Numerics v3 and try again?

Thanks,
Christoph
Jun 28, 2014 at 3:40 PM
Edited Jun 28, 2014 at 3:40 PM
Thanks Christoph, that indeed takes things a bit further. I now get following build error: error CS0012: The type 'System.Numerics.Complex' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
However, if I add:
using MathNet.Numerics.LinearAlgebra.Complex;
error CS0104: 'DenseMatrix' is an ambiguous reference between 'MathNet.Numerics.LinearAlgebra.Double.DenseMatrix' and 'MathNet.Numerics.LinearAlgebra.Complex.DenseMatrix'

And besides, this does not solve the original error either. So the Complex include must be somewhere else. System.Numerics does not exist as an inlude.
The example doesn't provide any clues, so I am a bit at a loss.

Jan
Coordinator
Jun 30, 2014 at 3:22 PM
I've just:
  • created an empty project in VS 2012
  • added the v3.0.2 NuGet package of Math.NET Numerics
  • copied over the body of the Main function you pasted
  • Added the "MathNet.Numerics.LinearAlgebra.Double" namespace
and it worked fine for me. I've uploaded it to http://1drv.ms/1vo80rz (also includes a small variation of the example since the custom formatting is no longer needed).

Does this version work for you? Can you spot a difference between this and your project? I'm certainly interested in what exactly is causing your problem.

Thanks,
Christoph
Marked as answer by cdrnet on 7/2/2014 at 1:24 PM
Jul 1, 2014 at 7:22 PM
What was missing is a reference to System.Numerics which is apparently required, but not referenced by default (in my case / VS config) when I create a new project. System.Numerics is part of VS and can be found in the Reference manager under Assemblies\Framework. With that included it runs fine.

Below I have copied your improved example to share the new formatting with others.
using System;
using System.Globalization;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;

namespace MathNetNumericsExamplesVS11
{
    class Program
    {
        static void Main(string[] args)
        {
            OriginalExample();
            MoreReasonableExample();

            Console.ReadKey();
        }

        static void OriginalExample()
        {
            // Format matrix output to console
            // NOTE: this is no longer, Math.NET Numerics now has decent matrix formatting,
            //       see MoreReasonableExample
            var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
            formatProvider.TextInfo.ListSeparator = " ";

            // Create square symmetric matrix
            var matrix = DenseMatrix.OfArray(new[,] { { 1.0, 2.0, 3.0 }, { 2.0, 1.0, 4.0 }, { 3.0, 4.0, 1.0 } });
            Console.WriteLine(@"Initial square symmetric matrix");
            Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // Perform eigenvalue decomposition of symmetric matrix
            var evd = matrix.Evd();
            Console.WriteLine(@"Perform eigenvalue decomposition of symmetric matrix");

            // 1. Eigen vectors
            Console.WriteLine(@"1. Eigen vectors");
            Console.WriteLine(evd.EigenVectors.ToString("#0.00\t", formatProvider));
            Console.WriteLine();
        }

        static void MoreReasonableExample()
        {
            // Create square symmetric matrix
            var matrix = Matrix<double>.Build.DenseOfArray(new[,] { { 1.0, 2.0, 3.0 }, { 2.0, 1.0, 4.0 }, { 3.0, 4.0, 1.0 } });
            Console.WriteLine(@"Initial square symmetric matrix");
            Console.WriteLine(matrix.ToString("G3"));
            Console.WriteLine();

            // Perform eigenvalue decomposition of symmetric matrix
            var evd = matrix.Evd();
            Console.WriteLine(@"Perform eigenvalue decomposition of symmetric matrix");

            // 1. Eigen vectors
            Console.WriteLine(@"1. Eigen vectors");
            Console.WriteLine(evd.EigenVectors.ToString("G3"));
            Console.WriteLine();
        }
    }
}
Thanks for helping me out! Jan
Marked as answer by cdrnet on 7/2/2014 at 12:59 PM
Coordinator
Jul 1, 2014 at 7:48 PM
Thanks for the update.

Quick question - how did you add the reference to the Math.NET assemblies? When using NuGet the reference to System.Numerics is supposed to be added automatically on those platforms where it is required (not all of them, since some do not support it) but maybe that was not working properly in your case.

Thanks,
Christoph
Jul 2, 2014 at 7:53 PM
I included the reference in the project separately, as I normally do with other .dlls. Next time I will use the NuGet method.