distance

 1 /*
 2 * Copyright 2016 E-Tool, Inc.
 3 */
 4
 5 #ifndef distance_h
 6 #define distance_h
 7 #include <math.h>
 8 #include <vector>
 9 using std::vector;
10
11 #define max(a,b) (a>b?a:b)
12 #define min(a,b) (a<b?a:b)
13
14 class Distance {
15 public:
16     static double euclidean_distance(double x1,double y1,double x2,double y2) {
17         return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
18     }
19     static double euclidean_distance(double x1, double y1, double z1, double x2, double y2,  double z2) {
20         return sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)+(z1-z2)*(z1-z2));
21     }
22     static double euclidean_distance(vector<double> x, vector<double> y) {
23         double result = 0;
24         for (int i = 0;i < x.size();i++) {
25             result += (x[i]-y[i])*(x[i]-y[i]);
26         }
27         return sqrt(result);
28     }
29     static double manhattan_distance(double x1, double y1, double x2, double y2) {
30         return abs(x1 - x2) + abs(y1 - y2);
31     }
32     static double manhattan_distance(vector<double> x, vector<double> y) {
33         double result = 0;
34         for (int i = 0;i < x.size();i++) {
35             result += abs(x[i] - y[i]);
36         }
37         return result;
38     }
39     static double chebyshev_distance(double x1, double y1, double x2, double y2) {
40         return max(abs(x1 - x2) ,abs(y1 - y2));
41     }
42     static double chebyshev_distance(vector<double> x, vector<double> y) {
43         double result=-1;
44         for (int i = 0;i < x.size();i++) {
45             result = max(result, abs(x[i] - y[i]));
46         }
47         return result;
48     }
49     static double minkowski_distance(vector<double> x, vector<double> y,int p) {
50         double result = 0;
51         for (int i = 0;i < x.size();i++) {
52             result += pow(abs(x[i]-y[i]),p);
53         }
54         return pow(result,1/p);
55     }
56     /*
57     *  Standardized Euclidean distance
58     */
59     static double standardized_euclidean_distance(vector<double> x, vector<double> y) {
60         double result = 0;
61         for (int i = 0;i < x.size();i++) {
62             double r=(x[i]+y[i])/2;
63             double s = (x[i]-r)*(x[i]-r)+(y[i]-r)*(y[i]-r);
64             result += (x[i]-y[i])*(x[i]-y[i]) / s;
65         }
66         return sqrt(result);
67     }
68     static double information_entropy(vector<double> p) {
69         double result=0;
70         for (int i = 0;i < p.size();i++) {
71             result -= p[i] * (log(p[i])/log(2));
72         }
73         return result;
74     }
75 };
76
77 #endif
时间: 2024-10-17 15:10:41

distance的相关文章

461.求两个数字转成二进制后的“汉明距离” Hamming Distance

public class Solution { public int HammingDistance(int x, int y) { int distance = 0; string sX = Convert.ToString(x, 2); string sY = Convert.ToString(y, 2); int maxLength = Math.Max(sX.Length, sY.Length); //填充0,使两个字符串右对齐 sX = sX.PadLeft(maxLength, '0

LeetCode 72 Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a character c) Repla

461. Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance. Note:0 ≤ x, y < 2^31. Example: Input: x = 1, y = 4 Output: 2 Explanation:

搬土距离(Earth Mover&#39;s Distance)

搬土距离(The Earth Mover's Distance,EMD)最早由Y. Rubner在1999年的文章<A Metric for Distributions with Applications to Image Databases>中提出,它是归一化的从一个分布变为另一个分布的最小代价,因此可用于表征两个分布之间的距离. 例如,对于图像而言,它可以看做是由色调.饱和度.亮度三个分量组成,每个分量的直方图就是一个分布.不同的图像对应的直方图不同,因此图像之间的距离可以用直方图的距离表

distance.c

1 #include "stdio.h" 2 #include "string.h" 3 #include "math.h" 4 #include "malloc.h" 5 6 const long long Max_size = 2000;//输入字符串的最大长度,可以由单个词条和多个词条组成 7 const long long N = 40;//输出与某个单词最接近的N个词 8 const long long Max_w

[Locked] One Edit Distance

One Edit Distance Given two strings S and T, determine if they are both one edit distance apart. 分析: 编辑距离复杂度为O(MN),而本题显然不能用这么高的复杂度:首先,可以通过判断两个字符串是否等长来决定用增一位.减一位.替换一位这三种方法之一来使得两个字符串等同,如果都不行,就return false:然后同时遍历S和T,第一次遇到不匹配的,就用刚才判断出的方法拯救一下:第二次还遇到不匹配的,就

I - Long Distance Racing(第二季水)

Description Bessie is training for her next race by running on a path that includes hills so that she will be prepared for any terrain. She has planned a straight path and wants to run as far as she can -- but she must be back to the farm within M se

[LeetCode] One Edit Distance 一个编辑距离

Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance的拓展,然而这道题并没有那道题难,这道题只让我们判断两个字符串的编辑距离是否为1,那么我们只需分下列三种情况来考虑就行了: 1. 两个字符串的长度之差大于1,那么直接返回False 2. 两个字符串的长度之差等于1,那么长的那个字符串去掉一个字符,剩下的应该和短的字符串相同 3. 两个字符串的长度之

[ACM] POJ 2689 Prime Distance (筛选范围大素数)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

HDU 4712 Hamming Distance (随机函数)

Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1806    Accepted Submission(s): 714 Problem Description (From wikipedia) For binary strings a and b the Hamming distance is equal