You're going to want to use Numpy arrays (if you're not already) to store your data. Then, you can take advantage of array broadcasting with np.newaxis
. For each value in wav
, you're going to want to compute a difference between that value and each value in laser_wav
. That suggests that you'll want a two-dimensional array, with the two dimensions being the wav
dimension and the laser
dimension.
In the example below, I'll pick the first index as the laser
index and the second index as the wav
index. With sample data, this becomes:
import numpy as np
LASER_LEN = 5
WAV_LEN = 10
laser_flux = np.arange(LASER_LEN)
wav = np.arange(WAV_LEN)
laser_wav = np.array(LASER_LEN)
# Tile wav into LASER_LEN rows and tile laser_wav into WAV_LEN columns
diff = wav[np.newaxis,:] - laser_wav[:,np.newaxis]
exp_arg = -diff ** 2
sum_arg = laser_flux[:,np.newaxis] * np.exp(exp_arg)
# Now, the resulting array sum_arg should be of size (LASER_LEN,WAV_LEN)
# Since your original sum was along each element of laser_flux/laser_wav,
# you'll need to sum along the first axis.
result = np.sum(sum_arg, axis=0)
Of course, you could just condense this down into a single statement:
result = np.sum(laser_flux[:,np.newaxis] *
np.exp(-(wav[np.newaxis,:]-laser_wav[:,np.newaxis])**2),axis=0)
Edit:
As noted in the comments to the question, you can take advantage of the "sum of multiplications" inherent in the definition of linear-algebra style multiplications. This then becomes:
result = np.dot(laser_flux,
np.exp(-(wav[np.newaxis,:] - laser_wav[:,np.newaxis])**2))