Overview
Install
Create new console application and install the MatrixDotNet NuGet package. We support:
- Projects: classic and modern with PackageReferences
- Runtimes: .NETStandard 2.1, NET Core 3.1+
- OS: Windows, Linux, MacOS
Matrix
There are many ways creates of matrix so let's consider the following sample how to create Matrix.
using System.Text;
using MatrixDotNet;
namespace Samples.logs.MatrixCreateSample
{
public class MatrixCreateSampleDocs
{
public static void Run()
{
int[,] a = new int[3, 3]
{
{10, -7, 0},
{-3, 6, 2},
{5, -1, 5}
};
// First way.
Matrix<int> matrixA = new Matrix<int>(a);
// Second way: primitive way, assign by deep copy nor by reference!!!
Matrix<int> matrixB = a;
Matrix<int> matrixC = new int[10, 10];
Matrix<int> matrixD = new[,]
{
{1, 2, 3},
{2, 4, 6},
};
// Third way initialize all values 0 or constant value.
Matrix<int> matrixE = new Matrix<int>(row: 5, col: 3);
Matrix<int> matrixF = new Matrix<int>(row: 3, col: 5, value: 5);
}
}
}
As you can see class Matrix
can assign any type implicit such as one, two dimensional and jugged array.
Matrix stores your data in one dimensional array, you can get with happen method GetArray()
.
Output
Matrix A:
10 | -7 | 0 |
-3 | 6 | 2 |
5 | -1 | 5 |
Matrix C:
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Matrix D:
1 | 2 | 3 |
2 | 4 | 6 |
Matrix E:
0 | 0 | 0 |
0 | 0 | 0 |
0 | 0 | 0 |
0 | 0 | 0 |
0 | 0 | 0 |
Matrix F:
5 | 5 | 5 | 5 | 5 |
5 | 5 | 5 | 5 | 5 |
5 | 5 | 5 | 5 | 5 |
Simple operations of matrix.
So, as we know how to create matrix let's to try make routine operations with matrix.
using System.Text;
using MatrixDotNet;
namespace Samples.logs.SimpleOperations
{
public class SimpleOperationsDocs
{
public static void Run()
{
// init matrix
int[,] a = new int[3, 3]
{
{10, -7, 0},
{-3, 6, 2},
{5, -1, 5}
};
int[,] b = new int[3, 4]
{
{11, -2, 1, 6},
{-8, 4, 2, 3},
{4, -4, 5, 8},
};
int[] vector = {2, 7, 5, 4};
int k = 3;
Matrix<int> matrixA = a;
Matrix<int> matrixB = b;
// Multiply.
Matrix<int> matrixC = matrixA * matrixB;
Matrix<int> matrixD = matrixC * k;
// Sum.
Matrix<int> matrixE = matrixB + k * matrixB;
// Subtract.
Matrix<int> matrixF = 2 * matrixA - matrixA;
// Divide.
Matrix<int> matrixG = matrixA / 2;
}
}
}
As you can see thanks overload operators, it's possible write less code and becomes more readable.
Output
matrix A:
10 | -7 | 0 |
-3 | 6 | 2 |
5 | -1 | 5 |
matrix B:
11 | -2 | 1 | 6 |
-8 | 4 | 2 | 3 |
4 | -4 | 5 | 8 |
matrix C:
166 | -48 | -4 | 39 |
-73 | 22 | 19 | 16 |
83 | -34 | 28 | 67 |
matrix D:
498 | -144 | -12 | 117 |
-219 | 66 | 57 | 48 |
249 | -102 | 84 | 201 |
matrix E:
44 | -8 | 4 | 24 |
-32 | 16 | 8 | 12 |
16 | -16 | 20 | 32 |
matrix F:
10 | -7 | 0 |
-3 | 6 | 2 |
5 | -1 | 5 |
matrix G:
5 | -3 | 0 |
-1 | 3 | 1 |
2 | 0 | 2 |
Vector
The next structure is Vector.
Let's consider the following sample which demonstrates how to create vector.
using System.Text;
using MatrixDotNet;
using MatrixDotNet.Vectorization;
namespace Samples.logs.CreateVectorSample
{
public class CreateVectorSampleDocs
{
// Below you can see all ways how to create vector in explicit and implicit form
public static void Run()
{
// basic approach for create matrix just pass length of vector
Vector<int> va = new Vector<int>(5);
// init vector with fill values.
Vector<int> vb = new Vector<int>(5, 5);
// third way is implicit assign array.
Vector<int> vc = new[] { 1, 2, 3, 4, 5 };
}
}
}
Output
<0,0,0,0,0>
<5,5,5,5,5>
<1,2,3,4,5>
Routine operations with Vector
So let's consider Vector
operations as well as in Matrix
using System.Text;
using MatrixDotNet;
using MatrixDotNet.Extensions.Builder;
using MatrixDotNet.Vectorization;
namespace Samples.logs.VectorSimpleOperations
{
public class VectorSimpleOperationsDocs
{
// Below you can see all ways how to execute routine operations with vector.
public static void Run()
{
// init vector va with fill three value.
Vector<int> va = new Vector<int>(5,3);
// init vector vb.
Vector<int> vb = new[] { 1, 2, 3, 4, 5 };
// add of two vectors.
var vc = vb + va;
// multiply vectors on constant.
var vk = 5 * vc;
// subtract of two vectors.
var vd = vk - vc;
// subtract of two vectors.
var ve = vd - vk;
// multiply vectors on constant.
var vg = ve * vc;
// multiply vector on matrix and vice versa
Matrix<int> ma = BuildMatrix.RandomInt(5, 5, -10, 10);
var vq = ma * ve;
var vt = ve * ma;
}
}
}
Output
<0,0,0,0,0>
<5,5,5,5,5>
<1,2,3,4,5>
MatrixComplex
The last structure and the newest is MatrixComplex
, for now MatrixComplex in difference from Matrix
and Vector
have only fundamental operations.
MatrixComplex still in development stage.
Take a look below on sample.
using System.Text;
using MatrixDotNet;
namespace Samples.logs.CreateMatrixComplex
{
public class CreateMatrixComplexDocs
{
// Below you can see all ways how to create MatrixComplex for now.
public static void Run()
{
MatrixComplex ma = new MatrixComplex(5,5);
MatrixComplex mb = new MatrixComplex(5,5);
// addition of two complex matrix.
var mc = ma + mb;
// subtraction of two complex matrix.
var md = ma - mc;
// addition of two complex matrix.
var me = ma * md;
}
}
}
Output
ma: MatrixDotNet.MatrixComplex
mb: MatrixDotNet.MatrixComplex
mc: MatrixDotNet.MatrixComplex
md: MatrixDotNet.MatrixComplex
me: MatrixDotNet.MatrixComplex
Note
If you didn't find answer for your question on this page, ask it on gitter.