The Euclidean distance between points p and q is the length of the line segment connecting them (
).

In Cartesian coordinates, if p = (p1, p2,..., pn) and q = (q1, q2,..., qn) are two points in Euclidean n-space, then the distance (d) from p to q, or from q to p is given by the Pythagorean formula:
- (1)
The position of a point in a Euclidean n-space is a Euclidean vector. So, p and q are Euclidean vectors, starting from the origin of the space, and their tips indicate two points. The Euclidean norm, or Euclidean length, or magnitude of a vector measures the length of the vector:
where the last equation involves the dot product.
A vector can be described as a directed line segment from the origin of the Euclidean space (vector tail), to a point in that space (vector tip). If we consider that its length is actually the distance from its tail to its tip, it becomes clear that the Euclidean norm of a vector is just a special case of Euclidean distance: the Euclidean distance between its tail and its tip.
The distance between points p and q may have a direction (e.g. from p to q), so it may be represented by another vector, given by
In a three-dimensional space (n=3), this is an arrow from p to q, which can be also regarded as the position of q relative to p. It may be also called a displacement vector if p and q represent two positions of the same point at two successive instants of time.
The Euclidean distance between p and q is just the Euclidean length of this distance (or displacement) vector:
- (2)
which is equivalent to equation 1, and also to:
One dimension[edit]
In one dimension, the distance between two points on the real line is the absolute value of their numerical difference. Thus if x and y are two points on the real line, then the distance between them is given by:
In one dimension, there is a single homogeneous, translation-invariant metric (in other words, a distance that is induced by a norm), up to a scale factor of length, which is the Euclidean distance. In higher dimensions there are other possible norms.
Two dimensions[edit]
In the Euclidean plane, if p = (p1, p2) and q = (q1, q2) then the distance is given by
This is equivalent to the Pythagorean theorem.
Alternatively, it follows from (2) that if the polar coordinates of the point p are (r1, θ1) and those of q are (r2, θ2), then the distance between the points is
Three dimensions[edit]
In three-dimensional Euclidean space, the distance is
n dimensions[edit]
In general, for an n-dimensional space, the distance is
Squared Euclidean distance[edit]
The standard Euclidean distance can be squared in order to place progressively greater weight on objects that are farther apart. In this case, the equation becomes
Squared Euclidean Distance is not a metric as it does not satisfy the triangle inequality, however, it is frequently used in optimization problems in which distances only have to be compared.
It is also referred to as quadrance within the field of rational trigonometry.
Algorithms.of.the.Intelligent.Web
public double getDistance(double[] x, double[] y) {
double sumXY2 = 0.0;
for(int i = 0, n = x.length; i < n; i++) {
sumXY2 += Math.pow(x[i] - y[i], 2);
}
return Math.sqrt(sumXY2);
}
public double getDistance(Double[] x, Double[] y) {
double sumXY2 = 0.0;
for(int i = 0, n = x.length; i < n; i++) {
sumXY2 += Math.pow(x[i] - y[i], 2);
}
return Math.sqrt(sumXY2);
}
| cs |
Programming Collective Intelligence
public double sim_distance (Map < String, Map < String, Double >> personalRate, String person1, String person2) {
Map<String, Double> Si = new HashMap<String, Double>();
double sum_of_squares = 0;
double result = 0;
for (Map.Entry<String, Double> item : personalRate.get(person1).entrySet()) {
for (Map.Entry<String, Double> item2 : personalRate.get(person2).entrySet()) {
if (item.getKey().equals(item2.getKey())) {
Si.put(item.getKey(), 1.0);
sum_of_squares += Math.pow((item.getValue() - item2.getValue()), 2);
}
}
}
if (Si.size() == 0){
result = 0;
}else {
result = 1 / (1 + sum_of_squares);
}
return result;
}
| cs |
DataSetting
Map<String, Map<String, Double>> personalRate = new HashMap<String, Map<String, Double>>();
Map<String, Double> Lisa = new HashMap<String, Double>();
Lisa.put("Lady in the Water", 2.5);
Lisa.put("Snakes on a Plane", 3.5);
Lisa.put("Just My Luck", 3.0);
Lisa.put("Superman Returns", 3.5);
Lisa.put("You, Me and Dupree", 2.5);
Lisa.put("The Night Listener", 3.0);
personalRate.put("Lisa Rose", Lisa);
Map<String, Double> GeneSeymour = new HashMap<String, Double>();
GeneSeymour.put("Lady in the Water", 3.0);
GeneSeymour.put("Snakes on a Plane", 3.5);
GeneSeymour.put("Just My Luck", 1.5);
GeneSeymour.put("Superman Returns", 5.0);
GeneSeymour.put("You, Me and Dupree", 3.5);
GeneSeymour.put("The Night Listener", 3.0);
personalRate.put("Gene Seymour", GeneSeymour);
Map<String, Double> MichaelPhillips = new HashMap<String, Double>();
MichaelPhillips.put("Lady in the Water", 2.5);
MichaelPhillips.put("Snakes on a Plane", 3.0);
MichaelPhillips.put("Superman Returns", 3.5);
MichaelPhillips.put("The Night Listener", 4.0);
personalRate.put("Michael Phillips", MichaelPhillips);
Map<String, Double> MickLaSalle = new HashMap<String, Double>();
MickLaSalle.put("Lady in the Water", 3.0);
MickLaSalle.put("Snakes on a Plane", 4.0);
MickLaSalle.put("Just My Luck", 2.0);
MickLaSalle.put("Superman Returns", 3.0);
MickLaSalle.put("The Night Listener", 3.0);
MickLaSalle.put("You, Me and Dupree", 2.0);
personalRate.put("Mick LaSalle", MickLaSalle);
Map<String, Double> JackMatthews = new HashMap<String, Double>();
JackMatthews.put("Lady in the Water", 3.0);
JackMatthews.put("Snakes on a Plane", 4.0);
JackMatthews.put("Superman Returns", 5.0);
JackMatthews.put("The Night Listener", 3.0);
JackMatthews.put("You, Me and Dupree", 3.5);
personalRate.put("Jack Matthews", JackMatthews);
| cs |
댓글 없음:
댓글 쓰기