Two speed challenges

• To: mathgroup at smc.vnet.net
• Subject: [mg47335] Two speed challenges
• From: "Gareth J. Russell" <gjr2008 at columbia.edu>
• Date: Tue, 6 Apr 2004 06:36:14 -0400 (EDT)
• Organization: Columbia University
• Sender: owner-wri-mathgroup at wolfram.com

```Hi,

I am creating a webMathematica application for calculating spatial autocorrelation using Moran's I. I am using randomization to test the significance of the I values, and so speed is of the essence.

I would appreciate any advice on the following two pieces of code, which are the rate-limiting factors and are as fast as I can make them. Can anyone do better?

Code 1

This simply randomizes a vector (in case it matters, typical input is a 500-element vector of Reals).

listRandomize = Compile[{{l, _Real, 1}}, Sort[Table[{Random[], l[[i]]}, {i, 1, Length[l]}]][[All, 2]]]

Code 2

This computes the only part of Moran's I that must change when the ordering of elements in the vector z changes. It calculates the sum over all i and j of z_i*z_j*w_ij, where w is (obviously) a square matrix

moranIPart1 = Compile[{{z, _Real, 1}, {w, _Real, 2}}, Apply[Plus,
Apply[Plus, Outer[Times, z, z]*w]]]