-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr4.cpp
412 lines (379 loc) · 12.6 KB
/
r4.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//============================================================================
// Daniel J. Greenhoe
// normed linear space R^2
//=============================================================================
//=====================================
// headers
//=====================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <Eigen/Dense> // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89325
#include "r1.h"
#include "r4.h"
typedef Eigen::Matrix< double, 4, 1 > Vector4d;
//=====================================
// oquad
//=====================================
//-----------------------------------------------------------------------------
// oquad constructors
//-----------------------------------------------------------------------------
oquad::oquad(void)
{
xx.at(0) = 0.0;
xx.at(1) = 0.0;
xx.at(2) = 0.0;
xx.at(3) = 0.0;
}
oquad::oquad(double u0, double u1, double u2, double u3)
{
xx.at(0) = u0;
xx.at(1) = u1;
xx.at(2) = u2;
xx.at(3) = u3;
}
oquad::oquad(double u)
{
xx.at(0) = u;
xx.at(1) = u;
xx.at(2) = u;
xx.at(3) = u;
}
//-----------------------------------------------------------------------------
//! \brief oquad put member functions
//-----------------------------------------------------------------------------
void oquad::put(double u)
{
xx.at(0) = u;
xx.at(1) = u;
xx.at(2) = u;
xx.at(3) = u;
}
void oquad::put(oquad u)
{
xx.at(0) = u.get(0);
xx.at(1) = u.get(1);
xx.at(2) = u.get(2);
xx.at(3) = u.get(3);
}
//-----------------------------------------------------------------------------
//! \brief Write values to oquad
//-----------------------------------------------------------------------------
void oquad::put(double u0, double u1, double u2, double u3)
{
xx.at(0) = u0;
xx.at(1) = u1;
xx.at(2) = u2;
xx.at(3) = u3;
}
//-----------------------------------------------------------------------------
//! \brief Write values to oquad
//-----------------------------------------------------------------------------
void oquad::put(int n,double u)
{
xx.at(n) = u;
}
//-----------------------------------------------------------------------------
//! \brief return the 4-tuple value
//-----------------------------------------------------------------------------
oquad oquad::get(void)
{
oquad u;
u.put( 0, get1() );
u.put( 1, get2() );
u.put( 2, get3() );
u.put( 3, get4() );
return u;
}
//-----------------------------------------------------------------------------
//! \brief return the minimum element of the 4 tupple
//-----------------------------------------------------------------------------
double oquad::min(void) const
{
const Eigen::Map< const Vector4d > a( getdata() );
const double minVal = a.minCoeff();
return minVal;
}
//-----------------------------------------------------------------------------
//! \brief return the maximum element of the 4 tupple
//-----------------------------------------------------------------------------
double oquad::max(void) const
{
const Eigen::Map< const Vector4d > a( getdata() );
const double maxVal = a.maxCoeff();
return maxVal;
}
//-----------------------------------------------------------------------------
//! \brief print the tuple
//-----------------------------------------------------------------------------
void oquad::list(const char *str1, const char *str2) const
{
if(strlen(str1)!=0)printf("%s",str1);
putchar('(');
printf("%9.6lf,", get1() );
printf("%9.6lf,", get2() );
printf("%9.6lf,", get3() );
printf("%9.6lf)", get4() );
if(strlen(str2)!=0)printf("%s",str2);
}
//=====================================
// vectR4 functions
//=====================================
//-----------------------------------------------------------------------------
//! \brief return the 4-tuple value
//-----------------------------------------------------------------------------
const vectR4 vectR4::get(void)
{
vectR4 u;
u.put( 0, get1() );
u.put( 1, get2() );
u.put( 2, get3() );
u.put( 3, get4() );
return u;
}
//-----------------------------------------------------------------------------
//! \brief magnitude
//-----------------------------------------------------------------------------
double vectR4::mag(void) const
{
const Eigen::Map< const Vector4d > a( getdata() );
return a.norm();
}
//-----------------------------------------------------------------------------
//! \brief Multiply the vector by a scalar a
//-----------------------------------------------------------------------------
vectR4 vectR4::mpy(const double a)
{
vectR4 w;
const Eigen::Map< const Vector4d > vv( getdata() );
Eigen::Map< Vector4d > ww( w.getdataa() );
ww = a * vv;
return w;
}
//-----------------------------------------------------------------------------
//! \brief operator: +=
//-----------------------------------------------------------------------------
void vectR4::operator+=(vectR4 q)
{
Eigen::Map< Vector4d > pp( getdataa() );
const Eigen::Map< const Vector4d > qq( q.getdata() );
pp = pp + qq;
}
//-----------------------------------------------------------------------------
//! \brief operator: -=
//-----------------------------------------------------------------------------
void vectR4::operator-=(vectR4 q)
{
Eigen::Map< Vector4d > pp( getdataa() );
const Eigen::Map< const Vector4d > qq( q.getdata() );
pp = pp - qq;
}
//-----------------------------------------------------------------------------
//! \brief operator: -=
//-----------------------------------------------------------------------------
void vectR4::operator*=(double a)
{
vectR4 p=get();
p = a*p;
put(p);
}
//-----------------------------------------------------------------------------
//! \brief operator: a*y
//-----------------------------------------------------------------------------
vectR4 operator*(const double a, const vectR4 x)
{
vectR4 y;
const Eigen::Map< const Vector4d > xx( x.getdata() );
Eigen::Map< Vector4d > yy( y.getdataa() );
yy = a * xx;
return y;
}
//-----------------------------------------------------------------------------
//! \brief operator: dot product of p and q
//-----------------------------------------------------------------------------
double operator^(vectR4 p,vectR4 q)
{
const Eigen::Map< const Vector4d > pp( p.getdata() );
const Eigen::Map< const Vector4d > qq( q.getdata() );
double innerProduct = pp.adjoint() * qq;
return innerProduct;
}
//=====================================
//! \brief seqR4
//=====================================
//-----------------------------------------------------------------------------
//! \brief constructor initializing seqR1 to 0
//-----------------------------------------------------------------------------
seqR4::seqR4(long M)
{
N=M;
x = new vectR4[N];
clear();
}
//-----------------------------------------------------------------------------
//! \brief constructor initializing seqR1 to <u>
//-----------------------------------------------------------------------------
seqR4::seqR4( long M, double u )
{
N=M;
x = new vectR4[N];
fill( u );
}
//-----------------------------------------------------------------------------
//! \brief Fill the seqR3 with a value 0
//-----------------------------------------------------------------------------
void seqR4::clear(void)
{
fill( 0.0 );
}
//-----------------------------------------------------------------------------
//! \brief fill the seqR4 with a value <u>
//-----------------------------------------------------------------------------
void seqR4::fill(double u)
{
for(long n=0; n<N; n++) put( n, u );
}
//-----------------------------------------------------------------------------
//! \brief put a single value u=(u1,u2,u3,u4) into the seqR4 x at location n
//-----------------------------------------------------------------------------
int seqR4::put(long n, double u1,double u2,double u3,double u4)
{
int retval=0;
if(n<N)x[n].put(u1,u2,u3,u4);
else{
fprintf(stderr,"n=%ld larger than seqR1 size N=%ld\n",n,N);
retval=-1;
}
return retval;
}
//-----------------------------------------------------------------------------
//! \brief put a single value (u,u,u,u) into the seqR4 x at location n
//-----------------------------------------------------------------------------
int seqR4::put(long n, vectR4 u)
{
int retval=0;
if(n<N)x[n].put(u);
else{
fprintf(stderr,"n=%ld larger than seqR4 size N=%ld\n",n,N);
retval=-1;
}
return retval;
}
//-----------------------------------------------------------------------------
//! \brief list contents of sequence
//-----------------------------------------------------------------------------
void seqR4::list(const long start, const long end, const char* str1, const char *str2, FILE *fptr){
long n,m;
vectR4 p;
if(strlen(str1)>0){
printf("%s",str1);
if(fptr!=NULL)fprintf(fptr,"%s",str1);
}
for(n=start,m=1; n<=end; n++,m++){
p=x[n];
printf("(%5.2lf,%5.2lf,%5.2lf,%5.2lf) ",p.get1(),p.get2(),p.get3(),p.get4());
if(m%2==0)printf("\n");
if(fptr!=NULL){
fprintf(fptr,"(%5.2lf,%5.2lf,%5.2lf,%5.2lf) ",p.get1(),p.get2(),p.get3(),p.get4());
if(m%2==0)fprintf(fptr,"\n");
}
}
if(strlen(str2)>0){
printf("%s",str2);
if(fptr!=NULL)fprintf(fptr,"%s",str2);
}
}
//-----------------------------------------------------------------------------
//! \brief list contents of seqR1 using 1 digit per element
//-----------------------------------------------------------------------------
void seqR4::list1(const long start, const long end, const char *str1, const char *str2,FILE *fptr){
long n,m;
vectR4 p;
if(strlen(str1)>0){
printf("%s",str1);
if(fptr!=NULL)fprintf(fptr,"%s",str1);
}
for(n=start,m=1; n<=end; n++,m++){
p=x[n];
printf(" %1.0lf%1.0lf%1.0lf%1.0lf",p.get1(),p.get2(),p.get3(),p.get4());
if(fptr!=NULL)fprintf(fptr," %1.0lf%1.0lf%1.0lf%1.0lf",p.get1(),p.get2(),p.get3(),p.get4());
if(m%10==0)printf("\n");
if(fptr!=NULL)if(m%10==0)fprintf(fptr,"\n");
}
if(strlen(str2)>0){
printf("%s",str2);
if(fptr!=NULL)fprintf(fptr,"%s",str2);
}
}
/*=====================================
//! \brief external operations
*=====================================*/
//-----------------------------------------------------------------------------
//! \brief operator: return p+q
//-----------------------------------------------------------------------------
vectR4 operator+(vectR4 p, vectR4 q){
int i;
vectR4 y;
for(i=0;i<4;i++)y.put(i,p.get(i)+q.get(i));
return y;
}
//-----------------------------------------------------------------------------
//! \brief operator: return p-q
//-----------------------------------------------------------------------------
vectR4 operator-(vectR4 p, vectR4 q){
int i;
vectR4 y;
for(i=0;i<4;i++)y.put(i,p.get(i)-q.get(i));
return y;
}
//-----------------------------------------------------------------------------
//! \brief operator: return -p
//-----------------------------------------------------------------------------
vectR4 operator-(vectR4 p){
vectR4 q;
int i;
for(i=0;i<4;i++)q.put(i,-p.get(i));
return q;
}
//-----------------------------------------------------------------------------
//! \brief return the angle theta in radians between the two vectors induced by
//! \brief the points <p> and <q> in the space R^4.
//! \brief on SUCCESS return theta in the closed interval [0:PI]
//! \brief on ERROR return negative value or exit with value EXIT_FAILURE
//-----------------------------------------------------------------------------
double pqtheta(const vectR4 p, const vectR4 q){
const double rp = p.r();
const double 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(vectR4 p, vectR4 q): (p^q)/(rp*rq)=%lf>+1\n",y); exit(EXIT_FAILURE);}
if(y<-1) {fprintf(stderr,"\nERROR using pqtheta(vectR4 p, vectR4 q): (p^q)/(rp*rq)=%lf<-1\n",y); exit(EXIT_FAILURE);}
theta = acos(y);
return theta;
}
/*=====================================
//! \brief external operations
*=====================================*/
//-----------------------------------------------------------------------------
//! \brief compute magnitude of R^1 sequence
//-----------------------------------------------------------------------------
//int mag(seqR4 *xR4, seqR1 *ymag){
// const long Nx=xR4->getN();
// const long Ny=ymag->getN();
// long n;
// int retval=0;
// vectR4 u;
// ymag->clear();
// if(Nx!=Ny){
// fprintf(stderr,"ERROR using y=mag(xR4): lengths of xR4 (%ld) and ymag (%ld) differ.\n",Nx,Ny);
// exit(EXIT_FAILURE);
// }
// for(n=0;n<Nx;n++){
// u=xR4->get(n);
// ymag->put(n,u.mag());
// }
// return retval;
// }