1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
|
/*------------------------------------------------------------------------
* Graphic library
* Copyright (C) 1998-2000 Enpc/Jean-Philippe Chancelier
* jpc@cereve.enpc.fr
--------------------------------------------------------------------------*/
/*------------------------------------------------------------------------
* Axis drawing for 2d plots (format selection)
*
* void ChoixFormatE(fmt, desres, xmin, xmax, xpas) : find a format
* void ChoixFormatE1(fmt, desres, xx, nx) : find a format
* int C2F(graduate)(xmi,xma,xi,xa,np1,np2,kminr,kmaxr,ar)
* : change [xmi,xmax] for pretty graduation
*--------------------------------------------------------------------------*/
#include <math.h>
#include <string.h>
#include <stdio.h>
#include "Math.h"
extern double C2F(dlamch) __PARAMS((char *CMACH, unsigned long int));
static void FormatPrec __PARAMS((char *fmt, integer *desres, double xmin, double xmax,
double xpas));
static void FormatPrec1 __PARAMS((char *fmt, integer *desres, double *xx, integer nx));
static int Fsepare __PARAMS((char *fmt, integer dec, integer *l, double xmin, double xmax,
double xpas));
static int Fsepare1 __PARAMS((char *fmt, integer dec, integer *l, double *xx, integer nx));
static void graduate1 __PARAMS((double *xmi,double * xma,double * xi,double * xa,
integer * np1,integer * np2,integer * kminr,integer * kmaxr,integer * ar,int count));
static void gradua __PARAMS(( double *xmi, double *xma,integer * kminr,integer *kmaxr,integer *ar,integer *npr,integer *b));
static void decompSup __PARAMS((double x,integer * xk,integer * xa,integer b));
static void decompInf __PARAMS((double x,integer * xk,integer * xa,integer b));
/*----------------------------------------------------
* ChoixFormatE returns a format ("%.*f" or "%.*e")
* in fmt given xmin,xmax,pas.
* fmt : character string
* fmt gives a format which can be used to display
* number in range xmin:step:xmax
* Exemple : ChoixFormatE(format,min,max,step);
* fprintf(format,min+k*step);
* The format is searched so as to give distinct values
* for the numeric values xmin + k*xpas in [xmin,xmax]
* and give enough precision.
*------------------------------------------------*/
void ChoixFormatE(fmt, xmin, xmax, xpas)
char *fmt;
double xmin,xmax,xpas;
{
char c;
integer des,len;
/* format f minimal */
for ( des = 0 ; des < 5 ; des++)
{
if (Fsepare("%.*f",des,&len,xmin,xmax,xpas)) break;
}
if ( des < 5 && len <= 6)
{
c='f';
strcpy(fmt,"%.*f");
}
else
{
for ( des = 0 ; des < 5 ; des++)
{
if (Fsepare("%.*e",des,&len,xmin,xmax,xpas)) break;
}
c='e';
strcpy(fmt,"%.*e");
}
FormatPrec(fmt,&des,xmin,xmax,xpas);
sprintf(fmt,"%%.%d%c",des,c);
}
/*
* checks if given format gives enough precision
* if not increase it (i.e increase desres)
*/
static void FormatPrec(fmt, desres, xmin, xmax, xpas)
char *fmt;
integer *desres;
double xmin,xmax,xpas;
{
char buf1[100],buf2[100];
integer i=0;
while ( xmin+((double)i)*xpas < xmax && *desres < 10 )
{
double x1,x2,yy1;
yy1=xmin+((double) i)*xpas;
sprintf(buf1,fmt,*desres,yy1);
sprintf(buf2,fmt,*desres,yy1+xpas );
sscanf(buf1,"%lf",&x1);
sscanf(buf2,"%lf",&x2);
if ( Abs((x2-x1 -xpas) /xpas) >= 0.1) *desres += 1;
if ( Abs((x1- yy1)/xpas) >= 0.01) *desres +=1;
i++;
}
}
/*
* checks if format fmt gives different values for numbers
* from xmin to xmax with step xpas. It also returns in variable l
* the string length that will result in using the format
*/
static int Fsepare(fmt, dec, l, xmin, xmax, xpas)
char *fmt;
integer dec;
integer *l;
double xmin,xmax,xpas;
{
double x=xmin;
char buf1[100],buf2[100];
*l = 0;
/** Take care of : sprintf(buf1,"%.*f",0,1.d230) which overflow in buf1 **/
/** we don't use %.*f format if numbers are two big **/
if (strcmp("%.*f",fmt)==0 && (Abs(xmax)> 1.e+10 || Abs(xmin) > 1.e+10))
return(0);
sprintf(buf1,fmt,dec,xmin);
while ( x < xmax )
{ x += xpas;
strcpy(buf2,buf1);
sprintf(buf1,fmt,dec,x);
*l = (((int)strlen(buf1) >= *l) ? strlen(buf1) : *l) ;
if ( strcmp(buf1,buf2) == 0) return(0);
}
return(1);
}
/*--------------------------------------------
* same as ChoixFormatE when numbers are given through an
* array xx[0:nx-1];
*------------------------------------------------*/
void ChoixFormatE1(fmt, xx, nx)
char *fmt;
double *xx;
integer nx;
{
char c;
integer des,len;
/* format f minimal */
for ( des = 0 ; des < 5 ; des++)
{
if (Fsepare1("%.*f",des,&len,xx,nx)) break;
}
if ( des < 5 && len <= 6)
{
c='f';
strcpy(fmt,"%.*f");
}
else
{
for ( des = 0 ; des < 5 ; des++)
{
if (Fsepare1("%.*e",des,&len,xx,nx)) break;
}
c='e';
strcpy(fmt,"%.*e");
}
FormatPrec1(fmt,&des,xx,nx);
sprintf(fmt,"%%.%d%c",des,c);
}
/*----------------------------------------------------------
* checks if format fmt gives different values for numbers
* from xmin to xmax with step xpas. It also returns in variable l
* the string length that will result in using the format
*------------------------------------------------------*/
static void FormatPrec1(fmt, desres, xx, nx)
char *fmt;
integer *desres;
double *xx;
integer nx;
{
char buf1[100],buf2[100];
double xpas;
integer i=0;
while ( i < nx-1 && *desres < 10 )
{
double x1,x2;
sprintf(buf1,fmt,*desres,xx[i]);
sprintf(buf2,fmt,*desres,xx[i+1]);
sscanf(buf1,"%lf",&x1);
sscanf(buf2,"%lf",&x2);
xpas = xx[i+1]-xx[i];
if ( xpas != 0.0)
{
if (Abs((x2-x1 - xpas) /xpas) >= 0.1) *desres += 1;
if (Abs((x1-xx[i])/xpas) >= 0.1) *desres +=1;
}
i++;
}
}
static int Fsepare1(fmt, dec, l, xx, nx)
char *fmt;
integer dec;
integer *l;
double *xx;
integer nx;
{
char buf1[100],buf2[100];
integer i=0;
*l = 0;
/** Take care of : sprintf(buf1,"%.*f",0,1.d230) which overflow in buf1 **/
/** we don't use %.*f format if numbers are two big **/
if (strcmp("%.*f",fmt)==0 && (Abs(xx[nx-1])> 1.e+10 || Abs(xx[0]) > 1.e+10))
return(0);
sprintf(buf1,fmt,dec,xx[0]);
for ( i=1 ; i < nx ; i++)
{ strcpy(buf2,buf1);
sprintf(buf1,fmt,dec,xx[i]);
*l = (((int)strlen(buf1) >= *l) ? strlen(buf1) : *l) ;
if ( strcmp(buf1,buf2) == 0) return(0);
}
return(1);
}
/*----------------------------------------------------
* int C2F(graduate)(xmi,xma,xi,xa,np1,np2,kminr,kmaxr,ar)
* (xgraduate at Scilab level)
* Rescale an interval so as to find a pretty graduation
* for [xmi,xma] given seeks (xi,xa,np1,np2)
* such that xi <= xmi <= xmax <= xa
* with xi et xa numbers of type kminr 10^ar and kmaxr 10^ar.
* then the interval [xi,xa] can be splited in *np2 sub-intervals
* ( kminr-kmaxr can be divided by *np2 )
* x_i= (kminr + i*(kmaxr-kminr)/ (*np2))*10^ar;
* i=0:(*np2)
* ecah of the np2 intervals can in turn be splited in np1 ungraduated
* subintervals
* [np1,np2] follow the nax parameters of plot2d.
*
* We also want to keep np2 small ( *np2 <=10 )
* and we want [xi,xa] to be as close as possible to the interval
* [xmi,xma]
*---------------------------------------------------- */
int C2F(graduate)(xmi,xma,xi,xa,np1,np2,kminr,kmaxr,ar)
double *xmi,*xma,*xi,*xa;
integer *np1,*np2,*kminr,*kmaxr,*ar;
{
if ( *xmi > *xma)
{
double xma1=*xmi, xmi1=*xma;
graduate1(&xmi1,&xma1,xi,xa,np1,np2,kminr,kmaxr,ar,0);
}
else
graduate1(xmi,xma,xi,xa,np1,np2,kminr,kmaxr,ar,0);
return(0);
}
static void graduate1(xmi, xma, xi, xa, np1, np2, kminr, kmaxr, ar,count)
double *xmi;
double *xma;
double *xi;
double *xa;
integer *np1;
integer *np2;
integer *kminr;
integer *kmaxr;
integer *ar;
int count;
{
integer npr,b,i,dx,dxmi,dxma;
/* fprintf(stderr,"[%20.10f,%20.10f]\n",*xmi,*xma); */
/*
*
*/
dx = ( (*xma) != (*xmi) ) ? (int) ceil(log10(Abs((*xma)-(*xmi)))) :0;
dxmi = (*xmi != 0 ) ? (int) ceil(log10(Abs((*xmi)))) : 0;
dxma = (*xma != 0 ) ? (int) ceil(log10(Abs((*xma)))) : 0;
dx=Max(dx-dxmi,dx-dxma);
/* il faut limiter b de sorte que dans la decomposition */
/* avec b les nombres entiers manipules ne deviennent pas trop grands */
/* on choisit donc b < 10 en considerant que le plus grand entier est */
/* 0x7FFFFFFF */
/* on prends aussi un b minimum de 3 : pour avoir des intervalles */
/* plus serr'es : ce nombre est 'eventuellement a affiner */
b=Max(-dx+2,3);
/* fprintf(stderr,"choix de b=%d",b); */
if ( b >= 10 )
{
double xmi1,xma1;
int iexp ;
/* fprintf(stderr,"je ne peux decomposer les 2 nombres sont identiques\n"); */
/*
a la precision donnee les deux nombre ne peuvent etre decomposer
kmin,kmax devrait sinon depasser maxint
on les ecarte de ce qu'il faut pour pouvoir
les separer.
Attention : il faut faire attention de bien choisir iexp
pour ne pas boucler la dedans
*/
iexp = 9 - dxmi -1;
xmi1 = *xmi-exp10((double) - iexp);
xma1 = *xmi+exp10((double) - iexp);
if ( count > 1 )
{
sciprint("Internal Error: Loop in graduate1 \r\n");
sciprint("Please send a Bug report to scilab@inria.fr\r\n");
}
graduate1(&xmi1,&xma1,xi,xa,np1,np2,kminr,kmaxr,ar,count+1);
return;
}
while ( b >= 1 )
{
/* fprintf(stderr,"\tAppel avec b=%d\n",b); */
gradua(xmi,xma,kminr,kmaxr,ar,&npr,&b) ;
*xi= (*kminr)*exp10((double) *ar);
*xa= (*kmaxr)*exp10((double) *ar);
/** fprintf(stderr,"\tRes=[%20.10f,%20.10f]-->[%d,%d,10^%d,%d]\n",*xi,*xa
,*kminr,*kmaxr,*ar,npr); */
*np2= npr;
if ( *np2 <= 20 )
break;
else
b--;
}
/*
on veut essayer de ne pas depasser 10 intervalles ( *np2 <= 10)
pour les intervalle ou on ecrit un nombre,
or on peut en avoir jusqu'a 20. On regarde si le nombre d'intervalle
est divisible. on aura alors une graduation en np2 pour l'ecriture
des nombres et une sous graduation np1 juste avec des tirets.
*/
*np1= 2 ;
if ( *np2 <= 10 ) return ;
/* le nombre est > 10 : s'il est impair on rajoute 1
pour diviser par deux */
if ( *np2 % 2 == 1 )
{
int step ;
step = (*kmaxr-*kminr)/(*np2);
(*np2)++;
*kmaxr += step;
*xa = (*kmaxr)*exp10((double) *ar);
}
/* On recherche des diviseurs a nouveaux pour diminuer le nombre
d'intervalles */
for ( i=2 ; i <=10 ; i++)
{
if ( *np2 % i == 0)
{
*np1=i,*np2 /= i;
return;
}
}
*np1=*np2;*np2=1;
}
/*
* renvoit kminr,kmaxr et ar tels que
* [kminr*10^ar,kmaxr*10^ar] contient [xmi,xma]
* b est un parametre de decompSup,decompInf
* on doit avoir a l'appel xmi < xma.
* le choix se fait entre deux intervalles possibles
* on choisit celui qui est le plus proche de [xmi,xma]
* a condition que (kmaxr-kminr) <= 20
* pour b=1 on sait que (kmaxr-kminr ) <= 20
* 20 intervalles au plus ( que l'on obtient si xmin et xmax sont
* de signe opposes sinon c'est 10 )
*/
/* np2, and np1 must be smaller than maxint */
#define DMAX 0xFFFFFFF
static void gradua(xmi,xma,kminr,kmaxr,ar,npr,b)
double *xmi,*xma;
integer *kminr,*kmaxr,*ar,*b,*npr;
{
double x0=*xmi,x1=*xma,loc;
integer x0k,x0a;
integer x1k,x1a;
integer kmin1,kmax1,a1,np1,kmin2,kmax2,a2,np2,kmin,kmax,a,np;
decompInf(x0,&x0k,&x0a,*b);
decompSup(x1,&x1k,&x1a,*b);
/** special cases **/
if ( x1 == 0.0 ) { x1a= x0a;}
if ( x0 == 0.0 ) { x0a= x1a;}
loc = Min( floor(x0*exp10((double) -x1a)),((double)DMAX));
if ( loc < 0) loc = Max( loc, -((double) DMAX));
kmin1=(int) loc;
kmax1=x1k;
np1= Abs(kmax1-kmin1);
np1= ( np1 < 0 ) ? DMAX : np1;
if ( np1 > 10 )
{
if ((np1 % 2) == 0)
np1 /=2;
else
{
np1++; np1 /= 2;
kmax1++;
}
}
a1=x1a;
/* fprintf(stderr,"\t\tsols : [%d,%d].10^%d,n=%d\t",kmin1,kmax1,a1,np1); */
kmin2=x0k;
loc = Min( ceil( x1*exp10((double) -x0a)),((double)DMAX));
kmax2=(int) loc ;
np2 = Abs(kmax2-kmin2);
np2= ( np2 < 0 ) ? DMAX : np2;
if ( np2 > 10 )
{
if ( np2 % 2 == 0)
np2 /=2;
else
{
np2++;
kmin2--;
}
}
a2=x0a;
/* fprintf(stderr,"[%d,%d].10^%d=%d\n",kmin2,kmax2,a2,np2); */
if ( np1*exp10((double)a1) < np2*exp10((double) a2) )
{
if ( np1 <= 20 )
{
kmin=kmin1; kmax=kmax1; np=np1; a=a1;
}
else
{
kmin=kmin2; kmax=kmax2; np=np2; a=a2;
}
}
else
{
if ( np2 <= 20 )
{
kmin=kmin2; kmax=kmax2; np=np2; a=a2;
}
else
{
kmin=kmin1; kmax=kmax1; np=np1; a=a1;
}
}
*kminr=kmin;
*kmaxr=kmax;
*ar=a;
*npr=np;
if ( kmin==kmax )
{
/*
* a la precision demandee les deux [xi,xa] est reduit a un point
* on elargit l'intervalle
*/
/* fprintf(stderr,"Arg : kmin=kmax=%d",kmin) ; */
/* fprintf(stderr," a=%d, x0=%f,x1=%f\n",a,x0,x1); */
(*kminr)-- ; (*kmaxr)++;*npr=2;
};
}
/*
* soit x > 0 reel fixe et b entier fixe : alors il existe un unique couple
* (k,a) dans NxZ avec k dans [10^(b-1)+1,10^b] tel que
* (k-1)*10^a < x <= k 10^a
* donne par a = ceil(log10(x))-b et k=ceil(x/10^a)
* decompSup renvoit xk=k et xa=a
* si x < 0 alors decompSup(x,xk,xa,b)
* s'obtient par decompInf(-x,xk,xa,b) et xk=-xk
* Remarque : la taille de l'entier k obtenu est controle par b
* il faut choisir b < 10 pour ne pas depasser dans k l'entier maximum
*/
static void decompSup(x, xk, xa, b)
double x;
integer *xk;
integer *xa;
integer b;
{
if ( x == 0.0 )
{
*xk=0 ; *xa= 1; /* jpc */
}
else
{
if ( x > 0 )
{
double xd;
static double epsilon;
static int first=0;
if ( first == 0) { epsilon = 10.0*F2C(dlamch)("e",1L); first++ ;}
/* if x is very near (k+1)10^a (epsilon machine)
* we increment xk
*/
*xa = (int) ceil(log10(x)) - b ;
*xk = (int) ceil(x/exp10((double) *xa));
xd = (*xk-1)*exp10((double) *xa);
if ( Abs((x-xd)/x) < epsilon ) *xk -= 1;
}
else
{
decompInf(-x,xk,xa,b);
*xk = -(*xk);
}
}
}
/*
* soit x > 0 alors il existe un unique couple
* (k,a) dans NxZ avec k in [10^(b-1),10^b-1] tel que
* (k)*10^a <= x < (k+1) 10^a
* donne par
* a = floor(log10(x))-b+1 et k = floor(x/10^a)
* decompInf renvoit xk=k et xa=a
* si x < 0 alors decompInf(x,xk,xa,b)
* s'obtient par decompSup(-x,xk,xa,b) et xk=-xk
*/
static void decompInf(x, xk, xa, b)
double x;
integer *xk;
integer *xa;
integer b;
{
if ( x == 0.0 )
{
*xk=0 ; *xa= 1; /* jpc */
}
else
{
if ( x > 0 )
{
double xup;
static double epsilon;
static int first=0;
if ( first == 0) { epsilon = 10.0*F2C(dlamch)("e",1L); first++ ;}
*xa = (int) floor(log10(x)) -b +1 ;
*xk = (int) floor(x/exp10((double) *xa));
/* if x is very near (k+1)10^a (epsilon machine)
* we increment xk
*/
xup = (*xk+1)*exp10((double) *xa);
if ( Abs((x-xup)/x) < epsilon ) *xk += 1;
}
else
{
decompSup(-x,xk,xa,b);
*xk = -(*xk);
}
}
}
#ifdef TEST
main()
{
double xmin=1.0,xmax=1.0,eps=1.e-10,xi,xa;
integer n1,n2,n,i;
int kminr,kmaxr,ar;
xmin=0.0;
xmax=1.006;
C2F(graduate)(&xmin,&xmax,&xi,&xa,&n1,&n2,&kminr,&kmaxr,&ar) ;
fprintf(stderr,"test, [%f,%f,%d,%d]\n",xi,xa,n1,n2);
}
C2F(dr)() {} ;
#endif
|