formatting part of expression



You may find the approach below useful.

          -s



---------- Forwarded message ----------
From: Stavros Macrakis <macrakis at alum.mit.edu>
Date: Mon, Aug 20, 2012 at 3:59 PM
Subject: Soft boxes ... or: how to parenthesize for fun and profit
To: maxima mailing list <maxima at math.utexas.edu>


Users often want to parenthesize expressions in ways that aren't simplified
according to Maxima's general simplifier, e.g.

       1 + (x?2 - 1)

so that they can then do map(factor,...) -- or simply to show a
step-by-step derivation.

Here's a simple way of doing this using my contrib package 'simplifying':

*load(simplifying)$*

*softbox_op : verbify("(")$      *
*     /* reuse the existing paren operator */*
*simplifying(softbox_op,lambda([[expr]],*
*        if ?substp then softbox(resimplify(last(expr))) else last(expr)))$
*
*     /* suppress **(x) => x within substpart, using special variable **?substp
*/*
*     /* '(a,b,c) formerly simplified to (a,b,c);*
*        it now simplifies to c -- which is correct if a and b are
side-effect-free */*
*softbox(expr):= simpfuncall(softbox_op,expr)$*
*     /* construct (x) with a simp flag already on it */*
*resimplify(expr) := expand(expr,0,0)$*
*     /* resimplifies everything, removing softboxes among other things */*
*remsoftbox(expr) := subst(lambda([[ex]],last(ex)),softbox_op,expr)$*

*/* removes softboxes without other resimplifications (could be more
efficient...) */*


Examples:

*     softbox(x) => (x)  ;   resimplify(%) => x*
*     softbox(x+1)-x => (x+1)-x  ;  resimplify(%) => 1*
*
*
*     ex: a*b + b*c + c*d$  => c*d + b*c + a*b*
*     factor(ex) == factorsum(ex) => unchanged*
*     ex1: substpart(softbox(piece),ex,[1,2]) => (c*d+b*c) + a*b*
*     map(factor,ex1) => c*(d+b) + a*b*
*
*
*     remsoftbox(softbox(factor(4))) => 2^2*
*     remsoftbox(softbox(5)-softbox(4)) => 1    ... doesn't suppress
simplification of outer operations*
*     resimplify(softbox(factor(4))) => 4*

Not all parentheses are soft boxes:

*box_softbox(expr) := subst('box,softbox_op,expr)$*
*     /* convert soft to hard boxes */*


Example:


*     box_softbox( (x-1)*a - softbox(x-1)*a ) => a*(x-1) - a*box(x-1)*

Now, softbox(x)-softbox(x) still simplifies to 0, and softbox(x)*softbox(x)
still simplifies to softbox(x)?2 -- just as with 'hard' boxes.  If we want to
avoid this, we can define softbox_unique, which includes a unique number in
each softbox:

*if not(numberp(softbox_counter)) then softbox_counter : 0$*
*     /* initialize only if not defined */*
*softbox_unique(expr):=simpfuncall(paren_op,softbox_counter :
softbox_counter + 1,expr)$*

This could be made less ugly by using a distinct softbox_op and hacking *
nformat* if really necessary.  Of course, with q: softbox_unique(ex)$, you
still have q-q => 0.

There are places in Maxima that resimplify unexpectedly (to me at least),
and therefore don't preserve softboxes.  For example, factor(softbox(4)) =>
2^2, not (x) or (2^2), though factor(g(4)) => g(4).

I look forward to your feedback.

              -s



On Fri, Oct 11, 2013 at 8:10 AM, Tamas Papp <tkpapp at gmail.com> wrote:

> Hi,
>
> I would like to display an equation, both in Maxima and for LaTeX, with
> certain parts arranged in a way that's meaningful for the problem.  A
> toy example, where I want 1-(a+b) preserved in that form:
>
> --8<---------------cut here---------------start------------->8---
> (%i288) eq: c*b/(1-(a+b));
>                                       b c
> (%o288)                           -----------
>                                   - b - a + 1
> (%i289) format(eq,%factor/(1-(a+b)));
>                                       b c
> (%o289)                           -----------
>                                   - b - a + 1
> --8<---------------cut here---------------end--------------->8---
>
> This does not work.  I would prefer to have something like
>
> --8<---------------cut here---------------start------------->8---
>                                      b c
> (%o289)                           ---------
>                                   1 - b - a
> --8<---------------cut here---------------end--------------->8---
>
> Neither does it work in tex1:
>
> --8<---------------cut here---------------start------------->8---
> (%i290) tex1(eq);
> (%o290)                      \ifrac{b\,c}{-b-a+1}
> (%i291) tex1(format(eq,%factor/(1-(a+b))));
> (%o291)                      \ifrac{b\,c}{-b-a+1}
> --8<---------------cut here---------------end--------------->8---
>
> Is there a way to do this?  Manually transcribing the equation is a bit
> error-prone, especially if it is more complicated.
>
> Best,
>
> Tamas
>
> PS.: This question must have come up before but I guess I am not using
> the right search terms because I can't find an answer in the archives.
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>