-
Notifications
You must be signed in to change notification settings - Fork 0
/
r6.cpp
349 lines (315 loc) · 10.2 KB
/
r6.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
/*============================================================================
* Daniel J. Greenhoe
* normed linear space R^2
*============================================================================*/
/*=====================================
* headers
*=====================================*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<main.h>
#include<r1.h>
#include<r6.h>
/*=====================================
* osix
*=====================================*/
/*-------------------------------------------------------------------------
* osix constructors
*-------------------------------------------------------------------------*/
osix::osix(void){
int i;
for(i=0;i<6;i++)x[i]=0;
}
osix::osix(double u0, double u1, double u2, double u3, double u4, double u5){
x[0]=u0;x[1]=u1;x[2]=u2;x[3]=u3;x[4]=u4;x[5]=u5;
}
osix::osix(double u){
int i;
for(i=0;i<6;i++)x[i]=u;
}
/*-------------------------------------------------------------------------
* osix put member functions
*-------------------------------------------------------------------------*/
void osix::put(double u){
int i;
for(i=0;i<6;i++)x[i]=u;
}
void osix::put(osix u){
int i;
for(i=0;i<6;i++)x[i]=u.get(i);
}
/*-------------------------------------------------------------------------
* return the 6-tuple value
*-------------------------------------------------------------------------*/
osix osix::get(void){
osix u;
int i;
for(i=0;i<6;i++)u.put(i,get(i));
return u;
}
/*-------------------------------------------------------------------------
* return the minimum element of the 6 tupple
*-------------------------------------------------------------------------*/
double osix::min(void){
int i;
double u,min;
min=fabs(x[0]);
for(i=1;i<6;i++){
u=fabs(x[i]);
if(u<min) min=u;
}
return min;
}
/*-------------------------------------------------------------------------
* return the maximum element of the 6 tupple
*-------------------------------------------------------------------------*/
double osix::max(void){
int i;
double u,max;
max=fabs(x[0]);
for(i=1;i<6;i++){
u=fabs(x[i]);
if(u>max) max=u;
}
return max;
}
/*-------------------------------------------------------------------------
* print the tuple
*-------------------------------------------------------------------------*/
void osix::list(const char *str1, const char *str2){
int i;
if(strlen(str1)!=0)printf("%s",str1);
putchar('(');
for(i=0;i<5;i++)printf("%9.6lf,",get(i));
printf("%9.6lf)",get(5));
if(strlen(str2)!=0)printf("%s",str2);
}
/*=====================================
* vectR6 functions
*=====================================*/
/*-------------------------------------------------------------------------
* return the 6-tuple value
*-------------------------------------------------------------------------*/
vectR6 vectR6::get(void){
vectR6 u;
int i;
for(i=0;i<6;i++)u.put(i,get(i));
return u;
}
/*-------------------------------------------------------------------------
* magnitude
*-------------------------------------------------------------------------*/
double vectR6::mag(void){
int i;
double u;
double sum=0;
for(i=0;i<6;i++){
u=get(i);
sum += u*u;
}
return sqrt(sum);
}
/*-------------------------------------------------------------------------
* add a scalar
*-------------------------------------------------------------------------*/
vectR6 vectR6::mpy(double a){
int i;
vectR6 y;
for(i=0;i<6;i++)y.put(get(i)*a);
return y;
}
/*-------------------------------------------------------------------------
* operator: +=
*-------------------------------------------------------------------------*/
void vectR6::operator+=(vectR6 q){
vectR6 p=get();
p = p+q;
put(p);
}
/*-------------------------------------------------------------------------
* operator: -=
*-------------------------------------------------------------------------*/
void vectR6::operator-=(vectR6 q){
vectR6 p=get();
p = p-q;
put(p);
}
/*-------------------------------------------------------------------------
* operator: -=
*-------------------------------------------------------------------------*/
void vectR6::operator*=(double a){
vectR6 p=get();
p = a*p;
put(p);
}
/*-------------------------------------------------------------------------
* operator: a*y
*-------------------------------------------------------------------------*/
vectR6 operator*(const double a, const vectR6 y){
vectR6 p;
int i;
for(i=0;i<6;i++)p.put(i,a*y.get(i));
return p;
}
/*-------------------------------------------------------------------------
* operator: dot product of p and q
*-------------------------------------------------------------------------*/
double operator^(vectR6 p,vectR6 q){
double sum=0;
int i;
for(i=0;i<6;i++)sum += p.get(i)*q.get(i);
return sum;
}
/*=====================================
* seqR6
*=====================================*/
/*-------------------------------------------------------------------------
* constructor initializing seqR1 to 0
*-------------------------------------------------------------------------*/
seqR6::seqR6(long M){
long n;
N=M;
x = (vectR6 *)malloc(N*sizeof(vectR6));
for(n=0; n<N; n++)x[n].clear();
}
/*-------------------------------------------------------------------------
* constructor initializing seqR1 to <u>
*-------------------------------------------------------------------------*/
seqR6::seqR6(long M,double u){
long n;
N=M;
x = (vectR6 *)malloc(N*sizeof(vectR6));
for(n=0; n<N; n++)x[n].put(u);
}
/*-------------------------------------------------------------------------
* fill the seqR1 with a value <u>
*-------------------------------------------------------------------------*/
void seqR6::fill(double u){
long n;
for(n=0; n<N; n++)x[n].put(u);
}
/*-------------------------------------------------------------------------
* put a single value u=(u1,u2,u3,u4,u5,u6) into the seqR1 x at location n
*-------------------------------------------------------------------------*/
int seqR6::put(long n, double u1,double u2,double u3,double u4,double u5,double u6){
int retval=0;
if(n<N)x[n].put(u1,u2,u3,u4,u5,u6);
else{
fprintf(stderr,"n=%ld larger than seqR1 size N=%ld\n",n,N);
retval=-1;
}
return retval;
}
int seqR6::put(long n, vectR6 u){
int retval=0;
if(n<N)x[n].put(u);
else{
fprintf(stderr,"n=%ld larger than seqR6 size N=%ld\n",n,N);
retval=-1;
}
return retval;
}
/*-------------------------------------------------------------------------
* list contents of sequence
*-------------------------------------------------------------------------*/
void seqR6::list(const long start, const long end, const char* str1, const char *str2, FILE *ptr){
long n,m;
vectR6 p;
if(ptr!=NULL){
if(strlen(str1)>0)fprintf(ptr,"%s",str1);
for(n=start,m=1; n<=end; n++,m++){
p=x[n];
fprintf(ptr,"(%5.2lf,%5.2lf,%5.2lf,%5.2lf,%5.2lf,%5.2lf) ",p.get1(),p.get2(),p.get3(),p.get4(),p.get5(),p.get6());
if(m%2==0)fprintf(ptr,"\n");
}
if(strlen(str2)>0)fprintf(ptr,"%s",str2);
}
}
/*-------------------------------------------------------------------------
* list contents of seqR1 using 1 digit per element
*-------------------------------------------------------------------------*/
void seqR6::list1(const long start, const long end, const char *str1, const char *str2,FILE *ptr){
long n,m;
vectR6 p;
if(ptr!=NULL){
if(strlen(str1)>0)fprintf(ptr,"%s",str1);
for(n=start,m=1; n<=end; n++,m++){
p=x[n];
fprintf(ptr," %1.0lf%1.0lf%1.0lf%1.0lf%1.0lf%1.0lf",p.get1(),p.get2(),p.get3(),p.get4(),p.get5(),p.get6());
if(m%10==0)fprintf(ptr,"\n");
}
if(strlen(str2)>0)fprintf(ptr,"%s",str2);
}
}
/*=====================================
* external operations
*=====================================*/
/*-------------------------------------------------------------------------
* operator: return p+q
*-------------------------------------------------------------------------*/
vectR6 operator+(vectR6 p, vectR6 q){
int i;
vectR6 y;
for(i=0;i<6;i++)y.put(i,p.get(i)+q.get(i));
return y;
}
/*-------------------------------------------------------------------------
* operator: return p-q
*-------------------------------------------------------------------------*/
vectR6 operator-(vectR6 p, vectR6 q){
int i;
vectR6 y;
for(i=0;i<6;i++)y.put(i,p.get(i)-q.get(i));
return y;
}
/*-------------------------------------------------------------------------
* operator: return -p
*-------------------------------------------------------------------------*/
vectR6 operator-(vectR6 p){
vectR6 q;
int i;
for(i=0;i<6;i++)q.put(i,-p.get(i));
return q;
}
/*-------------------------------------------------------------------------
* return the angle theta in radians between the two vectors induced by
* the points <p> and <q> in the space R^6.
* on SUCCESS return theta in the closed interval [0:PI]
* on ERROR return negative value or exit with value EXIT_FAILURE
*-------------------------------------------------------------------------*/
double pqtheta(const vectR6 p, const vectR6 q){
const double rp=p.r(), rq=q.r();
double y,theta;
if(rp==0) return -1;
if(rq==0) return -2;
y = (p^q)/(rp*rq);
if(y>+1) {fprintf(stderr,"\nERROR using pqtheta(vectR6 p, vectR6 q): (p^q)/(rp*rq)=%lf>+1\n",y); exit(EXIT_FAILURE);}
if(y<-1) {fprintf(stderr,"\nERROR using pqtheta(vectR6 p, vectR6 q): (p^q)/(rp*rq)=%lf<-1\n",y); exit(EXIT_FAILURE);}
theta = acos(y);
return theta;
}
/*=====================================
* external operations
*=====================================*/
/*-------------------------------------------------------------------------
* compute magnitude of R^1 sequence
*-------------------------------------------------------------------------*/
int mag(seqR6 *xR6, seqR1 *ymag){
const long Nx=xR6->getN();
const long Ny=ymag->getN();
long n;
int retval=0;
vectR6 u;
ymag->clear();
if(Nx!=Ny){
fprintf(stderr,"\nERROR using y=mag(xR6): lengths of xR6 (%ld) and ymag (%ld) differ.\n",Nx,Ny);
exit(EXIT_FAILURE);
}
for(n=0;n<Nx;n++){
u=xR6->get(n);
ymag->put(n,u.mag());
}
return retval;
}