looking for a faster way
- To: mathgroup at smc.vnet.net
- Subject: [mg24998] looking for a faster way
- From: Otto Linsuain <linsuain+ at andrew.cmu.edu>
- Date: Fri, 1 Sep 2000 01:09:31 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Hi to all. I am looking for a faster way to achieve the following:
Have two lists with the formats:
xlist = { x1, x2, x3, ....... xN }
flist = { f1, f2, f3, ....... fN } the x's and f's are just numbers.
Want the matrix:
myMatrix [[ i, j ]] = ( fi - fj ) / ( xi- xj )
There are probably a thousand ways to avoid the Indeterminate along the
diagonal. Any of them is fine, as long as the off-diagonal part is not
touched. Speed is the real problem.
My solution so far is:
myMatrix[ xlist_, flist_ ] :=
Outer[Subtract, flist, flist] /
( Outer[Subtract, xlist, xlist] + IdentityMatrix[Length[xlist]])
the IdentityMatrix makes the diagonal zero, rather than Indeterminate.
This solution is not very fast because it involves 2 Outers, so that the
speed goes roughly as 2 N^2. If I could avoid one of them I would have
just one N^2, possibly + something-linear-in-N.
I have tried
myMatrix[ xlist_, flist_ ] :=
Outer[myFunction, Transpose[{flist,xlist}], Transpose[{flist,xlist}], 2]
this uses one Outer. This produces:
myMatrix [[ i, j ]] = myFunction[ {fi, xi}, {fj, xj} ]
myFuction is of course chosen to produce the desired combination of f's
and x's.
For lists in the length range I am interested in ( from 200 to 1000
elements ) the second solution is slower than the first, despite the
fact that it uses only one Outer. If anyone can think of something
faster than the first solution I would really appreciate it. Otto
Linsuain.