Did you know ... Search Documentation:
Pack matrix -- prolog/matrix.pl
PublicShow source

This module performs matrix operations. Impemented operations:

  • sum
  • difference
  • multiplication
  • Cholesky decomposition https://en.wikipedia.org/wiki/Cholesky_decomposition
  • determinant for positive semi-definite matrices (using Cholesky decomposition)
  • inversion for positive semi-definite matrices (using Cholesky decomposition)
  • inversion for lower triangular matrices
  • identity matrix
  • diagonal matrix

The library was developed for dealing with multivariate Gaussian distributions, that's the reson for the focus on positive semi-definite matrices

author
- Fabrizio Riguzzi
license
- Artistic License 2.0
 matrix_div_scal(+A, +V, -B) is det
divide matrix A by scalar V
 matrix_mult_scal(+A, +V, -B) is det
multiply matrix A by scalar V
 determinant(+A, -D) is det
computes the determinant for a positive semi-definite matrix. Uses the Cholenski decomposition
?- determinant([[2,-1,0],[-1,2,-1],[0,-1,2]],D).
D = 3.999999999999999.
 matrix_identity(+N, -M) is det
generates identity matrix of size NxN
 matrix_diagonal(+Vect, -M) is det
generates diagonal matrix from vector Vect M=diag(Vect)
 matrix_inversion(+M, -IM) is det
inversion of a positive semi-definite matrix. Uses the Cholenski decomposition
?- matrix_inversion([[2,-1,0],[-1,2,-1],[0,-1,2]],L).
L = [[0.7499999999999999, 0.5000000000000001, 0.2500000000000001], [0.5000000000000001, 1.0000000000000004, 0.5000000000000002], [0.2500000000000001, 0.5000000000000002, 0.7500000000000001]].
 matrix_inv_triang(+M, -IM) is det
inversion of a lower triangular matrix code from http://www.mymathlib.com/c_source/matrices/linearsystems/unit_lower_triangular.c http://www.mcs.csueastbay.edu/~malek/TeX/Triangle.pdf code from
?- matrix_inv_triang([[2,0,0],[-1,2,0],[0,-1,2]],L).
L = [[0.5, 0.0, 0.0], [0.25, 0.5, 0.0], [0.125, 0.25, 0.5]].
 matrix_multiply(+X, +Y, -M) is det
X(N*P),Y(P*M),M(N*M)
?- matrix_multiply([[1,2],[3,4],[5,6]], [[1,1,1],[1,1,1]],R).
R = [[3, 3, 3], [7, 7, 7], [11, 11, 11]].

code from http://stackoverflow.com/questions/34206275/matrix-multiplication-with-prolog

 dot_product(+X, +Y, -D) is det
computes the dot produce of two vectors
 matrix_diff(+A, +B, -C) is det
 matrix_sum(+A, +B, -C) is det
matrix_sum([[1,2],[3,4],[5,6]],[[1,2],[3,4],[5,6]],M).
M = [[2, 4], [6, 8], [10, 12]].
 cholesky_decomposition(+A, -L) is det
computes the Cholesky decomposition of a positive semi-definite matrix code from https://rosettacode.org/wiki/Cholesky_decomposition#C
cholesky_decomposition([[25, 15, -5], [15, 18,  0], [-5,  0, 11]],L).
L = [[5.0, 0, 0], [3.0, 3.0, 0], [-1.0, 1.0, 3.0]].
cholesky_decomposition([[18, 22,  54,  42],[22, 70,  86,  62],[ 54, 86, 174, 134],[ 42, 62, 134, 106]],L).
L = [[4.242640687119285, 0, 0, 0], [5.185449728701349, 6.565905201197403, 0, 0], [12.727922061357857, 3.0460384954008553, 1.6497422479090704, 0], [9.899494936611667, 1.624553864213788, 1.8497110052313648, 1.3926212476456026]].
 list0(+N, -L) is det
returns a list of N zeros