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

Explain xkcd: It's 'cause you're dumb.
Jump to: navigation, search
(...actually, the only sort *needed* is in GMDN)
(Perl one-liner)
Line 24: Line 24:
 
  print "Values:              @test\nGeothmetic Meandian: ".GMDN(@test)."\n";
 
  print "Values:              @test\nGeothmetic Meandian: ".GMDN(@test)."\n";
 
...debugged in my head, so probably fatally flawed but easily fixed/adapted anyway. [[Special:Contributions/141.101.99.109|141.101.99.109]] 03:04, 11 March 2021 (UTC)
 
...debugged in my head, so probably fatally flawed but easily fixed/adapted anyway. [[Special:Contributions/141.101.99.109|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 -- [[User:Xorg|Xorg]] ([[User talk:Xorg|talk]]) 03:18, 11 March 2021 (UTC)

Revision as of 03:18, 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 $nVals=int(@vals);
 my ($arith,$geo,$med);
 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/$nVals, $geo**(1/$nVals), $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)