-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.cpp
87 lines (72 loc) · 1.91 KB
/
util.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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include "util.h"
static const float EPS = 1E-5;
int randomize (int lower, int upper) {
return rand() % (upper-lower+1) + lower;
}
float randomize (float lower, float upper) {
return float(rand())/RAND_MAX * (upper-lower) + lower;
}
bool comparar_vectores (vector<float> & a, vector<float> & b) {
// Devuelve true si son iguales
if (a.size() != b.size()) {
cerr << "comparar_vectores: distinto size" << endl;
throw 1;
}
for (int i = 0; i < a.size(); i++) {
if (fabs(a[i] - b[i]) > EPS) {
return false;
}
}
return true;
}
vector<float> sumar_vectores (vector<float> & a, vector<float> & b) {
vector<float> c(a.size());
if (a.size() != b.size()) {
cerr << "sumar_vectores: distinto size" << endl;
throw 1;
}
for (int i = 0; i < a.size(); i++) {
c[i] = a[i] + b[i];
}
return c;
}
vector<float> restar_vectores (vector<float> & a, vector<float> & b) {
vector<float> c(a.size());
if (a.size() != b.size()) {
cerr << "restar_vectores: distinto size" << endl;
throw 1;
}
for (int i = 0; i < a.size(); i++) {
c[i] = a[i] - b[i];
}
return c;
}
float multiplicar_vectores (vector<float> & a, vector<float> & b) {
float accum = 0;
vector<float> c(a.size());
if (a.size() != b.size()) {
cerr << "multiplicar_vectores: distinto size" << endl;
throw 1;
}
for (int i = 0; i < a.size(); i++) {
accum += a[i] * b[i];
}
return accum;
}
vector<float> dividir_vector (vector<float> & a, float divisor) {
vector<float> b(a.size());
for (int i = 0; i < a.size(); i++) {
b[i] = a[i] / divisor;
}
return b;
}
float norma2 (vector<float> & a) {
float accum = 0;
for (int i = 0; i < a.size(); i++) {
accum += a[i]*a[i];
}
return sqrt(accum);
}