=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2018/engine/p-itv.c,v retrieving revision 1.1 retrieving revision 1.6 diff -u -p -r1.1 -r1.6 --- OpenXM_contrib2/asir2018/engine/p-itv.c 2018/09/19 05:45:07 1.1 +++ OpenXM_contrib2/asir2018/engine/p-itv.c 2019/11/12 10:53:22 1.6 @@ -1,13 +1,20 @@ /* - * $OpenXM$ + * $OpenXM: OpenXM_contrib2/asir2018/engine/p-itv.c,v 1.5 2019/11/07 08:50:49 kondoh Exp $ */ #if defined(INTERVAL) #include "ca.h" #include "base.h" +#if 0 #if defined(PARI) #include "genpari.h" #endif +#endif +// in engine/bf.c +Num tobf(Num,int); +//int initvp(Num , Itv ); + +extern int mpfr_roundmode; extern int zerorewrite; void itvtois(Itv a, Num *inf, Num *sup) @@ -16,6 +23,12 @@ void itvtois(Itv a, Num *inf, Num *sup) *inf = *sup = (Num)0; else if ( NID(a) <= N_B ) { *inf = (Num)a; *sup = (Num)a; + } else if ( ITVD(a) ) { + double dinf, dsup; + Real rinf, rsup; + dinf = INF((IntervalDouble)a); dsup = SUP((IntervalDouble)a); + MKReal(dinf, rinf); MKReal(dsup, rsup); + *inf = (Num)rinf; *sup = (Num)rsup; } else { *inf = INF(a); *sup = SUP(a); @@ -24,52 +37,61 @@ void itvtois(Itv a, Num *inf, Num *sup) void istoitv(Num inf, Num sup, Itv *rp) { - Num i, s; + Num ii, ss; + Num ni, ns; Itv c; int type=0; + int current_roundmode; if ( !inf && !sup ) { *rp = 0; return; } - if ( !sup ) { - s = 0; + if ( compnum(0,sup,inf) >= 0 ) { + ns = sup; ni = inf; + } else { + ni = sup; ns = inf; } - else if ( NID( sup )==N_B ) { + + current_roundmode = mpfr_roundmode; + if ( !ns ) { + ss = 0; + } + else if ( NID( ns )==N_B ) { type = 1; - ToBf(sup, (BF *)&s); + + mpfr_roundmode = MPFR_RNDU; + ss = tobf(ns, DEFAULTPREC); + //ToBf(sup, (BF *)&s); } else { - s = sup; + ss = ns; } - if ( !inf ) { - i = 0; + if ( !ni ) { + ii = 0; } - else if ( NID( inf )==N_B ) { + else if ( NID( ni )==N_B ) { type = 1; - ToBf(inf, (BF *)&i); + + mpfr_roundmode = MPFR_RNDD; + ii = tobf(ni, DEFAULTPREC); + //ToBf(inf, (BF *)&i); } else { - i = inf; + ii = ni; } - if ( type ) { -// NEWIntervalBigFloat((IntervalBigFloat)c); - c=MALLOC(sizeof(struct oIntervalBigFloat)); - OID(c)=O_N; - NID(c)=N_IntervalBigFloat; - } else - NEWItvP(c); + mpfr_roundmode = current_roundmode; - if ( compnum(0,i,s) >= 0 ) { - INF(c) = s; SUP(c) = i; + if ( type ) { + IntervalBigFloat cc; + NEWIntervalBigFloat(cc); + c = (Itv)cc; } else { - INF(c) = i; SUP(c) = s; + NEWItvP(c); } + INF(c) = ii; SUP(c) = ss; - if (zerorewrite) - if ( initvp(0,c) ) { - *rp = 0; - return; - } *rp = c; + + ZEROREWRITE } void additvp(Itv a, Itv b, Itv *c) @@ -252,7 +274,7 @@ void divitvp(Itv a, Itv b, Itv *c) void pwritvp(Itv a, Num e, Itv *c) { - int ei; + long ei; Itv t; if ( !e ) @@ -268,16 +290,18 @@ void pwritvp(Itv a, Num e, Itv *c) error("pwritv : can't calculate a fractional power"); #endif } else { - ei = QTOS((Q)e); + //ei = QTOS((Q)e); + ei = mpz_get_si(BDY((Q)e)); pwritv0p(a,ei,&t); - if ( SGN((Q)e) < 0 ) +// if ( SGN((Q)e) < 0 ) + if ( sgnq((Q)e) < 0 ) divnum(0,(Num)ONE,(Num)t,(Num *)c); else *c = t; } } -void pwritv0p(Itv a, int e, Itv *c) +void pwritv0p(Itv a, long e, Itv *c) { Num inf, sup; Num ai,Xmin,Xmax; @@ -286,7 +310,7 @@ void pwritv0p(Itv a, int e, Itv *c) if ( e == 1 ) *c = a; else { - STOQ(e,ne); + STOZ(e,ne); if ( !(e%2) ) { if ( initvp(0,a) ) { Xmin = 0; @@ -374,7 +398,7 @@ void miditvp(Itv a, Num *b) else if ( (NID(a) <= N_B) ) *b = (Num)a; else { - STOQ(2,TWO); + //STOZ(2,TWO); itvtois(a,&ai,&as); addnum(0,ai,as,&t); divnum(0,t,(Num)TWO,b); @@ -442,6 +466,28 @@ void absitvp(Itv a, Num *b) else *b = bs; } } + +void absintvalp(Itv a, Itv *c) +{ + Num ai,as,mai; + Num inf,sup; + + itvtois(a,&ai,&as); + if ( compnum(0,as,0 ) < 0 ) { + chsgnnum(ai, &sup); + chsgnnum(as, &inf); + } else if ( compnum(0,ai,0) < 0 ) { + inf = 0; + chsgnnum(ai, &mai); + if ( compnum(0,as,mai ) > 0 ) sup = as; + else sup = mai; + } else { + inf = ai; + sup = as; + } + istoitv(inf,sup,c); +} + void distanceitvp(Itv a, Itv b, Num *c) {