-
Notifications
You must be signed in to change notification settings - Fork 33
/
recursive_matrix_multiplication.pl
executable file
·212 lines (166 loc) · 4.13 KB
/
recursive_matrix_multiplication.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 04 April 2016
# Website: https://github.com/trizen
# Recursive matrix multiplication, using a divide and conquer algorithm.
# See also: https://en.wikipedia.org/wiki/Matrix_multiplication
# NOTE: works only with n*n matrices, where n must be a power of 2.
use 5.010;
use strict;
use warnings;
sub add {
my ($A, $B) = @_;
my $C = [[]];
foreach my $i (0 .. $#{$A}) {
foreach my $j (0 .. $#{$A->[$i]}) {
$C->[$i][$j] += $A->[$i][$j] + $B->[$i][$j];
}
}
$C;
}
sub msplit {
my ($A, $B, $C, $D) = @_;
my $end = $#{$A};
my $mid = int($end / 2);
my @A = @{$A}[0 .. $mid];
my @B = @{$B}[0 .. $mid];
my @C = @{$A}[$mid + 1 .. $end];
my @D = @{$B}[$mid + 1 .. $end];
my @E = @{$C}[0 .. $mid];
my @F = @{$D}[0 .. $mid];
my @G = @{$C}[$mid + 1 .. $end];
my @H = @{$D}[$mid + 1 .. $end];
#<<<
if ($end > 3) {
return
msplit(\@A, \@C, \@B, \@D),
msplit(\@E, \@G, \@F, \@H);
}
#>>>
#<<<
[
[@A, @B],
[@C, @D],
[@E, @F],
[@G, @H],
]
#>>>
}
#
## Known issue: broken
#
sub merge_rows {
my (@blocks) = @_;
if (@{$blocks[0]} > 4) {
my @merged;
while (@{$blocks[0]}) {
my @rows;
foreach my $block (@blocks) {
push @rows, [splice(@{$block}, 0, 4)];
}
push @merged, @{merge_rows(@rows)};
}
return \@merged;
}
my @A;
foreach my $i (0 .. 3) {
push @{$A[$i]}, @{$blocks[0][$i]}, @{$blocks[1][$i]};
push @{$A[$i + 4]}, @{$blocks[2][$i]}, @{$blocks[3][$i]};
}
return \@A;
}
#
## Known issue: broken
#
sub merge {
my (@blocks) = @_;
while (@blocks > 4) {
push @blocks, merge_rows(splice(@blocks, 0, 4));
}
return merge_rows(@blocks);
}
sub mul {
my ($A, $B) = @_;
## Base case:
#<<<
if ($#{$A} == 1 and $#{$A->[0]} == 1 and $#{$B} == 1 and $#{$B->[0]} == 1) {
return [
[
$A->[0][0] * $B->[0][0] + $A->[0][1] * $B->[1][0],
$A->[0][0] * $B->[0][1] + $A->[0][1] * $B->[1][1],
],
[
$A->[1][0] * $B->[0][0] + $A->[1][1] * $B->[1][0],
$A->[1][0] * $B->[0][1] + $A->[1][1] * $B->[1][1],
],
];
}
#>>>
my $end = $#{$A};
my $mid = int($end / 2);
my @A = map { [@{$_}[0 .. $mid]] } @{$A}[0 .. $mid];
my @B = map { [@{$_}[$mid + 1 .. $end]] } @{$A}[0 .. $mid];
my @C = map { [@{$_}[0 .. $mid]] } @{$A}[$mid + 1 .. $end];
my @D = map { [@{$_}[$mid + 1 .. $end]] } @{$A}[$mid + 1 .. $end];
my @E = map { [@{$_}[0 .. $mid]] } @{$B}[0 .. $mid];
my @F = map { [@{$_}[$mid + 1 .. $end]] } @{$B}[0 .. $mid];
my @G = map { [@{$_}[0 .. $mid]] } @{$B}[$mid + 1 .. $end];
my @H = map { [@{$_}[$mid + 1 .. $end]] } @{$B}[$mid + 1 .. $end];
#<<<
[
(
[map{@{$_}} @{add(mul(\@A, \@E), mul(\@B, \@G))}],
[map{@{$_}} @{add(mul(\@A, \@F), mul(\@B, \@H))}],
[map{@{$_}} @{add(mul(\@C, \@E), mul(\@D, \@G))}],
[map{@{$_}} @{add(mul(\@C, \@F), mul(\@D, \@H))}]
),
];
#>>>
}
sub mmult {
our @a;
local *a = shift;
our @b;
local *b = shift;
my @p = [];
my $rows = @a;
my $cols = @{$b[0]};
my $n = @b - 1;
for (my $r = 0 ; $r < $rows ; ++$r) {
for (my $c = 0 ; $c < $cols ; ++$c) {
foreach (0 .. $n) {
$p[$r][$c] += $a[$r][$_] * $b[$_][$c];
}
}
}
return [@p];
}
sub new_matrix {
my ($n) = @_;
[map { [$n * $_ - $n + 1 .. $_ * $n] } 1 .. $n];
}
sub display_matrix {
my ($A, $w) = @_;
say join(
"\n",
map {
join(' ', map { sprintf("%${w}d", $_) } @{$_})
} @{$A}
);
}
#
## Demo:
#
my $A = [[3, 4], [5, 6]];
use Data::Dump qw(pp);
pp mul($A, $A);
pp mmult($A, $A);
my $B = new_matrix(4);
pp mmult($B, $B);
pp mul($B, $B);
my $C = new_matrix(8);
my $D = mmult($C, $C);
display_matrix($D, 6);
my $x = mul($C, $C);
pp msplit(@{$x});