-
Notifications
You must be signed in to change notification settings - Fork 33
/
LUP_decomposition.pl
executable file
·165 lines (115 loc) · 3.11 KB
/
LUP_decomposition.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/perl
# Simple implementation of the LU decomposition.
# See also:
# https://en.wikipedia.org/wiki/LU_decomposition
use 5.014;
use warnings;
use Math::AnyNum qw(:overload);
# Code translated from Wikipedia (+ minor tweaks):
# https://en.wikipedia.org/wiki/LU_decomposition#C_code_examples
sub _LUP_decompose {
my ($matrix) = @_;
my @A = map { [@$_] } @$matrix;
my $N = $#A;
my @P = (0 .. $N + 1);
foreach my $i (0 .. $N) {
my $maxA = 0;
my $imax = $i;
foreach my $k ($i .. $N) {
my $absA = abs($A[$k][$i] // return ($N, \@A, \@P));
if ($absA > $maxA) {
$maxA = $absA;
$imax = $k;
}
}
if ($imax != $i) {
@P[$i, $imax] = @P[$imax, $i];
@A[$i, $imax] = @A[$imax, $i];
++$P[$N + 1];
}
foreach my $j ($i + 1 .. $N) {
if ($A[$i][$i] == 0) {
return ($N, \@A, \@P);
}
$A[$j][$i] /= $A[$i][$i];
foreach my $k ($i + 1 .. $N) {
$A[$j][$k] -= $A[$j][$i] * $A[$i][$k];
}
}
}
return ($N, \@A, \@P);
}
sub solve {
my ($matrix, $vector) = @_;
my ($N, $A, $P) = _LUP_decompose($matrix);
my @x = map { $vector->[$P->[$_]] } 0 .. $N;
foreach my $i (1 .. $N) {
foreach my $k (0 .. $i - 1) {
$x[$i] -= $A->[$i][$k] * $x[$k];
}
}
for (my $i = $N ; $i >= 0 ; --$i) {
foreach my $k ($i + 1 .. $N) {
$x[$i] -= $A->[$i][$k] * $x[$k];
}
$x[$i] /= $A->[$i][$i];
}
return \@x;
}
sub invert {
my ($matrix) = @_;
my ($N, $A, $P) = _LUP_decompose($matrix);
my @I;
foreach my $j (0 .. $N) {
foreach my $i (0 .. $N) {
$I[$i][$j] = ($P->[$i] == $j) ? 1 : 0;
foreach my $k (0 .. $i - 1) {
$I[$i][$j] -= $A->[$i][$k] * $I[$k][$j];
}
}
for (my $i = $N ; $i >= 0 ; --$i) {
foreach my $k ($i + 1 .. $N) {
$I[$i][$j] -= $A->[$i][$k] * $I[$k][$j];
}
$I[$i][$j] /= $A->[$i][$i] // return [[]];
}
}
return \@I;
}
sub determinant {
my ($matrix) = @_;
my ($N, $A, $P) = _LUP_decompose($matrix);
my $det = $A->[0][0] // return 1;
foreach my $i (1 .. $N) {
$det *= $A->[$i][$i];
}
if (($P->[$N + 1] - $N) % 2 == 0) {
$det *= -1;
}
return $det;
}
#
## Examples
#
# Defining a matrix
my $A = [
[2, -1, 5, 1],
[3, 2, 2, -6],
[1, 3, 3, -1],
[5, -2, -3, 3],
];
# Determinant of a matrix
say "det(A) = ", determinant($A);
# Solve a system of linear equations
my $v = [-3, -32, -47, 49];
say '(', join(', ', @{solve($A, $v)}), ')';
# Invert a matrix
my $inv = invert($A);
say join(",\n", map { '[' . join(', ', map { sprintf('%8s', $_) } @$_) . ']' } @$inv);
__END__
det(A) = 684
(2, -12, -4, 1)
[ 4/171, 11/171, 10/171, 8/57],
[ -55/342, -23/342, 119/342, 2/57],
[ 107/684, -5/684, 11/684, -7/114],
[ 7/684, -109/684, 103/684, 7/114]