FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
perl-scripts/Math/matrix_determinant_bareiss.pl at master · trizen/perl-scripts · GitHub
trizen
perl-scripts
Repository navigation
Code
Issues
Pull requests
Security and quality
Insights
Expand file tree
Breadcrumbs
perl-scripts
/
Math
/
matrix_determinant_bareiss.pl
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
61 lines (45 loc) · 1.16 KB
Breadcrumbs
perl-scripts
/
Math
/
matrix_determinant_bareiss.pl
Copy path
File metadata and controls
executable file
·
61 lines (45 loc) · 1.16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
!/usr/bin/perl
#
Daniel "Trizen" Șuteu
#
License: GPLv3
#
Date: 20 November 2016
#
https://github.com/trizen
#
The Bareiss algorithm for computing the determinant of a (square) matrix.
#
Algorithm from:
#
https://apidock.com/ruby/v1_9_3_125/Matrix/determinant_bareiss
#
See also:
#
https://en.wikipedia.org/wiki/Bareiss_algorithm
use
5.010;
use
strict;
use
warnings;
use
List::Util
qw(
first
)
;
sub
det
{
my
(
$m
) =
@_
;
my
@m
=
map
{ [
@$_
] }
@$m
;
my
$sign
= +1;
my
$pivot
= 1;
my
$end
=
$#m
;
foreach
my
$k
(0 ..
$end
) {
my
@r
= (
$k
+ 1 ..
$end
);
my
$prev_pivot
=
$pivot
;
$pivot
=
$m
[
$k
][
$k
];
if
(
$pivot
== 0) {
my
$i
= (first {
$m
[
$_
][
$k
] }
@r
) //
return
0;
@m
[
$i
,
$k
] =
@m
[
$k
,
$i
];
$pivot
=
$m
[
$k
][
$k
];
$sign
= -
$sign
;
}
foreach
my
$i
(
@r
) {
foreach
my
$j
(
@r
) {
((
$m
[
$i
][
$j
] *=
$pivot
) -=
$m
[
$i
][
$k
] *
$m
[
$k
][
$j
]) /=
$prev_pivot
;
}
}
}
$sign
*
$pivot
;
}
my
$matrix
= [
[2, -1, 5, 1],
[3, 2, 2, -6],
[1, 3, 3, -1],
[5, -2, -3, 3],
];
say
det(
$matrix
);
#
=> 684
Back
|
FazBrowse Home
|
New Git URL