MatrixLib
MatrixLib is a c# library that helps you to create matrices and do matrix calculations in c#. It is very simple to use.
How to add
You need to download the dll , or you can build the dll from the source code.
- Create a project
- Open solution explorer menu
- Right click on references and click Add Reference -
- Click Browse
- Select MatrixLib.dll and click Add
- We have successfully added the library .
- Don't forget the include the library.
- Now we are ready to use the library
Create matrix
There are different ways to create matrix , but one of them is
Empty matrix creation
Matrix matrix = new Matrix(3,3);
We specified the row length and column length of the matrix. After doing that we can add rows and columns. Adding rows and columns will be explained later.
Matlab like matrix creation
Matrix matrix = new Matrix("[12 6 3;15 56 88;55 32 18]");
Created matrix will look like:
You have to type ';' semicolon after row. You have to type space after element. Typing unnecessary spaces causes error.
If the string is not in desired format this will throw MatrixFormatException You can catch it :)
Create matrix from two dimensional double array
double[,] twoDimensionalArr = new double[2, 2] {
{80,9 },
{16,71 }
};
Matrix matrix = new Matrix(twoDimensionalArr);
Created matrix will look like:
Adding rows and columns
There are 2 functions to adding rows and columns;
Adding as rows AddAssRow(int index, double [] arr)
Matrix matrix = new Matrix(3,3);
double[] row1 = { 1, 6, 6 };
double[] row2 = { 5, 7, 3 };
double[] row3 = { 3, 4, 8 };
matrix.AddAsRow(1 , row1);
matrix.AddAsRow(2 , row2);
matrix.AddAsRow(3 , row3)
Adding as columns AddAssColumn(int index, double [] arr)
Matrix matrix = new Matrix(3,3);
double[] col1 = { 7, 4, 1 };
double[] col2 = { 4, 5, 7 };
double[] col3 = { 9, 8, 6 };
matrix.AddAsColumn(1 , col1);
matrix.AddAsColumn(2 , col2);
matrix.AddAsColumn(3, col3);
Matrix row length and column length
You may want to get row length and column length of the matrix. There are two properties for that;
Matrix matrix = new Matrix("[12 6 3;15 56 88;55 32 18]");
Console.WriteLine("Row length of the matrix is = " + matrix.RowLength);
Console.WriteLine("Column length of the matrix is = " + matrix.ColLength);
Displaying matrices
There are ToString() method to display matrices.
Matrix matrix = new Matrix("[12 6 3;15 56 88;55 32 18]");
Console.WriteLine(matrix.ToString());
Getting and setting matrix elements
We can get and set matrix elements by using indexer.
Getting matrix element
Matrix matrix = new Matrix("[12 6 3;15 56 88;55 32 18]");
Console.WriteLine(matrix[1,1]);
// this will output the first row and first column element (12)
Setting matrix element
Matrix matrix = new Matrix("[12 6 3;15 56 88;55 32 18]");
matrix[2, 3] = 42;
//this will change the second row and third column element to (42)
Matrix addition , subtraction and multiplication
You can simply add or subtract matrices like int or double.
Addition
Matrix m1 = new Matrix("[12 6 3;15 56 88;55 32 18]");
Matrix m2 = new Matrix("[8 5 4;12 55 12;43 18 12]");
Matrix m3 = m2 + m1;
Console.WriteLine(m3.ToString());
if the row and column lengths of the matrices are not equal this will throw MatrixAdditionException
Subtraction
Matrix m1 = new Matrix("[12 6 3;15 56 88;55 32 18]");
Matrix m2 = new Matrix("[8 5 4;12 55 12;43 18 12]");
Matrix m3 = m2 - m1;
Console.WriteLine(m3.ToString());
if the row and column lengths of the matrices are not equal this will throw MatrixSubtractionException Output is:
Multiplication
Matrix m1 = new Matrix("[1 2;2 0;3 1]");
Matrix m2 = new Matrix("[4 3;1 2]");
Matrix m3 = m1* m2;
Console.WriteLine(m3.ToString());
if the number of columns of the first matrix is not equal to the number of rows of the second matrix this will throw MatrixMultiplicationException Output is :
Multiplication matrix with scalar number
Matrix m1 = new Matrix("[1 2;2 0;3 1]");
Matrix m3 = m1* 2;
Console.WriteLine(m3.ToString());
Transpose of the matrix
You can easily get the transpose of the matrix using Transpose(); method.
Matrix m1 = new Matrix("[2 6 7;3 6 2;4 6 5]");
Matrix m2 = m1.Transpose();
Console.WriteLine(m2.ToString());
Determinant of the matrix
There is Det(); method for calculate determinant.
Matrix m1 = new Matrix("[1 5 3;2 4 7;4 6 2]");
Console.WriteLine(m1.Det());
// output is (74)
If matrix is not square this will throw MatrixIsNotSquareException
Turning matrix into two dimensional double array
Matrix m1 = new Matrix("[1 5 3;2 4 7;4 6 2]");
double[,] arr = m1.ToDoubleArray();
Contributors
- Emir Yusuf Topbaş emiryusuftopbas@gmail.com
License & copyright
Licensed under the MIT License