Deconvolution/2D+: Difference between revisions
→{{header|Perl 6}}: modernize with shaped arrays, more idiomatic code
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: Combine files to make runnable code) |
SqrtNegInf (talk | contribs) (→{{header|Perl 6}}: modernize with shaped arrays, more idiomatic code) |
||
Line 631:
Translation of Tcl.
<lang perl6># Deconvolution of N dimensional
sub
my @
my @
{ [row(@g, @f, $^coords, @hsize)] };
my @solved = rref( @toSolve );
my @h;
for flat coords(@hsize) Z @solved[*;*-1] -> $_, $v {
@h.AT-POS(|$_) = $v;
}
return @h;
}
# Construct a row for each value in @g to be sent to the simultaneous equation solver
sub row ( @g, @f, @gcoord, $hsize ) {
my @row;
@gcoord = @gcoord[(^@f.shape)]; # clip extraneous values
for coords( $hsize )
for
@fcoord.push($window) and next if 0 <= $window < @f.shape[$i];
last;
}
@row.push
}
@row.push
return @row;
}
# Constructs an
sub coords ( @dim ) {
@[reverse $_ for [X] ([^$_] for reverse @dim)];
}
# Reduced Row Echelon Form simultaneous equation solver
# Can handle over-specified systems
sub rref (@m) {
return unless @m;
my ($lead, $rows, $cols) = 0, +@m, +@m[0];
# Trim off over specified rows if they exist
if $rows >= $cols {
$rows = +
}
for ^$rows -> $r {
$lead < $cols or return
my $i = $r;
until
++$i == $rows or next;
$i = $r;
++$lead == $cols and return
}
my $lv =
for ^$rows -> $n {
next if $n == $r;
}
++$lead;
}
return
# Reduce a system of equations to
sub trim_system ($m) {
my ($vars, @t) = +$m[0]-1;
for ^$vars -> $lead {
for ^$m -> $row {
@t.push
}
}
while (+@t < $vars) and +$m { @t.push
return @t;
}
}
# Pretty printer for N dimensional arrays
# Assumes if first element in level is an array, then all are
sub pretty_print ( @array, $indent = 0 ) {
if @array[0] ~~ Array {
say ' ' x $indent,"[";
pretty_print( $_, $indent +
say ' ' x $indent, "]{$indent??','!!''}";
} else {
Line 781 ⟶ 724:
}
sub say_it ( @array ) { return join ",", @array».fmt("%4s"); }
}
my @f[3;2;3] = (
[
[ -9, 5, -8
[ 3, 5, 1 ],
],
[
[ -1, -7, 2
[ -5, -6, 6 ],
],
[
[ 8, 5, 8
[ -2, -6, -4 ],
]
);
my @g[4;4;6] = (
[
[ 54, 42, 53, -42, 85, -72 ],
Line 825 ⟶ 769:
);
say "# {+@f.shape}D array:";
say "h =";
pretty_print(
Output:
|