Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > dynamic execution of equations

Reply
Thread Tools

dynamic execution of equations

 
 
lapenta[
Guest
Posts: n/a
 
      07-29-2005

Hello,

I am trying to map out a simple PERL program to optimize coefficents for
a dynamic list of equations based on a set of rules. I'm having trouble
figuring out how to efficiently and dynamically load different
equations. Idealy separate files would each define the equation and
rules, while the main tool would load 1...n equations at a time and run
the optimization algorithm. As the equation will be executed many times
during the optimization, execution speed is a factor. I was thinking
dynamic modules would work nicely, but I can not figure out how to get
them to work. I would like to avoid doing something like 'exec "perl
$eqn"' as I imagine that would be much too slow for many equations.

for example...

file1.pl
----------------------------------------------
#!/usr/bin/perl -w

BEGIN{
$g_module = $ARGV[0];
}

print "optimizing for $g_module\n";
require "$g_module.pm";

$g_module::eqn( 5, 6 );
----------------------------------------------

simple_eqn.pm
----------------------------------------------
package simple_eqn;

require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(eqn $g_vars);

$g_vars = 2;

sub eqn {

my $a = shift;
my $b = shift;

return ($a / $b)**($a);

}
----------------------------------------------

$ perl file1.pl simple_eqn
syntax error at file1.pl line 14, near "$g_module::eqn( "
Execution of file1.pl aborted due to compilation errors.

Thanks in advance,
Jason
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-29-2005
> file1.pl
> ----------------------------------------------
> #!/usr/bin/perl -w
>
> BEGIN{
> $g_module = $ARGV[0];
> }
>
> print "optimizing for $g_module\n";
> require "$g_module.pm";
>
> $g_module::eqn( 5, 6 );


Here you are attempting to use the eqn subroutine from whatever package
is named by $g_module, by fully qualifying it. Your syntax is wrong
unfortunately. It would be something along the lines of:
no strict 'refs';
&{${$g_module.'::'}{eqn}}(5,6);

If you don't understand that (and I don't blame you if you don't), read
up on typeglobs. I think you can find them in
perldoc perldata


> simple_eqn.pm
> ----------------------------------------------
> package simple_eqn;
>
> require Exporter;
> @ISA = qw(Exporter);
> @EXPORT = qw(eqn $g_vars);


Here, however, you've wisely chosen to export the subroutine in
question. Which means you have no reason to attempt to fully qualify
it in the main package. Simply add a
import $g_module;
statement below the require, and then you can call eqn(5,6); normally.

Paul Lalli

 
Reply With Quote
 
 
 
 
attn.steven.kuo@gmail.com
Guest
Posts: n/a
 
      07-29-2005
Paul Lalli wrote:

....
> Here you are attempting to use the eqn subroutine from whatever package
> is named by $g_module, by fully qualifying it. Your syntax is wrong
> unfortunately. It would be something along the lines of:
> no strict 'refs';
> &{${$g_module.'::'}{eqn}}(5,6);
>
> If you don't understand that (and I don't blame you if you don't), read
> up on typeglobs. I think you can find them in
> perldoc perldata




I think that can be made a bit simpler:

no strict 'refs';
"${g_module}::eqn"->(5,6);

--
Regards,
Steven

 
Reply With Quote
 
lapenta[
Guest
Posts: n/a
 
      08-01-2005

Hi Paul,

Gee thanks for the help, worked like a charm.

> &{${$g_module.'::'}{eqn}}(5,6);


woah, that is quite a nutty statement. if anyone has a more readable
approach I would certainly be interested.

Thanks
Jason
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-01-2005
lapenta[ wrote:
> Hi Paul,
>
> Gee thanks for the help, worked like a charm.


Quite welcome. Glad I could help.

> > &{${$g_module.'::'}{eqn}}(5,6);

>
> woah, that is quite a nutty statement. if anyone has a more readable
> approach I would certainly be interested.


See the post elsewhere in this thread by Steven. It didn't occur to me
to take full advantage of the fact that I'd already turned off strict's
symref checking. As Steven said, the above can be more concisely
written:
"${g_module}::eqn"->(5,6);

Note two important things here: strict 'refs' must be turned off, and
the Quotes are required. What's happening is that we're generating a
string containing the characters "foo::eqn", and then using that string
as a symbolic reference. The arrow notation dereferences that symbolic
reference and calls the resultant function.

In my original, I was generating the string "foo::", and then using
that string as a symbolic reference to the hash %{'foo::'}. This is
the symbol table for the package foo. I then accessed one element of
that symbol table - keyed at the string 'eqn' - to obtain the *foo::eqn
typeglob. The & and () were then used to access the subroutine piece
of that typeglob and call the function.

All in all, I'd say Steve's solution is far better.

Paul Lalli

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      08-01-2005
"lapenta[" <"lapenta["@]ll.mit.edu> wrote:
> Hello,
>
> I am trying to map out a simple PERL program to optimize coefficents for
> a dynamic list of equations based on a set of rules.


What does dynamic mean in this context?

> I'm having trouble
> figuring out how to efficiently and dynamically load different
> equations.


Can these "equations" be some abstract mathematical constructs that don't
necessarily map directly into Perl expressions, or are they always
simply valid Perl expressions?


> Idealy separate files would each define the equation and
> rules, while the main tool would load 1...n equations at a time and run
> the optimization algorithm. As the equation will be executed many times
> during the optimization, execution speed is a factor. I was thinking
> dynamic modules would work nicely,


What is a dynamic module?

> but I can not figure out how to get
> them to work. I would like to avoid doing something like 'exec "perl
> $eqn"' as I imagine that would be much too slow for many equations.


exec is inherently run in perl, so you don't
exec "perl $eqn" ;
you just
exec "$eqn";

And for efficiency's sake, you can do:
my $subref = exec "sub {$eqn}";
and then efficiently invoke $subref->(@vars) to your hearts' content.

or
push @subref, exec "sub {...}";
and then invoke $subref[1]->(@vars);



>
> for example...
>
> file1.pl
> ----------------------------------------------
> #!/usr/bin/perl -w
>
> BEGIN{
> $g_module = $ARGV[0];
> }


This doesn't seem very dynamic.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
nobull@mail.com
Guest
Posts: n/a
 
      08-01-2005

wrote:

> "lapenta[" <"lapenta["@]ll.mit.edu> wrote:
>
> > I would like to avoid doing something like 'exec "perl
> > $eqn"' as I imagine that would be much too slow for many equations.

>
> exec is inherently run in perl, so you don't
> exec "perl $eqn" ;
> you just
> exec "$eqn";


You are confusing exec() with eval()

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      08-02-2005
wrote:
> wrote:
>
> > "lapenta[" <"lapenta["@]ll.mit.edu> wrote:
> >
> > > I would like to avoid doing something like 'exec "perl
> > > $eqn"' as I imagine that would be much too slow for many equations.

> >
> > exec is inherently run in perl, so you don't
> > exec "perl $eqn" ;
> > you just
> > exec "$eqn";

>
> You are confusing exec() with eval()


Crap. You are right. My bad.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
private data stashed in local/global execution context of PyEval_EvalCode disappears down the execution stack sndive@gmail.com Python 9 11-14-2007 10:31 PM
Code for linear congruences, diophantine linear equations alessandra_cabrini@virgilio.it Java 1 11-15-2005 12:23 PM
design boolean equations Hendrik Greving VHDL 2 06-17-2005 05:14 AM
VHDL and extracing equations buke2 VHDL 2 07-28-2004 02:14 PM
parsing equations munehiro Python 4 12-09-2003 03:58 PM



Advertisments