Getting started
In this Basic section of Guides we will cover all general operations with matrix.
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
- Languages: C#
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.
Note
If you didn't find answer for your question on this page, ask it on gitter.