-
Notifications
You must be signed in to change notification settings - Fork 0
/
c4.cpp
382 lines (349 loc) · 10.8 KB
/
c4.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
/*============================================================================
* 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<r2.h>
#include<r4.h>
#include<c1.h>
#include<c4.h>
/*=====================================
* cfour
*=====================================*/
/*-------------------------------------------------------------------------
* cfour constructors
*-------------------------------------------------------------------------*/
cfour::cfour(void){
int i;
complex p(0,0);
for(i=0;i<4;i++)z[i]=p;
}
//cfour::cfour(double u0, double u1, double u2, double u3, double u4, double u5){
// z[0]=u0;z[1]=u1;z[2]=u2;z[3]=u3;z[4]=u4;z[5]=u5;
// }
cfour::cfour(double ur,double ui){
int i;
complex p(ur,ui);
for(i=0;i<4;i++)z[i]=p;
}
/*-------------------------------------------------------------------------
* cfour put member functions
*-------------------------------------------------------------------------*/
void cfour::put(double ur, double ui){
int i;
complex p(ur,ui);
for(i=0;i<4;i++)z[i]=p;
}
void cfour::put(cfour u){
int i;
for(i=0;i<4;i++)z[i]=u.get(i);
}
/*-------------------------------------------------------------------------
* return the 4-tuple value
*-------------------------------------------------------------------------*/
cfour cfour::get(void){
cfour u;
int i;
for(i=0;i<4;i++)u.puti(i,get(i));
return u;
}
/*-------------------------------------------------------------------------
* print the tuple
*-------------------------------------------------------------------------*/
void cfour::list(const char *str1, const char *str2){
int i;
if(strlen(str1)!=0)printf("%s",str1);
putchar('(');
for(i=0;i<5;i++)printf("%+.6lf%+.6lfi,",get(i).real(),get(i).imag());
printf("%+.6lf%+.6lfi)",get(5).real(),get(5).imag());
if(strlen(str2)!=0)printf("%s",str2);
}
/*=====================================
* vectC4 functions
*=====================================*/
/*-------------------------------------------------------------------------
* return the 4-tuple value
*-------------------------------------------------------------------------*/
vectC4 vectC4::get(void){
vectC4 u;
int i;
for(i=0;i<4;i++)u.puti(i,get(i));
return u;
}
/*-------------------------------------------------------------------------
* conjugate
*-------------------------------------------------------------------------*/
vectC4 vectC4::conj(void){
complex z,zc;
vectC4 u;
int i;
for(i=0;i<4;i++){
z=get(i);
zc=z.conj();
u.puti(i,zc);
}
return u;
}
/*-------------------------------------------------------------------------
* magnitude
*-------------------------------------------------------------------------*/
double vectC4::mag(void){
int i;
complex u;
double sum=0;
for(i=0;i<4;i++){
u=get(i);
sum += u.magsq();
}
return sqrt(sum);
}
/*-------------------------------------------------------------------------
* operator: +=
*-------------------------------------------------------------------------*/
void vectC4::operator+=(vectC4 q){
vectC4 p=get();
p = p+q;
put(p);
}
/*-------------------------------------------------------------------------
* operator: -=
*-------------------------------------------------------------------------*/
void vectC4::operator-=(vectC4 q){
vectC4 p=get();
p = p-q;
put(p);
}
/*-------------------------------------------------------------------------
* operator: -=
*-------------------------------------------------------------------------*/
void vectC4::operator*=(double a){
vectC4 p=get();
p = a*p;
put(p);
}
/*-------------------------------------------------------------------------
* operator: a*y
*-------------------------------------------------------------------------*/
vectC4 operator*(const double a, const vectC4 y){
vectC4 p;
int i;
for(i=0;i<4;i++)p.puti(i,a*y.get(i));
return p;
}
/*-------------------------------------------------------------------------
* operator: z*y
*-------------------------------------------------------------------------*/
vectC4 operator*(const complex z, const vectR4 x){
vectC4 y;
double xe;
complex xez;
int i;
for(i=0;i<4;i++){
xe=x.get(i);
xez=xe*z;
y.puti(i,xez);
}
return y;
}
/*-------------------------------------------------------------------------
* operator: dot product of p and q
*-------------------------------------------------------------------------*/
double operator^(vectC4 p,vectC4 q){
complex sum(0,0);
complex pp,qq,qqc;
int i;
for(i=0;i<4;i++){
pp = p.get(i);
qq = q.get(i);
qqc= qq.conj();
sum += (pp*(~qq));
}
return sum.mag();
}
/*=====================================
* seqC4
*=====================================*/
/*-------------------------------------------------------------------------
* constructor initializing seqR1 to 0
*-------------------------------------------------------------------------*/
seqC4::seqC4(long M){
long n;
N=M;
x = (vectC4 *)malloc(N*sizeof(vectC4));
for(n=0; n<N; n++)x[n].clear();
}
/*-------------------------------------------------------------------------
* constructor initializing seqR1 to <u>
*-------------------------------------------------------------------------*/
//seqC4::seqC4(long M,double ur,double ui){
// long n;
// N=M;
// x = (vectC4 *)malloc(N*sizeof(vectC4));
// for(n=0; n<N; n++)x[n].put(ur,ui);
// }
/*-------------------------------------------------------------------------
* fill the seqR1 with a value <u>
*-------------------------------------------------------------------------*/
void seqC4::fill(double ur,double ui){
long n;
for(n=0; n<N; n++)x[n].put(ur,ui);
}
/*-------------------------------------------------------------------------
* put a single value u=(u1,u2,u3,u4) into the seqC4 x at location n
*-------------------------------------------------------------------------*/
int seqC4::put(long n, vectC4 u){
int retval=0;
if(n<N)x[n].put(u);
else{
fprintf(stderr,"n=%ld larger than seqC4 size N=%ld\n",n,N);
retval=-1;
}
return retval;
}
/*-------------------------------------------------------------------------
* list contents of sequence
*-------------------------------------------------------------------------*/
void seqC4::list(long start, long end){
int i;
long n,m;
vectC4 p;
for(n=start,m=1; n<=end; n++,m++){
p=x[n];
printf("(");
for(i=0;i<5;i++){
(p.get(i)).list();
printf(",");
}
(p.get(5)).list();
printf(")\n");
//if(m%2==0)printf("\n");
}
}
/*-------------------------------------------------------------------------
* list contents of seqR1 using 1 digit per element
*-------------------------------------------------------------------------*/
//void seqC4::list1(long start, long end){
// long n,m;
// vectC4 p;
// for(n=start,m=1; n<=end; n++,m++){
// p=z[n];
// printf(" %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)printf("\n");
// }
// }
/*=====================================
* external operations
*=====================================*/
/*-------------------------------------------------------------------------
* operator: return p+q
*-------------------------------------------------------------------------*/
vectC4 operator+(vectC4 p, vectC4 q){
int i;
vectC4 y;
for(i=0;i<4;i++)y.puti(i,p.get(i)+q.get(i));
return y;
}
/*-------------------------------------------------------------------------
* operator: return p-q
*-------------------------------------------------------------------------*/
vectC4 operator-(vectC4 p, vectC4 q){
int i;
vectC4 y;
for(i=0;i<4;i++)y.puti(i,p.get(i)-q.get(i));
return y;
}
/*-------------------------------------------------------------------------
* operator: return -p
*-------------------------------------------------------------------------*/
vectC4 operator-(vectC4 p){
vectC4 q;
int i;
for(i=0;i<4;i++)q.puti(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^4.
* on SUCCESS return theta in the half open interval [0:PI)
* on ERROR return negative integer
*-------------------------------------------------------------------------*/
double pqtheta(vectC4 p, vectC4 q){
double rp=p.r(), rq=q.r();
if(rp==0) return -1;
if(rq==0) return -2;
double y = (p^q)/(p.mag()*q.mag());
if(y>+1) return -3;
if(y<-1) return -4;
double theta = acos(y);
return theta;
}
/*-------------------------------------------------------------------------
* compute magnitude of C^1 sequence
*-------------------------------------------------------------------------*/
int mag(seqC4 *xC4, seqR1 *ymag){
long Nx=xC4->getN();
long Ny=ymag->getN();
long N=(Nx<Ny)?Nx:Ny;
long n;
vectC4 v;
int retval=0;
double u;
double vmag;
ymag->clear();
if(Nx!=Ny){
fprintf(stderr,"WARNING using y=mag(seqC4): lengths of seqC4 (%ld) and y (%ld) differ.\n",Nx,Ny);
retval=-1;
}
else for(n=0;n<N;n++){
v=xC4->get(n);
vmag=v.norm();
ymag->put(n,vmag);
}
return retval;
}
/*-------------------------------------------------------------------------
* z = x * y where * represents convolution
* let x be a seqR1 of length M1 and y be a seqR1 of length M2
* then z is a seqR1 of length N=M1+M2-1
*
* m=n
* z[n] = z[n] * y[n] = SUM z[m]*y[n-m]
* m=0
*-------------------------------------------------------------------------*/
//seqC4 operator*(seqC4 xx,seqR1 y){
// long n,m,nm;
// long Nx=xx.getN();
// long Ny=y.getN();
// long N=Nx+Ny-1;
// seqC4 z(N);
// double u1,u2,u3,u4,u5,u6;
// double v;
// double sum1,sum2,sum3,sum4,sum5,sum6;
// for(n=0;n<N;n++){
// sum1=0; sum2=0; sum3=0; sum4=0; sum5=0; sum6=0;
// for(m=0;m<=n;m++){
// nm=n-m;
// if(m>=Nx){ u1=0; u2=0; u3=0; u3=0; u4=0; u5=0; u6=0; }
// else { u1=xx.get1(m); u2=xx.get2(m); u3=xx.get3(m); u3=xx.get3(m); u4=xx.get4(m); u5=xx.get5(m); u6=xx.get6(m);}
// if(nm<0 || nm>=Ny)v=0;
// else v=y.get(nm);
// sum1 += u1*v;
// sum2 += u2*v;
// sum3 += u3*v;
// sum4 += u4*v;
// sum5 += u5*v;
// sum6 += u6*v;
// //printf("n=%3ld m=%3ld n-m=%3ld u=%7.3lf v=%7.3lf sum=%7.3lf\n",n,m,nm,u,v,sum);
// }
// z.put(n,sum1,sum2,sum3,sum4,sum5,sum6);
// }
// return z;
// }