Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Debugging a sort in PDL routine

by Evanovich (Scribe)
on May 16, 2003 at 02:58 UTC ( [id://258575]=perlquestion: print w/replies, xml ) Need Help??

Evanovich has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I've been sitting here for an hour trying to figure out this debugging problem. So, in a last ditch effort I'm hoping someone can pick out what's going on here. Here is the relevant snippet (this is a PDL routine):
my ($matrix, $limits) = @_; # I get my data and limits from the argum +ents of subroutine my $j = 1; my ($pdl1, $pdl2, $n1, $n2); my ($angle, $dotproduct, $d, %angles); $pdl1 = pdl (@{$matrix}[0]); $n1 = norm $pdl1; while ($j<=$#{$matrix}) { $pdl2 = pdl (@{$matrix}[$j]); $n2 = norm $pdl2; $dotproduct = inner ($n1, $n2); $d = $dotproduct->sclr(); $angle = (180/3.14159)*acos($d); $angles{$j} = $angle; $j++; } my @sorted = sort {$angles{$a} <=> $angles{$b}} keys %angles;
And at this point I can't sort! It tells me that it returns an un-numeric value. But I SWEAR that my values are all numeric: if I push all $angle values into an array directly, I can sort them. Can anyone help me figure what's going on here?

20030516 Edit by Corion - Changed empty CODE blocks to BR tags

Title by tye

Replies are listed 'Best First'.
Re: Maybe I'm just dumb...
by pfaut (Priest) on May 16, 2003 at 03:15 UTC

    You are using strict, right?

    Add a 'use Data::Dumper' at the top and dump the hash with 'print Dumper(\%angles)' just before the sort. This should tell you what's in the hash.

    90% of every Perl application is already written.
    dragonchild
Re: Maybe I'm just dumb...
by Zaxo (Archbishop) on May 16, 2003 at 06:02 UTC

    The lines

    006 $pdl1 = pdl (@{$matrix}[0]); ... 009 $pdl2 = pdl (@{$matrix}[$j]);
    are likely to be incorrect. Check the syntax.

    If $matrix is a 2-d array reference, you probably want to dereference that as $matrix->[0]. PDL likes array references just fine for initializing piddles.

    After Compline,
    Zaxo

Re: Maybe I'm just dumb...
by BrowserUk (Patriarch) on May 16, 2003 at 03:20 UTC

    It all looks pretty sane to me with seeing the data. I would suggest the you include a print statement in the sort block and see what you are getting.

    my @sorted = sort { print "$a|$b|$angles{$a}|$angles{$b}\n"; $angles{$a} <=> $angles{$b} } keys %angles;
    That should show what it doesn't like, then all that's left to do it track down where it's coming from:)
    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      I just did what you said, and keys '33' and '32' print with their values. But their values are just random numbers in my hash. My question now is, can anyone see anything in my calculations that would have screwed up the values? Does PDL return values that can't be sorted?
Re: Maybe I'm just dumb...
by edoc (Chaplain) on May 16, 2003 at 03:19 UTC

    You may need to re-evaluate where your error is coming from.. this runs fine on perl 5.8

    my %hash = ( key1 => 1, key2 => 2, key3 => 3, empty => undef, string => 'blah', string2 => '4', ); my @sorted = sort {$hash{$a} <=> $hash{$b}} keys %hash; print "\nSorted:\n"; foreach(@sorted){ print "$_\n"; } __END__ prints: Sorted: string empty key1 key2 key3 string2

    cheers,

    J

Re: Maybe I'm just dumb...
by Evanovich (Scribe) on May 16, 2003 at 04:30 UTC
    Folks. I figured out the problem, but I can't figure out why it would give me an error. If I don't take the acos of $d, and just multiply (180/3.14159)*$d, then I can sort normally. Can anyone explain to me why the acos() function would be screwing up my sort?
      Try this:
      use Math::Cephes qw/acos/; for ( -2, -1.1, -1, 0, 1, 1.1, 2 ) { print acos($_),$/; }
      I expect you are getting something similar (i.e. #QNAN), as I am guessing some of the values $d are outside the domain of the "Real" solutions of acos.

      -enlil

      Can't acos return undef (for invalid input)?

      --
      I'm not belgian but I play one on TV.

      Just a thought, but is $dotproduct a PDL? If so, using acos should screw things up. But it's been a while since I used PDL.
Re: Debugging a sort in PDL routine
by etj (Deacon) on Jun 26, 2022 at 02:47 UTC
    Now (in 2022) this probably works fine. An ndarray in Perl is a blessed scalar ref, so naively using its value risks its memory address and/or the string value being used. In the latter case, that would (if it has "[]" around it) be something Perl thinks is a non-numeric value.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://258575]
Approved by halley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-19 03:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found