-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealdie.cpp
395 lines (369 loc) · 12 KB
/
realdie.cpp
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
/*============================================================================
* Daniel J. Greenhoe
* routines for Real Die rdieseqs
*============================================================================*/
/*=====================================
* headers
*=====================================*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "main.h"
#include "symseq.h"
#include "r1.h"
#include "r2.h"
#include "r3.h"
#include "r4.h"
#include "r6.h"
#include "c1.h"
#include "euclid.h"
#include "larc.h"
#include "die.h"
#include "realdie.h"
/*-------------------------------------------------------------------------
* display real die metric table
*-------------------------------------------------------------------------*/
int rdieseq::metrictbl(void){
char a,b;
for(a='A';a<='F';a++){
for(b='A';b<='F';b++)printf("d(%c,%c)=%.1lf ",a,b,rdie_metric(a,b));
printf("\n");
}
return 1;
}
/*-------------------------------------------------------------------------
* autocorrelation Rxx of a real die seqR1 x with 2N offset
*-------------------------------------------------------------------------*/
int rdieseq::Rxxo(seqR1 *rxx, const int showcount){
const long N=getN();
int rval;
rval=Rxx(rxx,showcount);
rxx->add(2*N);
return rval;
}
/*-------------------------------------------------------------------------
* autocorrelation Rxx of a real die seqR1 x
*-------------------------------------------------------------------------*/
int rdieseq::Rxx(seqR1 *rxx, const int showcount){
long m;
const long N=getN();
int rval=0;
double rxxm;
if(showcount)fprintf(stderr," Calculate %ld auto-correlation values ... n=",2*N+1);
for(m=-N;m<=N;m++){
if(showcount)fprintf(stderr,"%8ld",m+N);
rxxm=Rxx(m);
if(rxxm>0)rval=-1;
rxx->put(m+N,rxxm);
if(showcount)fprintf(stderr,"\b\b\b\b\b\b\b\b");
}
if(showcount)fprintf(stderr,"%8ld .... done.\n",m+N);
return rval;
}
/*-------------------------------------------------------------------------
* autocorrelation Rxx(m)
*-------------------------------------------------------------------------*/
double rdieseq::Rxx(const long m){
const long mm=labs(m);
const long N=getN();
long n,nmm;
double d,sum;
char a,b;
for(n=0,sum=0;n<(N+mm);n++){
nmm=n-mm;
a=(n <0 || n >=N)? 0.0 : get(n);
b=(nmm<0 || nmm>=N)? 0.0 : get(nmm);
d=(a==0 || b==0)? 1.0 : rdie_metric(a,b);
sum+=d;
}
return -sum;
}
/*=====================================
* operators
*=====================================*/
/*-------------------------------------------------------------------------
* operator rdieseq x = dieseq y
*-------------------------------------------------------------------------*/
void rdieseq::operator=(dieseq y){
long n;
const long N=getN();
const long M=y.getN();
char symbol;
if(N!=M){
fprintf(stderr,"ERROR using rdieseq x = rdieseq y operation: size of x (%ld) does not equal size of y (%ld)\n",N,M);
exit(EXIT_FAILURE);
}
for(n=0;n<N;n++){
symbol=y.get(n);
put(n,symbol);
}
}
/*=====================================
* external operations
*=====================================*/
/*-------------------------------------------------------------------------
* map die face values to R^1
* A-->1 B-->2 C-->3 D-->4 E-->5 F-->6
*-------------------------------------------------------------------------*/
int rdie_dietoR1(char c){
int n,rval;
char domain[6]={'A','B','C','D','E','F'};
for(n=0,rval=-1;n<6;n++)if(c==domain[n])rval=n;
if(rval==-1){
fprintf(stderr,"ERROR using rdie_dietoR1(char c): c=%c(0x%x) is not in the valid domain {0,A,B,C,D,E,F}\n",c,c);
exit(EXIT_FAILURE);
}
return rval;
}
/*-------------------------------------------------------------------------
* map die face values to R^3
* |+1| | 0| | 0| | 0| | 0| |-1| | 0|
* A-->| 0| B-->|+1| C-->| 0| D-->| 0| E-->|-1| F-->| 0| 0-->| 0|
* | 0| | 0| |+1| |-1| | 0| | 0| | 0|
*-------------------------------------------------------------------------*/
vectR3 rdie_dietoR3(char c){
vectR3 xyz;
switch(c){
case 'A': xyz.put(+1, 0, 0); break;
case 'B': xyz.put( 0,+1, 0); break;
case 'C': xyz.put( 0, 0,+1); break;
case 'D': xyz.put( 0, 0,-1); break;
case 'E': xyz.put( 0,-1, 0); break;
case 'F': xyz.put(-1, 0, 0); break;
default:
fprintf(stderr,"ERROR using rdie_dietoR3(char c): c=%c(0x%x) is not in the valid domain {A,B,C,D,E,F}\n",c,c);
exit(EXIT_FAILURE);
}
return xyz;
}
/*-------------------------------------------------------------------------
* map R^3 values to die face values using Lagrange Arc metric
*-------------------------------------------------------------------------*/
rdieseq rdie_R3todie_larc(seqR3 xyz){
long n;
int m;
long N=xyz.getN();
double d[7];
double smallestd;
char closestface;
vectR3 p,q[7];
rdieseq rdie(N);
//q[0].put(0,0,0);
q[1]=rdie_dietoR3('A');
q[2]=rdie_dietoR3('B');
q[3]=rdie_dietoR3('C');
q[4]=rdie_dietoR3('D');
q[5]=rdie_dietoR3('E');
q[6]=rdie_dietoR3('F');
for(n=0; n<N; n++){
p.put(xyz.getx(n),xyz.gety(n),xyz.getz(n));
smallestd=larc_metric(p,q[1]);
closestface='A';
for(m=2;m<7;m++){
d[m] = larc_metric(p,q[m]);
if(((m&0x01) && (d[m]<smallestd)) || ((!(m&0x01)) && (d[m]<=smallestd))){
//---------------------------- ----------------------------------
// bias odd samples bias even samples
// towards smaller values towards larger values
smallestd=d[m];
closestface='A'+m-1;
}
}
rdie.put(n,closestface);
}
return rdie;
}
/*-------------------------------------------------------------------------
* map R^3 values to die face values and (0,0,0) using Lagrange Arc metric
*-------------------------------------------------------------------------*/
rdieseq rdie_R3todie0_larc(seqR3 xyz){
long n;
int m;
long N=xyz.getN();
double d[7];
double smallestd;
char closestface;
vectR3 p,q[7];
rdieseq rdie(N);
q[0].put(0,0,0);
q[1]=rdie_dietoR3('A');
q[2]=rdie_dietoR3('B');
q[3]=rdie_dietoR3('C');
q[4]=rdie_dietoR3('D');
q[5]=rdie_dietoR3('E');
q[6]=rdie_dietoR3('F');
for(n=0; n<N; n++){
p.put(xyz.getx(n),xyz.gety(n),xyz.getz(n));
smallestd=larc_metric(p,q[0]);
//smallestd=ae_metric(1,p,q[0]);
closestface='0';
for(m=1;m<7;m++){
d[m] = larc_metric(p,q[m]);
if(((m&0x01) && (d[m]<smallestd)) || ((!(m&0x01)) && (d[m]<=smallestd))){
//---------------------------- ----------------------------------
// bias odd samples bias even samples
// towards smaller values towards larger values
smallestd=d[m];
closestface='A'+m-1;
}
}
rdie.put(n,closestface);
}
return rdie;
}
/*-------------------------------------------------------------------------
* map R^3 values to die face values using Euclidean metric
* 0 A B C D E F A+..+F
*-------------------------------------------------------------------------*/
rdieseq rdie_R3todie_euclid(seqR3 xyz){
long n;
int m;
long N=xyz.getN();
double d[7];
double smallestd;
char closestface;
vectR3 p,q[7];
rdieseq rdie(N);
q[1]=rdie_dietoR3('A');
q[2]=rdie_dietoR3('B');
q[3]=rdie_dietoR3('C');
q[4]=rdie_dietoR3('D');
q[5]=rdie_dietoR3('E');
q[6]=rdie_dietoR3('F');
for(n=0; n<N; n++){
p.put(xyz.getx(n),xyz.gety(n),xyz.getz(n));
smallestd=ae_metric(1,p,q[1]);
closestface='A';
for(m=2;m<=6;m++){
d[m] = ae_metric(1,p,q[m]);
if(((m&0x01) && (d[m]<smallestd)) || ((!(m&0x01)) && (d[m]<=smallestd))){
//---------------------------- ----------------------------------
// bias towards smaller values bias towards larger values
smallestd=d[m];
closestface='A'+m-1;
}
}
rdie.put(n,closestface);
}
return rdie;
}
/*-------------------------------------------------------------------------
* map R^3 values to die face and (0,0,0) values using Euclidean metric
* 0 A B C D E F A+..+F
*-------------------------------------------------------------------------*/
rdieseq rdie_R3todie0_euclid(seqR3 xyz){
long n;
int m;
long N=xyz.getN();
double d[7];
double smallestd;
char closestface;
vectR3 p,q[7];
rdieseq rdie(N);
q[0]=rdie_dietoR3('0');
q[1]=rdie_dietoR3('A');
q[2]=rdie_dietoR3('B');
q[3]=rdie_dietoR3('C');
q[4]=rdie_dietoR3('D');
q[5]=rdie_dietoR3('E');
q[6]=rdie_dietoR3('F');
for(n=0; n<N; n++){
p.put(xyz.getx(n),xyz.gety(n),xyz.getz(n));
smallestd=ae_metric(1,p,q[0]);
closestface='0';
for(m=1;m<=6;m++){
d[m] = ae_metric(1,p,q[m]);
//if(d[m]<smallestd)
//if(d[m]<=smallestd)
//if(((m&0x01) && (d[m]<smallestd)) || ((!(m&0x01)) && (d[m]<smallestd)))
//if(((m&0x01) && (d[m]<=smallestd)) || ((!(m&0x01)) && (d[m]<=smallestd)))
if(((m&0x01) && (d[m]<smallestd)) || ((!(m&0x01)) && (d[m]<=smallestd))){
//---------------------------- ----------------------------------
// bias towards smaller values bias towards larger values
smallestd=d[m];
closestface='A'+m-1;
}
//if(m&0x01){ (alternative coding)
// if(d[m]<smallestd){
// smallestd=d[m];
// closestface='A'+m-1;
// }}
//else
// if(d[m]<=smallestd){
// smallestd=d[m];
// closestface='A'+m-1;
// }
}
rdie.put(n,closestface);
}
return rdie;
}
/*-------------------------------------------------------------------------
* map R^1 values to die face values using Euclidean metric
*-------------------------------------------------------------------------*/
rdieseq rdie_R1todie_euclid(seqR1 xyz){
long n;
long N=xyz.getN();
char closestface;
double p;
rdieseq rdie(N);
for(n=0; n<N; n++){
p = xyz.get(n);
if(p<1.5) closestface='A';
else if(p>=5.5) closestface='F';
else closestface=(char)(p+0.5-1)+'A';
rdie.put(n,closestface);
}
return rdie;
}
/*-------------------------------------------------------------------------
* real die metric d(a,b)
* d(a,b) | 0 A B C D E F (b)
* -------|-------------------------------------
* a= 0| 0 1 1 1 1 1 1
* a= A| 1 0 1 1 1 1 2
* a= B| 1 1 0 1 1 2 1
* a= C| 1 1 1 0 2 1 1
* a= D| 1 1 1 2 0 1 1
* a= E| 1 1 2 1 1 0 1
* a= F| 1 2 1 1 1 1 0
* On success return d(a,b). On error return -1.
*-------------------------------------------------------------------------*/
double rdie_metric(char a, char b){
int ra=rdie_dietoR1(a);
int rb=rdie_dietoR1(b);
double d;
if(ra<0)fprintf(stderr,"a=%c(0x%x) not in domain of rdie metric d(a,b)\n",a,a);
if(rb<0)fprintf(stderr,"b=%c(0x%x) not in domain of rdie metric d(a,b)\n",b,b);
if(ra<0) d=-1;
else if(rb<0) d=-1;
else if(ra==rb) {d=0; }
else if(ra==0) d=1;
else if(rb==0) d=1;
else if(ra+rb==7) d=2;
else d=1;
return d;
}
/*-------------------------------------------------------------------------
* real die metric p(x,y) where x and y are rdie sequences computed as
* p(x,y) = d(x0,y0) + d(x1,y1) + d(x2,y2) + ... + d(x{N-1},y{N-1})
* where d(a,b) is defined above.
* On success return d(x,y). On error return -1.
*-------------------------------------------------------------------------*/
double rdie_metric(rdieseq x, rdieseq y){
double rval,d;
long n;
long N=x.getN();
long M=y.getN();
long NM=(N<M)?M:N; //NM = the larger of N and M
for(n=0,d=0;n<NM;n++){
rval=rdie_metric(x.get(n),y.get(n));
if(rval<0){d+=0.0; printf("rval=%lf ",rval);}
else d+=rval;
}
if(N!=M){
fprintf(stderr,"ERROR using rdie_metric(rdieseq x,rdieseq y): size of x (%ld) does not equal the size of y (%ld).\n",N,M);
exit(EXIT_FAILURE);
}
return d;
}