First question here... I'm interested in creating all the polynomials, their derivatives & sets of zeros of the derivatives, & keeping them all together. So a hash of hashes seems appropriate to me (if anyone has a better idea I'm all ears). I've managed to print out all of those things using nested for loops (to see if it would work) but putting them into a hash is what's giving me trouble. Using bits that I found on this site & Programming Perl I also found that the only key is 6 (just the number 6). A complicating factor is that I'm also only interested in cases where a, b & c are all different. So issues here that I can see:
1. definitely my hash definition
2. possibly the if loop
3. possibly using foreach loops
I've tried other things, like a while loop, putting the if on the "outside", etc but haven't had much luck with anything else, not the way I did it anyway.
Here's the relevant stuff that I've done so far:
use Math::Polynomial::Solve qw!poly_derivative poly_roots!;
my %haystack;
my $rep = int 5; # $rep means right endpoint of the interval [0, $rep]
foreach ( $a = 1; $a <= $rep; $a++ ) {
foreach ( $b = 1; $b <= $rep; $b++ ) {
foreach ( $c = 1; $c <= $rep; $c++ ) {
if ( $a == $b || $b == $c || $c == $a ){
next;
} else {
# expanded form of (x^2)*(x - a)*(x - b)*(x - c)
# coeffs are in an array
my @quintic = (1, -$a - $b - $c, $a*$b + $a*$c + $b*$c
+, -$a*$b*$c, 0, 0);
my @derivative = poly_derivative(@quintic);
my @zeros = poly_roots(@derivative);
$haystack{@quintic}{@derivative} = @zeros;
}
}
}
}
UPDATE: Like I said in the comment below, I've gone with an ordinry hash, so I've changed hdb's code a bit to simply say
$haystack{"$a, $b, $c"} = \@zeros;
Seems to work ok. Now I want to search the values, so I reversed the hash because I read that searching the keys is optimized in perl. But I still have a tricky problem because the new keys aren't strings, but arrays.
I've also been reading up on grep for how to pick out the (new) values that I'm interested in. I read that in perl it's supposed to be more efficient to search the keys rather than the values. so I reversed the hash but the problem with that is that instead of just strings, I have arrays of strings. Here's what I've come up with. It compiles ok but I'm not 100% sure it does what I want it to:
# the arrays of zeros are now the keys
%haystack = reverse %haystack;
# supposed to search the keys for zero sets that only contain numbers
+within 0.0001 of an integer
my @wants = grep { / (.*\.0000.*)|(.*\.9999.*) / } keys %haystack;
# supposed to print out the corresponding values from %haystack
print Dumper{ values of a, b, c corresponding to the elements of @want
+s };
Part of the problem is I don't think I know how to make sure that grep will look for the zero sets whose elements ALL have the form indicated, or that it will look for solution sets that just have at least one (which would be all of them in this case & not very interesting). Something else that caught my eye at the bottom of the Dumper manpage is a subroutine that filters keys out of a hash; I wonder if that could be adapted here because that module is really cool.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.