MathGroup Archive 1995

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

Search the Archive

Functional programming puzzle

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg1821] Functional programming puzzle
  • From: "Wm. Martin McClain" <wmm at chem.wayne.edu>
  • Date: Thu, 3 Aug 1995 23:51:45 -0400
  • Organization: Wayne State University, College of Science

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 can do it for a short ptList this way:

toNbrs = Thread[{p1,p2,p3}->ptList]
{p1->{1,1,2}, p2->{2,1,3}, p3->{1,3,2}}

RijArray = Outer[Rij,{p1,p2,p3},{p1,p2,p3}];
(gives output in terms of symbols p1, p2, and p3)

Then replace the symbolic points by numerical
points, and give it a hand with Dot[0,0]:

RijNbrs = RijArray /.toNbrs /. Sqrt[0 . 0]->0;

The answer is, as desired, 

{{  0,     Sqrt[2],    2   }, 
 {Sqrt[2],    0,    Sqrt[6]}, 
 {  2,     Sqrt[6],    0   }}

But this gets out of hand when ptList is long.

I am so frustrated that I am TEMPTED to do it with 
explicit indices.  

Please, somebody help me before this happens.

Also, with explicit indices I could cut the number of 
multiplies more than in half, because I know the 
result will be symmetric, with only zeroes on the 
diagonal.  Is there some way to include this 
knowledge in a functionally programmed version?

TIA-  Martin McClain, Chemistry, Wayne State, Detroit


  • Prev by Date: RE: Bugs with transcendental functions?
  • Next by Date: [Q] weighted regression with Regress
  • Previous by thread: Functional programming puzzle
  • Next by thread: Re: Functional programming puzzle