Skip to content

Commit 7d7c157

Browse files
author
Daniel J. Greenhoe
committed
R3 to std::vector
1 parent a74adc7 commit 7d7c157

File tree

2 files changed

+124
-86
lines changed

2 files changed

+124
-86
lines changed

r3.cpp

Lines changed: 116 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -19,116 +19,147 @@
1919
/*-------------------------------------------------------------------------
2020
* constructor initializing seqR3 to 0
2121
*-------------------------------------------------------------------------*/
22-
seqR3::seqR3(long M){
23-
long n;
22+
seqR3::seqR3(long M)
23+
{
24+
// long n;
2425
N=M;
25-
x = (double *)malloc(N*sizeof(double));
26-
y = (double *)malloc(N*sizeof(double));
27-
z = (double *)malloc(N*sizeof(double));
28-
for(n=0; n<N; n++){
29-
x[n]=0.0;
30-
y[n]=0.0;
31-
z[n]=0.0;
32-
}
33-
}
26+
//x = (double *)malloc(N*sizeof(double));
27+
//y = (double *)malloc(N*sizeof(double));
28+
//z = (double *)malloc(N*sizeof(double));
29+
seqr3 = new vectR3[N];
30+
clear();
31+
//for(n=0; n<N; n++){
32+
// x[n]=0.0;
33+
// y[n]=0.0;
34+
// z[n]=0.0;
35+
// }
36+
}
3437

3538
/*-------------------------------------------------------------------------
3639
* constructor initializing seqR3 to <u>
3740
*-------------------------------------------------------------------------*/
38-
seqR3::seqR3(long M, double u){
39-
long n;
41+
seqR3::seqR3(long M, double u)
42+
{
43+
// long n;
4044
N=M;
41-
x = (double *)malloc(N*sizeof(double));
42-
y = (double *)malloc(N*sizeof(double));
43-
z = (double *)malloc(N*sizeof(double));
44-
for(n=0; n<N; n++){
45-
x[n]=u;
46-
y[n]=u;
47-
z[n]=u;
48-
}
49-
}
45+
//x = (double *)malloc(N*sizeof(double));
46+
//y = (double *)malloc(N*sizeof(double));
47+
//z = (double *)malloc(N*sizeof(double));
48+
seqr3 = new vectR3[N];
49+
fill( u );
50+
//for(n=0; n<N; n++){
51+
// x[n]=u;
52+
// y[n]=u;
53+
// z[n]=u;
54+
// }
55+
}
5056

5157
/*-------------------------------------------------------------------------
5258
* fill the seqR3 with a value 0
5359
*-------------------------------------------------------------------------*/
54-
void seqR3::clear(void){
55-
long n;
56-
for(n=0; n<N; n++){
57-
x[n]=0;
58-
y[n]=0;
59-
z[n]=0;
60+
void seqR3::clear(void)
61+
{
62+
for( long n=0; n<N; n++){
63+
put( n, 0 );
64+
// x[n]=0;
65+
// y[n]=0;
66+
// z[n]=0;
6067
}
61-
}
68+
}
6269

63-
/*-------------------------------------------------------------------------
64-
* fill the seqR3 with a value <u>
65-
*-------------------------------------------------------------------------*/
66-
void seqR3::fill(double u){
70+
//-----------------------------------------------------------------------------
71+
//! \brief Fill the seqR3 with a value <u>
72+
//-----------------------------------------------------------------------------
73+
void seqR3::fill(double u)
74+
{
6775
long n;
68-
for(n=0; n<N; n++){
69-
x[n]=u;
70-
y[n]=u;
71-
z[n]=u;
72-
}
76+
for(n=0; n<N; n++)
77+
{
78+
put( n, u );
79+
// x[n]=u;
80+
// y[n]=u;
81+
// z[n]=u;
7382
}
83+
}
7484

75-
/*-------------------------------------------------------------------------
76-
* put a single value <u> into the seqR3 x at location n
77-
*-------------------------------------------------------------------------*/
78-
int seqR3::put(long n, double u, double v, double w){
85+
//-----------------------------------------------------------------------------
86+
//! \brief Put a single value <u> into the seqR3 x at location n
87+
//-----------------------------------------------------------------------------
88+
int seqR3::put(long n, double u, double v, double w)
89+
{
7990
if(n<N){
80-
x[n]=u;
81-
y[n]=v;
82-
z[n]=w;
91+
seqr3[n].put( u, v, w );
92+
//x[n]=u;
93+
//y[n]=v;
94+
//z[n]=w;
8395
return 0;
8496
}
8597
else{
8698
fprintf(stderr,"n=%ld larger than seqR3 size N=%ld\n",n,N);
8799
return -1;
88100
}
89-
}
101+
}
90102

91-
int seqR3::put(long n, vectR3 xyz){
92-
if(n<N){
93-
x[n]=xyz.getx();
94-
y[n]=xyz.gety();
95-
z[n]=xyz.getz();
103+
//-----------------------------------------------------------------------------
104+
// \brief Put a single value <u> into the seqR3 x at location n
105+
//-----------------------------------------------------------------------------
106+
int seqR3::put(long n, vectR3 abc)
107+
{
108+
if(n<N)
109+
{
110+
//x[n]=xyz.getx();
111+
//y[n]=xyz.gety();
112+
//z[n]=xyz.getz();
113+
(seqr3[n]).put( abc.getx(), abc.gety(), abc.gety() );
96114
return 0;
97-
}
98-
else{
115+
}
116+
else
117+
{
99118
fprintf(stderr,"n=%ld larger than seqR3 size N=%ld\n",n,N);
100119
return -1;
101-
}
102120
}
121+
}
103122

104-
/*-------------------------------------------------------------------------
105-
* get a single value from the sequence x at location n
106-
*-------------------------------------------------------------------------*/
123+
//-----------------------------------------------------------------------------
124+
//! \brief Get a single value from the sequence x at location n
125+
//-----------------------------------------------------------------------------
107126
vectR3 seqR3::get(long n){
108-
vectR3 xyz(0,0,0);
109-
if(n<N)xyz.put(x[n],y[n],z[n]);
110-
else fprintf(stderr,"n=%ld larger than seqR3 size N=%ld\n",n,N);
111-
return xyz;
127+
//vectR3 xyz(0,0,0);
128+
//if(n<N)xyz.put(x[n],y[n],z[n]);
129+
//else fprintf(stderr,"n=%ld larger than seqR3 size N=%ld\n",n,N);
130+
return seqr3[n];
131+
//return xyz;
112132
}
113133

114-
/*-------------------------------------------------------------------------
115-
* get a single value from the sequence x,y, or z at location n
116-
*-------------------------------------------------------------------------*/
134+
//-----------------------------------------------------------------------------
135+
//! \brief Get the x element from the sequence at location n
136+
//-----------------------------------------------------------------------------
117137
double seqR3::getx(long n){
118138
double u=0;
119-
if(n<N)u=x[n];
139+
//if(n<N)u=x[n];
140+
if(n<N)u = seqr3[n].getx();
120141
else fprintf(stderr,"n=%ld larger than x seqR3 size N=%ld\n",n,N);
121142
return u;
122143
}
144+
145+
//-----------------------------------------------------------------------------
146+
//! \brief Get the y element from the sequence at location n
147+
//-----------------------------------------------------------------------------
123148
double seqR3::gety(long n){
124149
double u=0;
125-
if(n<N)u=y[n];
150+
if(n<N)u = seqr3[n].gety();
151+
//if(n<N)u=y[n];
126152
else fprintf(stderr,"n=%ld larger than y seqR3 size N=%ld\n",n,N);
127153
return u;
128154
}
155+
156+
//-----------------------------------------------------------------------------
157+
//! \brief Get the z element from the sequence at location n
158+
//-----------------------------------------------------------------------------
129159
double seqR3::getz(long n){
130160
double u=0;
131-
if(n<N)u=z[n];
161+
//if(n<N)u=z[n];
162+
if(n<N)u = seqr3[n].getz();
132163
else fprintf(stderr,"n=%ld larger than z seqR3 size N=%ld\n",n,N);
133164
return u;
134165
}
@@ -141,7 +172,8 @@ void seqR3::list(const long start, const long end, const char *str1, const char
141172
if(ptr!=NULL){
142173
if(strlen(str1)>0) fprintf(ptr,"%s",str1);
143174
for(n=start,m=1; n<=end; n++,m++){
144-
fprintf(ptr,"(%6.3lf,%6.3lf,%6.3lf) ",x[n],y[n],z[n]);
175+
//fprintf(ptr,"(%6.3lf,%6.3lf,%6.3lf) ",x[n],y[n],z[n]);
176+
fprintf(ptr,"(%6.3lf,%6.3lf,%6.3lf) ", seqr3[n].getx(), seqr3[n].gety(), seqr3[n].getz() );
145177
if(m%3==0)fprintf(ptr,"\n");
146178
}
147179
if(strlen(str2)>0)fprintf(ptr,"%s",str2);
@@ -154,14 +186,16 @@ void seqR3::list(const long start, const long end, const char *str1, const char
154186
void seqR3::list1(void){
155187
long n,m;
156188
for(n=0,m=1; n<N; n++,m++){
157-
printf("(%2.0lf,%2.0lf,%2.0lf) ",x[n],y[n],z[n]);
189+
//printf("(%2.0lf,%2.0lf,%2.0lf) ",x[n],y[n],z[n]);
190+
printf("(%2.0lf,%2.0lf,%2.0lf) ", seqr3[n].getx(), seqr3[n].gety(), seqr3[n].getz() );
158191
if(m%5==0)printf("\n");
159192
}
160193
}
161194
void seqR3::list1(long start, long end){
162195
long n,m;
163196
for(n=start,m=1; n<=end; n++,m++){
164-
printf("(%2.0lf,%2.0lf,%2.0lf) ",x[n],y[n],z[n]);
197+
// printf("(%2.0lf,%2.0lf,%2.0lf) ",x[n],y[n],z[n]);
198+
printf("(%2.0lf,%2.0lf,%2.0lf) ", seqr3[n].getx(), seqr3[n].gety(), seqr3[n].getz() );
165199
if(m%50==0)printf("\n");
166200
else if(m%10==0)printf(" ");
167201
}
@@ -210,12 +244,12 @@ vectR3 operator-(vectR3 p){
210244
return q;
211245
}
212246

213-
/*-------------------------------------------------------------------------
214-
* return the angle theta in radians between the two vectors induced by
215-
* the points <p> and <q> in the space R^3.
216-
* on SUCCESS return theta in the closed interval [0:PI]
217-
* on ERROR return negative value or exit with value EXIT_FAILURE
218-
*-------------------------------------------------------------------------*/
247+
//-------------------------------------------------------------------------
248+
//! \brief return the angle theta in radians between the two vectors induced by
249+
//! the points <p> and <q> in the space R^3.
250+
//! \returns On SUCCESS return theta in the closed interval [0:PI];
251+
//! On ERROR return negative value or exit with value EXIT_FAILURE
252+
//-------------------------------------------------------------------------*/
219253
double pqtheta(const vectR3 p, const vectR3 q){
220254
const double rp=p.mag(), rq=q.mag();
221255
double y,theta;
@@ -228,9 +262,9 @@ double pqtheta(const vectR3 p, const vectR3 q){
228262
return theta;
229263
}
230264

231-
/*-------------------------------------------------------------------------
232-
* return the minimum element of the 6 tupple
233-
*-------------------------------------------------------------------------*/
265+
//-----------------------------------------------------------------------------
266+
//! \brief Return the minimum element of the 6 tupple
267+
//-----------------------------------------------------------------------------
234268
double otriple::min(void) const
235269
{
236270
int i;
@@ -245,9 +279,9 @@ double otriple::min(void) const
245279
return min;
246280
}
247281

248-
/*-------------------------------------------------------------------------
249-
* return the maximum element of the 6 tupple
250-
*-------------------------------------------------------------------------*/
282+
//-----------------------------------------------------------------------------
283+
//! \brief Return the maximum element of the 6 tupple
284+
//-----------------------------------------------------------------------------
251285
double otriple::max(void) const
252286
{
253287
int i;

r3.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class otriple
1818
otriple(double u){ xyz.front()=u; xyz.at(1)=u; xyz.back()=u; }
1919
otriple(void) { xyz.front()=0; xyz.at(1)=0; xyz.back()=0; }
2020
void put(double u,double v,double w){xyz.front()=u; xyz.at(1)=v; xyz.back()=w; }
21+
void put(int u, int v, int w){ put((double)u, (double)v, (double)w); }
2122
void clear(void) { xyz.front()=0; xyz.at(1)=0; xyz.back()=0; }
2223
double getx(void) const {return xyz.front(); }; //get component x
2324
double gety(void) const {return xyz.at(1) ; }; //get component y
@@ -38,19 +39,22 @@ class vectR3: public otriple
3839
{
3940
public:
4041
vectR3(double u, double v, double w) : otriple(u,v,w){}; //constructor using 2 long float arguments
41-
vectR3(double u) : otriple(u){}; //constructor using 1 long float argument
42+
vectR3(double u) : otriple(u){}; //constructor using 1 long float argument
4243
vectR3(void) : otriple(){}; //constructor using no arguments (set to 0,0)
44+
void put(vectR3 abc){ otriple::put(abc.getx(), abc.gety(), abc.getz()); }
45+
void put(int u, int v, int w){ otriple::put(u, v, w); }
4346
double mag(void) const {return sqrt(getx()*getx()+gety()*gety()+getz()*getz());};//norm of ordered pair
4447
double norm(void) const {return mag();}
45-
void polartoxyz(double r, double theta,double phi){put(r*cos(phi)*cos(theta),r*cos(phi)*sin(theta),r*sin(phi));}//set (x,y,z) using polar coordinates (r,theta,phi)
46-
void operator+=(vectR3 q){put(getx()+q.getx(), gety()+q.gety(), getz()+q.getz());} //p=p+q
47-
void operator-=(vectR3 q){put(getx()-q.getx(), gety()-q.gety(), getz()-q.getz());} //p=p-q
48+
void polartoxyz(double r, double theta,double phi){otriple::put(r*cos(phi)*cos(theta),r*cos(phi)*sin(theta),r*sin(phi));}//set (x,y,z) using polar coordinates (r,theta,phi)
49+
void operator+=(vectR3 q){otriple::put(getx()+q.getx(), gety()+q.gety(), getz()+q.getz());} //p=p+q
50+
void operator-=(vectR3 q){otriple::put(getx()-q.getx(), gety()-q.gety(), getz()-q.getz());} //p=p-q
4851
};
4952

5053
class seqR3 {
5154
private:
5255
long N;
5356
double *x,*y,*z;
57+
vectR3 *seqr3;
5458
public:
5559
seqR3(long M); //constructor
5660
seqR3(long M, double u); //constructor

0 commit comments

Comments
 (0)