Deconvolution/1D: Difference between revisions
m
→{{header|Raku}}: better sigil, consistent matrix notation, bells, whistles
No edit summary |
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: better sigil, consistent matrix notation, bells, whistles) |
||
Line 1,264:
=={{header|Raku}}==
(formerly Perl 6)
Translation of Python, using a modified version of the
<lang perl6>sub deconvolve (@g, @f) {
my
my @m;
@m[^@g;^
@m[^@g;
for ^
}
sub convolve (@f, @h) {
my @g = 0 xx + @f + @h - 1;
@g[^@f X+ ^@h]
}
# Reduced Row Echelon Form simultaneous equation solver
# Can handle over-specified systems of equations
@m = trim-system @m;
▲sub rref ($m is copy) {
return unless $m;▼
▲ my ($lead, $rows, $cols) = 0, +$m, +$m[0];
}▼
for ^$rows -> $r {
return @m unless $lead < $cols
my $i = $r;
until
next unless ++$i == $rows
$i = $r;
return @m if ++$lead == $cols
}
for ^$rows -> $n {
next if $n == $r;
}
++$lead;
}
}
# Reduce to N equations in N unknowns; a no-op unless rows > cols
sub trim-system (@m) {
▲ my ($vars, @t) = +$m[0]-1, ();
▲ for ^$vars -> $lead {
▲ for ^$m -> $row {
▲ @t.push: | $m.splice( $row, 1 ) and last if $m[$row][$lead];
}
while (+@t < $vars) and +$m { @t.push: $m.splice( 0, 1 ) };▼
}
}
my @h = (-8,-9,-3,-1,-6,7);
my @f = (-3,-6,-1,8,-6,3,-1,-9,-9,3,-2,5,2,-2,-7,-1);
my @g = (24,75,71,-34,3,22,-45,23,245,25,52,25,-67,-96,96,31,55,36,29,-43,-7);
.say for ~@g, ~convolve(@f, @h),'';▼
▲.say for ~@g, ~convolve(@f, @h),'';
.say for ~@h, ~deconvolve(@g, @f),'';
.say for ~@f, ~deconvolve(@g, @h),'';</lang>
{{out}}
<pre>24 75 71 -34 3 22 -45 23 245 25 52 25 -67 -96 96 31 55 36 29 -43 -7▼
▲24 75 71 -34 3 22 -45 23 245 25 52 25 -67 -96 96 31 55 36 29 -43 -7
24 75 71 -34 3 22 -45 23 245 25 52 25 -67 -96 96 31 55 36 29 -43 -7
Line 1,355 ⟶ 1,335:
-3 -6 -1 8 -6 3 -1 -9 -9 3 -2 5 2 -2 -7 -1
-3 -6 -1 8 -6 3 -1 -9 -9 3 -2 5 2 -2 -7 -1</pre>
=={{header|REXX}}==
|