Home | Develop | Download | Contact
testpdsrv1.c
1 /*
2  * testpdsrv1.c
3  *
4  * Copyright 2011 Fernando Pujaico Rivera <fernando.pujaico.rivera@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  *
21  */
22 
36 #include <stdlib.h>
37 #include <pds/pdsrv.h>
38 
43 #define NUMELEMENTOS PDS_RAND_MAX/1000
44 
45 int pds_octave_plot_pdf(char NombreOctaveFile[],char NombreOctaveDataFile[]);
46 
47 
48 int main(int argc, char** argv)
49 {
50  PdsRvReal x1,x2,x3,x4,x5;
51  PdsRvInteger x6;
52  PdsRvNaturalD xc;
53  unsigned long int i;
54  PdsCongruential *Xc=NULL;
55  PdsUniform *X1=NULL;
56  PdsUniform *X2=NULL;
57  PdsGaussian *X3=NULL;
58  PdsRayleigh *X4=NULL;
59  PdsExponential *X5=NULL;
60  PdsPoisson *X6=NULL;
61  FILE *fd=NULL;
62 
63  Xc=pds_congruential_new(); if(Xc==NULL) return EXIT_FAILURE;
64  X1=pds_uniform_new(1.5,3.5); if(X1==NULL) return EXIT_FAILURE;
65  X2=pds_uniform_new(1.5,3.5); if(X2==NULL) return EXIT_FAILURE;
66  X3=pds_gaussian_new(0.0,1.0); if(X3==NULL) return EXIT_FAILURE;
67  X4=pds_rayleigh_new(1.0); if(X4==NULL) return EXIT_FAILURE;
68  X5=pds_exponential_new(1.0); if(X5==NULL) return EXIT_FAILURE;
69  X6=pds_poisson_new(4.0); if(X6==NULL) return EXIT_FAILURE;
70 
71  pds_octave_plot_pdf("plot_octave_pds.m","prueba.txt");
72 
73  fd=fopen("prueba.txt","w");
74  if(fd==NULL) {printf("ERROR:fopen de datos.\n");return EXIT_FAILURE;}
75 
76  for(i=0;i<NUMELEMENTOS;i++)
77  {
79  pds_uniform_get_value(X1,&x1);
80  pds_uniform_get_value(X2,&x2);
81  pds_gaussian_get_value(X3,&x3);
82  pds_rayleigh_get_value(X4,&x4);
84  pds_poisson_get_value(X6,&x6);
85 
86  fprintf(fd,"%f\t%f\t%f\t%f\t%f\t%f\t%f\n",x1,x2,x3,x4,x5,1.0*x6,1.0*xc);
87  }
88 
89  fclose(fd);
90 
98 
99  return EXIT_SUCCESS;
100 }
101 
102 
111 int pds_octave_plot_pdf(char NombreOctaveFile[],char NombreOctaveDataFile[])
112 {
113  FILE *fd;
114 
115  fd=fopen(NombreOctaveFile,"w");
116  if(fd==NULL) {printf("ERROR:fopen de octave.\n");return EXIT_FAILURE;}
117 
118  fprintf(fd,"X=load(\'%s\')\';\n",NombreOctaveDataFile);
119  fprintf(fd,"intervalos=100;\n");
120  fprintf(fd,"\n");
121  fprintf(fd,"[Nj1 abscisax1]=hist(X(1,:),intervalos);\n");
122  fprintf(fd,"Rango1=max(X(1,:))-min(X(1,:));\n");
123  fprintf(fd,"figure(1);bar(abscisax1,Nj1*intervalos/(Rango1*%lu));\n",NUMELEMENTOS);
124  fprintf(fd,"\n");
125  fprintf(fd,"[Nj2 abscisax2]=hist(X(2,:),intervalos);\n");
126  fprintf(fd,"Rango2=max(X(2,:))-min(X(2,:));\n");
127  fprintf(fd,"figure(2);bar(abscisax2,Nj2*intervalos/(Rango2*%lu));\n",NUMELEMENTOS);
128  fprintf(fd,"\n");
129  fprintf(fd,"Ex1=mean(X(1,:))\n");
130  fprintf(fd,"Ex2=mean(X(2,:))\n");
131  fprintf(fd,"cov_xy=mean(cov((X(1,:)-Ex1),(X(2,:)-Ex1)))\n");
132  fprintf(fd,"\n");
133  fprintf(fd,"[Nj3 abscisax3]=hist(X(3,:),intervalos);\n");
134  fprintf(fd,"Rango3=max(X(3,:))-min(X(3,:));\n");
135  fprintf(fd,"figure(3);bar(abscisax3,Nj3*intervalos/(Rango3*%lu));\n",NUMELEMENTOS);
136  fprintf(fd,"Ex3=mean(X(3,:))\n");
137  fprintf(fd,"E2x3=mean((X(3,:)-Ex3).*(X(3,:)-Ex3))\n");
138  fprintf(fd,"\n");
139  fprintf(fd,"[Nj4 abscisax4]=hist(X(4,:),intervalos);\n");
140  fprintf(fd,"Rango4=max(X(4,:))-min(X(4,:));\n");
141  fprintf(fd,"figure(4);bar(abscisax4,Nj4*intervalos/(Rango4*%lu));\n",NUMELEMENTOS);
142  fprintf(fd,"Ex4=mean(X(4,:))\n");
143  fprintf(fd,"E2x4=mean((X(4,:)-Ex4).*(X(4,:)-Ex4))\n");
144  fprintf(fd,"\n");
145  fprintf(fd,"[Nj5 abscisax5]=hist(X(5,:),intervalos);\n");
146  fprintf(fd,"Rango5=max(X(5,:))-min(X(5,:));\n");
147  fprintf(fd,"figure(5);bar(abscisax5,Nj5*intervalos/(Rango5*%lu));\n",NUMELEMENTOS);
148  fprintf(fd,"Ex5=mean(X(5,:))\n");
149  fprintf(fd,"E2x5=mean((X(5,:)-Ex5).*(X(5,:)-Ex5))\n");
150  fprintf(fd,"\n");
151  fprintf(fd,"[Nj6 abscisax6]=hist(X(6,:),intervalos);\n");
152  fprintf(fd,"Rango6=max(X(6,:))-min(X(6,:));\n");
153  fprintf(fd,"figure(6);bar(abscisax6,Nj6*1.0/(%lu));\n",NUMELEMENTOS);
154  fprintf(fd,"Ex6=mean(X(6,:))\n");
155  fprintf(fd,"E2x6=mean((X(6,:)-Ex6).*(X(6,:)-Ex6))\n");
156  fprintf(fd,"\n");
157  fprintf(fd,"\n");
158  fprintf(fd,"C=min(size(X))\n");
159  fprintf(fd,"[NjC abscisaxC]=hist(X(C,:),intervalos);\n");
160  fprintf(fd,"RangoC=max(X(C,:))-min(X(C,:));\n");
161  fprintf(fd,"figure(C);bar(abscisaxC,NjC*intervalos/(RangoC*%lu));\n",NUMELEMENTOS);
162  fprintf(fd,"\n");
163  fclose(fd);
164 
165  return EXIT_SUCCESS;
166 }
167 
int pds_exponential_get_value(PdsExponential *X, PdsRvReal *x)
Retorna un número pseudo aleatorio exponencial, Exponential(Lambda) .
int pds_gaussian_get_value(PdsGaussian *X, PdsRvReal *x)
Retorna un número pseudo aleatorio gaussiano, N(U,Sigma^2) .
PdsRayleigh * pds_rayleigh_new(PdsRvReal Sigma)
Crea una variable aleatoria de tipo PdsRayleigh.
void pds_poisson_destroy(PdsPoisson **X)
Libera la variable aleatoria de tipo puntero PdsPoisson, y la carga con NULL.
unsigned long PdsRvNaturalD
Definition: pdsrvglobal.h:81
void pds_rayleigh_destroy(PdsRayleigh **X)
Libera la variable aleatoria de tipo puntero PdsRayleigh, y la carga con NULL.
void pds_congruential_destroy(PdsCongruential **X)
Libera la variable secuencia aleatoria de tipo puntero PdsCongruential, y la carga con NULL...
PdsUniform * pds_uniform_new(PdsRvReal A, PdsRvReal B)
Crea una variable aleatoria de tipo PdsUniform.
float PdsRvReal
Definition: pdsrvglobal.h:59
La estructura tipo PdsExponential. Esta estructura genera una variable aleatoria exponencial. Exponential(Lambda) Para usar incluir pdsrv/pdsrv.h. La función de densidad de probabilidad es: fx(x)=lambda e^{-lambda x} , x [0,oo) .
La estructura tipo PdsCongruential. Esta estructura genera una secuencia aleatoria congruential...
PdsCongruential * pds_congruential_new(void)
Crea una secuencia aleatoria de tipo PdsCongruential.
void pds_uniform_destroy(PdsUniform **X)
Libera la variable aleatoria de tipo puntero PdsUniform, y la carga con NULL.
PdsGaussian * pds_gaussian_new(PdsRvReal U, PdsRvReal Sigma)
Crea una variable aleatoria de tipo PdsGaussian.
void pds_exponential_destroy(PdsExponential **X)
Libera la variable aleatoria de tipo puntero PdsExponential, y la carga con NULL. ...
La estructura tipo PdsPoisson. Esta estructura genera una variable aleatoria poissoniana, Poisson(lambda) . Para usar incluir pdsrv/pdsrv.h. La función de densidad de probabilidad es: f_k(k;lambda)= e^{-lambda} lambda^k / k!, k en [0,oo) .
Definition: pdspoisson.h:63
int pds_congruential_get_value(PdsCongruential *X, PdsRvNaturalD *x)
Retorna un número de la secuencia pseudo aleatorio entre [0,PDS_RAND_MAX).
PdsPoisson * pds_poisson_new(PdsRvReal Lambda)
Crea una variable aleatoria de tipo PdsPoisson.
int pds_poisson_get_value(PdsPoisson *X, PdsRvInteger *x)
Retorna un número pseudo aleatorio poissoniano, Poisson(lambda) .
int pds_rayleigh_get_value(PdsRayleigh *X, PdsRvReal *x)
Retorna un número pseudo aleatorio rayleighiano, Rayleighiana(Sigma) .
La estructura tipo PdsUniform. Esta estructura genera una variable aleatoria uniformemente distribuid...
Definition: pdsuniform.h:69
PdsExponential * pds_exponential_new(PdsRvReal Lambda)
Crea una variable aleatoria de tipo PdsExponential.
void pds_gaussian_destroy(PdsGaussian **X)
Libera la variable aleatoria de tipo puntero PdsGaussian, y la carga con NULL.
La estructura tipo PdsGaussian. Esta estructura genera una variable aleatoria gaussiana normalizada...
Definition: pdsgaussian.h:64
int PdsRvInteger
Definition: pdsrvglobal.h:73
La estructura tipo PdsRayleigh. Esta estructura genera una variable aleatoria rayleighiana, Rayleighiana(Sigma) . Para usar incluir pdsrv/pdsrv.h. La función de densidad de probabilidad es: fx(x)=(x/Sigma^2)e^{-x^2/(2 Sigma^2)} x>=0 .
Definition: pdsrayleigh.h:64
int pds_uniform_get_value(PdsUniform *X, PdsRvReal *x)
Retorna un número pseudo aleatorio y uniforme entre [A,B), con PDS_RAND_MAX divisiones de resolución...

Enlaces de interés

HomePage Bazaar Download Bug report Ayuda Developer Feed