Functional programming puzzle
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1825] Functional programming puzzle
- From: Count Dracula <lk3a at kelvin.seas.virginia.edu>
- Date: Thu, 3 Aug 1995 23:52:28 -0400
- Organization: University of Virginia
In Article: 1079 of comp.soft-sys.math.mathematica "Wm. Martin McClain" <wmm at chem.wayne.edu> wrote: > Let ptList = {{1,1,2},{2,1,3},{1,3,2}}; > I want a matrix of distances between the points in ptList. > Any human would think you could get it with > Rij[a_,b_]:=Sqrt[(a-b).(a-b)] > followed by > Outer[Rij,ptList,ptList] > But Mathematica returns a mess, because Outer seems > to Flatten the ptList before running the outer > product loops. This will work if you give Outer a Level specification: dist[x_List] := Sqrt[Map[#1 . #1 & , Outer[Subtract, x, x, 1], {2}]] ltest = {{1,1,2},{2,1,3},{1,3,2}} In[4]:= dist[ltest]//MatrixForm Out[4]//MatrixForm= 0 Sqrt[2] 2 Sqrt[2] 0 Sqrt[6] 2 Sqrt[6] 0 -- Levent Kitis lk3a at cars.mech.virginia.edu lk3a at kelvin.seas.virginia.edu University of Virginia Department of Mechanical, Aerospace, and Nuclear Engineering