Chi-SquaredFit v1.0.0-beta.8 Chi2fit.Matrix View Source

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

[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

Types

A list of vectors (list of lists of numbers)

A list of numbers

Functions

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

Constructs a unit matrix of size n. All diagonal elements have value 1 and the rest has value 0

Link to this section Types

A list of vectors (list of lists of numbers)

A list of numbers

Link to this section Functions

Link to this function diagonal(matrix) View Source
diagonal(matrix()) :: vector()

Returns the diagonal elements of the matrix as a vector.

Example

iex> diagonal [[1,2,3],[4,5,6],[7,8,9]]
[1, 5, 9]
Link to this function dotproduct(vector1, vector2) View Source
dotproduct(vector(), vector()) :: number()

Calculates the inner product of two vectors.

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
Link to this function from_diagonal(vector) View Source
from_diagonal(vector()) :: matrix()

Returns a matrix with the supplied vector as its diagonal elements.

Examples

iex> from_diagonal [1,5,9]
[[1, 0, 0], [0, 5, 0], [0, 0, 9]]
Link to this function inverse(matrix, options \\ []) View Source
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

`:tolerance` - Iterate until the [`norm_1/1`](#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

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]]
{:ok,
  [[0.2000000000000267, -0.19999999999873883, 0.1999999999998383],
   [0.19999999999995877, 0.29999999999804805, -0.29999999999974974],
   [-1.204377690852969e-13, 0.9999999999943042, 7.3034853221406e-13]]}

iex> inverse [[3,2,0],[0,0,1],[2,-2,1]], algorithm: :soleymani
{:ok,
  [[0.2000000000000003, -0.19999999999999973, 0.19999999999999926],
   [0.2000000000000003, 0.29999999999999966, -0.29999999999999893],
   [-6.617444900424221e-23, 0.9999999999999988, -1.1183551388713516e-21]]}

iex> inverse [[3,2,0],[0,0,1],[2,-2,1]], tolerance: 1.0e-15
{:ok,
  [[0.20000000000000004, -0.2, 0.2],
   [0.20000000000000004, 0.3, -0.3],
   [-5.048709793414476e-29, 0.9999999999999999, -1.6087621596054126e-28]]}

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

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

iex> norm_1 [[1,2,3],[4,5,6],[7,8,9]]
24
Link to this function norm_inf(matrix) View Source
norm_inf(matrix()) :: number()

Calculates the norm of the matrix as norm_1/1 but over the columns instead of over the rows.

Example

iex> norm_inf [[1,2,3],[4,5,6],[7,8,9]]
18
Link to this function unit(n) View Source
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

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'