This was sent before (from the wrong account) and washeld up for
approval of the moderator. Sorry for double posting.
I've got a problem that uses tex(x) to generate expressions for a paper
I'm working on. The expressions are most conveniently represented as a
sum of rational expressions times exponentials of simple polynomials,
and I've written a parser to extract the various terms and feed the raw
result into tex(). After manually modifying the tex output, as a
check, I manually reconverted the expression into maxima, parsed it and
compared with the original. This fails. I've traced it down to the
forms of the exponentials
Basically, I'm looking for coeff(expression, exp(bb[i]) (I tried
ratcoef, but this failed on the original expression as well). It seems
that even though I apply expand in various ways to the expression I'm
trying to parse, this doesn't seem always work.
Here's a sample code that illustrates the problem. Look for the output
of bb[6] and the remainder in the following line ar = ...
It's clear that the first term in ar should have matched but didn't
because expand wasn't applied to the exponent, even after my applying
myexp which should do this. Is this a bug?
------------------------------------------
VERBOSE:true;
assume(abs(xi-k[0])-xi-k[0]<0);
assume(notequal(xi^2*D-k[0]^2*D-Gam,0));
assume(notequal(xi^2*D+k[0]^2*D-Gam,0));
assume(notequal(xi^2*D-k[0]^2*D+Gam,0));
assume(notequal((-xi^2*D-k[0]^2*D-Gam)^(-1)*(2*xi^2*D+2*k[0]^2*D)+1,
0));
assume(Gam>0, S>0, S[eq]>0, D>0, k[0]>0);
ratvars(K, S, Gam, D, k[0], xi, t[3], t[0]);
matchdeclare(h, true);
defrule(myexp, exp(h), exp(expand(h)));
defrule(myexp2, exp(h), exp(factorsum(h)));
/* These are the arguments of exp(bb[i]) that will be used to parse an
expression */
bb:[-k[0]^2*t[3]*D-k[0]^2*t[0]*D,
k[0]^2*t[0]*D-k[0]^2*t[3]*D-2*t[0]*Gam,
-k[0]^2*t[3]*D-k[0]^2*t[0]*D-2*t[0]*Gam,
k[0]^2*t[3]*D-k[0]^2*t[0]*D-2*t[0]*Gam,
k[0]^2*t[3]*D-k[0]^2*t[0]*D-2*t[3]*Gam,
-t[0]*xi^2*D-k[0]^2*t[3]*D-t[0]*Gam,
t[3]*xi^2*D-t[0]*xi^2*D-t[3]*Gam-t[0]*Gam,
-t[3]*xi^2*D-k[0]^2*t[0]*D-t[3]*Gam,
-t[3]*xi^2*D-k[0]^2*t[0]*D-2*t[3]*Gam,
-k[0]^2*t[3]*D-k[0]^2*t[0]*D-2*t[3]*Gam,
-2*t[3]*xi^2*D+k[0]^2*t[3]*D-k[0]^2*t[0]*D-2*t[3]*Gam,
t[3]*(xi^2*D-2*k[0]^2*D+Gam)+t[0]*(-xi^2 *D-Gam),
t[3]*(-xi^2*D-Gam)+t[0]*(-xi^2*D-Gam),
(-D*(xi^2+k[0]^2)-Gam)*t[0],
-2*D*k[0]^2*t[0],
-2*Gam*t[0]
];
/*
I've tried expand, factorsum and ratsimp to try to get the exponentials
into some consistent form.
*/
for i:1 thru length(bb) do
bb[i]:expand(bb[i]),
done;
/*
This is my parser, sans some extra I/O parts
*/
Parse(x) := block(
[a, an, ad, aaa, ar],
a:factor(x),
expand(num(a)),
an:apply1(%%, myexp),
factorsum(denom(a)),
ad:apply1(%%, myexp),
if VERBOSE then
display(a, an, ad),
aaa:0,
ar:an,
for i:1 thru length(bb) do
(
apply1(ar, myexp),
aa[i]:expand(coeff(ar, exp(bb[i]))),
ratsimp(ar - aa[i]*exp(bb[i])),
scanmap(expand,%%, bottomup), /* This should't be needed if
myexp works as expected in
the next line.*/
ar:apply1(%%, myexp),
aa[i]:scanmap(factorsum, aa[i]/ad)*exp(bb[i]),
if VERBOSE then
display(aa[i], bb[i], ar),
aaa : aaa + aa[i]
),
expand(ar),
ar:apply1(%%, myexp),
if ar = 0 then
print( "All terms parsed." )
else
( scanmap(factorsum, ar/ad, bottomup),
print( "All terms did not parse:", %%) ),
print( "Checking if original = reconstituted " ),
apply1(aaa-x, myexp),
radcan(%%),
fullratsimp(%%),
a:apply1(%%, myexp),
if a = 0 then
( aaa:apply1(aaa, myexp2), print("They are the same." ) )
else
print("They differ. Difference: ", a ),
if VERBOSE then
aaa
)$
/* The following expression was generated by some other maxima code,
converted to tex, manually adjusted, and then recoverted back to
maxima resulting in the expression given below. My intention was to
compare the original expression to my massaged one. */
Expression:-((K[0](k[0],xi)*xi*((S(k[0])*xi^2
+k[0]^2*S(k[0])-2*k[0]^2*S[eq](k[0]))*D-S(k[0])*Gam)
*exp(t[0](-D*xi^2-Gam)-D*k[0]^2*t[3]))
/((D*(xi^2-k[0]^2)-Gam)*(D*(xi^2-k[0]^2)+Gam)*(D*(xi^2+k[0]^2)-Gam)))
-((S[eq](k[0])*K[0](k[0],xi)*xi
*exp(t[0](D*k[0]^2-2*Gam)-D*k[0]^2*t[3]))
/(2*(D*k[0]^2-Gam)*(D*(xi^2+k[0]^2)-Gam)))
+(((S(k[0])-S[eq](k[0]))*K[0](k[0],xi)*xi*exp(t[0](-D*k[0]^2-2*Gam)-D*k[0]^2*t[3]))
/(2*Gam*(D*(xi^2-k[0]^2)-Gam)))
-((K[0](k[0],xi)*xi*(k[0]^2*(S(k[0])-S[eq](k[0]))*D-S(k[0])*Gam)*exp(-D*k[0]^2*t[3]-D*k[0]^2*t[0]))
/(2*Gam*(D*k[0]^2-Gam)*(D*(xi^2-k[0]^2)+Gam)))$
Parse(Expression);