I have been using Mathematica for some time now but I have a feeling that I am not using it as efficiently as it could be.

I have the following problem which I am unable to figure out and it would be great if someone could point me in the right direction:

I have a function which is of the following form:

f[x_]:= Total[A1/(A2+x),Infinity];

where A1 and A2 are very large arrays (2500x2500 real numbers)
(Sorry for the way I wrote the function but I haven't figured out how to insert code into the question (not very good at this type of typesetting))

The problem is when I want to create another list (large list) of values of f[x]. It seems that Total is what slows things down here and I was wondering if there is a way to replace Total with something faster.

Are A1 and A2 different each time you call f or are they constant?
–
wxfflesOct 11 '12 at 21:26

1

Mine is very fast, I can run about 10000 x values per second. What hardware are you using?
–
s0rceOct 11 '12 at 21:50

1

@user72409 actually, what are the exact sizes of A1 and A2? You said 2D Array, we've been using single arrays in answers...
–
tkottOct 11 '12 at 21:59

1

A1 and A2 are 2500x2500 (but this is an example, it could be more but let's go with this because I know that it takes about 10 mins on my machine). But as I said before, these remain constant. This was the whole idea. I wanted to precalculate them in order to gain speed, and I did but the final Table[f[x],{x,1,2500}], this takes 10 mins to calculate.
–
lucianOct 11 '12 at 22:06

Odd, I tried f[x_] = Total[a1/(a2 + x), Infinity] followed by Timing[f[#] & /@ RandomReal[{0, 1}, 2000]][[1]] and it is about 4 seconds on my machine vs 0.25 for your solution. I would have thought that precomputing the Total would have been faster. Any ideas?
–
tkottOct 11 '12 at 21:56

I am afraid that I do not understand how you made f faster. Can you explain a bit more please?
–
lucianOct 11 '12 at 22:10

@user72409 sorry I botched the pasting and did not actually paste the faster version. In any case I don't know if this will remain faster for such large arrays (and it needs modification to work with 2D arrays anyway; perhaps you could mention this in your question!)
–
aclOct 11 '12 at 22:14

@acl Thank you fo the extensive post. I have never used Compile successfully but I will try and give it another go.
–
lucianOct 11 '12 at 22:27

@acl I tried running my problem using Table instead of ParallelTable but the latter is considerably faster. I get 649 seconds vs a lot more for the case of Table (I actually stopped it after it passed the 10 minute mark)
–
lucianOct 11 '12 at 22:31

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.