MathGroup Archive 1995

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

Search the Archive

Re: Functional programming puzzle

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg1822] Re: Functional programming puzzle
  • From: drc at gate.net (David Cabana)
  • Date: Thu, 3 Aug 1995 23:51:55 -0400

In article <DCGM0w.5GH at wri.com>, "Wm. Martin McClain" <wmm at chem.wayne.edu>
wrote:

>Dear functional programers:
>
>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.
...
>I am so frustrated that I am TEMPTED to do it with 
>explicit indices.  
>
>Please, somebody help me before this happens.

Martin,
One way to clean things up is to introduce a datatype, the point.  
Then things will work the way you want:

In[54]:=
ptList = {point[1,1,2], point[2,1,3], point[1,3,2]};
distance[x_point,y_point]:=
Module[{a,b},
   a=Apply[List,x];
   b=Apply[List,y];
   Sqrt[(a-b).(a-b)]
];
Outer[distance,ptList,ptList]

Out[56]=
{{0, Sqrt[2], 2}, {Sqrt[2], 0, Sqrt[6]}, {2, Sqrt[6], 0}}

Regards,

-- 
David Cabana   drc at gate.net


  • Prev by Date: Re: Re: Options in self-defined functions
  • Next by Date: Re: Fit[] problem
  • Previous by thread: Functional programming puzzle
  • Next by thread: Re: Functional programming puzzle