Re: Norm
- To: mathgroup at smc.vnet.net
- Subject: [mg68019] Re: Norm
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Thu, 20 Jul 2006 06:04:58 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <e9kuel$l3h$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Clausenator at 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