Mark Dickinson on Tue, 17 Apr 2001 13:04:44 -0400 (EDT)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

greal() bug for type t_RFRAC?


Hi,

I encountered the following behaviour:

  GP/PARI CALCULATOR Version 2.1.0 (released)
 UltraSparc (MicroSparc kernel) 32-bit version

[snipped]

? w=quadgen(-3)
%1 = w
? real(1/(a+b*w))
%2 = a/(a^2 + b^2)

Unless I've misunderstood something, this doesn't seem like a useful
answer;  I was expecting (a+b)/(a^2+a*b+b^2). The problem seems to lie in
the treatment of type t_RFRAC in the function op_ReIm(GEN f(GEN), GEN x)
in basemath/gen3.c, where the formulas for real and imaginary parts appear
to assume a complex type.  The following patch (against the source for
version 2.1.0) fixes the problem for me: 


*** pari-2.1.0/src/basemath/gen3.c	Fri Nov  3 16:00:22 2000
--- pari-2.1.0/src/basemath/gen3fix.c	Tue Apr 17 12:53:44 2001
***************
*** 2335,2347 ****
      case t_RFRAC: case t_RFRACN:
      {
        av=avma; r2=greal((GEN)x[2]); i2=gimag((GEN)x[2]);
!       if (f==greal)
!         p1 = gadd(gmul(greal((GEN)x[1]),r2),
!                   gmul(gimag((GEN)x[1]),i2));
!       else
!         p1 = gsub(gmul(gimag((GEN)x[1]),r2),
!                   gmul(greal((GEN)x[1]),i2));
!       p2=gadd(gsqr(r2), gsqr(i2));
        tetpil=avma; return gerepile(av,tetpil,gdiv(p1,p2));
      }
  
--- 2335,2342 ----
      case t_RFRAC: case t_RFRACN:
      {
        av=avma; r2=greal((GEN)x[2]); i2=gimag((GEN)x[2]);
!       p1 = f(gmul((GEN)x[1],gconj((GEN)x[2])));
!       p2 = gnorm((GEN)x[2]);
        tetpil=avma; return gerepile(av,tetpil,gdiv(p1,p2));
      }
  

However, this is the first time I've delved into the PARI source, so I'm
not guaranteeing either that this patch is sensible or that it doesn't
break something else :) 

Thanks,

Mark Dickinson