Re: function for finding the index of an element in an array?

2002-07-25 Thread Kay Bieri


> Right! I have the same idea as you, and I have this :
>
> my @list = ('1234', '4567', '789A', 'BCDE', 'FGHI');
> my $GetLocation = 0;
> my $value = 'BCDE';
>
> for (my $atLoc = 0; $atLoc <= $#list and ! $GetLocation ; $atLoc++)
> { $GetLocation = $atLoc if ($value eq  $list[$atLoc])  }
>
> And I finally got the same ans. =)
> Deal with it by a hash is the only choice
>

Your script does have some problems. For instance, if the string you're
looking for is in the first (index = 0) position and also in a later
position, the for loop will only find the latter. Also if $GetLocation
stays 0, you don't know whether the string is not present in the array at
all or whether it is just in the beginning position.


Kay




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




regex substituting math-functions

2002-07-28 Thread Kay Bieri

Hello all

I sometimes need to convert mathematical expressions from maple to
mathematica in order to compare my results with somebody else's. There are
simple functions like 'cos(arg)' which must be convertet to 'Cos[arg]' and
a little more involved ones like 'dilog(arg)' which goes to
'PolyLog[2,1-arg]'. For the easier ones I put up a hash with the key
being the maple-function and the values being the mathematica-function.
The input files are set up as follows: They start with a possible
non-maple header (e.g. if the results were emailed to me) followed by
definitions in the format
$var_name := whatever_that_variable_in_maple_holds;
Those definitions can span over several lines separated by newlines, but
each definition end with a ";\n". The goal now is to bring those
definitions into mathematica-format and save them in separate files,
each named after its own $var_name.
Here's what I've come up with (please bear with me, I'm pretty much a
Perl beginner):




#!/usr/local/bin/perl -w
use strict;

my %vars;
my ($key, $func,$head) = (0,0,0);
my %funcs = ( cos => 'Cos', sin => 'Sin', tan => 'Tan', ln => 'Log');
while (<>) {
next unless ($head == 1 || /:=/);
if (/:=/) {
$head = 1;
s/(.*?)\s:=\s//;
$key = $1;
};
$vars{$key} .= $_;
}
my $names = join(', ', keys %vars);
print "There are " . scalar (keys %vars) . " variables stored\n";
print "Their names are $names.\n";
foreach (keys %vars) {
if (-e $_) {
next unless ( &overwrite($_));
};
open (FH, ">$_") or die "Can't open $_ for output: $!\n";
my $subs = $vars{$_};
$subs =~ s/\\//g;   # needed for compatibility reasons
$subs =~ s/\n//g;
$subs =~ s/;//;
foreach $func (keys %funcs) {
$subs =~ s/$func\(([^()]*?)\)/$funcs{$func}~[$1]/g;
$subs =~ 
s/$func\(([^\)]*?\((?:.*?\)[^()]*?\()*?[^()]*?\))\)/$funcs{$func}~[$1]/g;
if ($subs =~ /$func/) {
print "WARNING: File $_ stills contains $func-functions.!\n";
};
};
$subs =~ s/dilog\(([^()]*?)\)/Polylog[2,1-($1)]/g;
$subs =~ s/dilog\(([^\)]*?\((?:.*?\)[^()]*?\()*?[^()]*?\))\)/PolyLog[2,1-($1)]/g;
if ($subs =~ /dilog/) {
print "WARNING: File $_ stills contains dilogs!\n";
};
$subs =~ s/~//g;   # get rid of the unwanted '~'-insertions
print FH $subs;
close FH;
}

sub overwrite {
my $file = shift;
print "The file $file exists already. Do you want to overwrite it (y/n)? ";
chomp (my $answer = <>);
if ( $answer =~ /^y|Y$/) {
return 1;
} else {
print "File will not be overwritten...\n";
return 0;
};
}



This solutions works for what I've been using it so far, but it is quite
limited and not well written:

1.) First of all, I'm unhappy with the '~' I had to plug in so
$funcs{$func}~[$1] would not be considered as a ref to an
array. Is there a better way to stop Perl from misinterpreting this?
I need the []-braces since arguments to functions in mathematica are
always enclosed in such braces.

2.) My regexes only work for expressions like ln(-(1-x)*(1-y)) but not
for nested expressions like ln(-1+3*((1-x*(1+x)+y))). Is there a way to
treat arbitrarily nested expressions with regexes? Or can one do it more
easily with 'index' and alike?

I'd be happy if somebody could give me some comment on this. I attached a
sample maple input file on this email.
I checked cpan for modules that convert maple input to mathematica output
but didn't find anything.


Best wishes
Kay



Kay Bieri
Sidlerstr. 5, 3012 Bern
Switzerland
email: [EMAIL PROTECTED]  phone: +41 (031) 631 8682





fincomb_nolog := 1/3072*cf*aqcd*aqed^2*c7^2*lamt*gf^2/Pi^6*mb^5*(3*cos(th)^2-1
)*(60*w^2*ln(w)-30*w^4+72*ln(w)+58+5*w^6-33*w^2+24*w^4*ln(w))/(2+w^2);
fincomb_log := -1/3072*cf*aqcd*aqed^2*c7^2*lamt*gf^2/Pi^6*mb^5*(3*cos(th)^2-1)
*(-21*w-3*w^5+90*w^2*dilog(w+1)-12*w^5*ln(w)+36*ln(w+1)*ln(w)+24*w^3+36*dilog(
w+1)-36*dilog(1-w)+96*dilog(-(w+1)*(w-1))*w+48*w^3*dilog(-(w+1)*(w-1))-6*w^6*
ln(w+1)*ln(w)+90*w^2*ln(w+1)*ln(w)-24*w^4*dilog(1-w)-96*w*dilog(1-w)-90*w^2*
dilog(1-w)-36*ln(1-w)*ln(w)+48*w^3*ln(w+1)*ln(w)-24*w^3*ln(w)-90*w^2*ln(1-w)*
ln(w)-96*w*dilog(w+1)-48*w^3*dilog(1-w)-48*w^3*dilog(w+1)+16*Pi^2*w+24*w^4*
dilog(w+1)+8*w^3*Pi^2-6*w^6*dilog(w+1)+48*w^3*ln(1-w)*ln(w)+96*w*ln(1-w)*ln(w)
+6*w^6*dilog(1-w)+96*w*ln(w+1)*ln(w)+24*w^4*ln(w+1)*ln(w)+6*w^6*ln(1-w)*ln(w)-\
24*w^4*ln(1-w)*ln(w))/(2+w^2)/w;
fincomb := -1/3072*cf*aqcd*aqed^2*c7^2*lamt*gf^2/Pi^6*mb^5*(3*cos(th)^2-1)*(-\
21*w-3*w^5+90*w^2*dilog(w+1)-12*w^

Re: regex substituting math-functions

2002-07-29 Thread Kay Bieri

On Mon, 29 Jul 2002, Janek Schleicher wrote:

>
> > 1.) First of all, I'm unhappy with the '~' I had to plug in so $funcs{$func}~[$1] 
>would not be
> > considered as a ref to an array. Is there a better way to stop Perl from 
>misinterpreting this? I
> > need the []-braces since arguments to functions in mathematica are always enclosed 
>in such
> > braces.
>
> You could try something like
>
> s/foo/$funcs{$func} . "[$1]"/e;
>
>
> Best Wishes,
> Janek
>

Thanks, that works. I tried concatenating and using /e as well before but
I forgot to add the doublequotes around the [$1].

Kay


Kay Bieri
Sidlerstr. 5, 3012 Bern
Switzerland
email: [EMAIL PROTECTED]  phone: +41 (031) 631 8682



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Editor

2002-07-31 Thread Kay Bieri

On Wed, 31 Jul 2002, Connie Chan wrote:

>
> I am new to Perl just over a month now. I have tried other  programming languages 
>and they just seem to hard to
> understand. I have found Perl to be a lot easier to understand, and that brings me 
>to my question. What is a good Perl
> Editor for writing scripts? I am currently using Crimson Editor.
>
> Thanks
>


Is anybody using GNU Emacs to write perl programs? Is its perl-mode good
enough to compete with nedit and alike? I've been using Emacs for almost
anything (including C++, Latex, Perl ...) and hardly ever tried another
editor.

Greetings
Kay

--------
Kay Bieri
Sidlerstr. 5, 3012 Bern
Switzerland
email: [EMAIL PROTECTED]  phone: +41 (031) 631 8682



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]