View Source Chi2fit.Matrix (Chi-SquaredFit v2.0.2)
This module provides matrix inverse operations and supporting functions.
It provides 2 types of matrix norms and an iterative approach to calculating the matrix inverse. The implementation is based on the work [1].
references
References
[1] F. Soleymani, A Rapid Numerical Algorithm to Compute Matrix Inversion, International Journal of Mathematics and Mathematical Sciences, Volume 2012, Article ID 134653, doi:10.1155/2012/134653
Link to this section Summary
Functions
Adds two vectors.
Calculates the determinant of the matrix.
Returns the diagonal elements of the matrix as a vector.
Calculates the inner product of two vectors.
Returns a matrix with the supplied vector as its diagonal elements.
Returns the matrix inverse of the argument.
Calculates the norm of the matrix as the sum of the absolutes value of all elements.
Calculates the norm of the matrix. All absolute values of the elements of each row are summed. The maximum value is returned
Calculates the norm of the matrix as norm_1/1
but over the columns instead of over the rows.
Subtracts two matrices and returns the result.
Returns the tranpose of the matrix
Constructs a unit matrix of size n. All diagonal elements have value 1 and the rest has value 0.
Link to this section Types
Link to this section Functions
Adds two vectors.
examples
Examples
iex> add [1,2], [3,4]
[4,6]
iex> add [], []
[]
iex> add [1], [5]
[6]
Calculates the determinant of the matrix.
Returns the diagonal elements of the matrix as a vector.
example
Example
iex> diagonal [[1,2,3],[4,5,6],[7,8,9]]
[1, 5, 9]
Calculates the inner product of two vectors.
examples
Examples
iex> dotproduct [1,2], [3,4]
11
iex> dotproduct [], []
0
iex> dotproduct [1,2], []
** (ArgumentError) Vectors of unequal length
iex> dotproduct [1,2], [1]
** (ArgumentError) Vectors of unequal length
Returns a matrix with the supplied vector as its diagonal elements.
examples
Examples
iex> from_diagonal [1,5,9]
[[1, 0, 0], [0, 5, 0], [0, 0, 9]]
@spec inverse(matrix(), options :: Keyword.t()) :: {:ok, inverse :: matrix()} | :failed_to_find_v0 | :no_inverse | {:failed_to_reach_tolerance, inverse :: matrix(), error :: float()}
Returns the matrix inverse of the argument.
options
Options
`:tolerance` - Iterate until the `norm_1/1` of I-AV is less than this value
`:algorithm` - Four algorithms are supported: `:hotelling_bodewig` (second order), `:lie` (third order),
`:krishnamurthy_sen` (sixth order), and `:soleymani` (seventh order); defaults to `:lie`
`:max_iterations` - Maximum number of iterations to perform; defaults to 500
`:range` - Range of values from -range to +range as a multiple of the unit matrix to try as an estimate
of the inverse matric; defaults to 100
`:size` - Number of tries to estimate initial inverse; defautls to 100
examples
Examples
iex> inverse [[3]]
{:ok,[[0.3333333333333333]]}
iex> inverse [[1,2],[3,4]]
{:ok,[[-2.0, 1.0], [1.5, -0.5]]}
iex> inverse([[3,2,0],[0,0,1],[2,-2,1]]) |> elem(1) |> Enum.map(fn row -> Enum.map(row, & Float.round(&1,10)) end)
[[0.2, -0.2, 0.2], [0.2, 0.3, -0.3], [0.0, 1.0, 0.0]]
iex> inverse([[3,2,0],[0,0,1],[2,-2,1]], algorithm: :soleymani) |> elem(1) |> Enum.map(fn row -> Enum.map(row, & Float.round(&1,14)) end)
[[0.2, -0.2, 0.2], [0.2, 0.3, -0.3], [0.0, 1.0, 0.0]]
iex> inverse([[3,2,0],[0,0,1],[2,-2,1]], tolerance: 1.0e-15) |> elem(1) |> Enum.map(fn row -> Enum.map(row, & Float.round(&1,14)) end)
[[0.2, -0.2, 0.2], [0.2, 0.3, -0.3], [0.0, 1.0, 0.0]]
For matrices that have no inverse:
iex> try do inverse [[1,2,3],[4,5,6],[7,8,9]] catch x->x end
:no_inverse
Calculates the norm of the matrix as the sum of the absolutes value of all elements.
example
Example
iex> norm [[1,2,3],[4,5,6],[7,8,9]]
45
Calculates the norm of the matrix. All absolute values of the elements of each row are summed. The maximum value is returned
example
Example
iex> norm_1 [[1,2,3],[4,5,6],[7,8,9]]
24
Calculates the norm of the matrix as norm_1/1
but over the columns instead of over the rows.
example
Example
iex> norm_inf [[1,2,3],[4,5,6],[7,8,9]]
18
Subtracts two matrices and returns the result.
Returns the tranpose of the matrix
examples
Examples:
iex> transpose [ [1] ]
[[1]]
iex> transpose [ [1,2], [3,4] ]
[[1, 3], [2, 4]]
@spec unit(n :: pos_integer()) :: [[0 | 1]]
Constructs a unit matrix of size n. All diagonal elements have value 1 and the rest has value 0.
examples
Examples
iex> unit(3)
[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
iex> unit(0)
** (ArgumentError) Illegal argument '0'
iex> unit -1
** (ArgumentError) Illegal argument '-1'
iex> unit 1.3
** (ArgumentError) Illegal argument '1.3'