Question about scientific calculations in Python
Andrew Dalke
dalke at dalkescientific.com
Wed Mar 13 20:07:54 EST 2002
Martin Kaufmann:
>I didn't quite understand this part. How should I precompute the zero
>entries? How do I know where they are? Or should I just cut them out
>before and access the histogram differently?
You have
for s in s_vector[1:]:
sum = 0
for entry in hist[1:]:
if entry[1] == 0:
continue
x = 2 * pi * s * entry[0]
sum = sum + (2 * entry[1] * (sin(x) / x))
intensity = atoms * pow(f_s_vector[n], 2) * (1 + sum / atoms)
i_vector.append(intensity)
n = n + 1
The hist entries are not dependent on s, so the non-zero elements
can be pulled out beforehand.
nonzero_hist = [entry for entry in hist[1:] if entry[1] != 0]
for s in s_vector[1:]:
sum = 0
for entry in nonzero_hist:
x = 2 * pi * s * entry[0]
sum = sum + (2 * entry[1] * (sin(x) / x))
intensity = atoms * pow(f_s_vector[n], 2) * (1 + sum / atoms)
i_vector.append(intensity)
n = n + 1
>The list are not that long. Probably only a couple of thousands
>entries.
Then don't worry about that tweak.
>I tried to use weave.inline. Here is my code. But put your coffee away
>before you read it (you might spill it otherwise over your keyboard)
>and _please_ don't laugh out loudly.. I really didn't know what I was
>doing, but it somehow worked...
Well, I don't drink coffee. But then, I also don't often write Python/C
extension code. Someone else will need to check that code.
> The speed of the weave solution is quite incredible!
Factor of 8 is pretty nice, and within the normal rule of thumb people
expect for an implementation in C.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list