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,17 +3,17 @@ package main
import (
"fmt"
"github.com/gonum/matrix/mat64"
"gonum.org/v1/gonum/mat"
)
func main() {
showLU(mat64.NewDense(3, 3, []float64{
showLU(mat.NewDense(3, 3, []float64{
1, 3, 5,
2, 4, 7,
1, 1, 0,
}))
fmt.Println()
showLU(mat64.NewDense(4, 4, []float64{
showLU(mat.NewDense(4, 4, []float64{
11, 9, 24, 2,
1, 5, 2, 6,
3, 17, 18, 1,
@ -21,14 +21,13 @@ func main() {
}))
}
func showLU(a *mat64.Dense) {
fmt.Printf("a: %v\n\n", mat64.Formatted(a, mat64.Prefix(" ")))
var lu mat64.LU
func showLU(a *mat.Dense) {
fmt.Printf("a: %v\n\n", mat.Formatted(a, mat.Prefix(" ")))
var lu mat.LU
lu.Factorize(a)
var l, u mat64.TriDense
l.LFrom(&lu)
u.UFrom(&lu)
fmt.Printf("l: %.5f\n\n", mat64.Formatted(&l, mat64.Prefix(" ")))
fmt.Printf("u: %.5f\n\n", mat64.Formatted(&u, mat64.Prefix(" ")))
l := lu.LTo(nil)
u := lu.UTo(nil)
fmt.Printf("l: %.5f\n\n", mat.Formatted(l, mat.Prefix(" ")))
fmt.Printf("u: %.5f\n\n", mat.Formatted(u, mat.Prefix(" ")))
fmt.Println("p:", lu.Pivot(nil))
}

View file

@ -0,0 +1,102 @@
// version 1.1.4-3
typealias Vector = DoubleArray
typealias Matrix = Array<Vector>
operator fun Matrix.times(other: Matrix): Matrix {
val rows1 = this.size
val cols1 = this[0].size
val rows2 = other.size
val cols2 = other[0].size
require(cols1 == rows2)
val result = Matrix(rows1) { Vector(cols2) }
for (i in 0 until rows1) {
for (j in 0 until cols2) {
for (k in 0 until rows2) {
result[i][j] += this[i][k] * other[k][j]
}
}
}
return result
}
fun pivotize(m: Matrix): Matrix {
val n = m.size
val im = Array(n) { Vector(n) }
for (i in 0 until n) im[i][i] = 1.0
for (i in 0 until n) {
var max = m[i][i]
var row = i
for (j in i until n) {
if (m[j][i] > max) {
max = m[j][i]
row = j
}
}
if (i != row) {
val t = im[i]
im[i] = im[row]
im[row] = t
}
}
return im
}
fun lu(a: Matrix): Array<Matrix> {
val n = a.size
val l = Array(n) { Vector(n) }
val u = Array(n) { Vector(n) }
val p = pivotize(a)
val a2 = p * a
for (j in 0 until n) {
l[j][j] = 1.0
for (i in 0 until j + 1) {
var sum = 0.0
for (k in 0 until i) sum += u[k][j] * l[i][k]
u[i][j] = a2[i][j] - sum
}
for (i in j until n) {
var sum2 = 0.0
for(k in 0 until j) sum2 += u[k][j] * l[i][k]
l[i][j] = (a2[i][j] - sum2) / u[j][j]
}
}
return arrayOf(l, u, p)
}
fun printMatrix(title: String, m: Matrix, f: String) {
val n = m.size
println("\n$title\n")
for (i in 0 until n) {
for (j in 0 until n) print("${f.format(m[i][j])} ")
println()
}
}
fun main(args: Array<String>) {
val a1 = arrayOf(
doubleArrayOf( 1.0, 3.0, 5.0),
doubleArrayOf( 2.0, 4.0, 7.0),
doubleArrayOf( 1.0, 1.0, 0.0)
)
val (l1, u1, p1) = lu(a1)
println("EXAMPLE 1:-")
printMatrix("A:", a1, "%1.0f")
printMatrix("L:", l1, "% 7.5f")
printMatrix("U:", u1, "% 8.5f")
printMatrix("P:", p1, "%1.0f")
val a2 = arrayOf(
doubleArrayOf(11.0, 9.0, 24.0, 2.0),
doubleArrayOf( 1.0, 5.0, 2.0, 6.0),
doubleArrayOf( 3.0, 17.0, 18.0, 1.0),
doubleArrayOf( 2.0, 5.0, 7.0, 1.0)
)
val (l2, u2, p2) = lu(a2)
println("\nEXAMPLE 2:-")
printMatrix("A:", a2, "%2.0f")
printMatrix("L:", l2, "%7.5f")
printMatrix("U:", u2, "%8.5f")
printMatrix("P:", p2, "%1.0f")
}

View file

@ -1,73 +1,62 @@
/*REXX program creates a matrix from console input, performs/shows LU decomposition.*/
#=0; P.=0; PA.=0; L.=0; U.=0 /*initialize some variables to zero. */
parse arg x /*obtain matrix elements from the C.L. */
call makeMat /*make the A matrix from the numbers.*/
call showMat 'A', N /*display the A matrix. */
call manPmat /*manufacture P (permutation). */
call showMat 'P', N /*display the P matrix. */
call multMat /*multiply the A and P matrices. */
call showMat 'PA', N /*display the PA matrix. */
do y=1 for N; call manUmat y /*manufacture U matrix, parts. */
call manLmat y /*manufacture L matrix, parts. */
end
call showMat 'L', N /*display the L matrix. */
call showMat 'U', N /*display the U matrix. */
call bldAMat; call showMat 'A' /*build and display A matrix.*/
call bldPmat; call showMat 'P' /* " " " P " */
call multMat; call showMat 'PA' /* " " " PA " */
do y=1 for N; call bldUmat; call bldLmat /*build U and L " */
end /*y*/
call showMat 'L'; call showMat 'U' /*display L and U " */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
er: say; say '***error!***'; say; say arg(1); say; exit 13
bldAMat: ?=words(x); do N=1 for ? until N**2>=? /*find matrix size. */
end /*N*/
if N**2\==? then do; say '***error*** wrong # of elements entered:' ?; exit 9
end
do r=1 for N /*build A matrix.*/
do c=1 for N; #=# + 1; _=word(x, #); A.r.c=_
if \datatype(_, 'N') then call er "element isn't numeric: " _
end /*c*/
end /*r*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
makeMat: ?=words(x); do N=1 for ?; if N**2==? then leave; end /*N*/
if N**2\==? then call er 'not correct number of elements entered: ' ?
do r=1 for N /*build the "A" matrix from the input*/
do c=1 for N; #=#+1; _=word(x,#); A.r.c=_
if \datatype(_,'N') then call er "element isn't numeric: " _
end /*c*/
end /*r*/
return
bldLmat: do r=1 for N /*build lower matrix*/
do c=1 for N; if r==c then do; L.r.c=1; iterate; end
if c\==y | r==c | c>r then iterate
_=PA.r.c
do k=1 for c-1; _=_ - U.k.c * L.r.k
end /*k*/
L.r.c=_ / U.c.c
end /*c*/
end /*r*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
manLmat: parse arg ? /*manufacture L (lower) matrix.*/
do r=1 for N
do c=1 for N; if r==c then do; L.r.c=1; iterate; end
if c\==? | r==c | c>r then iterate
_=PA.r.c
do k=1 for c-1; _=_-U.k.c*L.r.k; end /*k*/
L.r.c=_/U.c.c
end /*c*/
end /*r*/
return
bldPmat: c=N; do r=N by -1 for N; P.r.c=1; c=c+1 /*build perm. matrix*/
if c>N then c=N%2; if c==N then c=1
end /*r*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
manPmat: c=N; do r=N by -1 for N /*manufacture P (permutation). */
P.r.c=1; c=c+1; if c>N then c=N%2; if c==N then c=1
end /*r*/
return
bldUmat: do r=1 for N; if r\==y then iterate /*build upper matrix*/
do c=1 for N; if c<r then iterate
_=PA.r.c
do k=1 for r-1; _=_ - U.k.c * L.r.k
end /*k*/
U.r.c=_ / 1
end /*c*/
end /*r*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
manUmat: parse arg ? /*manufacture U (upper) matrix.*/
do r=1 for N; if r\==? then iterate
do c=1 for N; if c<r then iterate
_=PA.r.c
do k=1 for r-1; _=_-U.k.c*L.r.k; end /*k*/
U.r.c=_/1
end /*c*/
end /*r*/
return
multMat: do i=1 for N /*multiply matrix P & A ──► PA */
do j=1 for N
do k=1 for N; pa.i.j=(pa.i.j + p.i.k * a.k.j) / 1
end /*k*/
end /*j*/
end /*i*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
multMat: do i=1 for N /*multiply matrix P & A ──► PA */
do j=1 for N
do k=1 for N; pa.i.j=(pa.i.j + p.i.k * a.k.j) / 1
end /*k*/
end /*j*/
end /*i*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
showMat: parse arg mat,rows,cols; w=0; cols=word(cols rows,1); say
do r=1 for rows
do c=1 for cols; w=max(w, length( value( mat'.'r"."c ) ) )
end /*c*/
end /*r*/
say center(mat 'matrix',cols*(w+1)+7,"")
do r=1 for rows; _=
do c=1 for cols; _=_ right(value(mat'.'r'.'c),w+1); end /*c*/
say _
end /*r*/
return
showMat: parse arg mat,rows,cols; say; rows=word(rows N,1); cols=word(cols rows,1)
w=0; do r=1 for rows
do c=1 for cols; w=max(w, length( value( mat'.'r"."c ) ) )
end /*c*/
end /*r*/
say center(mat 'matrix', cols * (w + 1) + 7, "") /*display the header*/
do r=1 for rows; _=
do c=1 for cols; _=_ right( value(mat'.'r"."c), w + 1)
end /*c*/
say _
end /*r*/; return

View file

@ -0,0 +1,23 @@
void ludec(real matrix a, real matrix l, real matrix u, real vector p) {
real scalar i,j,n,s
real vector js
l = a
n = rows(a)
p = 1::n
for (i=1; i<n; i++) {
maxindex(abs(l[i::n,i]), 1, js=., .)
j = js[1]+i-1
if (j!=i) {
l[(i\j),.] = l[(j\i),.]
p[(i\j)] = p[(j\i)]
}
for (j=i+1; j<=n; j++) {
l[j,i] = s = l[j,i]/l[i,i]
l[j,i+1..n] = l[j,i+1..n]-s*l[i,i+1..n]
}
}
u = uppertriangle(l)
l = lowertriangle(l, 1)
}

View file

@ -0,0 +1,33 @@
: ludec(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 |
+-----+