blib/lib/Math/MatrixDecomposition/LU.pm untested

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 28 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 211 CC 20 SCAR 40.0
CC
20
Cov
0%
solve
110.0
decompose
72.0
det
12.0
CRAP 54.4
1 ## Math/MatrixDecomposition/LU.pm --- LU decomposition.
2  
3 # Copyright (C) 2010 Ralph Schleicher. All rights reserved.
4  
5 # This program is free software; you can redistribute it and/or
6 # modify it under the same terms as Perl itself.
7  
8 ## Code:
9  
10 package Math::MatrixDecomposition::LU;
11  
12 use strict;
13 use warnings;
14 use Carp;
15 use Exporter qw(import);
16 use Scalar::Util qw(looks_like_number);
17  
18 use Math::BLAS;
19 use Math::MatrixDecomposition::Util qw(mod min);
20  
21 BEGIN
22 {
23 our $VERSION = '1.05';
24 our @EXPORT_OK = qw(lu);
25 }
26  
27 # Create a LU decomposition (convenience function).
28 sub lu
29 {
30 __PACKAGE__->new (@_);
31 }
32  
33 # Standard constructor.
34 sub new
35 {
36 my $class = shift;
37 my $self =
38 {
39 # LU matrix in row major layout.
40 LU => [],
41  
42 # Number of matrix rows and columns.
43 rows => 0,
44 columns => 0,
45  
46 # Pivot row indices (row permutations).
47 pivot => [],
48  
49 # Sign of determinant. A value of zero means that
50 # the matrix is singular.
51 sign => 0,
52 };
53  
54 bless ($self, ref ($class) || $class);
55  
56 # Process arguments.
57 $self->decompose (@_)
58 if @_ > 0;
59  
60 # Return object.
61 $self;
62 }
63  
64 # LU decomposition.
65 sub decompose
66 {
67 my $self = shift;
68  
69 # Check arguments.
70 my $a = shift;
71  
72 my $m = @_ > 0 && looks_like_number ($_[0]) ? shift : sqrt (@$a);
73 my $n = @_ > 0 && looks_like_number ($_[0]) ? shift : $m ? @$a / $m : 0;
74  
75 croak ('Invalid argument')
76 if (@$a != ($m * $n)
77 || mod ($m, 1) != 0 || $m < 1
78 || mod ($n, 1) != 0 || $n < 1);
79  
80 # Get properties.
81 my %prop = @_;
82  
83 $prop{capture} //= 0;
84  
85 # Index of last row/column.
86 my $last_row = $m - 1;
87 my $last_col = $n - 1;
88  
89 # Matrix elements.
90 if ($prop{capture})
91 {
92 # Work in-place.
93 $$self{LU} = $a;
94 }
95 else
96 {
97 # Copy matrix elements.
98 $$self{LU} //= [];
99 @{$$self{LU}} = @$a;
100 $a = $$self{LU};
101 }
102  
103 # Matrix size.
104 $$self{rows} = $m;
105 $$self{columns} = $n;
106  
107 # Pivot row indices (row permutations).
108 my $pivot = $$self{pivot};
109  
110 @$pivot = ();
111  
112 # Sign of determinant.
113 my $sign = 1;
114  
115 # Work variables.
116 my ($i, $j, $k, $p, $row, $tem);
117  
118 for $k (0 .. min ($last_row, $last_col))
119 {
120 # Search pivot element.
121 $p = $k + (blas_amax_val ($m - $k, $a,
122 x_ind => $k * $n + $k,
123 x_incr => $n))[0];
124  
125 push (@$pivot, $p);
126  
127 # Swap rows.
128 if ($p != $k)
129 {
130 blas_swap ($n, $a, $a,
131 x_ind => $k * $n,
132 y_ind => $p * $n);
133  
134 $sign = - $sign;
135 }
136  
137 # Divide by the pivot element.
138 if ($$a[$k * $n + $k] == 0)
139 {
140 $sign = 0;
141 next;
142 }
143  
144 for $i ($k + 1 .. $last_row)
145 {
146 $$a[$i * $n + $k] /= $$a[$k * $n + $k];
147  
148 blas_axpby ($last_col - $k, $a, $a,
149 alpha => - $$a[$i * $n + $k],
150 x_ind => $k * $n + $k + 1,
151 y_ind => $i * $n + $k + 1);
152 }
153 }
154  
155 # Save sign of determinant.
156 $$self{sign} = $sign;
157  
158 # Return object.
159 $self;
160 }
161  
162 # Solve a system of linear equations.
163 sub solve
164 {
165 my $self = shift;
166  
167 my $b = shift;
168 my $x = shift;
169  
170 # LU matrix elements.
171 my $a = $$self{LU};
172  
173 # Number of matrix rows and columns.
174 my $m = $$self{rows};
175 my $n = $$self{columns};
176  
177 croak ('Invalid argument')
178 if @$b == 0 || @$b % $m != 0;
179  
180 my $l = @$b / $m;
181  
182 # Copy right-hand side.
183 if (defined ($x))
184 {
185 @$x = @$b;
186 }
187 else
188 {
189 # Work in-place.
190 $x = $b;
191 }
192  
193 # Index of last row/column.
194 my $last_row = $m - 1;
195 my $last_col = $n - 1;
196 my $last_vec = $l - 1;
197  
198 # Pivot row indices.
199 my $pivot = $$self{pivot};
200  
201 # Work variables.
202 my ($i, $j, $k, $p, $row, $tem);
203  
204 if ($l == 1)
205 {
206 # Apply row permutations.
207 for $i (0 .. $#$pivot)
208 {
209 $p = $$pivot[$i];
210 next if $p == $i;
211  
212 @$x[$i, $p] = @$x[$p, $i];
213 }
214  
215 # Forward substitution, that is solve 'Ly = b' for 'y'.
216 # Elements of 'y' are saved in 'x'.
217 for $i (1 .. $last_row)
218 {
219 $row = $i * $n;
220  
221 for $j (0 .. $i - 1)
222 {
223 $$x[$i] -= $$x[$j] * $$a[$row + $j];
224 }
225 }
226  
227 # Backward substitution, that is solve 'Ux = y' for 'x'.
228 for $i (reverse (0 .. $last_row))
229 {
230 $row = $i * $n;
231  
232 if ($$a[$row + $i] == 0)
233 {
234 # Matrix A is singular.
235 return unless $$x[$i] == 0;
236 }
237 else
238 {
239 for $j ($i + 1 .. $last_col)
240 {
241 $$x[$i] -= $$x[$j] * $$a[$row + $j];
242 }
243  
244 $$x[$i] /= $$a[$row + $i];
245 }
246 }
247 }
248 else
249 {
250 # Matrix A is singular.
251 return if $$self{sign} == 0;
252  
253 # Apply row permutations.
254 for $i (0 .. $#$pivot)
255 {
256 $p = $$pivot[$i];
257 next if $p == $i;
258  
259 blas_swap ($l, $x, $x,
260 x_ind => $i * $l,
261 y_ind => $p * $l);
262 }
263  
264 # Forward substitution.
265 for $k (0 .. $last_col)
266 {
267 for $i ($k + 1 .. $last_col)
268 {
269 blas_axpby ($l, $x, $x,
270 alpha => - $$a[$i * $n + $k],
271 x_ind => $k * $l,
272 y_ind => $i * $l);
273 }
274 }
275  
276 # Backward substitution.
277 for $k (reverse (0 .. $last_col))
278 {
279 blas_rscale ($l, $x,
280 alpha => $$a[$k * $n + $k],
281 x_ind => $k * $l);
282  
283 for $i (0 .. $k - 1)
284 {
285 blas_axpby ($l, $x, $x,
286 alpha => - $$a[$i * $n + $k],
287 x_ind => $k * $l,
288 y_ind => $i * $l);
289 }
290 }
291 }
292  
293 # Resize result.
294 splice (@$x, $n * $l)
295 if $m > $n;
296  
297 # Fix rounding errors.
298 for (@$x)
299 {
300 $_ = 0 + "$_";
301 }
302  
303 # Return value.
304 $x;
305 }
306  
307 # Determinant.
308 sub det
309 {
310 my $self = shift;
311  
312 my $m = $$self{rows};
313 my $n = $$self{columns};
314  
315 croak ('Matrix has to be square')
316 if $m != $n;
317  
318 # Calculate determinant.
319 my $det = $$self{sign};
320  
321 if ($det)
322 {
323 my $u = $$self{LU};
324  
325 my $ind = 0;
326 my $incr = $n + 1;
327  
328 for (1 .. $n)
329 {
330 $det *= $$u[$ind];
331 $ind += $incr;
332 }
333 }
334  
335 $det;
336 }
337  
338 1;
339  
340 __END__