Difference between revisions of "Talk:2435: Geothmetic Meandian"

Explain xkcd: It's 'cause you're dumb.
Jump to: navigation, search
(...neater.)
Line 28: Line 28:
 
  perl -e 'use strict; use warnings; sub F { my ($s,$p) = (0,1); my @srt = sort {$a<=>$b} @_; for (@_) { $s += $_; $p *= $_; } return ($s/@_,$p**(1/@_),$srt[$#_/2]); } sub Gmdn { print join(", ",@_=F(@_)),"\n" for 0..20; return @_; } print join(", ",Gmdn(1,1,2,3,5)),"\n";'
 
  perl -e 'use strict; use warnings; sub F { my ($s,$p) = (0,1); my @srt = sort {$a<=>$b} @_; for (@_) { $s += $_; $p *= $_; } return ($s/@_,$p**(1/@_),$srt[$#_/2]); } sub Gmdn { print join(", ",@_=F(@_)),"\n" for 0..20; return @_; } print join(", ",Gmdn(1,1,2,3,5)),"\n";'
 
(With interim results) SCNR -- [[User:Xorg|Xorg]] ([[User talk:Xorg|talk]]) 03:18, 11 March 2021 (UTC)
 
(With interim results) SCNR -- [[User:Xorg|Xorg]] ([[User talk:Xorg|talk]]) 03:18, 11 March 2021 (UTC)
 +
:''I'' can read your version (and I see you do explicit {$a<=>$b}, which indeed ''may'' be necessary in mine for real use, along with additional sanity checks, I will check later) but I wanted to make mine neat, and ''slightly'' tricksy in implementation, but still not quite so entirely obfuscated to the more uninitiated. TIMTOWTDI, etc, so I like your (almost) bare-bones version too. ;)
 +
:(Is 20 cycles enough to converge in sufficiently extreme cases? Won't give "Too deep" error, though, even  if it takes at least that long.) [[Special:Contributions/141.101.99.229|141.101.99.229]] 03:45, 11 March 2021 (UTC)

Revision as of 03:45, 11 March 2021

Oh, this one's good. Just checked in (no, I wasn't hovering over the refresh button, my first visit today!) and one glance had me in paroxysms of laughter. But how to explain it? Gonna have to think about that. 141.101.98.96 01:12, 11 March 2021 (UTC)

I made a really bad spreadsheet to understand better how it works: https://docs.google.com/spreadsheets/d/1fqmHwDmirJrsKPdf94PutFDw31DMAYxNeR7jef1jneE/edit?usp=sharing

Someone fix my awful transcript edits please. --Char Latte49 (talk) 02:31, 11 March 2021 (UTC)

Seeing the Python added to the Explanation, try this Perl (typed straight here, so not tested)...

## Your prefered variations of "#!/usr/bin/perl", "use strict;" and "use warnings;" here! ##
sub F { my (@vals)=@_; my $invVals=1/int(@vals);
 my ($geo,$arith,$med)=(1); # Only defining $geo, so first *= works correctly!
 while (@vals) { my($lo,$hi)=(shift @vals,pop @vals); # $hi may be undef - this is intended!
  $arith+=$lo; $geo*=$lo; unless (defined $hi) {  $med =  $lo;     last }
  $arith+=$hi; $geo*=$hi; unless (@vals)       { ($med)=F($lo,$hi)      }
 }
 return ($arith*$invVals, $geo**$invVals, $med);
}
sub GMDN { my (@vals)=sort @_; my $lim=10**(-5); # Adjust $lim to taste...
  return "Error: No vals!" unless  @vals; # Catch!
  return $vals[0]          unless ($vals[$#vals]-$vals[0]) > $lim;
  return GMDM(F(@vals));
}
my @test=(1,1,2,3,5);
print "Values:              @test\nGeothmetic Meandian: ".GMDN(@test)."\n";

...debugged in my head, so probably fatally flawed but easily fixed/adapted anyway. 141.101.99.109 03:04, 11 March 2021 (UTC)

Why so complicated?

perl -e 'use strict; use warnings; sub F { my ($s,$p) = (0,1); my @srt = sort {$a<=>$b} @_; for (@_) { $s += $_; $p *= $_; } return ($s/@_,$p**(1/@_),$srt[$#_/2]); } sub Gmdn { print join(", ",@_=F(@_)),"\n" for 0..20; return @_; } print join(", ",Gmdn(1,1,2,3,5)),"\n";'

(With interim results) SCNR -- Xorg (talk) 03:18, 11 March 2021 (UTC)

I can read your version (and I see you do explicit {$a<=>$b}, which indeed may be necessary in mine for real use, along with additional sanity checks, I will check later) but I wanted to make mine neat, and slightly tricksy in implementation, but still not quite so entirely obfuscated to the more uninitiated. TIMTOWTDI, etc, so I like your (almost) bare-bones version too. ;)
(Is 20 cycles enough to converge in sufficiently extreme cases? Won't give "Too deep" error, though, even if it takes at least that long.) 141.101.99.229 03:45, 11 March 2021 (UTC)