Project Description
MatrixExtensions provides extension methods for numeric arrays. Arrays are treated as vectors and 2D matrices. Operations provided for fast prototyping and integration of scientific libraries. Does NOT have Matrix or Vector class.



Built using extension methods - array treated as vectors and matrices.
Contains only static classes - does not have Matrix or Vector.
Side effect free - all methods create new arrays and return them.
Uses implicit casting - only MatrixExtensions.Conversion.Explicit namespace contains explicit casts.
Changing array passed to extension methods in other thread - results unpredictable calculation (Clone method is not called automatically).
All methods contain contracts.

Examples of usage

1. SVM.NET accepts Node[][] as data. Each node contains index.
        public void ToJaggedWithIndecesTest()
            //given a matrix
            var matrix = new[,] { { 1, 2 },
                                  { 3, 4 },
                                  {1, 1.5}};

            //you can convert it to jagged containing elements of different type
            //outer and inner loop indexes are used in lambda expression
            var actual = matrix.ToJagged((x,r,c) => new Node(c+1,x));

            var expected = new[] { new[] { new Node(1,1), new Node(2,2) }, 
                                   new[] { new Node(1,3), new Node(2,4)},
                                   new[] { new Node(1,1), new Node(2,1.5) } };
            Assert.AreEqual(actual[0][0], expected[0][0]);
            Assert.AreEqual(actual[0][1], expected[0][1]);
            Assert.AreEqual(actual[1][0], expected[1][0]);
            Assert.AreEqual(actual[1][1], expected[1][1]);
            Assert.AreEqual(actual[2][0], expected[2][0]);
            Assert.AreEqual(actual[2][1], expected[2][1]);

2. ALGLIB.NET Data Analysis routines accept double[,] xy. Each row is point. Each column - variable. Last column - categories. So if we have double[,] x - data, int[] y - categories.
        public void AppendVectorTest()
            //given a matrix 
            var data = new [,]{{1.2, 2.2},
                               {0.4, 0.6},
                               {1.7, 0.1}};
            //given a vector with length equal to number of matrix's rows
            var categories = new[]{1,
            //we can append vector to the right side of matrix
            var actual = data.Append(categories);

            var expected = new[,]{{1.2, 2.2,1},
                                  {0.4, 0.6, 0},
                                  {1.7, 0.1, 1}};


See also .NET Computer Science


Runtime: .NET Framework 4.0 or Silverlight 4,
Development: VS2010, NUnit, Code Contracts

Last edited Apr 8, 2012 at 9:47 PM by asdandRizzo, version 55