dimmesdale has asked for the wisdom of the Perl Monks concerning the following question:
(The math is hard to write, and therefore to read, but not fundamental to reading the rest of this)
EQ1: nb + SUM(xi,i=1 TO n)a = SUM(yi,i=1 TO n)
EQ2: SUM(xi,i=1 TO n)b + SUM(xi^2,i=1 TO n)a = SUM(xi*yi, i=1 TO n)
The (x,y) points I need (quite a few of them!) are read in line by line, so the problem comes up with storage; it requires far too much space to store these, so I decided to make a closure (with the hope of) summing as we go along, and then (when passed the right argument) calculating the data (actually, I was rather happy with the code I wrote to solve the sytem of equations; when I looked at it I didn't know what to do, and thought that it was the type of thing complex C code has been written for. . . but where there's perl there's a way, and eval popped in my head, but I'd be interested to hear other ideas).
I'm not familiar with closures (I haven't used them often), so I looked through this site and at perldoc's closure information (including perlref). Well, even after all that I'm still getting some problems. Now, I imagine its a simple (as in basic/fundamental) closure issue which I'm missing, but when I call the closure (&$closure($args)) and through a debugging statement print the arguments, I get . . . NOTHING.
If closures aren't the way to go, fine (but I'd be interested in knowing what to do). The code is below (just the closure; I just made up some simple data through a 1..10 and 20..30 statement for x y coordinates and tested it with that; the call mimicks what I have above).
I wrote this just now quickly in an attempt to see what I'll need to do with the final thing, so any suggestions are more than welcome.
# bf stands for best fit; eventually I'll need several that # will mimick this format closely. my $bf_1 = sub { # Arguments are: Add|Equate; [x value; y value]. my $sum_x; my $sum_y; my $sum_x2; my $sum_xy; my $n; $sum_x = 0 unless $sum_x; $sum_y = 0 unless $sum_y; $sum_x2 = 0 unless $sum_x2; $sum_xy = 0 unless $sum_xy; $n = 0 unless $n; if($ARGV[0] eq 'Add') { my @args = @_; $sum_x += $args[1]; $sum_y += $args[2]; $sum_x2 += $args[1]*$args[2]; $sum_xy += $args[1] ** 2; $n++; } elsif ($ARGV[0] eq 'Equate') { my $a; my $b; $a = "(($sum_y - $n*$b)/$sum_x)"; $b = "($sum_xy-$sum_x2*$a)/$sum_x"; eval $b; eval $a; ($a,$b); } else { print "Called with unkown argument\n" } };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Closures and Statistics
by broquaint (Abbot) on Jun 27, 2002 at 00:31 UTC | |
|
Re: Closures and Statistics
by bronto (Priest) on Jun 27, 2002 at 07:58 UTC | |
|
•Re: Closures and Statistics
by merlyn (Sage) on Jun 27, 2002 at 00:17 UTC | |
by dimmesdale (Friar) on Jun 27, 2002 at 00:28 UTC | |
|
Re: Closures and Statistics
by sfink (Deacon) on Jun 27, 2002 at 07:05 UTC | |
|
Re: Closures and Statistics
by rjray (Chaplain) on Jun 27, 2002 at 07:18 UTC | |
by meta4 (Monk) on Jun 27, 2002 at 23:36 UTC | |
|
Re: Closures and Statistics
by meta4 (Monk) on Jun 27, 2002 at 23:56 UTC |