matrix_operation v0.5.0 MatrixOperation

MatrixOperation is a linear algebra library in Elixir language. Matrix indices of a row and column is an integer starting from 1 (not from 0).

Link to this section Summary

Functions

Matrix addition

A matrix is added by a constant.

A matrix is multiplied by a constant.

A column of a matrix is deleted.

A row of a matrix is deleted.

A determinant of a n×n square matrix is got.

Matrix diagonalization using the QR decomposition.

Calculate eigenvalues and eigenvectors by using QR decomposition.

Calculate eigenvalue using algebra method [R^2×R^2/R^3×R^3 matrix]

Calculate eigenvalues and eigenvectors by using QR decomposition for symmetric matrices.

A m×n matrix having even-elements is got.

Frobenius norm

A column of a matrix is got.

An element of a matrix is got.

A row of a matrix is got.

Hadamard division

Hadamard power

Hadamard product

Inverse Matrix

Calculate eigenvalues and eigenvectors by using Jacobi method

Jordan_normal_form [R^2×R^2/R^3×R^3 matrix]

Leading principal minor is generetaed.

LU decomposition

The max norm

Moore-Penrose general inverse matrix

The one norm

Power iteration method (maximum eigen value and eigen vector)

Matrix product

A m×n matrix having random elements is got.

Calculate the rank of a matrix by using QR decomposition

Calculate singular Value by using QR decomposition.

Numbers of rows and columns of a matrix are got.

Linear equations are solved by LU decomposition.

Matrix subtraction

Singular Value Decomposition (SVD) using Jacobi method.

Tensor product

Trace of a matrix

Transpose of a matrix

The two norm

A n-th unit matrix is got.

A variance-covariance matrix is generated.

Link to this section Functions

Matrix addition

Argument

  • a: Left side of the addition of matrices.
  • b: Right side of the addition of matrices.

Output

Addition of two matrices

Example

iex> MatrixOperation.add([[3, 2, 3], [2, 1, 2]], [[2, 3, 1], [3, 2, 2]])
[[5, 5, 4], [5, 3, 4]]
Link to this function

const_addition(const, x)

A matrix is added by a constant.

Argument

  • const: Constant to add the matrix.
  • matrix: Target vector/matrix to be added by a constant.

Output

Vector/Matrix multiplied by the constant.

Example

iex> MatrixOperation.const_addition(1, [1.0, 2.0, 3.0])
[2.0, 3.0, 4.0]
iex> MatrixOperation.const_addition(1, [[1, 2, 3], [2, 2, 2], [3, 8, 9]])
[[2, 3, 4], [3, 3, 3], [4, 9, 10]]
Link to this function

const_multiple(const, x)

A matrix is multiplied by a constant.

Argument

  • const: Constant to multiply the matrix.
  • matrix: Target vector/matrix to be multiplied by a constant.

Output

Vector/Matrix multiplied by the constant.

Example

iex> MatrixOperation.const_multiple(-1, [1.0, 2.0, 3.0])
[-1.0, -2.0, -3.0]
iex> MatrixOperation.const_multiple(2, [[1, 2, 3], [2, 2, 2], [3, 8, 9]])
[[2, 4, 6], [4, 4, 4], [6, 16, 18]]
Link to this function

cramer(matrix, vertical_vec, select_idx)

Cramer's rule

Argument

  • matrix: Target matrix to perform Cramer's rule.
  • vertical_vec: Vertical vector to perform Cramer's rule.
  • select_idx: Index of the target to perform Cramer's rule.

Output

Solution to the linear equation when Cramer's rule is applied.

Example

iex> MatrixOperation.cramer([[1, 0, 0], [0, 1, 0], [0, 0, 1]], [[1], [0], [0]], 1)
1.0
iex> MatrixOperation.cramer([[0, -2, 1], [-1, 1, -4], [3, 3, 1]], [[3], [-7], [4]], 1)
2.0
Link to this function

delete_one_column(matrix, del_idx)

A column of a matrix is deleted.

Argument

  • matrix: Target matrix from which to delete the column.
  • del_idx: Index of the column to be deleted.

Output

The matrix from which the specified column was deleted.

Example

iex> MatrixOperation.delete_one_column([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 2)
[[1, 3], [4, 6], [7, 9]]
Link to this function

delete_one_row(matrix, del_idx)

A row of a matrix is deleted.

Argument

  • matrix: Target matrix from which to delete the row.
  • del_idx: Index of the row to be deleted.

Output

The matrix from which the specified row was deleted.

Example

iex> MatrixOperation.delete_one_row([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3)
[[1, 2, 3], [4, 5, 6]]
Link to this function

determinant(matrix)

A determinant of a n×n square matrix is got.

Argument

  • matrix: Target matrix to output determinant.

Output

Determinant of the matrix

Example

iex> MatrixOperation.determinant([[1, 2, 1], [2, 1, 0], [1, 1, 2]])
-5
iex> MatrixOperation.determinant([[1, 2, 1, 1], [2, 1, 0, 1], [1, 1, 2, 1], [1, 2, 3, 4]])
-13
iex> MatrixOperation.determinant([ [3,1,1,2,1], [5,1,3,4,1], [2,0,1,0,1], [1,3,2,1,1], [1,1,1,1,1] ])
-14
Link to this function

diagonalization(a)

Matrix diagonalization using the QR decomposition.

Argument

  • a: Matrix to be diagonalized by using the QR decomposition.

Output

Diagonalized matrix

Example

iex> MatrixOperation.diagonalization([[1, 3], [4, 2]])
[[5.000000000000018, 0], [0, -1.999999999999997]]
iex> MatrixOperation.diagonalization([[2, 1, -1], [1, 1, 5], [-1, 2, 1]])
[[4.101784906061095, 0, 0], [0, -2.6170329440542233, 0], [0, 0, 2.515248037993127]]
iex> MatrixOperation.diagonalization([[2, 1, -1], [1, 1, 0], [-1, 0, 1]])
nil
iex> MatrixOperation.diagonalization([[2, 1, -1], [1, 1, 0], [-1, 0, 1]])
nil
iex> MatrixOperation.diagonalization([[16, -1, 1, 2, 3], [2, 12, 1, 5, 6], [1, 3, -24, 8, 9], [3, 4, 9, 1, 23], [5, 3, 1, 2, 1]])
[
  [-26.608939298557207, 0, 0, 0, 0],
  [0, 20.42436493500135, 0, 0, 0],
  [0, 0, 14.665793374162678, 0, 0],
  [0, 0, 0, -3.5477665464080044, 0],
  [0, 0, 0, 0, 1.0665475358009446]
]

Calculate eigenvalues and eigenvectors by using QR decomposition.

Argument

  • a: Matrix to calculate eigenvalues and eigenvectors by using the QR decomposition.

Output

[Eigenvalues list, Eigenvectors list]: Eigenvalues and eigenvectors. Eigenvalue is a non-trivial value other than zero, and complex numbers are not supported.

Example

iex> MatrixOperation.eigen([[1, 4, 5], [4, 2, 6], [5, 6, 3]])
{
  [12.175971065046914, -3.6686830979532736, -2.507287967093643],
  [
    [0.4965997845461912, 0.5773502691896258, 0.6481167492476514],
    [-0.3129856771935595, -0.5773502691896258, 0.7541264035547063],
    [-0.8095854617397507, 0.577350269189626, 0.10600965430705471]
  ]
}
Link to this function

eigenvalue_algebra(arg1)

Calculate eigenvalue using algebra method [R^2×R^2/R^3×R^3 matrix]

Argument

  • [[a11, a12], [a21, a22]] or [[a11, a12, a13], [a21, a22, a23], [a31, a32, a33]]: R^2×R^2/R^3×R^3 matrix

Output

Eigenvalues which is a non-trivial value other than zero.

Example

iex> MatrixOperation.eigenvalue_algebra([[3, 1], [2, 2]])
{4.0, 1.0}
iex> MatrixOperation.eigenvalue_algebra([[6, -3], [4, -1]])
{3.0, 2.0}
iex> MatrixOperation.eigenvalue_algebra([[1, 1, 1], [1, 2, 1], [1, 2, 3]])
{4.561552806429505, 0.43844714673139706, 1.0000000468390973}
iex> MatrixOperation.eigenvalue_algebra([[2, 1, -1], [1, 1, 0], [-1, 0, 1]])
{3.0000000027003626, 0.9999999918989121}

Calculate eigenvalues and eigenvectors by using QR decomposition for symmetric matrices.

Argument

  • a: Symmetric matrix to calculate eigenvalues and eigenvectors by using the QR decomposition.

Output

[Eigenvalues list, Eigenvectors list]: Eigenvalues and eigenvectors. Eigenvalue is a non-trivial value other than zero, and complex numbers are not supported.

Example

iex> MatrixOperation.eigh([[3, 0], [0, 2]])
{[3.0, 2.0], [[1.0, 0.0], [0.0, 1.0]]}
iex> MatrixOperation.eigh([[1, 4, 5], [4, 2, 6], [5, 6, 3]])
{
  [12.175971065046884, -2.50728796709364, -3.6686830979532647],
  [
    [0.496599784546191, 0.8095854617397509, -0.3129856771935597],
    [0.577350269189626, -0.577350269189626, -0.5773502691896257],
    [0.6481167492476515, -0.10600965430705458, 0.7541264035547063]
  ]
}
iex> row1 = [ 5, -1, 0, 1, 2]
iex> row2 = [-1,  5, 0, 5, 3]
iex> row3 = [ 0,  0, 4, 7, 2]
iex> row4 = [ 1,  5, 7, 0, 9]
iex> row5 = [ 2,  3, 2, 9, 2]
iex> MatrixOperation.eigh([row1, row2, row3, row4, row5])
{
  [16.394097630317376, 5.901499037899706, 4.334013998770404, -0.891690865956603, -9.737919801031268],
  [
    [0.11199211262602528, -0.8283773397697639, -0.4403916223463706, 0.3275456024443265, -0.00422456530824197],
    [0.39542664705563546, 0.5332887206459925, -0.5342108202525103, 0.4973517482650887, 0.16279110925630544],
    [0.4267472595014673, -0.13695943658576812, 0.6991586689712901, 0.4519460705200494, 0.3256544091239611],
    [0.6029452475982553, -0.007822597120772413, 0.07907415791820135, -0.1297224632045824, -0.7831444282267664],
    [0.5342652322719152, -0.10283502852688214, -0.15999516131462643, -0.651361611317911, 0.5040984210950804]
  ]
}
Link to this function

even_matrix(elem, arg2)

A m×n matrix having even-elements is got.

Argument

  • elem: Value of the common element of the matrix to output.
  • {row_num, col_num}: Size of the matrix to output.

Output

A row_num×col_num matrix having even elements

Example

iex> MatrixOperation.even_matrix(0, {2, 3})
[[0, 0, 0], [0, 0, 0]]
iex> MatrixOperation.even_matrix(1, {3, 2})
[[1, 1], [1, 1], [1, 1]]
Link to this function

exchange_one_column(matrix, exchange_idx, exchange_list)

A column of a matrix is exchanged.

Argument

  • matrix: Target matrix from which to exchange the column.
  • exchange_idx: Index of the column to be exchanged.
  • exchange_list: List of the column to be exchanged.

Output

The matrix from which the specified column was exchanged.

Example

iex> MatrixOperation.exchange_one_column([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 2, [1, 1, 1])
[[1, 1, 3], [4, 1, 6], [7, 1, 9]]
Link to this function

exchange_one_row(matrix, exchange_idx, exchange_list)

A row of a matrix is exchanged.

Argument

  • matrix: Target matrix from which to exchange the row.
  • exchange_idx: Index of the row to be exchanged.
  • exchange_list: List of the row to be exchanged.

Output

The matrix from which the specified row was exchanged.

Example

iex> MatrixOperation.exchange_one_row([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3, [1, 1, 1])
[[1, 2, 3], [4, 5, 6], [1, 1, 1]]
Link to this function

frobenius_norm(a)

Frobenius norm

Argument

  • a: Matrix to calculate Frobenius norm.

Output

Frobenius norm

Example

iex> MatrixOperation.frobenius_norm([[2, 3], [1, 4], [2, 1]])
5.916079783099616
iex> MatrixOperation.frobenius_norm([[1, 3, 3], [2, 4, 1], [2, 3, 2]])
7.54983443527075
Link to this function

get_one_column(matrix, col_idx)

A column of a matrix is got.

Argument

  • matrix: Target matrix from which to extract the column.
  • col_idx: Index of the column to be extracted.

Output

A column of a matrix

Example

iex> MatrixOperation.get_one_column([[1, 2, 3], [4, 5, 6], [7, 8, 9] ], 1)
[1, 4, 7]
Link to this function

get_one_element(matrix, arg)

An element of a matrix is got.

Argument

  • matrix: Target matrix from which to extract the element.
  • {row_idx, col_idx}: Index of row and column of the element to be extracted.

Output

An element of a matrix

Example

iex> MatrixOperation.get_one_element([[1, 2, 3], [4, 5, 6], [7, 8, 9] ], {1, 1})
1
Link to this function

get_one_row(matrix, row_idx)

A row of a matrix is got.

Argument

  • matrix: Target matrix from which to extract the row.
  • row_idx: Index of the row to be extracted.

Output

A row of a matrix

Example

iex> MatrixOperation.get_one_row([[1, 2, 3], [4, 5, 6], [7, 8, 9] ], 1)
[1, 2, 3]
Link to this function

hadamard_division(a, b)

Hadamard division

Argument

  • a: Left side of the Hadamard division of matrices.
  • b: Right side of the Hadamard division of matrices.

Output

Hadamard division of two matrices

Example

iex> MatrixOperation.hadamard_division([[3, 2, 3], [2, 1, 2]], [[2, 3, 1], [3, 2, 2]])
[[1.5, 0.6666666666666666, 3.0], [0.6666666666666666, 0.5, 1.0]]
Link to this function

hadamard_power(matrix, n)

Hadamard power

Argument

  • matrix: Target matrix that elements are to be n-th powered.
  • n: Exponent of a power.

Output

Matrix that elements are to be n-th powered

Example

iex> MatrixOperation.hadamard_power([[3, 2, 3], [2, 1, 2]], 2)
[[9.0, 4.0, 9.0], [4.0, 1.0, 4.0]]
Link to this function

hadamard_product(a, b)

Hadamard product

Argument

  • a: Left side of the Hadamard production of matrices.
  • b: Right side of the Hadamard production of matrices.

Output

Hadamard production of two matrices

Example

iex> MatrixOperation.hadamard_product([[3, 2, 3], [2, 1, 2]], [[2, 3, 1], [3, 2, 2]])
[[6, 6, 3], [6, 2, 4]]
Link to this function

inverse_matrix(matrix)

Inverse Matrix

Argument

  • matrix: Matrix to be inverse Matrix.

Output

Inverse Matrix

Example

iex> MatrixOperation.inverse_matrix([[1, 1, -1], [-2, -1, 1], [-1, -2, 1]])
[[-1.0, -1.0, 0.0], [-1.0, 0.0, -1.0], [-3.0, -1.0, -1.0]]
Link to this function

jacobi(matrix, iter_max \\ 1000)

Calculate eigenvalues and eigenvectors by using Jacobi method

Argument

  • matrix: Matrix to adapt the power iteration method.
  • iter_max: iteration number of the power iteration method. The default value is 1000.

Output

[Eigenvalues list, Eigenvectors list]: Eigenvalues and eigenvectors

Example

iex> MatrixOperation.jacobi([[10, 3, 2], [3, 5, 1], [2, 1, 0]])
{
  [11.827601656660915, 3.5956497715829547, -0.42325142824210527],
  [
    [0.8892872578006493, -0.42761854121985043, -0.16220529066103917],
    [0.4179466723082575, 0.9038581385546461, -0.09143874712126684],
    [0.1857114757355714, 0.013522151221627882, 0.982511271796136]
  ]
}
Link to this function

jordan_normal_form(arg1)

Jordan_normal_form [R^2×R^2/R^3×R^3 matrix]

Argument

  • matrix: R^2×R^2/R^3×R^3 matrix. Target matrix to be Jordan normal form.

Output

Jordan normal form matrix

Example

iex> MatrixOperation.jordan_normal_form([[1, 3], [4, 2]])
[[5.0, 0], [0, -2.0]]
iex> MatrixOperation.jordan_normal_form([[7, 2], [-2, 3]])
[[5.0, 1], [0, 5.0]]
iex> MatrixOperation.jordan_normal_form([[2, 1, -1], [1, 1, 0], [-1, 0, 1]])
nil
iex> MatrixOperation.jordan_normal_form([[1, -1, 1], [0, 2, -2], [1, 1, 3]])
[[2.0, 1, 0], [0, 2.0, 1], [0, 0, 2.0]]
iex> MatrixOperation.jordan_normal_form([[3, 0, 1], [-1, 2, -1], [-1, 0, 1]])
[[2.0, 1, 0], [0, 2.0, 0], [0, 0, 2.0]]
iex> MatrixOperation.jordan_normal_form([[1, 0, -1], [0, 2, 0], [0, 1, 1]])
[[2.0, 0, 0], [0, 0.9999999999999999, 1], [0, 0, 0.9999999999999999]]
iex> MatrixOperation.jordan_normal_form([[6, 2, 3], [-3, 0, -2], [-4, -2, -1]])
[[1.0, 0, 0], [0, 2.0, 1], [0, 0, 2.0]]
Link to this function

leading_principal_minor(matrix, idx)

Leading principal minor is generetaed.

Argument

  • matrix: Target matrix to find leading principal minor.
  • idx: Index of a row and column to find leading principal minor.

Output

Leading principal minor

Example

iex> MatrixOperation.leading_principal_minor([[1, 3, 2], [2, 5, 1], [3, 4, 5]], 2)
[[1, 3], [2, 5]]
Link to this function

lu_decomposition(matrix)

LU decomposition

Argument

  • matrix: Target matrix to solve LU decomposition.

Output

{L, U}. L(U) is L(U)-matrix of LU decomposition.

Example

iex> MatrixOperation.lu_decomposition([[1, 1, 0, 3], [2, 1, -1, 1], [3, -1, -1, 2], [-1, 2, 3, -1]])
{
  [[1, 0, 0, 0], [2.0, 1, 0, 0], [3.0, 4.0, 1, 0], [-1.0, -3.0, 0.0, 1]],
  [[1, 1, 0, 3], [0, -1.0, -1.0, -5.0], [0, 0, 3.0, 13.0], [0, 0, 0, -13.0]]
}

The max norm

Argument

  • a: Matrix to calculate the max norm.

Output

The max norm

Example

iex> MatrixOperation.max_norm([[2, 3], [1, 4], [2, 1]])
8
iex> MatrixOperation.max_norm([[1, 3, 3], [2, 4, 1], [2, 3, 2]])
10
Link to this function

mp_inverse_matrix(matrix)

Moore-Penrose general inverse matrix

Argument

  • matrix: Matrix to be Moore-Penrose general inverse matrix.

Output

Moore-Penrose general inverse matrix

Example

iex> MatrixOperation.mp_inverse_matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
[
  [-0.6388888888888877, -0.16666666666666777, 0.30555555555555647],
  [-0.0555555555555557, -1.8041124150158794e-16, 0.05555555555555575],
  [0.5277777777777768, 0.16666666666666755, -0.19444444444444522]
]

The one norm

Argument

  • a: Matrix to calculate the one norm.

Output

one norm

Example

iex> MatrixOperation.one_norm([[2, 3], [1, 4], [2, 1]])
5
iex> MatrixOperation.one_norm([[1, 3, 3], [2, 4, 1], [2, 3, 2]])
7
Link to this function

power_iteration(matrix, iter_max \\ 1000)

Power iteration method (maximum eigen value and eigen vector)

Argument

  • matrix: Matrix to adapt the power iteration method.
  • iter_max: iteration number of the power iteration method. The default value is 1000.

Output

Maximum eigenvalue and normalized eigenvector corresponding to the maximum eigenvalue

Example

iex> MatrixOperation.power_iteration([[3, 1], [2, 2]])
{
  4.0,
  [0.7071067811865476, 0.7071067811865476]
}
iex> MatrixOperation.power_iteration([[1, 1, 2], [0, 2, -1], [0, 0, 3]])
{
  3.0,
  [0.3333333333333333, -0.6666666666666666, 0.6666666666666666]
}

Matrix product

Argument

  • a: Left side of the product of matrices.
  • b: Right side of the product of matrices.

Output

Product of two matrices

Example

iex> MatrixOperation.product([[3, 2, 3], [2, 1, 2]], [[2, 3], [2, 1], [3, 5]])
[[19, 26], [12, 17]]
Link to this function

random_matrix(min_val, max_val, arg3, type \\ "int")

A m×n matrix having random elements is got.

Argument

  • min_val: Minimum value of random number.
  • max_val: Maximum value of random number.
  • {row_num, col_num}: Size of the matrix to output.
  • type: Data type of elements. "int" or "real".

Output

A row_num×col_num matrix having random elements

Calculate the rank of a matrix by using QR decomposition

Example

iex> MatrixOperation.rank([[2, 3, 4], [1, 4, 2], [2, 1, 4]])
2
iex> MatrixOperation.rank([[2, 3, 4, 2], [1, 4, 2, 3], [2, 1, 4, 4]])
3
iex> input = [[2, 3, 4, 3], [1, 42, 2, 11], [2, 1, 4, 4], [3, 7, 2, 2], [35, 6, 4, 6], [7, 23, 5, 2]]
iex> MatrixOperation.rank(input)
4
Link to this function

singular_value(a)

Calculate singular Value by using QR decomposition.

Argument

  • a: Matrix to calculate singular values.

Output

Singular values list. Singular value is a non-trivial value other than zero.

Example

iex> MatrixOperation.singular_value([[1, 2, 3, 1], [2, 4, 1, 5], [3, 3, 10, 8]])
{14.9121726205599, 4.23646340778201, 1.6369134152873912}

Numbers of rows and columns of a matrix are got.

Argument

  • matrix: Target matrix for finding the numbers of rows and columns.

Output

{num_rows, num_cols}: Numbers of rows and columns of a matrix

Example

iex> MatrixOperation.size([[3, 2, 3], [2, 1, 2]])
{2, 3}
Link to this function

solve_sle(matrix, vertical_vec)

Linear equations are solved by LU decomposition.

Argument

  • matrix: Target matrix to solve simultaneous linear equations.
  • vertical_vec: Vertical vector to solve linear equations.

Output

Solutions of the linear equations

Example

iex> MatrixOperation.solve_sle([[1, 0, 0], [0, 1, 0], [0, 0, 1]], [[1], [0], [0]])
[1.0, 0.0, 0.0]
iex> MatrixOperation.solve_sle([[4, 1, 1], [1, 3, 1], [2, 1, 5]], [[9], [10], [19]])
[1.0, 2.0, 3.0]

Matrix subtraction

Argument

  • a: Left side of the subtraction of matrices.
  • b: Right side of the subtraction of matrices.

Output

Subtraction of two matrices

Example

iex> MatrixOperation.subtract([[3, 2, 3], [2, 1, 2]], [[2, 3, 1], [3, 2, 2]])
[[1, -1, 2], [-1, -1, 0]]

Singular Value Decomposition (SVD) using Jacobi method.

Argument

  • matrix: Matrix to adapt the SVD by using the QR decomposition method.

Output

[Singular values, U-matrix, V-matrix]:

Singular values, U-matrix and V-matrix.
Singular value is a non-trivial value other than zero.

Example

iex> MatrixOperation.svd([[1, 0, 0], [0, 1, 1]])
{
  [1.0, 1.4142135623730951],
  [
    [1.0, 0.0],
    [0.0, 1.0]
  ],
  [
    [1.0, 0.0, 0.0],
    [0.0, 0.7071067811865475, 0.7071067811865475]
  ]
}
iex> MatrixOperation.svd([[1, 1], [1, -1], [1, 0]])
{
  [1.7320508075688772, 1.4142135623730951],
  [
    [0.5773502691896258, 0.5773502691896258, 0.5773502691896258],
    [0.7071067811865476, -0.7071067811865476, 0.0]
  ],
  [
    [1.0, 0.0],
    [0.0, 1.0]
  ]
}
iex> MatrixOperation.svd([[1, 1], [1, 1]])
{
  [1.9999999999999998],
  [[0.7071067811865476, 0.7071067811865476]],
  [[0.7071067811865476, 0.7071067811865476]]
}
Link to this function

svd_sub(a, a_t)

Link to this function

tensor_product(a, b)

Tensor product

Argument

  • a: Left side of the tensor production of matrices.
  • b: Right side of the tensor production of matrices.

Output

Tensor production of two matrices

Example

iex> MatrixOperation.tensor_product([[3, 2, 3], [2, 1, 2]], [[2, 3, 1], [2, 1, 2], [3, 5, 3]])
[
  [
    [[6, 9, 3], [6, 3, 6], [9, 15, 9]],
    [[4, 6, 2], [4, 2, 4], [6, 10, 6]],
    [[6, 9, 3], [6, 3, 6], [9, 15, 9]]
  ],
  [
    [[4, 6, 2], [4, 2, 4], [6, 10, 6]],
    [[2, 3, 1], [2, 1, 2], [3, 5, 3]],
    [[4, 6, 2], [4, 2, 4], [6, 10, 6]]
  ]
]

Trace of a matrix

Argument

  • matrix: Target matrix to output trace.

Output

Trance of the matrix

Example

iex> MatrixOperation.trace([[1.0, 2.0], [3.0, 4.0]])
5.0
Link to this function

transpose(matrix)

Transpose of a matrix

Argument

  • matrix: Target matrix to transpose.

Output

Transposed matrix

Example

iex> MatrixOperation.transpose([[1.0, 2.0], [3.0, 4.0]])
[[1.0, 3.0], [2.0, 4.0]]

The two norm

Argument

  • a: Matrix to calculate the two norm.

Output

The two norm

Example

iex> MatrixOperation.two_norm([[2, 3], [1, 4], [2, 1]])
5.674983803488139
iex> MatrixOperation.two_norm([[1, 3, 3], [2, 4, 1], [2, 3, 2]])
7.329546646114923

A n-th unit matrix is got.

Argument

  • n: Number of rows / columns in the unit matrix to output.

Output

A n-th unit matrix

Example

iex> MatrixOperation.unit_matrix(3)
[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
Link to this function

variance_covariance_matrix(data)

A variance-covariance matrix is generated.

Argument

  • data: x and y coordinate lists ([[x_1, y_1], [x_2, y_2], ...]) to calculate variance-covariance matrix.

Output

Variance-covariance matrix

Example

iex> MatrixOperation.variance_covariance_matrix([[40, 80], [80, 90], [90, 100]])
[
  [466.66666666666663, 166.66666666666666],
  [166.66666666666666, 66.66666666666666]
]