[AstroPy] Composite models or input arrays

Eric Emsellem eric.emsellem at eso.org
Tue Aug 25 07:43:14 EDT 2015


Hi

I just realised something odd (but nice!) about astropy.modeling.

If I have a model, e.g., with 3 parameters (p1, p2, p3), I can do two 
things to get some sort of composite model :

Option 1
 >>> M1 = mymodel(p1=1,p2=1,p3=0)
 >>> M2 = mymodel(p1=2,p2=2,p3=0)

 >>> Mtotal1 = M1 + M2

or

Option 2
 >>> Mtotal2 = mymodel(p1=[1,2],p2=[1,2], p3=[0,0])

In the first case, I'll have parameters set up one block after the other 
(parameters = [1, 1, 0, 2, 2, 0]) while in the second example, I'll have 
them as I did write them: parameters = [1, 2, 1, 2, 0, 0].
In the first case, if I evaluate the model it gives the sum as written 
in Mtotal1(x) = (M1+M2)(x), while in the second case, I get [M1(x), 
M2(x)], so the broadcasting is different indeed.

Both ways are useful. On one hand, Option 1 is the natural way to work 
out a composite model. But Option 2 is using an internal broadcasting I 
guess, hence it is just a matter of providing the right input arrays of 
parameters, and you can always do the sum after the fact by doing e.g., 
Mtotal(x).sum() for example.

However, Mtotal2 does not seem to act on arrays (again broadcasting 
issues?), so I cannot do Mtotal2(x) where x is an array, while it seems 
I can do that with Mtotal1 and it will return the right output.

Is that correct, or am I on the wrong track here?

thanks for any tips :-)

Eric



More information about the AstroPy mailing list