factor/simplification problem



I have run into a problem when part(ex,0) doesn't equal "+".  For a
slightly different problem I have:
(%i75) test;
(%o75) (fv3*L^3*(cos(beta)*sinh(beta)*c*s*L-cosh(beta)*sin(beta)*c*s*L+cos(beta)*sinh(beta)*k*L-
cosh(beta)*sin(beta)*k*L-2*beta*sin(beta)*sinh(beta)*EI))/(beta^3*EI*(cos(beta)*cosh(beta)*c*s*L+
c*s*L+cos(beta)*cosh(beta)*k*L+k*L+beta*cos(beta)*sinh(beta)*EI-beta*cosh(beta)*sin(beta)*EI))

(%i76) part(test,0);
(%o76) //

and

(%i77) N:num(test);
(%o77) fv3*L^3*(cos(beta)*sinh(beta)*c*s*L-cosh(beta)*sin(beta)*c*s*L+cos(beta)*sinh(beta)*k*L-
cosh(beta)*sin(beta)*k*L-2*beta*sin(beta)*sinh(beta)*EI)
(%i78) part(N,0);
(%o78) *

I created a divsimp2 that eliminates the part(ex,0)="+" test and it
seems to work ,but I don't understand the ramifications of what I did.
 Obviously you put that test there for a reason, but I don't fully
understand the reason.  So, I need to know when I will get into
trouble with my modified function:

divsimp2(ex,div):=
  if atom(ex) then ex
  else block([quorem: divide(ex,div)],
                factorsum(quorem[1])*factorsum(div)
                   + factorsum(quorem[2]));

(%i61) N;
(%o61) fv3*L^3*(cos(beta)*sinh(beta)*c*s*L-cosh(beta)*sin(beta)*c*s*L+cos(beta)*sinh(beta)*k*L-
cosh(beta)*sin(beta)*k*L-2*beta*sin(beta)*sinh(beta)*EI)

(%i62) divsimp(N,c*s+k);
(%o62) false

(%i63) divsimp2(N,c*s+k);
(%o63) (cos(beta)*sinh(beta)-cosh(beta)*sin(beta))*fv3*(c*s+k)*L^4-2*beta*sin(beta)*sinh(beta)*fv3*
EI*L^3


Thanks again for all your help,

Ryan

On 5/16/07, Stavros Macrakis <macrakis at alum.mit.edu> wrote:
> On 5/16/07, Ryan Krauss <ryanlists at gmail.com> wrote:
> > This seems really powerful and I am increasingly interested in
> > learning to program in Maxima (and I planning to learn more lisp for
> > emacs anyways).  Can you give me a bit more explanation on the divsimp
> > function.  A brief comment for each line would help me learn a lot.
> >
>
> divsimp(ex,div):=
>   if atom(ex)                 /* Is ex a symbol or a number? i.e. has no
> subparts */
>         then ex                 /* if so, return it */
>   elseif part(ex,0) ="+"  /* part 0 of an expression is its operator:
>                                           a+b+c is
> equivalent to "+"(a,b,c) */
>   then block(                /* a block is like begin/end or {} -- it groups
> a series of statements, possibly
>                                           with local
> variables */
>                [quorem      /* list of local variables */
>                          :          /* can be initialized using var:val
> instead of just var */
>                           divide(ex,div)]    /* divides ex by div and
> returns [<quotient>,<remainder>] as a list */
>                  ,               /* statements separated with commas */
>                 factorsum(quorem[1])      /* quorem[1] is the quotient --
> apply factorsum to it */
>                       *factorsum(div)
>                    + factorsum(quorem[2])  /* quorem[2] is the remainder */
>                  )        /* close off the block */
>  else map(      /* map a function over expression subparts; map(f,a*b) ==
> f(a)*f(b)
>            lambda([q],divsimp(q,div)),      /*
> lambda([q],...) is an anonymous function, like
>
>         defining fff(q):=... and then using fff here */
>           ex)$
>
> To see what is going on, try trace(divsimp).
>
>                  -s
>
>