//------------------------------------------------------ // module : IFT2425_H25_Intra_Prog.c // author : Mignotte Max // date : H25 // version : 1.0 // language: C++ // note : //------------------------------------------------------ // //Pour Compiler: //g++ -o IFT2425_H25_Intra_Prog IFT2425_H25_Intra_Prog.c -lm -L/usr/X11R6/lib -lX11 // //------------------------------------------------ // FICHIERS INCLUS ------------------------------- //------------------------------------------------ #include #include #include #include #include #include #include //------------------------------------------------ // DEFINITIONS ----------------------------------- //------------------------------------------------ #define TROIS 3 #define PI 3.1415926535897 #define SQUARE(X) ((X)*(X)) #define CARRE(X) ((X)*(X)) #define MAX(i,j) ((i)>(j)?(i):(j)) #define MIN(i,j) ((j)>(i)?(i):(j)) //------------------------- //--- Windows ------------- //------------------------- #include Display *display; int screen_num; int depth; Window root; Visual* visual; GC gc; /************************************************************************/ /* OPEN_DISPLAY() */ /************************************************************************/ int open_display() { if ((display=XOpenDisplay(NULL))==NULL) { printf("Connection impossible\n"); return(-1); } else { screen_num=DefaultScreen(display); visual=DefaultVisual(display,screen_num); depth=DefaultDepth(display,screen_num); root=RootWindow(display,screen_num); return 0; } } /************************************************************************/ /* FABRIQUE_WINDOW() */ /* Cette fonction crée une fenetre X et l'affiche à l'écran. */ /************************************************************************/ Window fabrique_window(char *nom_fen,int x,int y,int width,int height,int zoom) { Window win; XSizeHints size_hints; XWMHints wm_hints; XClassHint class_hints; XTextProperty windowName, iconName; char *name=nom_fen; if(zoom<0) { width/=-zoom; height/=-zoom; } if(zoom>0) { width*=zoom; height*=zoom; } win=XCreateSimpleWindow(display,root,x,y,width,height,1,0,255); size_hints.flags=PPosition|PSize|PMinSize; size_hints.min_width=width; size_hints.min_height=height; XStringListToTextProperty(&name,1,&windowName); XStringListToTextProperty(&name,1,&iconName); wm_hints.initial_state=NormalState; wm_hints.input=True; wm_hints.flags=StateHint|InputHint; class_hints.res_name=nom_fen; class_hints.res_class=nom_fen; XSetWMProperties(display,win,&windowName,&iconName, NULL,0,&size_hints,&wm_hints,&class_hints); gc=XCreateGC(display,win,0,NULL); XSelectInput(display,win,ExposureMask|KeyPressMask|ButtonPressMask| ButtonReleaseMask|ButtonMotionMask|PointerMotionHintMask| StructureNotifyMask); XMapWindow(display,win); return(win); } /****************************************************************************/ /* CREE_XIMAGECOUL() */ /****************************************************************************/ XImage* cree_XimageCoul(float*** matRVB,int z,int length,int width) { int i; int lgth,wdth,lig,col,zoom_col,zoom_lig; float sum[3]; unsigned char pixR,pixV,pixB; unsigned char* dat; XImage* imageX; /*Zoom positif*/ if (z>0) { lgth=length*z; wdth=width*z; dat=(unsigned char*)malloc(lgth*(wdth*4)*sizeof(unsigned char)); if (dat==NULL) { printf("Impossible d'allouer de la memoire."); exit(-1); } for(lig=0;lig0) { lgth=length*z; wdth=width*z; dat=(unsigned char*)malloc(lgth*(wdth*4)*sizeof(unsigned char)); if (dat==NULL) { printf("Impossible d'allouer de la memoire."); exit(-1); } for(lig=0;lig au format pgm //---------------------------------------------------------- void SaveImagePgm(char* bruit,char* name,float** mat,int lgth,int wdth) { int i,j; char buff[300]; FILE* fic; //--extension-- strcpy(buff,bruit); strcat(buff,name); strcat(buff,".pgm"); //--ouverture fichier-- fic=fopen(buff,"wb"); if (fic==NULL) { printf("Probleme dans la sauvegarde de %s",buff); exit(-1); } printf("\n Sauvegarde de %s au format pgm\n",buff); //--sauvegarde de l'entete-- fprintf(fic,"P5"); fprintf(fic,"\n# IMG Module"); fprintf(fic,"\n%d %d",wdth,lgth); fprintf(fic,"\n255\n"); //--enregistrement-- for(i=0;i au format ppm //---------------------------------------------------------- void SaveImagePpm(char* Path_out,char* Name,float*** matrvb,int wdth,int lgth) { int i,j; char buff[200]; FILE* fuser; //extension strcpy(buff,Path_out); strcat(buff,Name); strcat(buff,".ppm"); //ouverture fichier fuser=fopen(buff,"w"); if (fuser==NULL) { printf(" probleme dans la sauvegarde de %s",buff); exit(-1); } //affichage printf("\n >> Sauvegarde dans -> %s au format %s",buff,".ppm"); fflush(stdout); //sauvegarde de l'entete fprintf(fuser,"P6"); fprintf(fuser,"\n# IMG Module"); fprintf(fuser,"\n%d %d",lgth,wdth); fprintf(fuser,"\n255\n"); //enregistrement for(i=0;i0)&&(pix_x0)&&(pix_y0)&&(pix_x0)&&(pix_y0)&&(pix_x0)&&(pix_y0)&&(pix_x0)&&(pix_yif (1) //-------------------------------------- //-------------------------------------- if (0) { float Val=10.0/9.0; for(int k=0;k<25;k++) { printf("\n %f",Val); Val=(Val-1.0)*10.0; } } //-------------------------------------- //--Question II.1 //>if (1) //-------------------------------------- //-------------------------------------- if (1) { printf("\n Entrez une valeur flottante: "); scanf("%f",&Val); Eps=1.0; for(k=0;;k++) { Eps/=1.0000001; if ((Val+Eps)==Val) break; } printf(" En flottant, [%.20f] sera considéré negligeable (nulle) une fois additioné à: [%.5f]\n",Eps,Val); Eps=Val; Val=1.0; for(k=0;;k++) { Val*=1.0000001; if ((Val+Eps)==Val) break; } printf(" et lui même sera négligable (nulle) lorsqu'il sera additionné avec: [%.5f]\n\n",Val); } //-------------------------------------- //--Question II.2 //>if (1) //-------------------------------------- //-------------------------------------- if (0) { //--------------------------- // Monstre Numérique //--------------------------- int l; int MidImg=length/2; flag_graph=1; float x,y; float x2,y2; double xx,yy; long double xxx,yyy; float x_inf,x_sup,y_inf,y_sup,x_scale,y_scale=(y_sup-y_inf)/length; double xx_inf,xx_sup,yy_inf,yy_sup,xx_scale,yy_scale=(yy_sup-yy_inf)/length; long double xxx_inf,xxx_sup,yyy_inf,yyy_sup,xxx_scale,yyy_scale=(yyy_sup-yyy_inf)/length; //=======// //==(a)==// //=======// //=============================================================== // 1.0-sin(x) EQV. cos^2(x)/(1+sin(x)) [Voisinage de x=PI/2] //=============================================================== if (1) { x_inf=(PI/2.0)-(1.0*10E-4); x_sup=(PI/2.0)+(1.0*10E-4); y_inf=-(1.0*10E-8); y_sup=+(1.0*10E-7); x_scale=(x_sup-x_inf)/width; y_scale=(y_sup-y_inf)/length; //Plot for(x=x_inf,xx=x_inf,l=0;l<=(20*width);l++) { //[Float]-Original_Expression__[BLACK] x=x+(0.05*x_scale); y=1.0-sin(x); printf("\n[%.20f %.20f]",x,y); PrintInTab3D(Graph3D,length,width,x,y,x_inf,x_sup,y_inf,y_sup); //Monstre //[Float]-Modified-Equivalent-Expression__[RED] xx=xx+(0.05*x_scale); y2=CARRE(cos(x))/(1+sin(x)); PrintInTab3DRed(Graph3D,length,width,xx,y2,x_inf,x_sup,y_inf,y_sup); //[Double]-Original-Expression (eqv. Analytique Expresion)__[BLUE] yy=1.0-sinl(xx); printf("\n[%.20f %.20f]",x,y); PrintInTab3DBlue(Graph3Db,length,width,xx,yy,x_inf,x_sup,y_inf,y_sup); printf(" TRUE: [%.20f %.20f]",xx,yy); } //VraiGraphe DisplayAxe(Graph3D,length,width); DisplayAxe(Graph3Db,length,width); } //=======// //==(b)==// //=======// //=============================================================== // ln(cos(x))-ln(sin(x)) EQV. ln(tg(x)) [Voisinage de x=(pi/4)] //=============================================================== if (0) { x_inf=(PI/4.0)-(1.0*10E-5); x_sup=(PI/4.0)+(1.0*10E-5); y_inf=-(1.0*10E-5); y_sup=+(1.0*10E-5); x_scale=(x_sup-x_inf)/width; y_scale=(y_sup-y_inf)/length; //Plot for(x=x_inf,xx=x_inf,l=0;l<=(20*width);l++) { //[Float]-Original_Expression__[BLACK] x=x+(0.05*x_scale); y=(logf(sin(x)))-(logf(cos(x))); printf("\n[%.20f %.20f]",x,y); PrintInTab3D(Graph3D,length,width,x,y,x_inf,x_sup,y_inf,y_sup); //Monstre //[Float]-Modified-Equivalent-Expression__[RED] xx=xx+(0.05*x_scale); y2=logf(tanf(x)); PrintInTab3DRed(Graph3D,length,width,xx,y2,x_inf,x_sup,y_inf,y_sup); //[Double]-Original-Expression (eqv. Analytique Expresion)__[BLUE] yy=(logl(sin(x)))-(logl(cos(x))); printf("\n[%.20f %.20f]",x,y); PrintInTab3DBlue(Graph3Db,length,width,xx,yy,x_inf,x_sup,y_inf,y_sup); printf(" TRUE: [%.20f %.20f]",xx,yy); } //VraiGraphe DisplayAxe(Graph3D,length,width); DisplayAxe(Graph3Db,length,width); } //=======// //==(c)==// //=======// //======================================================================= // 1.0/((sqrt(x^2+1) - x) EQV. (sqrt(x^2+1) + x) [Voisinage de infty] //======================================================================= if (0) { x_inf=(+0.5*10E+1); x_sup=(+0.5*10E+3); y_inf=(+0.25*10E+1); y_sup=(+0.5*10E+3); x_scale=(x_sup-x_inf)/width; y_scale=(y_sup-y_inf)/length; //Plot for(x=x_inf,xx=x_inf,l=0;l<=(20*width);l++) { //[Float]-Original_Expression__[BLACK] x=x+(0.05*x_scale); y=1.0/(sqrt(CARRE(x)+1)-x); printf("\n[%.20f %.20f]",x,y); PrintInTab3D(Graph3D,length,width,x,y,x_inf,x_sup,y_inf,y_sup); //Monstre //[Float]-Modified-Equivalent-Expression__[RED] xx=xx+(0.05*x_scale); y2=(sqrt(CARRE(x)+1)+x); PrintInTab3DRed(Graph3D,length,width,xx,y2,x_inf,x_sup,y_inf,y_sup); //[Double]-Original-Expression (eqv. Analytique Expresion)__[BLUE] yy=1.0/(sqrt(CARRE(xx)+1)-xx); PrintInTab3DBlue(Graph3Db,length,width,xx,yy,x_inf,x_sup,y_inf,y_sup); printf(" TRUE: [%.20f %.20f]",xx,yy); } //VraiGraphe //DisplayAxe(Graph3D,length,width); //DisplayAxe(Graph3Db,length,width); } //=======// //==(d)==// //=======// //===================================================== // e^(x) - e ^(-x) EQV. 2sinhf (x) [Voisinage de 0] //===================================================== if (0) { x_inf=-0.2*(1.0*10E-7); x_sup=0.2*(1.0*10E-7); y_inf=-0.2*(1.0*10E-7); y_sup=0.2*(1.0*10E-7); x_scale=(x_sup-x_inf)/width; y_scale=(y_sup-y_inf)/length; //Plot for(x=x_inf,xx=x_inf,l=0;l<=(20*width);l++) { //[Float]-Original_Expression__[BLACK] x=x+(0.05*x_scale); y=exp(x)-exp(-x); //printf("\n[%.20f %.20f]",x,y); PrintInTab3D(Graph3D,length,width,x,y,x_inf,x_sup,y_inf,y_sup); //Monstre //[Float]-Modified-Equivalent-Expression__[RED] xx=xx+(0.05*x_scale); y2=2*sinhf(x); PrintInTab3DRed(Graph3D,length,width,xx,y2,x_inf,x_sup,y_inf,y_sup); //[Double]-Original-Expression (eqv. Analytique Expresion)__[BLUE] yy=exp(xx)-exp(-xx); PrintInTab3DBlue(Graph3Db,length,width,xx,yy,x_inf,x_sup,y_inf,y_sup); } //VraiGraphe DisplayAxe(Graph3D,length,width); DisplayAxe(Graph3Db,length,width); } } //-------------------------------------- //--Question II.3 //>if (1) //-------------------------------------- //-------------------------------------- if (0) { int i,j,k; int NbCells; float SumFloat; float tmp; double SumDouble; long double SumLongDouble; SumFloat=0.0; SumDouble=0.0; SumLongDouble=0.0; if (argc>1) NbCells=MAX(1,atoi(argv[1])); else NbCells=500; float* x=fmatrix_allocate_1d(NbCells); float* y=fmatrix_allocate_1d(NbCells); for(k=0;ky[j+1]) { tmp=y[j+1]; y[j+1]=y[j]; y[j]=tmp; } for(k=0;k>%.30f",k,SumFloat); printf("\n [NbCells:%d Doubl]>>%.30f",k,SumDouble); printf("\n\n"); //-[2]- printf("\n ============================"); printf("\n Classé par ordre décroissant"); printf("\n ============================"); //--Float for(k=(NbCells-1);k>=0;k--) { if (k!=(NbCells-1)) SumFloat*=y[k]; else SumFloat=y[(NbCells-1)]; } //--Double for(k=(NbCells-1);k>=0;k--) { if (k!=(NbCells-1)) SumDouble*=y[k]; else SumDouble=y[(NbCells-1)]; } printf("\n [NbCells:%d Float]>>%.30f",k,SumFloat); printf("\n [NbCells:%d Doubl]>>%.30f",k,SumDouble); printf("\n\n"); //-[3]- printf("\n ==========="); printf("\n Deux-a-Deux"); printf("\n ==========="); //--Float for(k=0;k>%.30f",k,SumFloat); printf("\n [NbCells:%d LDoub]>>%.30Lf",k,SumLongDouble); printf("\n\n"); //-[4]- printf("\n ======="); printf("\n Mélangé"); printf("\n ======="); //--Float for(k=0;k>%.30f",k,SumFloat); printf("\n [NbCells:%d LDoub]>>%.30Lf",k,SumLongDouble); printf("\n\n"); //-[5]- printf("\n ========"); printf("\n Log(Exp)"); printf("\n ========"); //--Float for(k=0;k>%.30f",k,SumFloat); //printf("\n [NbCells:%d Doubl]>>%.30f",k,SumDouble); printf("\n [NbCells:%d Ground-Truth]>>%.30Lf",k,SumLongDouble); printf("\n\n"); } //-------------------------------------- //--Question III.4 //>if (1) //-------------------------------------- //-------------------------------------- if (0) { int k; double NumA,NumB,DenA,DenB,DenC; double x=1.5; //Loop-HouseHolder printf("\n\n >HouseHolder"); for(k=0;k<4;k++) { printf("\n [%d::%.10f]",k,x); NumA=(log(x)-exp(-x)); NumB=((1.0/x)+exp(-x)); DenA=NumA; DenB=( (-1.0/CARRE(x)) -exp(-x) ); DenC=NumB; //printf("\n Num::[%f | %f]",NumA,NumB); //printf("\n Den::[%f | %f | %f]",DenA,DenB,DenC); x=x - ( (2.0*NumA*NumB)/( -(DenA*DenB) + 2.0* CARRE(DenC)) ); } printf("\n\n\n\n"); } //-------------------------------------------------------------------------------- //---------------- visu sous XWINDOW --------------------------------------------- //-------------------------------------------------------------------------------- if (flag_graph) { //ouverture session graphique if (open_display()<0) printf(" Impossible d'ouvrir une session graphique"); sprintf(nomfen_ppicturebRVB,"Graphe :"); sprintf(nomfen_ppictureRVB,"Graphe :"); win_ppicturebRVB=fabrique_window(nomfen_ppicturebRVB,10,10,width,length,zoom); win_ppictureRVB=fabrique_window(nomfen_ppictureRVB,10,10,width,length,zoom); x_ppicturebRVB=cree_XimageCoul(Graph3Db,zoom,length,width); x_ppictureRVB=cree_XimageCoul(Graph3D,zoom,length,width); //Sauvegarde SaveImagePpm((char*)"",(char*)"Graph3D",Graph3D,length,width); SaveImagePpm((char*)"",(char*)"Graph3Db",Graph3Db,length,width); printf("\n\n Pour quitter,appuyer sur la barre d'espace"); fflush(stdout); //boucle d'evenements for(;;) { XNextEvent(display,&ev); switch(ev.type) { case Expose: XPutImage(display,win_ppictureRVB,gc,x_ppictureRVB,0,0,0,0,x_ppictureRVB->width,x_ppictureRVB->height); XPutImage(display,win_ppicturebRVB,gc,x_ppicturebRVB,0,0,0,0,x_ppicturebRVB->width,x_ppicturebRVB->height); break; case KeyPress: XDestroyImage(x_ppictureRVB); XDestroyImage(x_ppicturebRVB); XFreeGC(display,gc); XCloseDisplay(display); flag_graph=0; break; } if (!flag_graph) break; } } //retour sans probleme printf("\n Fini... \n\n\n"); return 0; }