3

First trial with Math.Net and moving from C++\Cli to C# to use Math.Net, so everything is new today.

How do I set-up and run the examples such as this one Matrix Transpose. Should I create a class and copy this code into it? I notice the interface is missing (Error: namespace IExample could not be found), but I also notice this may be provided here Interface. Where do I put this?

This is what I have Program.cs (left out basic details):

namespace Examples.LinearAlgebraExamples
{
  /// Defines the base interface for examples.
   public interface IExample
    {
        string Name
        {
            get;
        }
        string Description
        {
            get;
        }
        void Run();
    }
   /// Matrix transpose and inverse
   public class MatrixTransposeAndInverse : IExample
    {
    // rest of the example code
    }
    class Program
    {
        static void Main(string[] args)
        {
           // how to call the above routines? 
        }
    }
} 
jdelange
  • 763
  • 2
  • 10
  • 22
  • Rephrased the question to make it more specific as suggested. – jdelange Jun 02 '14 at 14:38
  • These examples are being phased out. Hopefully the short examples [here](http://numerics.mathdotnet.com/docs/) can get you started? – Christoph Rüegg Jun 02 '14 at 14:41
  • If you would like to run the examples anyway, I suggest to just copy the body of the Run function to a clean CLI project's Main function (and add a reference to the MathNet.Numerics NuGet package) - and ignore all the IExample stuff. – Christoph Rüegg Jun 02 '14 at 14:59
  • Thanks Christoph. For me the examples are helpful, it's more to do with me starting out with C#. I tried the C# example in the link you sent, Start.cs (section Linux with Mono) however I get an error Matrix (and Vector) does not exist in the current context. With regard to your second note, are you saying that I can call these Math.net libraries from Cli also? Did not see any examples of that. – jdelange Jun 02 '14 at 15:13

1 Answers1

2

This is what works: create a C# console application (VS2012), and then paste the main body of the Math.Net example in the main body of the console app. Add includes and namespace. The above referenced example then runs.

Code snippet (left out items 2-5):

using System;
using MathNet.Numerics;
using MathNet.Numerics.LinearAlgebra.Double;
using System.Globalization;

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

            // Create random square matrix
            var matrix = new DenseMatrix(5);
            var rnd = new Random(1);
            for (var i = 0; i < matrix.RowCount; i++)
            {
                for (var j = 0; j < matrix.ColumnCount; j++)
                {
                    matrix[i, j] = rnd.NextDouble();
                }
            }

            Console.WriteLine(@"Initial matrix");
            Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 1. Get matrix inverse
            var inverse = matrix.Inverse();
            Console.WriteLine(@"1. Matrix inverse");
            Console.WriteLine(inverse.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

// removed examples here

            Console.WriteLine();
            Console.ReadLine();
        }
    }
}
jdelange
  • 763
  • 2
  • 10
  • 22