Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
9
Task/LU-decomposition/Ada/lu-decomposition-1.adb
Normal file
9
Task/LU-decomposition/Ada/lu-decomposition-1.adb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
with Ada.Numerics.Generic_Real_Arrays;
|
||||
generic
|
||||
with package Matrix is new Ada.Numerics.Generic_Real_Arrays (<>);
|
||||
package Decomposition is
|
||||
|
||||
-- decompose a square matrix A by PA = LU
|
||||
procedure Decompose (A : Matrix.Real_Matrix; P, L, U : out Matrix.Real_Matrix);
|
||||
|
||||
end Decomposition;
|
||||
77
Task/LU-decomposition/Ada/lu-decomposition-2.adb
Normal file
77
Task/LU-decomposition/Ada/lu-decomposition-2.adb
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
package body Decomposition is
|
||||
|
||||
procedure Swap_Rows (M : in out Matrix.Real_Matrix; From, To : Natural) is
|
||||
Temporary : Matrix.Real;
|
||||
begin
|
||||
if From = To then
|
||||
return;
|
||||
end if;
|
||||
for I in M'Range (2) loop
|
||||
Temporary := M (M'First (1) + From, I);
|
||||
M (M'First (1) + From, I) := M (M'First (1) + To, I);
|
||||
M (M'First (1) + To, I) := Temporary;
|
||||
end loop;
|
||||
end Swap_Rows;
|
||||
|
||||
function Pivoting_Matrix
|
||||
(M : Matrix.Real_Matrix)
|
||||
return Matrix.Real_Matrix
|
||||
is
|
||||
use type Matrix.Real;
|
||||
Order : constant Positive := M'Length (1);
|
||||
Result : Matrix.Real_Matrix := Matrix.Unit_Matrix (Order);
|
||||
Max : Matrix.Real;
|
||||
Row : Natural;
|
||||
begin
|
||||
for J in 0 .. Order - 1 loop
|
||||
Max := M (M'First (1) + J, M'First (2) + J);
|
||||
Row := J;
|
||||
for I in J .. Order - 1 loop
|
||||
if M (M'First (1) + I, M'First (2) + J) > Max then
|
||||
Max := M (M'First (1) + I, M'First (2) + J);
|
||||
Row := I;
|
||||
end if;
|
||||
end loop;
|
||||
if J /= Row then
|
||||
-- swap rows J and Row
|
||||
Swap_Rows (Result, J, Row);
|
||||
end if;
|
||||
end loop;
|
||||
return Result;
|
||||
end Pivoting_Matrix;
|
||||
|
||||
procedure Decompose (A : Matrix.Real_Matrix; P, L, U : out Matrix.Real_Matrix) is
|
||||
use type Matrix.Real_Matrix, Matrix.Real;
|
||||
Order : constant Positive := A'Length (1);
|
||||
A2 : Matrix.Real_Matrix (A'Range (1), A'Range (2));
|
||||
S : Matrix.Real;
|
||||
begin
|
||||
L := (others => (others => 0.0));
|
||||
U := (others => (others => 0.0));
|
||||
P := Pivoting_Matrix (A);
|
||||
A2 := P * A;
|
||||
for J in 0 .. Order - 1 loop
|
||||
L (L'First (1) + J, L'First (2) + J) := 1.0;
|
||||
for I in 0 .. J loop
|
||||
S := 0.0;
|
||||
for K in 0 .. I - 1 loop
|
||||
S := S + U (U'First (1) + K, U'First (2) + J) *
|
||||
L (L'First (1) + I, L'First (2) + K);
|
||||
end loop;
|
||||
U (U'First (1) + I, U'First (2) + J) :=
|
||||
A2 (A2'First (1) + I, A2'First (2) + J) - S;
|
||||
end loop;
|
||||
for I in J + 1 .. Order - 1 loop
|
||||
S := 0.0;
|
||||
for K in 0 .. J loop
|
||||
S := S + U (U'First (1) + K, U'First (2) + J) *
|
||||
L (L'First (1) + I, L'First (2) + K);
|
||||
end loop;
|
||||
L (L'First (1) + I, L'First (2) + J) :=
|
||||
(A2 (A2'First (1) + I, A2'First (2) + J) - S) /
|
||||
U (U'First (1) + J, U'First (2) + J);
|
||||
end loop;
|
||||
end loop;
|
||||
end Decompose;
|
||||
|
||||
end Decomposition;
|
||||
53
Task/LU-decomposition/Ada/lu-decomposition-3.adb
Normal file
53
Task/LU-decomposition/Ada/lu-decomposition-3.adb
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
with Ada.Numerics.Real_Arrays;
|
||||
with Ada.Text_IO;
|
||||
with Decomposition;
|
||||
procedure Decompose_Example is
|
||||
package Real_Decomposition is new Decomposition
|
||||
(Matrix => Ada.Numerics.Real_Arrays);
|
||||
|
||||
package Real_IO is new Ada.Text_IO.Float_IO (Float);
|
||||
|
||||
procedure Print (M : Ada.Numerics.Real_Arrays.Real_Matrix) is
|
||||
begin
|
||||
for Row in M'Range (1) loop
|
||||
for Col in M'Range (2) loop
|
||||
Real_IO.Put (M (Row, Col), 3, 2, 0);
|
||||
end loop;
|
||||
Ada.Text_IO.New_Line;
|
||||
end loop;
|
||||
end Print;
|
||||
|
||||
Example_1 : constant Ada.Numerics.Real_Arrays.Real_Matrix :=
|
||||
((1.0, 3.0, 5.0),
|
||||
(2.0, 4.0, 7.0),
|
||||
(1.0, 1.0, 0.0));
|
||||
P_1, L_1, U_1 : Ada.Numerics.Real_Arrays.Real_Matrix (Example_1'Range (1),
|
||||
Example_1'Range (2));
|
||||
Example_2 : constant Ada.Numerics.Real_Arrays.Real_Matrix :=
|
||||
((11.0, 9.0, 24.0, 2.0),
|
||||
(1.0, 5.0, 2.0, 6.0),
|
||||
(3.0, 17.0, 18.0, 1.0),
|
||||
(2.0, 5.0, 7.0, 1.0));
|
||||
P_2, L_2, U_2 : Ada.Numerics.Real_Arrays.Real_Matrix (Example_2'Range (1),
|
||||
Example_2'Range (2));
|
||||
begin
|
||||
Real_Decomposition.Decompose (A => Example_1,
|
||||
P => P_1,
|
||||
L => L_1,
|
||||
U => U_1);
|
||||
Real_Decomposition.Decompose (A => Example_2,
|
||||
P => P_2,
|
||||
L => L_2,
|
||||
U => U_2);
|
||||
Ada.Text_IO.Put_Line ("Example 1:");
|
||||
Ada.Text_IO.Put_Line ("A:"); Print (Example_1);
|
||||
Ada.Text_IO.Put_Line ("L:"); Print (L_1);
|
||||
Ada.Text_IO.Put_Line ("U:"); Print (U_1);
|
||||
Ada.Text_IO.Put_Line ("P:"); Print (P_1);
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put_Line ("Example 2:");
|
||||
Ada.Text_IO.Put_Line ("A:"); Print (Example_2);
|
||||
Ada.Text_IO.Put_Line ("L:"); Print (L_2);
|
||||
Ada.Text_IO.Put_Line ("U:"); Print (U_2);
|
||||
Ada.Text_IO.Put_Line ("P:"); Print (P_2);
|
||||
end Decompose_Example;
|
||||
75
Task/LU-decomposition/EasyLang/lu-decomposition.easy
Normal file
75
Task/LU-decomposition/EasyLang/lu-decomposition.easy
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
func[][] mmul m1[][] m2[][] .
|
||||
for i to len m1[][]
|
||||
r[][] &= [ ]
|
||||
for j = 1 to len m2[1][]
|
||||
r[i][] &= 0
|
||||
for k to len m2[][]
|
||||
r[i][j] += m1[i][k] * m2[k][j]
|
||||
.
|
||||
.
|
||||
.
|
||||
return r[][]
|
||||
.
|
||||
func[][] midm n .
|
||||
len m[][] n
|
||||
for i to n
|
||||
len m[i][] n
|
||||
m[i][i] = 1
|
||||
.
|
||||
return m[][]
|
||||
.
|
||||
func[][] pivotize m[][] .
|
||||
n = len m[][]
|
||||
im[][] = midm n
|
||||
for i to n
|
||||
mx = abs m[i][i]
|
||||
fila = i
|
||||
for j = i to n
|
||||
if abs m[j][i] > mx
|
||||
mx = abs m[j][i]
|
||||
fila = j
|
||||
.
|
||||
.
|
||||
if i <> fila
|
||||
for j to n : swap im[i][j] im[fila][j]
|
||||
.
|
||||
.
|
||||
return im[][]
|
||||
.
|
||||
proc ludecomp a[][] &l[][] &u[][] &p[][] .
|
||||
n = len a[][]
|
||||
len l[][] n
|
||||
len u[][] n
|
||||
for i to n
|
||||
len l[i][] n
|
||||
len u[i][] n
|
||||
.
|
||||
p[][] = pivotize a[][]
|
||||
b[][] = mmul p[][] a[][]
|
||||
for j to n
|
||||
l[j][j] = 1
|
||||
for i to j
|
||||
s = 0
|
||||
for k to i - 1
|
||||
s += u[k][j] * l[i][k]
|
||||
.
|
||||
u[i][j] = b[i][j] - s
|
||||
.
|
||||
for i = j + 1 to n
|
||||
s = 0
|
||||
for k to j - 1
|
||||
s += u[k][j] * l[i][k]
|
||||
.
|
||||
l[i][j] = (b[i][j] - s) / u[j][j]
|
||||
.
|
||||
.
|
||||
.
|
||||
proc go a[][] .
|
||||
ludecomp a[][] l[][] u[][] p[][]
|
||||
print l[][]
|
||||
print u[][]
|
||||
print p[][]
|
||||
print ""
|
||||
.
|
||||
go [ [ 1 3 5 ] [ 2 4 7 ] [ 1 1 0 ] ]
|
||||
go [ [ 11 9 24 2 ] [ 1 5 2 6 ] [ 3 17 18 1 ] [ 2 5 7 1 ] ]
|
||||
26
Task/LU-decomposition/Pluto/lu-decomposition.pluto
Normal file
26
Task/LU-decomposition/Pluto/lu-decomposition.pluto
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
require "matrix"
|
||||
|
||||
local arrays = {
|
||||
{ {1, 3, 5},
|
||||
{2, 4, 7},
|
||||
{1, 1, 0} },
|
||||
|
||||
{ {11, 9, 24, 2},
|
||||
{ 1, 5, 2, 6},
|
||||
{ 3, 17, 18, 1},
|
||||
{ 2, 5, 7, 1} }
|
||||
}
|
||||
|
||||
for arrays as array do
|
||||
local m = matrix.from(array)
|
||||
print("A\n")
|
||||
print(m)
|
||||
print("\nL\n")
|
||||
local [l, u, p] = m:lup()
|
||||
print(l:format("%8.5f"))
|
||||
print("\nU\n")
|
||||
print(u:format("%8.5f"))
|
||||
print("\nP\n")
|
||||
print(p)
|
||||
print()
|
||||
end
|
||||
|
|
@ -1,15 +1,15 @@
|
|||
from pprint import pprint
|
||||
|
||||
def matrixMul(A, B):
|
||||
TB = zip(*B)
|
||||
return [[sum(ea*eb for ea,eb in zip(a,b)) for b in TB] for a in A]
|
||||
TB = list(zip(*B))
|
||||
return [[sum(ea * eb for ea, eb in zip(a, b)) for b in TB] for a in A]
|
||||
|
||||
def pivotize(m):
|
||||
"""Creates the pivoting matrix for m."""
|
||||
n = len(m)
|
||||
ID = [[float(i == j) for i in xrange(n)] for j in xrange(n)]
|
||||
for j in xrange(n):
|
||||
row = max(xrange(j, n), key=lambda i: abs(m[i][j]))
|
||||
ID = [[float(i == j) for i in range(n)] for j in range(n)]
|
||||
for j in range(n):
|
||||
row = max(range(j, n), key=lambda i: abs(m[i][j]))
|
||||
if j != row:
|
||||
ID[j], ID[row] = ID[row], ID[j]
|
||||
return ID
|
||||
|
|
@ -17,26 +17,28 @@ def pivotize(m):
|
|||
def lu(A):
|
||||
"""Decomposes a nxn matrix A by PA=LU and returns L, U and P."""
|
||||
n = len(A)
|
||||
L = [[0.0] * n for i in xrange(n)]
|
||||
U = [[0.0] * n for i in xrange(n)]
|
||||
L = [[0.0] * n for i in range(n)]
|
||||
U = [[0.0] * n for i in range(n)]
|
||||
P = pivotize(A)
|
||||
A2 = matrixMul(P, A)
|
||||
for j in xrange(n):
|
||||
for j in range(n):
|
||||
L[j][j] = 1.0
|
||||
for i in xrange(j+1):
|
||||
s1 = sum(U[k][j] * L[i][k] for k in xrange(i))
|
||||
for i in range(j + 1):
|
||||
s1 = sum(U[k][j] * L[i][k] for k in range(i))
|
||||
U[i][j] = A2[i][j] - s1
|
||||
for i in xrange(j, n):
|
||||
s2 = sum(U[k][j] * L[i][k] for k in xrange(j))
|
||||
for i in range(j, n):
|
||||
s2 = sum(U[k][j] * L[i][k] for k in range(j))
|
||||
L[i][j] = (A2[i][j] - s2) / U[j][j]
|
||||
return (L, U, P)
|
||||
|
||||
|
||||
a = [[1, 3, 5], [2, 4, 7], [1, 1, 0]]
|
||||
for part in lu(a):
|
||||
pprint(part, width=19)
|
||||
print
|
||||
print
|
||||
b = [[11,9,24,2],[1,5,2,6],[3,17,18,1],[2,5,7,1]]
|
||||
print()
|
||||
|
||||
print()
|
||||
b = [[11, 9, 24, 2], [1, 5, 2, 6], [3, 17, 18, 1], [2, 5, 7, 1]]
|
||||
for part in lu(b):
|
||||
pprint(part)
|
||||
print
|
||||
print()
|
||||
|
|
|
|||
114
Task/LU-decomposition/V-(Vlang)/lu-decomposition.v
Normal file
114
Task/LU-decomposition/V-(Vlang)/lu-decomposition.v
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
type Vector = []f64
|
||||
type Matrix = [][]f64
|
||||
|
||||
fn matrix_multiply(amx Matrix, bmx Matrix) Matrix {
|
||||
rows1 := amx.len
|
||||
cols1 := amx[0].len
|
||||
rows2 := bmx.len
|
||||
cols2 := bmx[0].len
|
||||
assert cols1 == rows2
|
||||
mut result := [][]f64{len: rows1, init: []f64{len: cols2, init: 0.0}}
|
||||
mut sum := f64(0)
|
||||
for ial in 0 .. rows1 {
|
||||
for jal in 0 .. cols2 {
|
||||
sum = 0.0
|
||||
for kal in 0 .. rows2 {
|
||||
sum += amx[ial][kal] * bmx[kal][jal]
|
||||
}
|
||||
result[ial][jal] = sum
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fn pivotize(mx Matrix) Matrix {
|
||||
nir := mx.len
|
||||
mut imx := [][]f64{len: nir, init: []f64{len: nir, init: 0.0}}
|
||||
mut max, mut row := f64(0), 0
|
||||
for ial in 0 .. nir {
|
||||
imx[ial][ial] = 1.0
|
||||
}
|
||||
for ial in 0 .. nir {
|
||||
max = mx[ial][ial]
|
||||
row = ial
|
||||
for jal in ial .. nir {
|
||||
if mx[jal][ial] > max {
|
||||
max = mx[jal][ial]
|
||||
row = jal
|
||||
}
|
||||
}
|
||||
if ial != row { imx[ial], imx[row] = imx[row], imx[ial] }
|
||||
}
|
||||
return imx
|
||||
}
|
||||
|
||||
fn lu(amx Matrix) (Matrix, Matrix, Matrix) {
|
||||
nir := amx.len
|
||||
mut lmx := [][]f64{len: nir, init: []f64{len: nir, init: 0.0}}
|
||||
mut umx := [][]f64{len: nir, init: []f64{len: nir, init: 0.0}}
|
||||
mut sum, mut sum2 := f64(0), f64(0)
|
||||
pmx := pivotize(amx)
|
||||
a2 := matrix_multiply(pmx, amx)
|
||||
|
||||
for jal in 0 .. nir {
|
||||
lmx[jal][jal] = 1.0
|
||||
for ial in 0 .. jal + 1 {
|
||||
sum = 0.0
|
||||
for kal in 0 .. ial {
|
||||
sum += umx[kal][jal] * lmx[ial][kal]
|
||||
}
|
||||
umx[ial][jal] = a2[ial][jal] - sum
|
||||
}
|
||||
for ial in jal .. nir {
|
||||
sum2 = 0.0
|
||||
for kal in 0 .. jal {
|
||||
sum2 += umx[kal][jal] * lmx[ial][kal]
|
||||
}
|
||||
lmx[ial][jal] = (a2[ial][jal] - sum2) / umx[jal][jal]
|
||||
}
|
||||
}
|
||||
return lmx, umx, pmx
|
||||
}
|
||||
|
||||
fn print_matrix(title string, mx Matrix, fsg string) {
|
||||
nir := mx.len
|
||||
println("\n$title\n")
|
||||
for ial in 0 .. nir {
|
||||
for jal in 0 .. nir {
|
||||
match fsg {
|
||||
"%8.5f" { print("${mx[ial][jal]:8.5f} ") }
|
||||
"%7.5f" { print("${mx[ial][jal]:7.5f} ") }
|
||||
"%2.0f" { print("${mx[ial][jal]:2} ") }
|
||||
"%1.0f" { print("${mx[ial][jal]:1} ") }
|
||||
else { print("${mx[ial][jal]} ") }
|
||||
}
|
||||
}
|
||||
println("")
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
a1 := [
|
||||
[1.0, 3.0, 5.0],
|
||||
[2.0, 4.0, 7.0],
|
||||
[1.0, 1.0, 0.0],
|
||||
]
|
||||
l1, u1, p1 := lu(a1)
|
||||
println("EXAMPLE 1:-")
|
||||
print_matrix("A:", a1, "%1.0f")
|
||||
print_matrix("L:", l1, "%8.5f")
|
||||
print_matrix("U:", u1, "%8.5f")
|
||||
print_matrix("P:", p1, "%1.0f")
|
||||
a2 := [
|
||||
[11.0, 9.0, 24.0, 2.0],
|
||||
[1.0, 5.0, 2.0, 6.0],
|
||||
[3.0, 17.0, 18.0, 1.0],
|
||||
[2.0, 5.0, 7.0, 1.0],
|
||||
]
|
||||
l2, u2, p2 := lu(a2)
|
||||
println("\nEXAMPLE 2:-")
|
||||
print_matrix("A:", a2, "%2.0f")
|
||||
print_matrix("L:", l2, "%7.5f")
|
||||
print_matrix("U:", u2, "%8.5f")
|
||||
print_matrix("P:", p2, "%1.0f")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue