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^