Sindbad~EG File Manager

Current Path : /usr/home/beeson/Otter-Lambda/yyy/polyval/
Upload File :
Current File : /usr/home/beeson/Otter-Lambda/yyy/polyval/ratsimp.c

/* M. Beeson, for Mathpert
ratsimp simplifies a rational function, if possible eliminating
common zeroes of num and denom.  This is used before calculating
singularities.

Original date 1.30.97
Last modified 1.29.98
1.16.05 removed include debug.h
*/

#include <assert.h>
#include <string.h>
#include <math.h>
#define POLYVAL_DLL
#include "globals.h"
#include "pvalaux.h"
#include "deval.h"
#include "order.h"
#include "cancel.h"
#include "probtype.h"
#include "prover.h"
#include "polynoms.h"
#include "mpmem.h"
#include "simpsums.h"
#include "binomial.h"
#include "trig.h"
#include "ratsimp.h"
#include "factor.h"
#include "polyquo.h"

MEXPORT_POLYVAL void ratsimp(term t, term *ans)
/* simplify rational function t, eliminating common zeroes of num and
denom if possible, but this is not guaranteed.
*/
{ term u,v,num,denom,w;
  POLYnomial p,q;
  term x = get_eigenvariable();
  int err,err2;
  void  *savenode = heapmax();
  int savegcdflag = get_polyvalgcdflag();
  int savefunctionflag = get_polyvalfunctionflag();
  int savecomdenomflag = get_polyvalcomdenomflag();
  int savefactorflag = get_polyvalfactorflag();
  int savenegexpflag = get_polyvalnegexpflag();
  int savefractexpflag = get_polyvalfractexpflag();
  int savelogflag = get_polyvallogflag();
  int savefactorflag2 = get_polyvalfactorflag2();
  err = polyval(t,&u);
  /* This will perform cancellations, etc., before we try polyquo */
  if(!err)
     { save_and_reset(u,savenode,&t);
       savenode = heapmax();  /* because now t is above the old savenode */
     }
  if(grat(t,x))
     { err = make_polyquo(t,x,&p,&q);
       if(!err)
          { u = make_fraction(poly_term(p,x),poly_term(q,x));
            save_and_reset(u,savenode,ans);
            return;
          }
     }
  reset_heap(savenode);  /* after make_polyquo */
  set_polyvalfactorflag(1);
  set_polyvalfactorflag2(0x100);  /* factor all sums */
  set_polyvalgcdflag(1);
  set_polyvalcomdenomflag(1);
  set_polyvalfractexpflag(1);
  set_polyvalnegexpflag(-1);  /* eliminate negative exponents */
  set_polyvallogflag(2);
  set_polyvalfunctionflag(1);
  copy(t,&w);
  clear_already(&w);
  polyval(w,&u);
  /* destroy_term(w) is permissible but superfluous
     in view of save_and_reset coming soon. */
  set_polyvalnegexpflag(savenegexpflag);
  set_polyvalgcdflag(savegcdflag);
  set_polyvalfactorflag(savefactorflag);
  set_polyvalfunctionflag(savefunctionflag);
  set_polyvalcomdenomflag(savecomdenomflag);
  set_polyvalfractexpflag(savefractexpflag);
  set_polyvalfactorflag2(savefactorflag2);
  set_polyvallogflag(savelogflag);
  save_and_reset(u,savenode,&t);
  if(FRACTION(t))
     { num = ARG(0,t);
       denom = ARG(1,t);
       if(mvpoly2(num))
          { err = purefactor(num,&u);
            if(err)
               u = num;
          }
       else
          { err = 1;
            u = num;
          }
       if(mvpoly2(denom))
          { err2 = purefactor(denom,&v);
            if(err2)
               v = denom;
          }
       else
          { err2 = 1;
            v = denom;
          }
       if(err && err2)
          { *ans = t;
            return;
          }
       ratsimp(make_fraction(u,v),ans);
     }
  *ans = t;
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists