ExMatrix
is a new Matrix library for Elixir. This library helps you to create a matrix,
manipulate
it with values and add/subtract
two matrices.
A matrix is a collection of numbers arranged into a fixed number of rows and columns.
Here is an example of a 3x2
matrix which means that we have 3 rows
and 2 columns
.
col 1 col 2
row 1 | 0 1 |
row 2 | 2 7 |
row 3 | 9 0 |
This is row 2
| 2 7 |
This is col 2
col 2
| 1 |
| 7 |
| 0 |
To get value from the above matrix we need to specify the row and colum that we need to get
the value from. The known syntax is (number_of_rows,number_of_columns)
(0,1) = 1
and (2,0) = 9
First, add ExMatrix to your mix.exs dependencies:
def deps do
[
{:ex_matrix, "~> 0.1.0"}
]
end
Then, update your dependencies:
$ mix deps.get
So to generate an elixir matrix you can use ExMatrix.create("RowsxColumns")
will generate a map that can be used as a matrix
But note that ExMatrix.create(...)
will generate an empty matrix as you can see in the example
iex> matrix = ExMatrix.create("3x2")
%{"0" => %{"0" => "", "1" => ""},
"1" => %{"0" => "", "1" => ""},
"2" => %{"0" => "", "1" => ""}
}
So to fill this matrix with values you can use ExMatrix.set_matrix(matrix,data)
matrix = %{"0" => %{"0" => "0", "1" => "1"},
"1" => %{"0" => "2", "1" => "7"},
"2" => %{"0" => "9", "1" => "0"}
}
Now you can get values matrix["0"]["1"] = 1
and matrix["2"]["0"] = 9
.
There are many operation that you can apply on matrices and one of these operations is to add and subtract two matrices.
Small review on how we deal with addition and subtraction of two matrices:
Matrix A + Matrix B = Result
| 0 1 | | 1 -1 | | 1 0 |
| 2 7 | | 7 -2 | | 9 5 |
| 9 0 | | -1 8 | | 8 8 |
You can use ExMatrix.add_matrices(matrix_1,matrix_2)
or ExMatrix.sub_matrices(matrix_1,matrix_2)
In case that there is something vague please use a helper function
iex> ExMatrix.example("2x2")
%{"0" => %{"0" => "(0,0)", "1" => "(0,1)"},
"1" => %{"0" => "(1,0)", "1" => "(1,1)"}
}
This library will be extend to have the ability to:
- Add or subtract two or more matrices
- Multiply and Divide two or more matrices
- Matrix Transpose
Please read the contribution requirements before start working
Copyright (c) 2018, Jad Tarabay.
ExMatrix source code is licensed under the MIT License.