September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,36 +1,36 @@
func is_square(m) { m.all { .len == m.len } }
func matrix_zero(n, m=n) { m.of { n.of(0) } }
func matrix_ident(n) { n.of {|i| [(i-1).of(0)..., 1, (n-i).of(0)...] } }
func matrix_ident(n) { n.of {|i| n.of {|j| i==j ? 1 : 0 } } }
 
func pivotize(m) {
var size = m.len
var id = matrix_ident(size)
for i in ^size {
for i (^size) {
var max = m[i][i]
var row = i
for j in range(i, size-1) {
for j (i .. size-1) {
if (m[j][i] > max) {
max = m[j][i]
row = j
}
}
if (row != i) {
if (row != i) {
id.swap(row, i)
}
}
return id
}
 
func mmult(a, b) {
var p = []
for r,c in (^a ~X ^b[0]) {
for i in ^b {
p[r][c] := 0 += (a[r][i] * b[i][c])
for r,c (^a ~X ^b[0]) {
for i (^b) {
p[r][c] := 0 += (a[r][i] * b[i][c])
}
}
return p
}
 
func lu(a) {
is_square(a) || die "Defined only for square matrices!";
var n = a.len
@ -38,23 +38,23 @@ func lu(a) {
var Aʼ = mmult(P, a)
var L = matrix_ident(n)
var U = matrix_zero(n)
for i,j in (^n ~X ^n) {
for i,j (^n ~X ^n) {
if (j >= i) {
U[i][j] = (Aʼ[i][j] - (^i->map { U[_][j] * L[i][_] }.sum(0)))
U[i][j] = (Aʼ[i][j] - ({ U[_][j] * L[i][_] }.map(^i).sum))
} else {
L[i][j] = ((Aʼ[i][j] - (^j->map { U[_][j] * L[i][_] }.sum(0))) / U[j][j])
L[i][j] = (Aʼ[i][j] - ({ U[_][j] * L[i][_] }.map(^j).sum))/U[j][j]
}
}
return [P, Aʼ, L, U]
}
 
func say_it(message, array) {
say "\n#{message}"
array.each { |row|
say row.map{"%7s" % .as_rat}.join(' ')
}
}
 
var t = [[
%n(1 3 5),
%n(2 4 7),
@ -65,10 +65,10 @@ var t = [[
%n( 3 17 18 1),
%n( 2 5 7 1),
]]
t.each { |test|
 
for test (t) {
say_it('A Matrix', test);
for a in (['P Matrix', 'Aʼ Matrix', 'L Matrix', 'U Matrix'] ~Z lu(test)) {
say_it(a[0], a[1])
for a,b (['P Matrix', 'Aʼ Matrix', 'L Matrix', 'U Matrix'] ~Z lu(test)) {
say_it(a, b)
}
}

View file

@ -0,0 +1,34 @@
mata
: lud(a=(1,3,5\2,4,7\1,1,0),l=.,u=.,p=.)
: a
1 2 3
+-------------+
1 | 1 3 5 |
2 | 2 4 7 |
3 | 1 1 0 |
+-------------+
: l
1 2 3
+----------------+
1 | 1 0 0 |
2 | .5 1 0 |
3 | .5 -1 1 |
+----------------+
: u
1 2 3
+-------------------+
1 | 2 4 7 |
2 | 0 1 1.5 |
3 | 0 0 -2 |
+-------------------+
: p
1
+-----+
1 | 2 |
2 | 1 |
3 | 3 |
+-----+

View file

@ -0,0 +1,18 @@
var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library)
fcn luTask(A){
A.LUDecompose(); // in place, contains L & U
L:=A.copy().lowerTriangle().setDiagonal(0,0,1);
U:=A.copy().upperTriangle();
return(L,U);
}
A:=GSL.Matrix(3,3).set(1,3,5, 2,4,7, 1,1,0); // example 1
L,U:=luTask(A);
println("L:\n",L.format(),"\nU:\n",U.format());
A:=GSL.Matrix(4,4).set(11.0, 9.0, 24.0, 2.0, // example 2
1.0, 5.0, 2.0, 6.0,
3.0, 17.0, 18.0, 1.0,
2.0, 5.0, 7.0, 1.0);
L,U:=luTask(A);
println("L:\n",L.format(8,4),"\nU:\n",U.format(8,4));

View file

@ -0,0 +1,50 @@
fcn make_array(n,m,v){ (m).pump(List.createLong(m).write,v)*n }
fcn eye(n){ // Creates a nxn identity matrix.
I:=make_array(n,n,0.0);
foreach j in (n){ I[j][j]=1.0 }
I
}
// Creates the pivoting matrix for A.
fcn pivotize(A){
n:=A.len(); // rows
P:=eye(n);
foreach i in (n){
max,row:=A[i][i],i;
foreach j in ([i..n-1]){
if(A[j][i]>max) max,row=A[j][i],j;
}
if(i!=row) P.swap(i,row);
}
// Return P.
P
}
// Decomposes a square matrix A by PA=LU and returns L, U and P.
fcn lu(A){
n:=A.len();
L:=eye(n);
U:=make_array(n,n,0.0);
P:=pivotize(A);
A=matMult(P,A);
foreach j in (n){
foreach i in (j+1){
U[i][j]=A[i][j] - (i).reduce('wrap(s,k){ s + U[k][j]*L[i][k] },0.0);
}
foreach i in ([j..n-1]){
L[i][j]=( A[i][j] -
(j).reduce('wrap(s,k){ s + U[k][j]*L[i][k] },0.0) ) /
U[j][j];
}
}
// Return L, U and P.
return(L,U,P);
}
fcn matMult(a,b){
n,m,p:=a[0].len(),a.len(),b[0].len();
ans:=make_array(n,m,0.0);
foreach i,j,k in (m,p,n){ ans[i][j]+=a[i][k]*b[k][j]; }
ans
}

View file

@ -0,0 +1,2 @@
g:=L(L(1.0,3.0,5.0),L(2.0,4.0,7.0),L(1.0,1.0,0.0));
lu(g).apply2("println");

View file

@ -0,0 +1,7 @@
lu(L( L(11.0, 9.0, 24.0, 2.0),
L( 1.0, 5.0, 2.0, 6.0),
L( 3.0, 17.0, 18.0, 1.0),
L( 2.0, 5.0, 7.0, 1.0) )).apply2(T(printM,Console.writeln.fpM("-")));
fcn printM(m) { m.pump(Console.println,rowFmt) }
fcn rowFmt(row){ ("%9.5f "*row.len()).fmt(row.xplode()) }