Skip to content
Patrick S. Tawil edited this page Apr 26, 2018 · 2 revisions

Initialization

There are many ways you can initialize Matrices in Stats.NET

Multidimensional Arrays

var MultiDimArray = new double[,]
    {
        {1, 2, 3, 4, 5},
        {6, 7, 8, 9, 10},
        {11, 12, 13, 14, 15},
    };
var testMatrix = new Matrix(MultiDimArray);

Jagged Arrays

var JaggedArray =
    {
        new double[]
            {
                1, 2, 3, 4, 5
            },
            new double[]
            {
                6, 7, 8, 9, 10
            },
            new double[]
            {
                11, 12, 13, 14, 15
            }
    };  
var testMatrix = new Matrix(JaggedArray);   

List of Rows

var rowOne = new List<double>() { 1, 2, 3, 4, 5 };
var rowTwo = new List<double>() { 6, 7, 8, 9, 10 };
var rowThree = new List<double>() { 11, 12, 13, 14, 15 };

var rows = new List<List<double>>() { rowOne, rowTwo, rowThree };

var testMatrix = new Matrix(rows);

All these matrices have 3 rows and 5 columns and here is their visual representation

Visual Representation of testMatrix

Operations

Addition, Subtraction and Multiplication

var mat1= new double[,]
    {
        {1, 2},
        {14, 15}
    };
var mat2= new double[,]
    {
        {5, 7},
        {13, 2}
    };
var firstMatrix = new Matrix(mat1);
var secondMatrix = new Matrix(mat2);

//Addition
var addedMatrix = firstMatrix + secondMatrix;
var addedMatrix = firstMatrix.Add(secondMatrix);

//Subtraction
var subtractedMatrix = firstMatrix - secondMatrix;
var subtractedMatrix = firstMatrix.Subtract(secondMatrix);

//Multiplication (Dot Product)
var prodMatrix= firstMatrix*secondMatrix;
var prodMatrix = firstMatrix.Multiply(secondMatrix);

Inverses

Yet to be implemented

Binding

There are two ways to bind your matrices together, by row or by column

//Column Bind
var cbind = firstMatrix.ColumnBind(secondMatrix);

Here is its visual representation

Visual Representation of ColumnBind

//Row Bind
var rbind = firstMatrix.RowBind(secondMatrix);

Here is its visual representation

Visual Representation of RowBind

Clone this wiki locally