From dcmertens.perl at gmail.com Wed Aug 12 11:25:27 2020 From: dcmertens.perl at gmail.com (David Mertens) Date: Wed, 12 Aug 2020 14:25:27 -0400 Subject: [Chicago-talk] Putting together a few short talks for August? Message-ID: Hello everyone, Ray Mannarelli has expressed interest in giving a short talk at our next meeting. If there are others who would like to give a short talk, we might be able to put two or three of those together for our next meeting. And by the way, we haven't set a date for the next meeting because I haven't lined up speakers. So if we can find another speaker to join Ray, then we can find a time that'll work and put the gears in motion. Any takers? David -- "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." -- Brian Kernighan -------------- next part -------------- An HTML attachment was scrubbed... URL: From amead at alanmead.org Wed Aug 19 14:36:12 2020 From: amead at alanmead.org (Alan Mead) Date: Wed, 19 Aug 2020 16:36:12 -0500 Subject: [Chicago-talk] Use of uninitialized value in printf Message-ID: <76f1706f-6cd1-49e8-33f0-84a4acbb0531@alanmead.org> I'm trying to use Statistics::Basic to calculate basic statistics like correlations. These functions return objects which are supposed to magically become numbers as needed,? https://metacpan.org/pod/distribution/Statistics-Basic/lib/Statistics/Basic.pod: > These actually return objects, not numbers. The objects will > interpolate as nicely formated numbers (using Number::Format). Or the > actual number will be returned when the object is used as a number. But I have a situation where the magic fails. When one (or both) vector has no variance, the correlation should be zero but the value of the object is neither defined, nor undefined, it's uninitialized. The code is below, here's the output that's driving me crazy. The 0.00 is correct, but the warnings are annoying: $ ./corr.pl 1,0.426 Use of uninitialized value in printf at ./corr.pl line 20. 2,0.000 Use of uninitialized value in addition (+) at ./corr.pl line 25. 3,0.000 Surely there's a way to force an object to be reduced into a scalar float (as opposed to just hiding the warnings)? I've tried the following: * using scalar() (does nothing); * adding 0 and 0.0 (just moves where the same error is reported); * dereferencing the object and trying to force a scalar with scalar references: "${ $corr }" and "${ $correlation( \@v1, \@v2 ) }" (these are all: Not a SCALAR reference); * checking if $corr is defined (it is, it's a defined object); and * adding ' || 0' to the end of the definition (does nothing). I can solve the issue with this: $corr = ( defined correlation( \@v1, \@v2 ) ? correlation( \@v1, \@v2 ) : 0 ); but then I'm forced to compute the correlation twice, which seems like a pretty poor solution. You might argue that corr = cov / ( sd1 sd2 ) and therefore that corr is undefined when sd1 or sd2 are zero, but in this case cov = 0 (by definition) and it's really unhelpful to me for this function to return an non-value, particularly because this issue would only arise occasionally leading to potentially difficult bugs. What I need is a way to force the object to collapse into a scalar value and assign that value to a variable. Any thoughts? -Alan #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Statistics::Basic qw(:all); my @v1 = ( 1, 2, 2, 3 ); my @v2 = ( 1, 0, 2, 2 ); # good data; everything's ok my $corr = correlation( \@v1, \@v2 ); printf "%d,%.3f\n", 1, $corr; # correlation is zero because v2 has no variance # Use of uninitialized value in printf at ./corr.pl line 20. @v2 = ( 1, 1, 1, 1 ); $corr = correlation( \@v1, \@v2 ); printf "%d,%.3f\n", 2, $corr; #print Dumper( $corr ), "\n"; # v2 still has no variance # Use of uninitialized value in addition (+) at ./corr.pl line 25. $corr = 0 + scalar( correlation( \@v1, \@v2 ) ); printf "%d,%.3f\n", 3, $corr; #print Dumper( $corr ), "\n"; -- Alan D. Mead, Ph.D. President, Talent Algorithms Inc. science + technology = better workers http://www.alanmead.org Acton's dictate: Power tends to corrupt, and absolute power corrupts absolutely. Great men are almost always bad men, even when they exercise influence and not authority. -------------- next part -------------- An HTML attachment was scrubbed... URL: From shlomif at shlomifish.org Thu Aug 20 00:13:09 2020 From: shlomif at shlomifish.org (Shlomi Fish) Date: Thu, 20 Aug 2020 10:13:09 +0300 Subject: [Chicago-talk] Use of uninitialized value in printf In-Reply-To: <76f1706f-6cd1-49e8-33f0-84a4acbb0531@alanmead.org> References: <76f1706f-6cd1-49e8-33f0-84a4acbb0531@alanmead.org> Message-ID: <20200820101309.1dcea430@telaviv1.shlomifish.org> Hi Alan, On Wed, 19 Aug 2020 16:36:12 -0500 Alan Mead wrote: > I'm trying to use Statistics::Basic to calculate basic statistics like > correlations. These functions return objects which are supposed to > magically become numbers as needed,? > https://metacpan.org/pod/distribution/Statistics-Basic/lib/Statistics/Basic.pod: > > These actually return objects, not numbers. The objects will > > interpolate as nicely formated numbers (using Number::Format). Or the > > actual number will be returned when the object is used as a number. > > But I have a situation where the magic fails. When one (or both) vector > has no variance, the correlation should be zero but the value of the > object is neither defined, nor undefined, it's uninitialized. > > The code is below, here's the output that's driving me crazy. The 0.00 > is correct, but the warnings are annoying: > > $ ./corr.pl > 1,0.426 > Use of uninitialized value in printf at ./corr.pl line 20. > 2,0.000 > Use of uninitialized value in addition (+) at ./corr.pl line 25. > 3,0.000 > > Surely there's a way to force an object to be reduced into a scalar > float (as opposed to just hiding the warnings)? > > I've tried the following: > > * using scalar() (does nothing); > * adding 0 and 0.0 (just moves where the same error is reported); > * dereferencing the object and trying to force a scalar with scalar > references: "${ $corr }" and "${ $correlation( \@v1, \@v2 ) }" > (these are all: Not a SCALAR reference); > * checking if $corr is defined (it is, it's a defined object); and > * adding ' || 0' to the end of the definition (does nothing). > > I can solve the issue with this: > > $corr = ( defined correlation( \@v1, \@v2 ) ? correlation( \@v1, \@v2 ) > : 0 ); > If your perl is recent enough, you can use https://perldoc.pl/perlop#Logical-Defined-Or : $corr = correlation(\@v1, \@v2) // 0; # untested one can also write: sub _my_dor { my $val = shift; return defined($val) ? $val : shift(@_)->(); } $corr = _my_dor(correlation(\@v1, \@v2), sub { return 0;}); which is kinda ugly. Also note that there are bindings to GNU R: https://metacpan.org/search?q=statistics%3A%3Ar as well as https://metacpan.org/pod/Statistics::Descriptive (which I comaintain) which has an OOP API. -- Shlomi Fish https://www.shlomifish.org/ Chuck Norris/etc. Facts - https://www.shlomifish.org/humour/bits/facts/ For every A, Chuck Norris is both A and not-A. Chuck Norris is freaking everything. ? https://www.shlomifish.org/humour/bits/facts/Chuck-Norris/ Please reply to list if it's a mailing list post - https://shlom.in/reply . From amead at alanmead.org Thu Aug 20 07:47:39 2020 From: amead at alanmead.org (Alan Mead) Date: Thu, 20 Aug 2020 09:47:39 -0500 Subject: [Chicago-talk] Use of uninitialized value in printf In-Reply-To: <20200820101309.1dcea430@telaviv1.shlomifish.org> References: <76f1706f-6cd1-49e8-33f0-84a4acbb0531@alanmead.org> <20200820101309.1dcea430@telaviv1.shlomifish.org> Message-ID: On 8/20/2020 2:13 AM, Shlomi Fish wrote: > Also note that there are bindings to GNU R: I'm temporarily confined to v5.10.1 and it doesn't seem to have defined-or, but I'll try your other suggestions, and bindings to R is a very good idea that I'd not considered. Thanks! -Alan -- Alan D. Mead, Ph.D. President, Talent Algorithms Inc. science + technology = better workers http://www.alanmead.org Acton's dictate: Power tends to corrupt, and absolute power corrupts absolutely. Great men are almost always bad men, even when they exercise influence and not authority. -------------- next part -------------- An HTML attachment was scrubbed... URL: From shlomif at shlomifish.org Thu Aug 20 23:05:24 2020 From: shlomif at shlomifish.org (Shlomi Fish) Date: Fri, 21 Aug 2020 09:05:24 +0300 Subject: [Chicago-talk] Use of uninitialized value in printf In-Reply-To: References: <76f1706f-6cd1-49e8-33f0-84a4acbb0531@alanmead.org> <20200820101309.1dcea430@telaviv1.shlomifish.org> Message-ID: <20200821090524.306d7756@telaviv1.shlomifish.org> Hi Alan, On Thu, 20 Aug 2020 09:47:39 -0500 Alan Mead wrote: > On 8/20/2020 2:13 AM, Shlomi Fish wrote: > > Also note that there are bindings to GNU R: > > I'm temporarily confined to v5.10.1 and it doesn't seem to have > defined-or, but I'll try your other suggestions, and bindings to R is a > very good idea that I'd not considered. Thanks! > I think defined-or was introduced in v5.10.0: perlbot: eval5.10: sub retun { return undef(); } sub retfa { return 0;} +{ 'undef' => (retun() // "was undef"), 'zero' => (retfa() // "was undef"), } rindolf: {undef => "was undef",zero => 0} perlbot: eval5.8: sub retun { return undef(); } sub retfa { return 0;} +{ 'undef' => (retun() // "was undef"), 'zero' => (retfa() // "was undef"), } rindolf: ERROR: Unmatched ) in regex; marked by <-- HERE in m/ "was undef") <-- HERE , 'zero' => (retfa() / at (IRC) line 1. Also see: * https://perlmaven.com/what-is-new-in-perl-5.10--say-defined-or-state * <<< [shlomif at telaviv1 ~]$ perl -v This is perl, v5.10.1 (*) built for x86_64-linux (with 1 registered patch, see perl -V for more detail) Copyright 1987-2009, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl". If you have access to the Internet, point your browser at http://www.perl.org/, the Perl Home Page. [shlomif at telaviv1 ~]$ perl -le 'print +(undef() // "was undef")' was undef [shlomif at telaviv1 ~]$ >>> Good luck with the other suggestions and this one. > -Alan > -- Shlomi Fish https://www.shlomifish.org/ Selina Mandrake - The Slayer (Buffy parody) - https://shlom.in/selina Academic Politics are so vicious precisely because the stakes are so low. ? https://quoteinvestigator.com/2013/08/18/acad-politics/ Please reply to list if it's a mailing list post - https://shlom.in/reply .