ex_matrix v0.1.0 ExMatrix View Source

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.

What is a Matrix

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

Elixir Matrix

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.

Adding or Subtracting two matrices

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)

Example Function

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)"}
    }

What’s Next

This library will be extend to have the ability to:

  1. Add or subtract two or more matrices
  2. Multiply and Divide two or more matrices
  3. Matrix Transpose

For more information please check the github

For contribution on GitHub

Please read the contribution requirements before start working

Link to this section Summary

Functions

Change type of values in the martix

Create a matrix

Helper function to undestand how elixir matrix is generated

Set values to the base matrix

Subtract two matrices

Link to this section Functions

Link to this function add_matrices(matrix_1, matrix_2) View Source

Add two matrices

You can add two matrices with string values or integers value where the return matrix will be the same type of the two matrices.

Example

iex> matrix_1 = %{"0" => %{"0" => 2, "1" => 7},"1" => %{"0" => 4, "1" => 4}}
iex> matrix_2 = %{"0" => %{"0" => 8, "1" => 3},"1" => %{"0" => 6, "1" => 0}}
iex> ExMatrix.add_matrices(matrix_1,matrix_2)
%{"0" => %{"0" => 10, "1" => 10},
  "1" => %{"0" => 10, "1" => 4}
}
Link to this function change_type(matrix, type) View Source

Change type of values in the martix

When you generate an elixir matrix the type of values will be in string, in case you need change this function gives you the ability to change them only for integers only.

Example

iex> ExMatrix.example("2x2")
%{"0" => %{"0" => "(0,0)", "1" => "(0,1)"},
  "1" => %{"0" => "(1,0)", "1" => "(1,1)"}
}

Create a matrix

Example

iex> ExMatrix.create("2x2")
%{"0" => %{"0" => "", "1" => ""},
  "1" => %{"0" => "", "1" => ""}
}

Helper function to undestand how elixir matrix is generated

Example

iex> ExMatrix.example("2x2")
%{"0" => %{"0" => "(0,0)", "1" => "(0,1)"},
  "1" => %{"0" => "(1,0)", "1" => "(1,1)"}
}
Link to this function set_matrix(matrix, data) View Source

Set values to the base matrix

Example

iex> matrix = ExMatrix.create("2x2")
iex> data = [{"(0,0)","1"},{"(0,1)","2"},{"(1,0)","3"},{"(1,1)","4"}]
iex> ExMatrix.set_matrix(matrix,data)
%{"0" => %{"0" => "1", "1" => "2"},
  "1" => %{"0" => "3", "1" => "4"}
}
Link to this function sub_matrices(matrix_1, matrix_2) View Source

Subtract two matrices

You can subtract two matrices with string values or integers value where the return matrix will be the same type of the two matrices.

Example

iex> matrix_1 = %{"0" => %{"0" => "2", "1" => "7"},"1" => %{"0" => "4", "1" => "4"}}
iex> matrix_2 = %{"0" => %{"0" => "8", "1" => "3"},"1" => %{"0" => "6", "1" => "0"}}
iex> ExMatrix.sub_matrices(matrix_1,matrix_2)
%{"0" => %{"0" => "-6", "1" => "4"},
  "1" => %{"0" => "-2", "1" => "4"}
}