sample.c
Go to the documentation of this file.
1 #include "sample.h"
2 
3 #include "defines.h"
4 #include "geom.h"
5 
6 #include <math.h>
7 
16 void InitSample(unsigned int sID,unsigned int n,double *v,unsigned int boxID,Tsample *s)
17 {
18  unsigned int i;
19 
20  InitVector(sizeof(double),CopyDouble,DeleteDouble,n,&(s->v));
21 
22  for(i=0;i<n;i++)
23  SetVectorElement(i,&(v[i]),&(s->v));
24 
25  s->boxID=boxID;
26  s->sID=sID;
27 }
28 
29 void InitSampleFromBox(unsigned int sID,boolean *systemVar,Tbox *b,unsigned int boxID,Tsample *s)
30 {
31  double *v;
32  unsigned int i,d,k,n;
33 
34  d=GetBoxNIntervals(b);
35  if (systemVar==NULL)
36  n=d;
37  else
38  {
39  n=0;
40  for(i=0;i<d;i++)
41  {
42  if (systemVar[i]) n++;
43  }
44  }
45 
46  NEW(v,n,double);
47  k=0;
48  for(i=0;i<d;i++)
49  {
50  if ((systemVar==NULL)||(systemVar[i]))
51  {
52  v[k]=IntervalCenter(GetBoxInterval(i,b));
53  k++;
54  }
55  }
56 
57  InitSample(sID,n,v,boxID,s);
58 
59  free(v);
60 }
61 
62 void CopySample(void *s_dst,void *s_src)
63 {
64  CopyVector(&(((Tsample *)s_dst)->v),&(((Tsample *)s_src)->v));
65  ((Tsample *)s_dst)->boxID=((Tsample *)s_src)->boxID;
66  ((Tsample *)s_dst)->sID=((Tsample *)s_src)->sID;
67 }
68 
69 unsigned int GetSampleDim(Tsample *s)
70 {
71  return(VectorSize(&(s->v)));
72 }
73 
75 {
76  return(&(s->v));
77 }
78 
79 double GetSampleValue(unsigned int n,Tsample *s)
80 {
81  double *v;
82 
83  v=(double *)GetVectorElement(n,&(s->v));
84 
85  if (v==NULL)
86  Error("Non-defined element in GetSampleValue");
87 
88  return(*v);
89 }
90 
91 unsigned int GetSampleBoxID(Tsample *s)
92 {
93  return(s->boxID);
94 }
95 
96 unsigned int GetSampleID(Tsample *s)
97 {
98  return(s->sID);
99 }
100 
101 void SetSampleBoxID(unsigned int boxID,Tsample *s)
102 {
103  s->boxID=boxID;
104 }
105 
107 {
108  unsigned int i,n1,n2;
109  double d,r;
110 
111  n1=GetSampleDim(s1);
112  n2=GetSampleDim(s2);
113  if (n1!=n2)
114  Error("Samples of different dimensionality in GetSampleDistance");
115 
116  d=0.0;
117  for(i=0;i<n1;i++)
118  {
119  r=GetSampleValue(i,s1)-GetSampleValue(i,s2);
120  d+=(r*r);
121  }
122 
123  return(sqrt(d));
124 }
125 
126 void PrintSample(FILE *f,Tsample *s)
127 {
128  unsigned int i,n;
129  double v;
130 
131  n=VectorSize(&(s->v));
132 
133  for(i=0;i<n;i++)
134  {
135  v=*((double *)GetVectorElement(i,&(s->v)));
136  PrintReal(f,v);
137  fprintf(f," ");
138  }
139  fprintf(f,"\n");
140 }
141 
142 void SaveSample(FILE *f,Tsample *s)
143 {
144  fprintf(f,"%u %u %u ",s->sID,s->boxID,VectorSize(&(s->v)));
145  PrintSample(f,s);
146 }
147 
148 void LoadSample(FILE *f,Tsample *s)
149 {
150  unsigned int i,n;
151  double v;
152 
153  fscanf(f,"%u %u %u",&(s->sID),&(s->boxID),&n);
154 
155  InitVector(sizeof(double),CopyDouble,DeleteDouble,n,&(s->v));
156 
157  for(i=0;i<n;i++)
158  {
159  fscanf(f,"%lf",&v);
160  SetVectorElement(i,&v,&(s->v));
161  }
162 }
163 
164 void DeleteSample(void *s)
165 {
166  DeleteVector(&(((Tsample *)s)->v));
167 }
A sample.
Definition: sample.h:38
unsigned int sID
Definition: sample.h:39
void DeleteVector(void *vector)
Destructor.
Definition: vector.c:389
Tinterval * GetBoxInterval(unsigned int n, Tbox *b)
Returns a pointer to one of the intervals defining the box.
Definition: box.c:270
Definition of basic functions.
unsigned int VectorSize(Tvector *vector)
Gets the number of elements in a vector.
Definition: vector.c:173
Tvector v
Definition: sample.h:42
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:385
void * GetVectorElement(unsigned int i, Tvector *vector)
Returns a pointer to a vector element.
Definition: vector.c:270
double GetSampleValue(unsigned int n, Tsample *s)
Gets the value for a given dimension of the sample.
Definition: sample.c:79
void PrintSample(FILE *f, Tsample *s)
Prints a sample.
Definition: sample.c:126
void CopyVector(Tvector *v_dst, Tvector *v_src)
Copy constructor.
Definition: vector.c:138
unsigned int boxID
Definition: sample.h:40
void InitSample(unsigned int sID, unsigned int n, double *v, unsigned int boxID, Tsample *s)
Constructor.
Definition: sample.c:16
void InitSampleFromBox(unsigned int sID, boolean *systemVar, Tbox *b, unsigned int boxID, Tsample *s)
Constructor.
Definition: sample.c:29
void LoadSample(FILE *f, Tsample *s)
Constructor.
Definition: sample.c:148
void Error(const char *s)
General error function.
Definition: error.c:80
void CopySample(void *s_dst, void *s_src)
Constructor.
Definition: sample.c:62
unsigned int GetBoxNIntervals(Tbox *b)
Box dimensionality.
Definition: box.c:1016
Tvector * GetSampleValues(Tsample *s)
Gets the values of the sample.
Definition: sample.c:74
void DeleteDouble(void *a)
Destructor for doubles.
Definition: vector.c:44
unsigned int GetSampleBoxID(Tsample *s)
Gets the identifier of the box that includes the sample.
Definition: sample.c:91
Definitions of constants and macros used in several parts of the cuik library.
void SetSampleBoxID(unsigned int boxID, Tsample *s)
Changes the identifier of the box to which the sample is linked.
Definition: sample.c:101
void InitVector(unsigned int ele_size, void(*Copy)(void *, void *), void(*Delete)(void *), unsigned int max_ele, Tvector *vector)
Constructor.
Definition: vector.c:100
void SetVectorElement(unsigned int i, void *e, Tvector *vector)
Adds an element to the vector in a given position.
Definition: vector.c:238
void PrintReal(FILE *f, double r)
Pretty print a real number.
Definition: geom.c:745
A generic vector.
Definition: vector.h:227
unsigned int GetSampleDim(Tsample *s)
Returns the dimensionality of the space where the sample is defined.
Definition: sample.c:69
double GetSampleDistance(Tsample *s1, Tsample *s2)
Computes the Euclidean distance between two samples.
Definition: sample.c:106
A box.
Definition: box.h:83
void SaveSample(FILE *f, Tsample *s)
Saves the information of the sample in a file.
Definition: sample.c:142
double IntervalCenter(Tinterval *i)
Gets the interval center.
Definition: interval.c:129
Definition of the Tsample type and the associated functions.
unsigned int GetSampleID(Tsample *s)
Gets the identifier of the sample.
Definition: sample.c:96
void DeleteSample(void *s)
Destructor.
Definition: sample.c:164
void CopyDouble(void *a, void *b)
Copy constructor for doubles.
Definition: vector.c:39