-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVector.cpp
More file actions
585 lines (532 loc) · 15.9 KB
/
Vector.cpp
File metadata and controls
585 lines (532 loc) · 15.9 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*! @file Vector.cpp
*
* @brief Archivo de implementación de la clase Vector.
*
* @author Luciana Melina Luque
*
*
* @details
* Implementation of vector mathematics operations critical for 3D spatial calculations
* in cancer simulations. These operations provide the foundation for representing cell
* positions, mechanical interactions, movement, and microenvironmental gradients.
*
* In cancer research, these vector operations enable:
* - Precise tracking of tumor cell positions and movements in tissue
* - Calculation of mechanical forces between cells that drive tumor morphology
* - Representation of biochemical gradients that influence cell migration
* - Quantification of spatial characteristics within the tumor microenvironment
*
* Inbound Dependencies: Vector.h
*
* Outbound Dependencies: Used by mechanical interaction, cell movement, and gradient calculations
*
* @license: Open Access - Creative Commons Attribution 4.0 International License (http://creativecommons.org/licenses/by/4.0/)
*/
#include "Vector.h"
using namespace std;
/**
* \brief Default constructor, initializes a zero vector (0,0,0).
*
* Creates a vector with all components set to zero, typically used as an
* initial state before accumulating forces or positions in cancer simulations.
*/
Vector::Vector(): x(0.0), y(0.0), z(0.0) {}
/**
* \brief Constructor with specific x, y, z components.
* \param x1 The x component
* \param y1 The y component
* \param z1 The z component
*
* Creates a vector with specific values, commonly used to initialize cell positions,
* direction vectors, or forces in cancer modeling.
*/
Vector::Vector(double x1, double y1, double z1) : x(x1), y(y1), z(z1) {}
/**
* \brief Vector addition operator.
* \param v Vector to add
* \return Sum vector
*
* Adds two vectors component-wise, essential for combining multiple forces
* acting on cells or calculating cumulative displacements in tumor growth models.
*/
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);
}
/**
* \brief Vector subtraction operator.
* \param v Vector to subtract
* \return Difference vector
*
* Subtracts a vector component-wise, critical for calculating displacement
* vectors between cells in contact or determining relative positions in
* tumor spatial analysis.
*/
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);
}
/**
* \brief Vector dot product operator.
* \param v Vector to dot with
* \return Scalar dot product result
*
* Computes the dot product, used for calculating projections and angles
* between cellular movement directions or interaction vectors. Essential for
* determining alignment of cells and forces in tumor models.
*/
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);
}
/**
* \brief Component-wise vector division.
* \param v Vector to divide by
* \return Result of component-wise division
*
* Performs component-wise division, useful for specialized calculations in
* spatial analysis of tumor structures and normalizing gradients.
*/
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);
}
/**
* \brief Vector scaling (multiplication by scalar).
* \param d Scale factor
* \return Scaled vector
*
* Multiplies a vector by a scalar, essential for scaling forces, velocities,
* or gradients in cancer modeling. Used extensively in mechanical interaction
* and diffusion calculations.
*/
Vector Vector::operator*(double d)
{
double x1, y1, z1;
x1 = x * d;
y1 = y * d;
z1 = z * d;
return Vector(x1, y1, z1);
}
/**
* \brief Vector division by scalar.
* \param d Divisor
* \return Divided vector
*
* Divides a vector by a scalar, useful for normalization or rescaling operations
* in cell mechanics calculations. Critical for creating unit vectors that
* represent direction without magnitude.
*/
Vector Vector::operator/(double d)
{
double x1, y1, z1;
x1 = x / d;
y1 = y / d;
z1 = z / d;
return Vector(x1, y1, z1);
}
/**
* \brief Calculates the magnitude (length) of the vector.
* \return The magnitude as a scalar value
*
* Computes the length of the vector, critical for distance calculations between cells,
* measuring tumor dimensions, or determining gradient strengths in the microenvironment.
*/
double Vector::modulo(){
return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
}
/**
* \brief Component-wise addition with scalar.
* \param d Value to add to each component
* \return Vector with scalar added to each component
*
* Adds a scalar to each component, used in specialized calculations for
* tumor microenvironment modeling, particularly for boundary adjustments
* and offset calculations.
*/
Vector Vector::operator+(double d)
{
double x1, y1, z1;
x1 = x + d;
y1 = y + d;
z1 = z + d;
return Vector(x1, y1, z1);
}
/**
* \brief Component-wise subtraction with scalar.
* \param d Value to subtract from each component
* \return Vector with scalar subtracted from each component
*
* Subtracts a scalar from each component, used in specialized calculations
* for tumor microenvironment modeling and cell position adjustments.
*/
Vector Vector::operator-(double d)
{
double x1, y1, z1;
x1 = x - d;
y1 = y - d;
z1 = z - d;
return Vector(x1, y1, z1);
}
/**
* \brief Normalizes a vector (creates unit vector).
* \param v Vector to normalize
* \return Normalized vector (unit length)
*
* Creates a unit vector in the same direction as the input, essential for
* representing directional information without magnitude, such as cell migration
* directions or substrate gradient directions in tumor models.
*/
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;
// Si la norma es muy chiquita, no tiene sentido normalizar.
// En tal caso, seteo todo el vector a 0.0.
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;
}
/**
* \brief Normalizes a vector in place.
* \param v Pointer to vector to normalize
*
* Modifies the vector to have unit length, used for efficient normalization
* in performance-critical code paths of cancer simulations, particularly when
* calculating directional forces and cell movements.
*/
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;
// Si la norma es muy chiquita, no tiene sentido normalizar.
// En tal caso, seteo todo el vector a 0.0.
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 normalizo a 0" << std::endl << std::endl;
te_lo_adverti = true;
}
v->x = 0.0;
v->y = 0.0;
v->z = 0.0;
};
}
/**
* \brief Stream output operator for Vector.
* \param out Output stream
* \param v Vector to output
* \return Modified output stream
*
* Enables printing vector data in a readable format, useful for debugging and
* visualization of cell positions, forces, or other spatial properties in
* cancer simulation outputs.
*/
std::ostream& operator<<(std::ostream& out, const Vector& v)
{
out << v.x << ", ";
out << v.y << ", ";
out << v.z << " ";
out << endl;
return out;
}
/**
* \brief Scalar-vector subtraction operator.
* \param d Scalar value
* \param v Vector to subtract
* \return Result vector of d-v
*
* Computes d-v element-wise, used in specialized micro-environment calculations
* and boundary condition implementations for tumor models.
*/
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);
}
/**
* \brief Vector axpy operation (y = y + a*x).
* \param v Pointer to y vector to be modified
* \param a Scalar multiplier
* \param vv x vector to be scaled and added
*
* Performs the operation y = y + a*x in place, a common operation in numerical
* methods for diffusion, mechanics, and cell movement calculations in cancer models.
*/
void axpy( Vector* v, double& a , Vector& vv )
{
v->x += a * vv.x;
v->y += a * vv.y;
v->z += a * vv.z;
// for( unsigned int i=0; i < (*y).size() ; i++ )
// {
// (*y)[i] += a * x[i] ;
// }
return ;
}
//Vector Vector::operator=(Vector v){
//
// double x1, y1, z1;
// x1 = v.x;
// y1 = v.y;
// z1 = v.z;
// return Vector(x1, y1, z1);
// return;
//
//}
/**
* \brief Component-wise addition of std::vector<double> containers.
* \param v1 First vector, modified in place (v1 = v1 + v2)
* \param v2 Second vector to add
*
* Adds two std::vector containers element-wise, used for operating on
* substrate concentration vectors in tumor microenvironment calculations.
*/
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;
}
/**
* \brief Component-wise subtraction of std::vector<double> containers.
* \param v1 First vector, modified in place (v1 = v1 - v2)
* \param v2 Second vector to subtract
*
* Subtracts two std::vector containers element-wise, used for
* calculating changes in substrate concentrations over time in
* tumor growth simulations.
*/
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;
}
/**
* \brief Component-wise division of std::vector<double> containers.
* \param v1 First vector, modified in place (v1 = v1 / v2)
* \param v2 Second vector as divisor
*
* Divides two std::vector containers element-wise, used for normalizing
* substrate concentrations or calculating ratios of different biochemical
* factors in cancer modeling.
*/
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;
}
/**
* \brief Scalar multiplication of std::vector<double>.
* \param v1 Vector to be scaled in place (v1 = v1 * a)
* \param a Scalar multiplier
*
* Multiplies each element by a scalar, used for scaling substrate
* concentrations or adjusting diffusion coefficients in cancer simulations.
*/
void operator*=( std::vector<double>& v1, const double& a )
{
for( unsigned int i=0; i < v1.size() ; i++ )
{ v1[i] *= a; }
return;
}
/**
* \brief Component-wise multiplication of std::vector<double> containers.
* \param v1 First vector, modified in place (v1 = v1 * v2)
* \param v2 Second vector to multiply with
*
* Multiplies two std::vector containers element-wise, used for combining
* factors that have multiplicative effects in cancer biology, such as
* reaction rates or synergistic factors.
*/
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;
}
/**
* \brief Scalar division of std::vector<double>.
* \param v1 Vector to be divided in place (v1 = v1 / a)
* \param a Scalar divisor
*
* Divides each element by a scalar, used for normalizing substrate
* concentrations or calculating relative values in cancer simulations.
*/
void operator/=( std::vector<double>& v1, const double& a )
{
for( unsigned int i=0; i < v1.size() ; i++ )
{ v1[i] /= a; }
return;
}
/**
* \brief Vector axpy operation for std::vector<double> (y = y + a*x).
* \param y Pointer to vector to be modified
* \param a Scalar multiplier
* \param x Vector to be scaled and added
*
* Performs the operation y = y + a*x in place for substrate vectors,
* essential in diffusion solvers and biochemical reaction calculations
* in tumor microenvironment modeling.
*/
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 ;
}
/**
* \brief Vector component-wise axpy operation (y = y + a.*x).
* \param y Pointer to vector to be modified
* \param a Vector of multipliers
* \param x Vector to be scaled and added
*
* Performs the operation y = y + a.*x in place (element-wise multiplication),
* used for complex substrate interaction calculations in tumor microenvironment
* models where each component has a different scaling factor.
*/
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;
}
/**
* \brief Vector negative axpy operation (y = y - a*x).
* \param y Pointer to vector to be modified
* \param a Scalar multiplier
* \param x Vector to be scaled and subtracted
*
* Performs the operation y = y - a*x in place, used in diffusion solvers and
* substrate consumption calculations when modeling cancer cell metabolism.
*/
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 ;
}
/**
* \brief Vector component-wise negative axpy operation (y = y - a.*x).
* \param y Pointer to vector to be modified
* \param a Vector of multipliers
* \param x Vector to be scaled and subtracted
*
* Performs the operation y = y - a.*x in place (element-wise multiplication),
* used for modeling differential consumption of multiple substrates in tumor
* metabolism with varying consumption rates.
*/
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;
}
/**
* \brief Squared norm of a std::vector<double>.
* \param v Vector to calculate squared norm
* \return The squared magnitude of the vector
*
* Calculates the sum of squares of elements, used for efficient distance
* calculations and convergence testing in cancer simulation algorithms
* without the computational cost of a square root operation.
*/
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;
}
/**
* \brief Squared norm of a Vector.
* \param v Vector to calculate squared norm
* \return The squared magnitude of the vector
*
* Calculates the squared length of the vector, used for efficient distance
* calculations between cells when the exact magnitude isn't required,
* reducing computational cost in tumor simulations.
*/
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;
}
/**
* \brief Norm (magnitude) of a Vector.
* \param v Vector to calculate norm
* \return The magnitude of the vector
*
* Calculates the length of the vector, used for measuring exact distances
* between cells or determining the strength of forces in cancer simulations.
*/
double norma(const Vector& v){
return sqrt( norm_squared( v ) );
}
/**
* \brief Norm (magnitude) of a std::vector<double>.
* \param v Vector to calculate norm
* \return The magnitude of the vector
*
* Calculates the Euclidean norm, used for measuring distances between
* substrate states or determining gradient magnitudes in tumor
* microenvironment modeling.
*/
double norma( const std::vector<double>& v ){
return sqrt( norm_squared( v ) );
}