Subject: ugly weird expression to be passed to R??
From: Stavros Macrakis
Date: Mon, 28 Jul 2008 20:52:40 -0400
Well, first of all, I think you can clean up this expression quite a
bit within Maxima. If you eyeball the expression, you can see that
1+k1*N1+k2*N2 shows up a lot within it. So you might be able to
substitute for it. Calling your initial expression ex, and setting
qval: 1+k1*N1+k2*N2
let's define a function that performs an operation only if it shortens
the expression:
maybesub(f) := buildq([f], lambda([expr], block([expr1 : f(expr)],
if slength(string(expr1)) < slength(string(expr)) then expr1 else
expr)))
Now...
scanmap(maybesub(lambda([z],ratsubst(q,qval,z))),ex) =>
(log(2)*%e^(q*T)-log(2)*%e^(O2p*q*T))*%e^(%e^-(q*T)*(log(2)*%e^(O2p*q*T+1)+((1-O2p)*q^2*T+(O2p-1)*q)*%e^(q*T)+((%e*log(2)-%e*log(2)*O2p)*q*T-%e*log(2))*%e^(q*T))/((O2p-1)*q))
Looks like some factorsums would help here...
scanmap(maybesub(factorsum),%)
=>
-log(2)*(%e^(O2p*q*T)-%e^(q*T))*%e^(%e^-(q*T)*(log(2)*%e^(O2p*q*T+1)-log(2)*((O2p-1)*q*T+1)*%e^(q*T+1)-(O2p-1)*q*(q*T-1)*%e^(q*T))/((O2p-1)*q))
and finally
scanmap(maybesub(divthru),%) =>
-log(2)*(%e^(O2p*q*T)-%e^(q*T))*%e^(log(2)*(%e^(O2p*q*T)-%e^(q*T))*%e^(1-q*T)/((O2p-1)*q)-q*T-%e*log(2)*T+1)
(see divthru definition below) and now all you have to do is set
q = k2*N2+k1*N1+1
and evaluate the above formula, which (except I guess for the name of
"%e") is pretty much standard algebraic notation in most programming
languages.
-s
-----------------------------------
divthru(a):=
block([n,d,div],
if mapatom(a) then a
elseif inpart(a,0)="*" and numberp(n:inpart(a,1)) then
/* handle 3*a/b, -a/b, etc. */
(div:divthru(a/n),
if not atom(div) and inpart(div,0)="+" then
multthru(n*div) else n*div )
elseif part(a,0)="/" then
/* handle a/b, (a/b)^n==a^n/b^n, etc. */
(n:part(a,1), d:part(a,2),
div:divide(n,d),
factor(div[1]) + factor(div[2]/d) )
else a)$
On Mon, Jul 28, 2008 at 6:47 PM, dlakelan <dlakelan at street-artists.org> wrote:
> I have a differential equation model for the probability density of a
> random variable. I am totally stoked on the fact that Maxima was able to
> solve this problem. The result is a maxima expression that is pretty
> damn ugly. here is the ratsimp version:
>
>
> (%o40) -log(2)*(%e^(O2p*(k2*N2+k1*N1+1)*T)-%e^((k2*N2+k1*N1+1)*T))
> *%e^(%e^-((k2*N2+k1*N1+1)*T)
> *(log(2)*%e^(k2*O2p*N2*T+k1*O2p*N1*T+O2p*T+1)
> -log(2)*k2*O2p*N2*T*%e^(k2*N2*T+k1*N1*T+T+1)
> +log(2)*k2*N2*T*%e^(k2*N2*T+k1*N1*T+T+1)
> -log(2)*k1*O2p*N1*T*%e^(k2*N2*T+k1*N1*T+T+1)
> +log(2)*k1*N1*T*%e^(k2*N2*T+k1*N1*T+T+1)
> -log(2)*O2p*T*%e^(k2*N2*T+k1*N1*T+T+1)
> +log(2)*T*%e^(k2*N2*T+k1*N1*T+T+1)
> -log(2)*%e^(k2*N2*T+k1*N1*T+T+1)
> -k2^2*O2p*N2^2*T*%e^((k2*N2+k1*N1+1)*T)
> +k2^2*N2^2*T*%e^((k2*N2+k1*N1+1)*T)
> -2*k1*k2*O2p*N1*N2*T*%e^((k2*N2+k1*N1+1)*T)
> +2*k1*k2*N1*N2*T*%e^((k2*N2+k1*N1+1)*T)
> -2*k2*O2p*N2*T*%e^((k2*N2+k1*N1+1)*T)
> +2*k2*N2*T*%e^((k2*N2+k1*N1+1)*T)
> -k1^2*O2p*N1^2*T*%e^((k2*N2+k1*N1+1)*T)
> +k1^2*N1^2*T*%e^((k2*N2+k1*N1+1)*T)
> -2*k1*O2p*N1*T*%e^((k2*N2+k1*N1+1)*T)
> +2*k1*N1*T*%e^((k2*N2+k1*N1+1)*T)
> -O2p*T*%e^((k2*N2+k1*N1+1)*T)+T*%e^((k2*N2+k1*N1+1)*T)
> +k2*O2p*N2*%e^((k2*N2+k1*N1+1)*T)
> -k2*N2*%e^((k2*N2+k1*N1+1)*T)
> +k1*O2p*N1*%e^((k2*N2+k1*N1+1)*T)
> -k1*N1*%e^((k2*N2+k1*N1+1)*T)+O2p*%e^((k2*N2+k1*N1+1)*T)
> -%e^((k2*N2+k1*N1+1)*T))
> /((O2p-1)*(k2*N2+k1*N1+1)))
>
> I want to do a maximum likelihood estimation of the values of k1,k2, and
> Cmax given data for other variables. I think I can do a little cleaning
> up of the output of maxima's function "fortran" and then hand it to R
> for numerical analysis.
>
> Does anyone have a suggestion for how to clean up this expression in
> maxima before outputting to fortran? I've tried pickapart and a few
> other things to figure out the structure of this expression, and so far
> haven't figured out anything helpful to simplify it further. Pickapart
> and reveal mostly make it harder to read for me.
>
> Has anyone created a function like "fortran" but that outputs something
> compatible with some sort of more modern language (ie. no line numbers
> and continuation lines, and column indentation etc?) anyone want to give
> a try at converting the "fortran" function in maxima to output "R" code?
> I think R and maxima together are an extremely powerful combo. it would
> be nice to be able to directly export to R syntax.
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>