-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vector.cpp
292 lines (235 loc) · 5.46 KB
/
Vector.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
/*! \file Vector.cpp
* \brief Archivo de implementación de la clase Vector.
* \author Luciana Luque <[email protected]>
* \version 1.0
* \date 2020.06.12
*/
#include "Vector.h"
using namespace std;
Vector::Vector(): x(0.0), y(0.0), z(0.0) {}
Vector::Vector(double x1, double y1, double z1) : x(x1), y(y1), z(z1) {}
Vector Vector::operator+(Vector v)
{
double x1, y1, z1;
x1 = x + v.x;
y1 = y + v.y;
z1 = z + v.z;
return Vector(x1, y1, z1);
}
Vector Vector::operator-(Vector v)
{
double x1, y1, z1;
x1 = x - v.x;
y1 = y - v.y;
z1 = z - v.z;
return Vector(x1, y1, z1);
}
double Vector::operator*(Vector v)
{
double x1, y1, z1;
x1 = x * v.x;
y1 = y * v.y;
z1 = z * v.z;
return (x1 + y1 + z1);
}
Vector Vector::operator/(Vector v)
{
double x1, y1, z1;
x1 = x / v.x;
y1 = y / v.y;
z1 = z / v.z;
return Vector(x1, y1, z1);
}
Vector Vector::operator*(double d)
{
double x1, y1, z1;
x1 = x * d;
y1 = y * d;
z1 = z * d;
return Vector(x1, y1, z1);
}
Vector Vector::operator/(double d)
{
double x1, y1, z1;
x1 = x / d;
y1 = y / d;
z1 = z / d;
return Vector(x1, y1, z1);
}
double Vector::modulo(){
return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
}
Vector Vector::operator+(double d)
{
double x1, y1, z1;
x1 = x + d;
y1 = y + d;
z1 = z + d;
return Vector(x1, y1, z1);
}
Vector Vector::operator-(double d)
{
double x1, y1, z1;
x1 = x - d;
y1 = y - d;
z1 = z - d;
return Vector(x1, y1, z1);
}
Vector Vector::normaliza(Vector& v)
{
Vector output = v;
double norm = 0.0;
norm += v.x * v.x;
norm += v.y * v.y;
norm += v.z * v.z;
norm = sqrt( norm );
output.x = v.x / norm;
output.y = v.y / norm;
output.z = v.z / norm;
static bool te_lo_adverti = false;
if( norm <= 1e-16 ){
if( te_lo_adverti == false ){
std::cout << "Advertencia: El vector es muy chiquito por lo que se \
lo normalizó a 0" << std::endl << std::endl;
te_lo_adverti = true;
}
output.x = 0.0;
output.y = 0.0;
output.z = 0.0;
}
return output;
}
void Vector::normalizame(Vector* v)
{
double norm = 1e-32;
norm += v->x * v->x;
norm += v->y * v->y;
norm += v->z * v->z;
norm = sqrt( norm );
v->x = v->x / norm;
v->y = v->y / norm;
v->z = v->z / norm;
static bool te_lo_adverti = false;
if( norm <= 1e-16 ){
if( te_lo_adverti == false ){
std::cout << "Advertencia: El vector es muy chiquito por lo que se \
lo normalizó a 0" << std::endl << std::endl;
te_lo_adverti = true;
}
v->x = 0.0;
v->y = 0.0;
v->z = 0.0;
};
}
std::ostream& operator<<(std::ostream& out, const Vector& v)
{
out << v.x << ", ";
out << v.y << ", ";
out << v.z << " ";
out << endl;
return out;
}
Vector operator-(double d, Vector v)
{
double x1, y1, z1;
x1 = d - v.x;
y1 = d - v.y;
z1 = d - v.z;
return Vector(x1, y1, z1);
}
void axpy( Vector* v, double& a , Vector& vv )
{
v->x += a * vv.x;
v->y += a * vv.y;
v->z += a * vv.z;
return ;
}
void operator+=( std::vector<double>& v1, const std::vector<double>& v2 )
{
for( unsigned int i=0; i < v1.size() ; i++ )
{ v1[i] += v2[i]; }
return;
}
void operator-=( std::vector<double>& v1, const std::vector<double>& v2 )
{
for( unsigned int i=0; i < v1.size() ; i++ )
{ v1[i] -= v2[i]; }
return;
}
void operator/=( std::vector<double>& v1, const std::vector<double>& v2 )
{
for( unsigned int i=0; i < v1.size() ; i++ )
{ v1[i] /= v2[i]; }
return;
}
void operator*=( std::vector<double>& v1, const double& a )
{
for( unsigned int i=0; i < v1.size() ; i++ )
{ v1[i] *= a; }
return;
}
void operator*=( std::vector<double>& v1, const std::vector<double>& v2 )
{
for( unsigned int i=0; i < v1.size() ; i++ )
{ v1[i] *= v2[i]; }
return;
}
void operator/=( std::vector<double>& v1, const double& a )
{
for( unsigned int i=0; i < v1.size() ; i++ )
{ v1[i] /= a; }
return;
}
void axpy( std::vector<double>* y, double& a , std::vector<double>& x )
{
for( unsigned int i=0; i < (*y).size() ; i++ )
{
(*y)[i] += a * x[i] ;
}
return ;
}
void axpy( std::vector<double>* y, std::vector<double>& a , std::vector<double>& x )
{
for( unsigned int i=0; i < (*y).size() ; i++ )
{
(*y)[i] += a[i] * x[i] ;
}
return;
}
void naxpy( std::vector<double>* y, double& a , std::vector<double>& x )
{
for( unsigned int i=0; i < (*y).size() ; i++ )
{
(*y)[i] -= a * x[i] ;
}
return ;
}
void naxpy( std::vector<double>* y, std::vector<double>& a , std::vector<double>& x )
{
for( unsigned int i=0; i < (*y).size() ; i++ )
{
(*y)[i] -= a[i] * x[i] ;
}
return;
}
double norm_squared( const std::vector<double>& v )
{
double out = 0.0;
for( unsigned int i=0 ; i < v.size() ; i++ )
{ out += ( v[i] * v[i] ); }
return out;
}
double norm_squared( const Vector& v )
{
double out = 0.0;
out += v.x * v.x;
out += v.y * v.y;
out += v.z * v.z;
return out;
}
double norma(const Vector& v){
return sqrt( norm_squared( v ) );
}
double norma( const std::vector<double>& v ){
return sqrt( norm_squared( v ) );
}