June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -3,13 +3,13 @@ package main
import (
"fmt"
"github.com/gonum/matrix/mat64"
"gonum.org/v1/gonum/mat"
)
func cholesky(order int, elements []float64) fmt.Formatter {
t := mat64.NewTriDense(order, false, nil)
t.Cholesky(mat64.NewSymDense(order, elements), false)
return mat64.Formatted(t)
var c mat.Cholesky
c.Factorize(mat.NewSymDense(order, elements))
return mat.Formatted(c.LTo(nil))
}
func main() {

View file

@ -0,0 +1,6 @@
chol[A_] :=
Module[{L},
L[k_, k_] := L[k, k] = Sqrt[A[[k, k]] - Sum[L[k, j]^2, {j, 1, k-1}]];
L[i_, k_] := L[i, k] = L[k, k]^-1 (A[[i, k]] - Sum[L[i, j] L[k, j], {j, 1, k-1}]);
PadRight[Table[L[i, j], {i, Length[A]}, {j, i}]]
]

View file

@ -1,51 +1,49 @@
/*REXX program performs the Cholesky decomposition on a square matrix. */
niner = '25 15 -5' , /*define a 3x3 matrix. */
/*REXX program performs the Cholesky decomposition on a square matrix & displays results*/
niner = '25 15 -5' , /*define a 3x3 matrix with elements. */
'15 18 0' ,
'-5 0 11'
call Cholesky niner
hexer = 18 22 54 42, /*define a 4x4 matrix. */
hexer = 18 22 54 42, /*define a 4x4 matrix with elements. */
22 70 86 62,
54 86 174 134,
42 62 134 106
call Cholesky hexer
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
Cholesky: procedure; parse arg mat; say; say; call tell 'input matrix',mat
do r=1 for ord
do c=1 for r; $=0; do i=1 for c-1; $=$+!.r.i*!.c.i; end /*i*/
if r=c then !.r.r=sqrt(!.r.r-$)
else !.r.c=1/!.c.c*(@.r.c-$)
end /*c*/
end /*r*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Cholesky: procedure; parse arg mat; say; say; call tell 'input array',mat
do r=1 for ord
do c=1 for r; $=0; do i=1 for c-1; $= $ + !.r.i * !.c.i; end /*i*/
if r=c then !.r.r= sqrt(!.r.r - $) / 1
else !.r.c= 1 / !.c.c * (@.r.c - $)
end /*c*/
end /*r*/
call tell 'Cholesky factor',,!.,''
return
/*────────────────────────────────────────────────────────────────────────────*/
err: say; say; say '***error***!'; say; say arg(1); say; say; exit 13
/*────────────────────────────────────────────────────────────────────────────*/
tell: parse arg hdr,x,y,sep; #=0; if sep=='' then sep=''
dPlaces= 5 /*# decimal places past the decimal point*/
width =10 /*width of field used to display elements*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
err: say; say; say '***error***!'; say; say arg(1); say; say; exit 13
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell: parse arg hdr,x,y,sep; #=0; if sep=='' then sep= ''
dPlaces= 5 /*# dec. places past the decimal point.*/
width =10 /*field width used to display elements.*/
if y=='' then !.=0
else do row=1 for ord; do col=1 for ord; x=x !.row.col; end; end
else do row=1 for ord; do col=1 for ord; x=x !.row.col; end; end
w=words(x)
do ord=1 until ord**2>=w; end /*a fast way to find matrix's order*/
do ord=1 until ord**2>=w; end /*a fast way to find the matrix's order*/
say
if ord**2\==w then call err "matrix elements don't form a square matrix."
say center(hdr, ((width+1)*w)%ord, sep)
say center(hdr, ((width + 1) * w) % ord, sep)
say
do row=1 for ord; z=
do col=1 for ord; #=#+1
@.row.col=word(x,#)
if col<=row then !.row.col=@.row.col
z=z right( format(@.row.col,, dPlaces) / 1, width)
end /*col*/
say z
do row=1 for ord; z=
do col=1 for ord; #= # + 1
@.row.col= word(x, #)
if col<=row then !.row.col= @.row.col
z=z right( format(@.row.col, , dPlaces) / 1, width)
end /*col*/ /* ↑↑↑ */
say z /* └┴┴──◄──normalization for zero*/
end /*row*/
return
/*────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); i=; m.=9
numeric digits 9; numeric form; h=d+6; if x<0 then do; x=-x; i='i'; end
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_%2
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
numeric digits d; return (g/1)i /*make complex if X < 0.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); numeric digits; h=d+6
numeric form; m.=9; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_ %2
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/; return g

View file

@ -0,0 +1,45 @@
# Project : Cholesky decomposition
# Date : 2017/11/12
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
load "stdlib.ring"
decimals(5)
m1 = [[25, 15, -5],
[15, 18, 0],
[-5, 0, 11]]
cholesky(m1)
printarray(m1)
see nl
m2 = [[18, 22, 54, 42],
[22, 70, 86, 62],
[54, 86, 174, 134],
[42, 62, 134, 106]]
cholesky(m2)
printarray(m2)
func cholesky(a)
l = newlist(len(a), len(a))
for i = 1 to len(a)
for j = 1 to i
s = 0
for k = 1 to j
s = s + l[i][k] * l[j][k]
next
if i = j
l[i][j] = sqrt(a[i][i] - s)
else
l[i][j] = (a[i][j] - s) / l[j][j]
ok
next
next
a = l
func printarray(a)
for row = 1 to len(a)
for col = 1 to len(a)
see "" + a[row][col] + " "
next
see nl
next

View file

@ -0,0 +1,19 @@
a = [25 15 -5; 15 18 0; -5 0 11];
chol(a)
ans =
5. 3. -1.
0. 3. 1.
0. 0. 3.
a = [18 22 54 42; 22 70 86 62;
54 86 174 134; 42 62 134 106];
chol(a)
ans =
4.2426407 5.1854497 12.727922 9.8994949
0. 6.5659052 3.0460385 1.6245539
0. 0. 1.6497422 1.849711
0. 0. 0. 1.3926212