Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: recursive formula.

by periapt (Hermit)
on Aug 05, 2004 at 15:56 UTC ( [id://380303]=note: print w/replies, xml ) Need Help??


in reply to recursive formula.

This seems to work. Loading the data array for the next recursion seemed, to me, to be the tricky part. I did this by hand and the numbers match. Of course, even with higher math, I still can't add very well ;o)
use strict; use warnings; use diagnostics; #main() { my @data = ([0.11,0.07,0.19], [0.43,0.31,0.37], [0.95,0.78,0.82], [0.91,0.12,0.15], [0.52,0.18,0.32]); foreach (@data){ my $pval = P($_); print "value = $pval\n"; } exit; } #end main() sub P{ my $data = shift; my $n = @{$data}; my $rslt1 = 0; my $rslt2 = 0; my $rslt = 0; return $$data[0] if($n == 1); #r(0) = 0 ==> r(1)-r(0) = r(1) #assume P(r(0)) = 1 unshift @{$data}, 0; for(my $i=1;$i <= $n; $i++){ my @nextdata = (); for (1..$n){ next if($_ == ($n-$i+1)); push @nextdata, $$data[$_]; } # split up rslt1 & 2 for clarity $rslt1 = ($$data[$n-$i+1] - $$data[$n-$i]); $rslt2 = P(\@nextdata); $rslt += $rslt1 * $rslt2; } return $rslt; } __DATA__ value = 0.001595 value = 0.046225 value = 0.549005 value = 0.439894 value = 0.010192

PJ
unspoken but ever present -- use strict; use warnings; use diagnostics; (if needed)

Log In?
Username:
Password:

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

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

    No recent polls found