Subject: Repeatedly applying trigsimp, ratsimp etc.
From: David Scherfgen
Date: Sat, 03 Nov 2012 21:04:41 +0100
Hi Stavros,
thanks for your input.
I'm aware of the fact that you might get caught in a "local minimum".
However, for my application it is not crucial to really get to the
global minimum.
I have now implemented my original iterative approach using the
"conssize" metric that Maxima uses as well in the "scsimp" function.
Here's my code. It might be useful to people looking for the same thing.
The function defined is "dssimp", which takes an expression and a list
of simplification functions to be called.
There's also a function "dssimpall", which simply takes into account all
of Maxima's simplification functions.
PS: It's my very first piece of Lisp code in my entire life, so don't be
harsh on me ;-)
File dssimp.lisp:
----------------------
(defmfun $conssize (x) (if (atom x) 0 (do ((x (cdr x) (cdr x)) (sz 1))
((null x) sz) (incf sz (1+ (conssize (car x)))))))
File dssimp.mac:
-----------------------
dssimp(expr, r) :=
block([exprSize, smallestExpr, smallestExprSize, simpdExpr,
simpdExprSize],
exprSize : conssize(expr),
smallestExpr : expr,
smallestExprSize : exprSize,
loop,
for i : 1 thru length(r) do
block(
simpdExpr : r[i](expr),
simpdExprSize : conssize(simpdExpr),
if simpdExprSize < smallestExprSize then
block(
smallestExpr : simpdExpr,
smallestExprSize : simpdExprSize
)
),
if smallestExprSize >= exprSize then
return(expr),
expr : smallestExpr,
exprSize : smallestExprSize,
go(loop)
);
dssimpall(expr) := dssimp(expr, [combine, logcontract, radcan, ratsimp,
rootscontract, trigrat, trigreduce, trigsimp, xthru]);
Am 31.10.2012 23:28, schrieb Stavros Macrakis:
> David,
>
> I did have some code for this a while ago; not sure whether I can
> dredge it up.
>
> Your suggested approach actually won't work -- you don't want to keep
> only the simplest version of each stage; you want to keep /all
> /versions because sometimes the only way to get from form A to form C
> is through an intermediate form B which is /not/ "simplest".
>
> Another important thing is of course defining what you mean by
> "simple". A simple metric is number of characters in string form, but
> that isn't necessarily the "nicest" form. Depending on your goal, any
> one of the following may be the "simplest" form:
>
> (y+x+1)^2-1 (11 chars)
> (y+x)*(y+x+2) (13 chars)
> y^2+(2*x+2)*y+x^2+2*x (21 chars)
> x^2+(2*y+2)*x+y^2+2*y (21 chars)
> y^2+2*x*y+2*y+x^2+2*x (21 chars)]
>
> Also, simply applying trigsimp/ratsimp/etc. multiple times is not the
> only way to shorten an expression. Sometimes you might want to look
> at all subexpressions or do other clever things -- how are you going
> to get (y+x+1)^2-1 from y^2+2*x*y+2*y+x^2+2*x?
>
> -s
>
> On Wed, Oct 31, 2012 at 4:04 PM, David Scherfgen
> <d.scherfgen at googlemail.com <mailto:d.scherfgen at googlemail.com>> wrote:
>
> Hello,
>
> Maxima has a lot of different simplification functions.
> I'm looking for a way to "try them all" and find the simplest
> expression.
> Basically I want to do this:
>
> Given an expression e, compute trigsimp(e), ratsimp(e), radcan(e) ...
> See which of these gives the "simplest" result and do the same
> again with this simpler result.
> Stop when none of the functions can produce a simpler result any more.
>
> First I thought that "scsimp" was the right function to do this,
> and I tried it like this:
>
> matchdeclare(x, true);
> defrule(r1, x, trigsimp(x));
> defrule(r2, x, ratsimp(x));
> // more rules follow
>
> // my expression to be simplified
> // it equals to 1, if you apply "trigsimp"
> expr : tan(x) * cot(x)
>
> scsimp(expr, r1, r2, ...);
>
> However, "scsimp" doesn't apply any of the rules.
> If I manually apply rule "r1", it gives me the correct result "1".
>
> Am I doing something wrong? Or does "scsimp" just not work like this?
> How could I do it manually? Is there a Maxima function that
> computes the length of an expression, so I could use it to select
> the best result in each step?
>
> I'm very grateful for any help.
>
> Kind regards,
> David Scherfgen
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu <mailto:Maxima at math.utexas.edu>
> http://www.math.utexas.edu/mailman/listinfo/maxima
>
>