MathGroup Archive 1995

[Date Index] [Thread Index] [Author Index]

Search the Archive

Functional programming puzzle


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  

   


  • Prev by Date: Q: Translator Mathematica -> C,C++
  • Next by Date: RE: Bugs with transcendental functions?
  • Previous by thread: Q: Translator Mathematica -> C,C++
  • Next by thread: Functional programming puzzle