Skip to content

Instantly share code, notes, and snippets.

@TkrUdagawa
Created February 24, 2017 02:27
Show Gist options
  • Save TkrUdagawa/4a202d8efa91179a0273e9d0fd9926e1 to your computer and use it in GitHub Desktop.
Save TkrUdagawa/4a202d8efa91179a0273e9d0fd9926e1 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <cmath>
using std::vector;
using std::sqrt;
float add_inp_scores(vector<float> a,
vector<float> b) {
unsigned int i;
float score = 0.0;
for(i = 0; i < a.size(); i++) {
score += a[i] * b[i];
}
return score;
}
float calc_squread_l2norm(vector<float> a) {
unsigned int i;
float ret = 0.f;
for(i = 0; i < a.size(); i++) {
ret += a[i] * a[i];
}
return ret;
}
void calc_euclid_score(vector<float> a, vector<float> b) {
float score = add_inp_scores(a, b);
float squared_a_norm = calc_squread_l2norm(a);
float squared_b_norm = calc_squread_l2norm(b);
std::cout << "score: " << score << std::endl;
std::cout << "squared_a_norm" << squared_a_norm << std::endl;
std::cout << "squared_b_norm" << squared_b_norm << std::endl;
float d2 = squared_a_norm + squared_b_norm - 2 * score;
std::cout << "d2: " << d2 << std::endl;
float res = -sqrt(d2);
std::cout << "res: " << res << std::endl;
}
int main() {
vector<float> a(3);
a[0] = 1.0;
a[1] = 1.0;
a[2] = 1.0;
vector<float> b(3);
b[0] = 1.00012;
b[1] = 1.00018;
b[2] = 1.00014;
vector<float> c(3);
c[0] = 1.00017;
c[1] = 1.00012;
c[2] = 1.00019;
std::cout << "a, b" << std::endl;
calc_euclid_score(a, b);
std::cout << "b, c" << std::endl;
calc_euclid_score(b, c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment