I am having trouble iterating every element of an array using the brentq
function. q
in the defined function below is a FITS file array, and we are using every element in this array as inputs to run through the brentq
function in order to solve for T
.
Essentially, my problem lies in not particularly knowing where or how to implement the appropriate for
loop to iterate the function over every element of q
.
Any suggestions on how to go about this problem?
def f(T,q,coeff1,coeff2,coeff3):
return q*const3 - ((exp(const2/T)-1)/(exp(const/T)-1))
a = brentq(f, 10, 435.1, args=(q,4351.041,4262.570,0.206))
print a
newhdu = fits.PrimaryHDU(a)
newhdulist = fits.HDUList([newhdu])
newhdulist.writeto('Temp21DCOT.fits')
Further explanation: The basis of what I'm trying to do is to use brentq
to solve for temperature values using the intensity values of our initial array (our FITS file).
The equation is derived from a ratio of two wavelengths of Plank's Equation, so q = B_1/B_2
if we want to be true to physics, where every element in q
are intensity values. brentq
would solve this analytically insolvable equation for T
(temperature) for every element in q
, and make a new temperature array of the same size as q
. In other words, I am trying to solve for the temperature of every pixel in a FITS file using Plank's Equation.
Note: I re-posted this to clarify the problem in a more efficient manner.
Are you having problems with iteration, or with efficiency?
This iteration works for me:
In the
brentq
docsf
returns one value, for one set ofargs
. There are some solvers, such as theode
ones, that let you define a function that takes a vector variable, and returns a matching vector derivative. It doesn't look like this root finder allows that. So you are stuck with iterating over theargs
values, and solving for each case. I wrote the iteration as a list comprehension. Other iteration formats are possible (for loop etc). We might even be able the wrap thisbrentq
call in a function that could be passed throughnp.vectorize
. But that is still going to be a iteration with minor time savings.There are various ways of handling a multidimensional array. One simple one is to
flatten
the input, do the 1d iteration, and then reshape the result. For example:I left off the
full_output
flag, since that adds complications.