From: Andrzej Kozlowski on 20 Jul 2006 06:45 On 19 Jul 2006, at 11:21, Clausenator(a)gmail.com wrote: > Hi, > I want to calculate a distance matrix, similar to (as poorly explained > at) http://en.wikipedia.org/wiki/Distance_matrix > > I found out about the Function "Norm" in mathematica 5. > > Here is a little example. I want to calculate the distance between > vectors {0,1} and {5,1}. The distance should be 5 > > Now, > > Norm[{{0., 1.}, {5., 1.}}, 2] > results 5.10293 > > Norm[{{0., 1.} - {5., 1.}}, 2] > results 5.0 > > According to the documentation I have (Mathematica Help Browser, > search > for "Norm" under "Built-in Functions") the version with the comma is > documented. I like the solution with the dash better. > Which one is it? In other words, is there some Wolfram description or > can you explain the difference? > > Thanks for your help, > Claus > Norm[{{0., 1.}, {5., 1.}}, 2] does not compute the distance between the points {0,1} and {5,1} but the 2-norm of the matrix {{0., 1.}, {5., 1.}}. This is defined as the square root of the sum of the squares of the matrix entries, in other words it is Sqrt[0^2+1^2+5^2+1^2]//N 5.19615 This is of course completely different form the distance, which is: Norm[{{0, 1}-{5, 1}}, 2] 5 where - is not the dash but the subtraction sign! Andrzej Kozlowski
From: Jean-Marc Gulliet on 20 Jul 2006 06:52 Clausenator(a)gmail.com wrote: > Hi, > I want to calculate a distance matrix, similar to (as poorly explained > at) http://en.wikipedia.org/wiki/Distance_matrix > > I found out about the Function "Norm" in mathematica 5. > > Here is a little example. I want to calculate the distance between > vectors {0,1} and {5,1}. The distance should be 5 And it is indeed. Let v1 = {0., 1.} and v2 = {5., 1.}. What you want is the length of the vector v1 - v2, which is equal to {-5., 0.}, length that can be computed by taking the square root of the dot product of v1 - v2 by itself (assuming that all the entries are reals): Sqrt[(v1 - v2) . (v1 - v2)] returns 5, as expected. This is equivalent to Norm[v1 - v2] or Norm[v1 - v2, 2]. > > Now, > > Norm[{{0., 1.}, {5., 1.}}, 2] > results 5.10293 Beware! Here you are computing a *matrix* 2-norm, which has nothing to do with Eucledian distance. The matrix 2-norm is defined as the max of the singular values of the given matrix Max[SingularValueList[{{0., 1.}, {5., 1.}}]] yields 5.10293407795794 which is the same values as the one returned by Norm[{{0., 1.}, {5., 1.}}, 2] Here you are dealing with the two-by-two matrix / \ ! 0. 1. ! ! ! ! 5. 1. ! \ / In Mathematica, a list of lists of equal lengths is usually interpreted as a matrix and a list of atomic expressions is interpreted as a vector. > > Norm[{{0., 1.} - {5., 1.}}, 2] > results 5.0 Even though you got the correct result, what you really want to write above is a vector, so get rid of the outermost curly braces: Norm[{0., 1.} - {5., 1.}, 2] Now, let's go back to your original problem. To compute the distance matrix of a list of points, say points = {{0., 1.}, {5., 1.}, {3., 4.}, {-1., -5.}}; you could use the generalized outer product as follows Outer[Norm[#1 - #2] & , points, points, 1] returns {{0., 5., 4.242640687119285, 6.082762530298219}, {5., 0., 3.605551275463989, 8.48528137423857}, {4.242640687119285, 3.605551275463989, 0., 9.848857801796104}, {6.082762530298219, 8.48528137423857, 9.848857801796104, 0.}} We can easily check that the resulting matrix is a well formed distance matrix (at least according to the description in wikipedia). MatrixForm[%] Regarding distance matrices, the following web sites might be of interest: http://planetmath.org/encyclopedia/EuclideanDistanceMatrix.html http://www.itl.nist.gov/div898/software/dataplot/refman2/auxillar/matrdist.htm You could also have a look at Stephen Boyd's and Lieven Vandenberghe's book _Convex Optimization_, Cambridge University Press, 2004, and the associated web sites at http://www.stanford.edu/~boyd/cvxbook/ where the text is available as well as lectures slides, code programs, etc. HTH, Jean-Marc
From: Murray Eisenberg on 21 Jul 2006 06:33
Correction to my posting: What I mean to write is that the desired euclidean distance is given by Norm[{0,1} - {5,1}] and the result is 5. Murray Eisenberg wrote: > Mathematically, the norm of a vector gives that vector's length. And > the distance between two vectors is the norm of the difference between > the two vectors. (What you call the "dash" is in fact a subtraction sign.) > > So, assuming you want the ordinary (that is, Euclidean) distance, the > desired result is given by > > Norm[{0, 1, 5, 1}] > > and the result (in InputForm) is 3 Sqrt[3]. > > The final argument, 2, is superfluous in the case of the ordinary > (Euclidean) norm, which is the 2-norm. > > It would help when doing such things if you were familiar, first, with > the underlying mathematical ideas and second, with the documentation > that Mathematica itself provides. For the latter, just evaluate > > ?Norm > > and then to get further information click the hyperlink in the output > produced (or in the first instance look up Norm directly in the > HelpBrowser). > > > > > Clausenator(a)gmail.com wrote: >> Hi, >> I want to calculate a distance matrix, similar to (as poorly explained >> at) http://en.wikipedia.org/wiki/Distance_matrix >> >> I found out about the Function "Norm" in mathematica 5. >> >> Here is a little example. I want to calculate the distance between >> vectors {0,1} and {5,1}. The distance should be 5 >> >> Now, >> >> Norm[{{0., 1.}, {5., 1.}}, 2] >> results 5.10293 >> >> Norm[{{0., 1.} - {5., 1.}}, 2] >> results 5.0 >> >> According to the documentation I have (Mathematica Help Browser, search >> for "Norm" under "Built-in Functions") the version with the comma is >> documented. I like the solution with the dash better. >> Which one is it? In other words, is there some Wolfram description or >> can you explain the difference? >> >> Thanks for your help, >> Claus >> >> > -- Murray Eisenberg murray(a)math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2859 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305 |