
    IR-e)                     j   d dl Z d dlZd dlZd dlmZmZ d dlmZ	 d dl
mZmZmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z! d dl"m#Z# d dl$m%Z% ej&        '                    dd	 d
d
gfd ddgfd ddgfd ddgfd ddgfg          d             Z(ej&        '                    dd d
d
gfd ddgfd ddgfd ddgfd ddgfg          d             Z)ej&        '                    dd d
fd dfd dfd dfd dfg          d             Z*d  Z+d! Z,d" Z-d# Z.d$ Z/ej&        '                    d% ed            ed           z    ed           z   ed            ed           z
   ed           z   ed            ed           z   ed           z   ed            ed           z   ed           z   ed            ed           z   ed           z   ed&d'd(           ed)d*d+          z  g          d,             Z0d- Z1d. Z2d/ Z3d0 Z4ej&        '                    d1 ed&d'           ed'           ed&d'          g          d2             Z5d3 Z6d4 Z7d5 Z8d6 Z9 G d7 d8e          Z: G d9 d:e          Z;d; Z<d< Z=d= Z>d> Z?d? Z@d@ ZAej&        B                    e% dAB          dC             ZCdD ZDej&        B                    e% dAB          dE             ZEej&        '                    d1 ed*           ed*           ed*          g          dF             ZFdG ZGej&        '                    dHdI dJ dK dL g          dM             ZHdN ZIdO ZJej&        '                    dHdP dQ dR dS g          dT             ZKdU ZLdV ZMdW ZNdX ZOdY ZPej&        B                    e% dAB          dZ             ZQej&        B                    e% dAB          d[             ZRej&        B                    e% dAB          d\             ZSej&        B                    e% dAB          d]             ZTdS )^    N)assert_allcloseassert_array_equal)CompoundModelModelModelDefinitionError)LevMarLSQFitter)Chebyshev1DChebyshev2DConst1D
Gaussian1D
Gaussian2DIdentity
Legendre1D
Legendre2DLinear1DMappingPolynomial1DPolynomial2D
Rotation2DScaleShift	Tabular1D
fix_inputs)	Parameter)	HAS_SCIPY)exprresultc                     | |z   S N xys     Dlib/python3.11/site-packages/astropy/modeling/tests/test_compound.py<lambda>r%   '   
    a!e           @c                     | |z
  S r   r    r!   s     r$   r%   r%   (   r&   r'   g      c                     | |z  S r   r    r!   s     r$   r%   r%   )   r&   r'   g      @c                     | |z  S r   r    r!   s     r$   r%   r%   *   r&   r'   gUUUUUU?c                     | |z  S r   r    r!   s     r$   r%   r%   +   
    ad r'   g       @c                      | t          dd          t          dd                    } |dd          }t          ||           d S )N   r0   r0   n_models)   r3   r   F)model_set_axis)r   r   r   r   souts       r$   test_model_setr8   $   sY     	WVa((('&1*E*E*EFFA
!Ae
$
$
$CsF#####r'   c                     | |z   S r   r    r!   s     r$   r%   r%   7   r&   r'   c                     | |z
  S r   r    r!   s     r$   r%   r%   8   r&   r'   c                     | |z  S r   r    r!   s     r$   r%   r%   9   r&   r'   c                     | |z  S r   r    r!   s     r$   r%   r%   :   r&   r'   c                     | |z  S r   r    r!   s     r$   r%   r%   ;   r-   r'   c                     d}t          j        t          |          5   | t          dd          t          dd                     ddd           dS # 1 swxY w Y   dS )	ziCheck that creating model sets with components whose _n_models are
    different raise a value error
    z+Both operands must have equal values for .*matchr/   r0   r1   r3      N)pytestraises
ValueErrorr   )r   r   MESSAGEs      r$   !test_model_set_raises_value_errorrF   4   s     =G	z	1	1	1 B BWVa((('!a*@*@*@AAAB B B B B B B B B B B B B B B B B Bs   +AAAc                     | |z   S r   r    r!   s     r$   r%   r%   J   r&   r'   c                     | |z
  S r   r    r!   s     r$   r%   r%   K   r&   r'   c                     | |z  S r   r    r!   s     r$   r%   r%   L   r&   r'   c                     | |z  S r   r    r!   s     r$   r%   r%   M   r&   r'   c                     | |z  S r   r    r!   s     r$   r%   r%   N   r-   r'   c                     | t          d          t          d                    }t          |t                    sJ |j        dk    sJ |j        dk    sJ  |d          }||k    sJ t          |t
                    sJ dS )z
    Like test_two_model_class_arithmetic_1d, but creates a new model from two
    model *instances* with fixed parameters.
    r0   r3   rA   r   N)r   
isinstancer   n_inputs	n_outputsfloatr5   s       r$   %test_two_model_instance_arithmetic_1drQ   G   s      	WQZZ$$Aa''''':????;!
!A$$C&====c5!!!!!!!r'   c                     t          d          t          d          z  } t          | t                    sJ | j        dk    sJ | j        dk    sJ  | d          dk    sJ t          d          t          d          z  }t          |t                    sJ |j        dk    sJ |j        dk    sJ  |d          dk    sJ t           |g d          g d           dS )	z\
    Shift and Scale are two of the simplest models to test model composition
    with.
    r0   r3   rA         "@r(   )rA   r0   r3   )r(   g      @rS   N)r   r   rM   r   rN   rO   r   )S1S2s     r$    test_simple_two_model_compose_1drV   c   s     
qE!HH	Bb-(((((;!<12a55C<<<<	qE!HH	Bb-(((((;!<12a55C<<<< rr)))}}ooo66666r'   c                     t          d          t          d          z  } t          | t                    sJ | j        dk    sJ | j        dk    sJ t           | dd          dd           t          d          t          d          z  }t           |dd          d	d           | | z  }t           |dd          d	d           d
S )z7
    A simple example consisting of two rotations.
    -   r0   r   rA   )r   g|=atolZ   )r   rY   N)r   rM   r   rN   rO   r   )r1r2r3s      r$    test_simple_two_model_compose_2dr`   y   s    
 
B*R..	(Bb-(((((;!<1BBq!HHgE2222	B*R..	(BBBq!HHgE2222 
bBBBq!HHgE222222r'   c                      t                      t                      z   } | j        dk    sJ | t                      z   }|j        dk    sJ || z  }|j        dk    sJ || z  }|j        dk    sJ dS )z\
    Test that CompoundModel.n_submodels properly returns the number
    of components.
    r0   r3         N)r   n_submodels)g2g3g5g7s       r$   test_n_submodelsri      s     

	$B>Q	jll	B>Q	bB>Q	bB>Qr'   c                     t          ddd          } t          dddddd          }| | z   }|                                dk    sJ | | z   | z   }|                                dk    sJ | | | z  z   }|                                d	k    sJ | | z  | z   }|                                d
k    sJ | | | z  z   | z   }|                                dk    sJ | | z   | | z   z  }|                                dk    sJ | | z  | | z  z   }|                                dk    sJ | | z  }|                                dk    sJ | | | z  z   }|                                dk    sJ | | z   | z  }|                                dk    sJ | | z   | z  }|                                dk    sJ | | | z  z   }|                                dk    sJ | | z  |z  }|                                dk    sJ | | | z  z  }|                                dk    sJ dS )z\
    Test that the expression strings from compound models are formatted
    correctly.
    rA   r0   r3      rb      z	[0] + [1]z[0] + [1] + [2]z[0] + [1] * [2]z[0] * [1] + [2]z[0] + [1] * [2] + [3]z([0] + [1]) * ([2] + [3])z[0] * [1] + [2] * [3]z
[0] ** [1]z[0] + [1] ** [2]z([0] + [1]) ** [2]z[0] + [1] | [2]z[0] + ([1] | [2])z[0] & [1] | [2]z[0] & ([1] | [2])N)r   r   _format_expression)GG2Ms      r$   test_expression_formattingrq      s    	1aA	Aq!Q1	%	%B	AA!![0000	A	A!!%66666	AE	A!!%66666	A	A!!%66666	AE	AA!!%<<<<<	
Q1q5A!!%@@@@@ 
Q1q5A!!%<<<<<	1A!!\1111	AqDA!!%77777	
Q1A!!%99999	A	A!!%66666	QUA!!%88888	A
A!!%66666	QUA!!%8888888r'   c                      t          d          t          d          z  t          d          t          d          z  z  t          d          z  } t           | j         | dd           d           dS )z
    Test basic inversion of compound models in the limited sense supported for
    models made from compositions and joins only.
    r0   r3   r\   r   rA   r   rA   N)r   r   r   r   inverse)ts    r$   test_basic_compound_inverserv      sd     
qE!HH	qE!HH!45
2FAIAIqqAww'00000r'   modelrA   r0   r3   rk   rb   rl   c                     d}t          j        t          |          5  | j         ddd           dS # 1 swxY w Y   dS )zJ
    Ensure inverses aren't supported in cases where it shouldn't be.
    z3No analytical or user-supplied inverse transform .*r?   N)rB   rC   NotImplementedErrorrt   )rw   rE   s     r$   !test_compound_unsupported_inverserz      s      EG	*'	:	:	:                   s   377c                      t          d          dd          \  } }t          d          t          d          z  } |dd          \  }}t          | |f||f           t          d          t          d          z  } |ddd          \  } }}|t          d          z  } |ddd          \  }}}t          | ||f|||f           dS )zx
    Tests a couple basic examples of the Mapping model--specifically examples
    that merely permute the outputs.
    r\   rA   r0   )rA   r   r3   r0   r   rA   N)r   r   r   r   )	r"   r#   rsx_primey_primemzmsz_primes	            r$   test_mapping_basic_permutationsr      s     :b>>!QDAq	B'&//	)Br!QxxGWQFWg./// 	2q!Aa1ajjGAq!	
WY	B "1aGWgQ1I':;;;;;r'   c            
          t          d          t          d          z  } t          d          t          d          z  }| t          g d          z  |z  }t          d |j         |ddd	           d
           dS )z;Tests inverting a compound model that includes a `Mapping`.g333333(@gffffff*@g,@g.@r|   )r   rA   r0   r   rA   r0   g:0yE>rZ   N)r   r   r   r   rt   )rs1rs2r   s      r$   test_mapping_inverser     s     T

U4[[
(C
T

U4[[
(C
 	giii   3&AIyqy!!Aq!**5EBBBBBBr'   c                      t          d          } t          d          }t          d          }| |z  |z  }t           |dd          ddg           dS )z
    Test a case where an Identity (or Mapping) model is the first in a chain
    of composite models and thus is responsible for handling input broadcasting
    properly.

    Regression test for https://github.com/astropy/astropy/pull/3362
    rA   r\   )angler0   g            ?N)r   r   r   r   )ident1shiftrotationrw   s       r$   test_identity_inputr     s\     a[[F!HHE###HUNX%EEE!QKK$-----r'   c                  `   d} t          j        t          |           5  t          d          t	          ddd          z   ddd           n# 1 swxY w Y   d} t          j        t          |           5  t          d          t	          ddd          z    ddd           dS # 1 swxY w Y   dS )	zq
    Test that certain operators do not work with models whose inputs/outputs do
    not match up correctly.
    zUnsupported operands for |:.*r?   r\   rA   r   皙?Nz6Both operands must match numbers of inputs and outputs)rB   rC   r   r   r   )rE   s    r$   test_invalid_operandsr   .  s0    /G	+7	;	;	; / /2Aq#..../ / / / / / / / / / / / / / / HG	+7	;	;	; / /2Aq#..../ / / / / / / / / / / / / / / / / /s#   "AAA4"B##B'*B'polyc                     g d| _         t          d          }| |z  }t          j        ddddf         \  }} |||          } | | ||                    }t	          ||           dS )zY
    Tests that polynomials are scaled when used in compound models.
    Issue #3699
    rA   r0   r3   rk   rA   r0   r3   N   %   )
parametersr   npmgridr   )r   r   rw   r"   r#   result_compoundr   s          r$   !test_compound_with_polynomials_2dr   =  s     )((DO!HHE5LE8CRC"HDAqeAqkkOU441::FFO,,,,,r'   c            	         t          ddddd          } t          ddddd          }t          | ddi          }t           |d           | dd                     t           |g d           | g dg d	                     t          | d
di          }t           |d           | dd                     | |z  }t          |ddd          }t           |dd           |dddd                     t          |ddd          }t           |dd           |dddd                     t           |dd           |dddd                     d S )NrA   r   r0   g      ?      ?gɿ333333?)r   rA   r3   )r   r   r   r"   r   皙?)rA   r3   g)x0r0   )y0y1)r   r   r   )g1re   sg1_1sg1_2gg1sgg1_1sgg1_2s          r$   test_fix_inputsr   L  s   	Aq!Q	"	"B	CdC	-	-BrAq6""EEE!HHbbAhh'''EE)))$$bbIII&>&>???rC8$$EEE#JJ1c

+++
r'C--..FFF1aLL##aa"5"5666DS1122FFF1aLL##dAsA"6"6777FFaA&&&D!S!(<(<=====r'   c                  |   t          ddddd          } d}t          j        t          |          5  t	          | ddd           d d d            n# 1 swxY w Y   t          j        t          |          5  t	          | ddi           d d d            n# 1 swxY w Y   t          j        t          |          5  t	          | t          j        d          di           d d d            n# 1 swxY w Y   t          j        t          |          5  t	          | t          j        d          di           d d d            n# 1 swxY w Y   t          j        t          |          5  t	          | ddi           d d d            n# 1 swxY w Y   d	}t          j        t          |          5  t	          | d
           d d d            n# 1 swxY w Y   d}t          j        t          |          5  t          d| |            d d d            n# 1 swxY w Y   d}t          j        t          |          5  t	          | ddi          } |dd           d d d            n# 1 swxY w Y   t          j        t          |          5  t	          | t          j        d          di          } |dd           d d d            n# 1 swxY w Y   t          j        t          |          5  t	          | t          j        d          di          } |dd           d d d            d S # 1 swxY w Y   d S )NrA   r   r0   z4Substitution key .* not among possible input choicesr?   )r   r   r3   wz9Expected a dictionary for second argument of "fix_inputs"rs   z.*Illegal operator: ', '#'.*#z,Too many input arguments - expected 1, got 2)r#   )
r   rB   rC   rD   r   r   int32int64r   r   )r   rE   r   s      r$   test_fix_inputs_invalidr   \  s   	Aq!Q	"	"BEG	z	1	1	1 ( (2aA'''( ( ( ( ( ( ( ( ( ( ( ( ( ( ( 
z	1	1	1  21v               
z	1	1	1 ) )2Q'((() ) ) ) ) ) ) ) ) ) ) ) ) ) ) 
z	1	1	1 ) )2Q'((() ) ) ) ) ) ) ) ) ) ) ) ) ) ) 
z	1	1	1 ! !2Qx   ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! KG	z	1	1	1  2v               .G	+7	;	;	; # #c2r"""# # # # # # # # # # # # # # # >G	z	1	1	1  aV$$A               
z	1	1	1  bhqkk1-..A               
z	1	1	1  bhqkk1-..A                 s   AAA7BBB<%C--C14C1%EEE*F		FF1GGG6HHH< I((I,/I,2KKK22L11L58L5c                  f   t          ddddd          } t          ddddd          }t          | j        d           | |z  }d|_        |j        dk    sJ t          |ddd          }|j        dk    sJ t	          ddd          } | | z  }d	|_        t          |ddi          }|j        d
k    sJ d S )NrA   r   )g      g      @r   )r   )gg@g333333g333333@)gg@)r   r0   )r   r   
   r3   )rA   rk   )rl      r   )r   r   bounding_boxr   r   )r   re   r   sgggfs         r$   !test_fix_inputs_with_bounding_boxr     s    	Aq!Q	"	"B	Aq!Q	"	"BBO%?@@@
r'CKCSSSSS	CQ1	&	&B?88888	B1		B
RA%AN	A1v		B?f$$$$$$r'   c                     t          ddd          t          d          z   } t          | d         t                     sJ t          | d         t                    sJ | j        dk    sJ | d         j        dcxk    r| j        k    sn J | d         j        dcxk    r| j        k    sn J | d         j        dcxk    r| j	        k    sn J | d         j        dcxk    r| j
        k    sn J | d         }d| _
        |j        dk    sJ d|_        | j
        dk    sJ t          dddd	
          }t          dd
          }||z   } | d         j        d	k    sJ | d         j        dk    sJ | d	         j        d	k    sJ | d         j        dk    sJ | d         }d| _        |j        dk    sJ d|_        | j        dk    sJ t          | d         t                    sJ t          | d         t                     sJ d}t#          j        t&          |          5  | d          ddd           n# 1 swxY w Y   d}t#          j        t&          |          5  | d          ddd           n# 1 swxY w Y   t)          dddddd	
          }t+          |ddi          } | d	         j        d	k    sJ t          dd
          }t          dd
          }t          dd
          }|||z  z   }	t-           |	dd         d          d           dS )z*Test indexing on compound model instances.rA   r   r   r0   )amplitude_0mean_0stddev_0amplitude_1*      r3   r   namepi90  i  rY   zlist index out of ranger?   Nz%No component with name 'foobar' foundfoobarrk   rb   g?Ag @Bg@Cgףp=
@)r   r   rM   param_names	amplituder   meanr   stddevr   r   r   r   c0_1c0c1c1_1rB   rC   
IndexErrorr   r   r   )
r   constr   r   r   rE   r   r   r   rp   s
             r$   test_indexing_on_instancer     s    	1a

*AadJ'''''adG$$$$$=PPPPP Q4>Q////!-//////Q49%%%%QX%%%%%%Q4;#++++++++++Q4>Q////!-////// aDEAM?b    EO=C 	1a%%%AQS!!!A	AAQ49Q49S6;#S6;#Q4DAF7eDG6T>>>> ae\*****aeZ((((((G	z	1	1	1  	"               7G	z	1	1	1  	(               	1aAqs+++A1q!fAS6;# 	#A#A#A	AE	AJAc#gJqMM#455555s$   	H..H25H2	I++I/2I/c                   Z    e Zd Z eddd          Z edd          Zed             ZdS )_ConstraintsTestAr   r   )defaultminmaxTr   fixedc                 
    | |fS r   r    )r   r   s     r$   evaluatez_ConstraintsTestA.evaluate  s    t|r'   N)__name__
__module____qualname__r   r   r   staticmethodr   r    r'   r$   r   r     sW        YqaS111F9Qd+++D  \  r'   r   c                   >    e Zd Z edd          Zed             ZdS )_ConstraintsTestBr   Tr   c                     | S r   r    )r   s    r$   r   z_ConstraintsTestB.evaluate  s    r'   N)r   r   r   r   r   r   r   r    r'   r$   r   r     sA        9Qd+++D  \  r'   r   c                     t          ddiddi          t          ddi          z   } d| j        v sJ | j        d         dk    sJ | j        j        dk    sJ d| j        v sJ | j        d         du sJ | j        j        du sJ d	| j        v sJ | j        d	         du sJ | j        j        du sJ | j        | d
         j        u sJ d| j        _        | d
         j        j        dk    sJ | d
         j        d         dk    sJ d| j        _        | d
         j        j        dk    sJ | d
         j        d         dk    sJ d| d         j        _        | j        j        du sJ | d         j        j        du sJ | j        d         dk    sJ d| _        d| d
         j        _        | j        d         dk    sJ d| _        | j        d         dk    sJ dS )z
    Various tests for copying of constraint values between compound models and
    their members.

    Regression test for https://github.com/astropy/astropy/issues/3481
    r   )r   r   r   T)boundsr   )r   r   r   mean_1r   )r   g?)r   r   FrA   )r   r   N)	r   r   r   r   r   r   r   r   sync_constraints)rw   s    r$   test_inherit_constraintsr     sK    x264.IIIJtnM M M E %%%%<
#x////> H,,,,u{"""";x D((((<%%%%u{"""";x D((((<%%%%>U1X_,,,, %EN8?!X----8?8$0000&EN8?!Z////8?8$
2222E!HM<&&&&8=%'''' <
#z1111"E%E!HO<
#z1111!E<
#x//////r'   c                     t          ddd          } t          d          }t          d          }| |z  }| |_        ||z  }t	          |                    d           | |j        z  d                     d}t          j        t          |          5  ||z   j         ddd           n# 1 swxY w Y   t          j        t          |          5  || z  j         ddd           dS # 1 swxY w Y   dS )z
    Test that a compound model with a custom inverse has that inverse applied
    when the inverse of another model, of which it is a component, is computed.
    Regression test for https://github.com/astropy/astropy/issues/3542
    rA   r0   )r   r   zTNo analytical or user-supplied inverse transform has been implemented for this modelr?   N)r   r   r   rt   r   rB   rC   ry   )r   scaler   model1model2rE   s         r$   test_compound_custom_inverser     s    aA&&&D!HHE!HHEE\FFN V^FFNN1%%(<u}(<a'@'@AAA
	  
*'	:	:	: ! !	  ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! 
*'	:	:	:    	$                                   s$   B**B.1B.C((C,/C,c                     t          ddd          } t          ddgddgddg          }| |z   }t          j        t          j        |                    }|j        |j        k    sJ |j        j        |j        j        k    sJ t          j        |j	        |j	        k              sJ t          j         |d           |d          k              sJ d	S )
zg
    Regression test for
    https://github.com/astropy/astropy/issues/3867#issuecomment-114547228
    r           r   g       @      @r   r   r   N)
r   pickleloadsdumpsr   	__class__r   r   allr   )r   re   r   m2s       r$   test_pickle_compoundr   9  s     
Cc	"	"B	S#Jc
S#J	7	7B
RA	fl1oo	&	&B=BN****;2<#888886!,"-/000006!!A$$""Q%%-       r'   c                      t          d          } t          d          }| |z  } |d          dk    sJ d| _         |d          dk    sJ d|_         |d          dk    sJ || z  } |d          dk    sJ d S )	NrA   r0   rk   r   V   d   i  i  )r   r   offsetfactor_1)offxsclr   r   s       r$   test_update_parametersr  J  s    88D
((Cs
A1Q4419999DK1Q442::::AJ1Q444<<<<	
TB2a55D======r'   c                     t          d          } t          d          }| |z  }d|_        |j        dk    sJ |j        J d|_        |j        dk    sJ |                    d          }|j        dk    sJ |j        dk    sJ d S )NrA   r0   r   )None_0r   rp   M1)r   r   r   submodel_namesrename)r   r   r   m1s       r$   	test_namer  Y  s    88D
((Cs
ACH222226>>>AF6S====	
$B6T>>>>7d??????r'   c                     t          ddd          } t          ddd          }| |z   }d}t          j        t          |          5  |d          d d d            n# 1 swxY w Y   d| _        |d         j        dk    sJ d|_        d}t          j        t          |          5  |d          d d d            d S # 1 swxY w Y   d S )NrA   r0   z#No component with name 'bozo' foundr?   bozoz0Multiple components found using 'bozo' as name.*)r   rB   rC   r   r   r   )r   re   r   rE   s       r$   test_name_indexr  g  sQ   	Aq!		B	Aq!		B
RA4G	z	1	1	1  	&		              BGV9>QBGAG	z	1	1	1  	&		                 s$   	AA!A#	B99B= B=zrequires scipy)reasonc                      t          g dfg dd          } t          d          }t          d          }t          j        d                              d          }|| z  |z  } |||          \  }}|j        dk    sJ |j        dk    sJ d	S )
zK
    Issue #7411 - evaluate should not change the shape of the output.
    )rA   rb   rc   )         F)pointslookup_tablebounds_errorr0   rA   r  )r3   rk   N)r   r   r   r   arangereshapendim)ru   rotr   r"   rw   x1r   s          r$   test_tabular_in_compoundr  w  s    
 	)))LLLuUUUA
Q--CQA
	"f%%A ECKEU1a[[FB7a<<<<7a<<<<<<r'   c                  	   t                      t          dddddd          z   } d| _        t          j        ddddf         \  }}|dz  }|dz  } | ||d	
          }t          j        ddt          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        g
ddt          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        g
ddt          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        g
ddt          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        g
t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        g
t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        g
t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        g
t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        g
t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        g
t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        g
g
          }t          j        |           }t          ||         ||                     | |dz   |dz   d	
          }t          j        |                                          dk    sJ d S )Nr0   r   r   r3   r   )rs   )r   r   r   r   Twith_bounding_boxg8>@g5B܀@g_P@g]@g(O@gVZ@g
w@ghh=@r   )	r   r   r   r   arraynanisnanr   sum)r   r#   r"   valcomparemaskval2s          r$   test_bounding_boxr%    s   z!S#q!Q777A'AN8AbD!B$JDAq	CA	CA
!AqD
)
)
)C hRVRVRVVRVRVRVRV5RVRVRVVRVRVRVRV5RVRVRVVRVRVRVRV5RVRVRVVRVRVRVRV5VRVRVRVRVVRVRVRVRV5VRVRVRVRVVRVRVRVRV5VRVRVRVRVVRVRVRVRV5VRVRVRVRVVRVRVRVRV5VRVRVRVRVVRVRVRVRV5VRVRVRVRVVRVRVRVRV5'	
 G4 HSMM>DCIwt}---1QUAET222D8D>>3&&&&&&r'   c                     t          j        d          t          j        z  } t          j        d          t          j        z  }t          | |          } |dt          j        z  d          dt          j        z  k    sJ d S )Nrb   rA   Tr  r   )r   r  upixAAr   )r  ltru   s      r$   test_bounding_box_with_unitsr+    sk    Yq\\AE!F	1	B&"A1QY$///3:======r'   c                     g d| _         t          d          }| |z  }t          j        ddd          } ||          } | | |                    }t	          ||           |j        dk    sJ dS )zY
    Tests that polynomials are offset when used in compound models.
    Issue #3699
    r   r3   rb   r   )c0_0c1_0c2_0c3_0c4_0c5_0offset_1N)r   r   r   linspacer   r   )r   r   rw   r"   r   r   s         r$   !test_compound_with_polynomials_1dr6    s     )((DO!HHE5LE
B2AeAhhOU4477^^FFO,,, !      r'   c                     t          dd          t          dd          z  } t          dd          t          dd          z  }| |z  } |dd          d	k    sJ |                    dt          d
d                    } |dd          dk    sJ  |dd          d	k    sJ |                    dd          dk    sJ |                    dt          d          t          d          z            } |dd          d	k    sJ  |dd          dk    sJ |                    dd          dk    sJ | |z   } |d          dk    sJ |                    dt          d
d                    } |d          dk    sJ t	          t          dd          ddi          }| |z  }t           |d          d           |                    dt          d                    }t           |d          d           d}t          j        t          |          5  |                    dt          d                    }ddd           n# 1 swxY w Y   t          ddd           }	t          ddgd!          }
|	|
z  }t           |ddg          d           d"}t          j        t          |          5  |                    dt          dd#                    }ddd           n# 1 swxY w Y   |                    dt          dddgd$                    }t           |ddg          d%           t          d          } t          d          }t          dd&          }||z  }t          d'          |_        | |z  }|                    d&t          d
                    } |dd          d(k    sJ |                    dd)          dk    sJ dS )*z-
    Replace a model in a Compound model
    r0   shift2r   r3   scale3scale2shift3rA   )	   rc   rk   scale4)r  rc   r  rc   )rA   r0   )   rc   r>        r\   rotate)r   r   r   )rA      )rY   rB  z"No submodels found named not_therer?   	not_thereNr   )degreer2   r   r1   z6New and old models must have equal values for n_models)rE  r   )rE  r   r2   )r0   rk   S3g)r0   r   r   )r   r   replace_submodelrt   r   r   r   rB   rC   rD   r   r   )rT   rU   r   r   m3Rm4m5rE   PSrF  S23s                r$   test_replace_submodelrO    s    
qx	 	 	 5#:#:#:	:B	qx	 	 	 5#:#:#:	:B
RA1Q77f	
		HeAH&=&=&=	>	>B2a88w1Q77f::b!&&&& 
		HeAhhq&9	:	:B1Q77f2a88w::b!&&&& 	RA1Q442::::	
		HeAH&=&=&=	>	>B2a55B;;;; 	:BX666A??A	aBBBqEE7###			Xz#	7	7BBBqEE8$$$ 4G	z	1	1	1 7 7U1XX667 7 7 7 7 7 7 7 7 7 7 7 7 7 7 	A777Aq!fq!!!A	AAqq!Qyy&)))GG	z	1	1	1 F FA!(D(D(DEEF F F F F F F F F F F F F F F	
		FLq!fq$Q$Q$Q	R	RBrr1a&zz6*** 
qB	qB	qt			B
r'C++CK
SA 
		D%((	+	+B2a88v::av%%%%%%s$   $I

II7&K))K-0K-r   c                     | |z   S r   r    r  r   s     r$   r%   r%     
    rBw r'   c                     | |z
  S r   r    rQ  s     r$   r%   r%     rR  r'   c                     | |z  S r   r    rQ  s     r$   r%   r%     rR  r'   c                     | |z  S r   r    rQ  s     r$   r%   r%     rR  r'   c           
      b   t          j        ddd          }t          j        g d          }t          j        g d          }t          d          }t	          ddd          } | ||          }t           |j        |g||R   |  |j        |g|R   |j        |g|R                       dS )	t
    Tests that compound evaluate function produces the same
    result as the models with the operator applied
    r-  rb   r   r   )rA   r   r   r0   rA   N)r   r5  r  r   r   r   r   r   r"   p1p2r   r   compounds          r$   test_compound_evaluater\    s     	B2A	$$$	%	%B	+++		B!__F1a  FtFF##H!&b&2&&&_V_Q$$$$ofoa&="&=&=&=>>    r'   c                  D   t          j        ddd          } t          j        g d          }t          j        dg          }t          ddd          }t	          d          }||z  }t           |j        | g||R   |j        | g|R   |j        | g|R  z             dS )	z
    Tests that compound evaluate function produces the same
    result as the models with the power operator applied
    r-  rb   r   rA   r   r   r3   r0   rA   Nr   r5  r  r   r   r   r   r"   rY  rZ  r   r   r[  s         r$   test_compound_evaluate_powerrb  5  s    
 	B2A	+++		B	1#B1a  FQZZFv~H!&b&2&&&B?6?1#:r#:#:#::    r'   c                  (   t          j        ddd          } t          j        ddd          }t          dddddd          }t          d          }t          d          }t          dddddd          t          d          z  t          d          z  }t	          |                    | || dz
  |dz   dddddddd          |                    | |dddddd          |                    | dz
  d          |                    |dz   d          g           d S )Nr-  rb   r   rA   r   r0   r   )r   r5  r   r   r   r   )r"   r#   r  r   rH  r   s         r$   #test_compound_evaluate_double_shiftrd  H  s   
B2A
B2A	Aq!Q1	%	%B	qB	qB1aAq!$$uQxx/%((:A	

1aRRAq!Q1a@@KK1aAq!Q//KKB""KKB""	
    r'   c                     | |z   S r   r    rQ  s     r$   r%   r%   ]  rR  r'   c                     | |z
  S r   r    rQ  s     r$   r%   r%   ^  rR  r'   c                     | |z  S r   r    rQ  s     r$   r%   r%   _  rR  r'   c                     | |z  S r   r    rQ  s     r$   r%   r%   `  rR  r'   c           
         t          j        ddd          }t          j        g d          }t          j        g d          }t          ddd          }t          ddd          } | ||          }t	           |j        |g|R |d         |d         |d         d	 |  |j        |g|R   |j        |g|R                       d
S )rW  r-  rb   r   r_  )r3   r   r   r0   rA   r   r   r   r   N)r   r5  r  r   r   r   rX  s          r$   "test_compound_evaluate_named_paramrk  Z  s     	B2A	+++		B	---	 	 B1a  F1a  FtFF##H!RbRRbeBqEBqERRR_V_Q$$$$ofoa&="&=&=&=>>    r'   c            	      n   t          j        ddd          } t          j        g d          }t          j        dg          }t          ddd          }t	          d          }||z  }t           |j        | g|R |d         |d         |d         d	 |j        | g|R   |j        | g|R  z             d
S )r^  r-  rb   r   r_  r3   r0   rA   r   rj  Nr`  ra  s         r$   'test_compound_evaluate_name_param_powerrm  v  s    
 	B2A	+++		B	1#B1a  FQZZFv~H!RbRRbeBqEBqERRRB?6?1#:r#:#:#::    r'   c                  L   t          j        ddd          } t          j        g d          }t          j        dg          }t                      }t	                      }||z  }t           |j        | | g||R   |j        | g|R  |                    | |          g           dS )rW  r-  rb   r   )rA   r   r   r3   N)r   r5  r  r   r   r   r   ra  s         r$   test_compound_evaluate_andro    s    
 	B2A	---	 	 B	1#B\\FWWFH!Q((R(((		 R	 	 	 &//!R"8"89    r'   c            	         t          j        ddd          } t          j        dg          }t          j        dg          }t          j        dg          }t          j        dg          }t          d          }t	          ddd          }||z  }t          |                    | ||||          |                    |                    | |          |||                     d	S )
rW  r-  rb   r   r   r3   r   r   rA   N)r   r5  r  r   r   r   r   )r"   rY  p2_amplitudep2_meanp2_stdr   r   r[  s           r$   test_compound_evaluate_orrt    s    
 	B2A	3%B8QC==LhsmmGXse__F3ZZF1c""FH!Rw??2..gvNN    r'   c                      t           j        ddddf         \  } }g d}t          dddd          }t          |d|d	z   i          }t	           |j        || g|R   |j        |d	z   | g|R             dS )
z
    Tests that compound evaluate function produces the same
    result as the models fix_inputs operator is applied
    when using the keyword
    Nr   r3   r   r   rA   r   r   rA   r0   r   r   r"   rb   r   r   r   r   r   r   r#   r"   model_paramsrw   r[  s        r$   ,test_compound_evaluate_fix_inputs_by_keywordrz    s     8CRC"HDAq)))Lq!Q$$E%#q1u..H!Q....q1ua/,///    r'   c                      t           j        ddddf         \  } }g d}t          dddd          }t          |d|dz   i          }t	           |j        || g|R   |j        |dz   | g|R             dS )	z
    Tests that compound evaluate function produces the same
    result as the models fix_inputs operator is applied
    when using the input index
    Nr   rv  rA   r0   r   r   rb   rw  rx  s        r$   -test_compound_evaluate_fix_inputs_by_positionr|    s     8CRC"HDAq)))Lq!Q$$E%!QU,,H!Q....q1ua/,///    r'   c                  @   t                      } t          j        ddd          t          j        z  }t          j        ddd          t          j        z  t          j        z  t          j        z  }t          dt          j        z  t          j        z  t          j        z  dt          j        z  t          j        z            }t          dt          j        z  t          j        z  d	t          j        z            }||z  } | |||          } ||          } ||          }|j        |j        cxk    r*t          j        t          j        z  t          j        z  k    sn J t          ||           |j
        D ]&}	t          ||	          t          ||	          k    sJ 'd
S z*
    Regression test for issue #12320
    r   rA   e   rb   r   r   )slope	interceptr   g      $@Nr   r   r5  r'  r6   r   kgr   unitr   r   getattr
fitterr"   r#   r  r   truthfitunfit_output
fit_outputr   s
             r$   3test_fit_multiplied_compound_model_with_mixed_unitsr    sj    F
Aq#$A
Ar3!#%,qs2A	AC!#+sQSy13	G	G	GB	ad
QS(D14K	@	@	@BGE
&1

C588LQJ
EEEEAD13J4DEEEEEEL*---! : :ud##wsD'9'999999: :r'   c                  d   t                      } t          j        ddd          t          j        z  }t          j        ddd          t          j        z  t          j        z  t          j        z  t          j        z  }t          dt          j        z  t          j        z  t          j        z  dt          j        z  t          j        z            }t          dt          j        z  t          j        z  d	t          j        z            }t          dt          j        z  t          j        z  d	t          j        z            }||z  |z  } | |||          } ||          } ||          }	|j        |	j        cxk    r7t          j        t          j        z  t          j        z  t          j        z  k    sn J t          ||	           |j
        D ]&}
t          ||
          t          ||
          k    sJ 't          j        ddd          t          j        z  }t          j        ddd          t          j        z  t          j        z  t          j        z  t          j        z  t          j        z  }t          dt          j        z  t          j        z  t          j        z  dt          j        z  t          j        z            }t          dt          j        z  t          j        z  d	t          j        z            }t          dt          j        z  t          j        z  d	t          j        z            }t          dt          j        z  t          j        z  d	t          j        z            }||z  }||z  }||z  } | |||          } ||          } ||          }	|j        |	j        cxk    rDt          j        t          j        z  t          j        z  t          j        z  t          j        z  k    sn J t          ||	           |j
        D ]&}
t          ||
          t          ||
          k    sJ 'd
S r~  r  r  r"   r#   r  r   rH  r  r  r  r  r   rJ  m11m22s                 r$   =test_fit_multiplied_recursive_compound_model_with_mixed_unitsr    sh    F
Aq#$A
Ar3!#%+ad2QS8A	AC!#+sQSy13	G	G	GB	ad
QS(D14K	@	@	@B	ac	AC4!#:	>	>	>BGbLE
&1

C588LQJ
KKKKAD13J4Dqs4JKKKKKKL*---! : :ud##wsD'9'999999
Aq#$A
Ar3!#%+ad2QT9AC?A	AC!#+sQSy13	G	G	GB	ad
QS(D14K	@	@	@B	ac	AC4!#:	>	>	>B	ad
QS(D14K	@	@	@B
r'C
r'C#IE
&1

C588LQJ
RRRRAD14K!#4E4Kac4QRRRRRRL*---! : :ud##wsD'9'999999: :r'   c                  @   t                      } t          j        ddd          t          j        z  }t          j        ddd          t          j        z  t          j        z  t          j        z  }t          dt          j        z  t          j        z  t          j        z  dt          j        z  t          j        z            }t          dt          j        z  t          j        z  d	t          j        z            }||z  } | |||          } ||          } ||          }|j        |j        cxk    r*t          j        t          j        z  t          j        z  k    sn J t          ||           |j
        D ]&}	t          ||	          t          ||	          k    sJ 'd
S r~  r   r   r5  r'  r6   r  r   r   r  r   r   r  r  s
             r$   0test_fit_divided_compound_model_with_mixed_unitsr     sj    F
Aq#$A
Ar3!$&,qs2A	AD13,ad
QS8H	I	I	IB	ac	AC4!#:	>	>	>BGE
&1

C588LQJ
EEEEAD13J4DEEEEEEL*---! : :ud##wsD'9'999999: :r'   c                  d   t                      } t          j        ddd          t          j        z  }t          j        ddd          t          j        z  t          j        z  t          j        z  t          j        z  }t          dt          j        z  t          j        z  t          j        z  dt          j        z  t          j        z            }t          dt          j        z  t          j        z  d	t          j        z            }t          dt          j        z  t          j        z  d	t          j        z            }||z  |z  } | |||          } ||          } ||          }	|j        |	j        cxk    r7t          j        t          j        z  t          j        z  t          j        z  k    sn J t          ||	           |j
        D ]&}
t          ||
          t          ||
          k    sJ 't          j        ddd          t          j        z  }t          j        ddd          t          j        z  t          j        z  t          j        z  t          j        z  t          j        z  }t          dt          j        z  t          j        z  t          j        z  dt          j        z  t          j        z            }t          dt          j        z  t          j        z  d	t          j        z            }t          dt          j        z  t          j        z  d	t          j        z            }t          dt          j        z  t          j        z  d	t          j        z            }||z  }||z  }||z  } | |||          } ||          } ||          }	|j        |	j        cxk    rDt          j        t          j        z  t          j        z  t          j        z  t          j        z  k    sn J t          ||	           |j
        D ]&}
t          ||
          t          ||
          k    sJ 'd
S r~  r  r  s                 r$   8test_fit_mixed_recursive_compound_model_with_mixed_unitsr  9  sh    F
Aq#$A
Ar3!$&,qs2QS8A	AD13,ad
QS8H	I	I	IB	ac	AC4!#:	>	>	>B	ac	AC4!#:	>	>	>BGbLE
&1

C588LQJ
KKKKAD13J4Dqs4JKKKKKKL*---! : :ud##wsD'9'999999
Aq#$A
Ar3!$&-3ac9AC?A	AD13,ad
QS8H	I	I	IB	ac	AC4!#:	>	>	>B	ac	AC4!#:	>	>	>B	ad
QS(D14K	@	@	@B
r'C
r'C#IE
&1

C588LQJ
RRRRAD14K!#4E4Kac4QRRRRRRL*---! : :ud##wsD'9'999999: :r'   )Ur   numpyr   rB   numpy.testingr   r   astropy.unitsunitsr'  astropy.modeling.corer   r   r   astropy.modeling.fittingr   astropy.modeling.modelsr	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   astropy.modeling.parametersr   "astropy.utils.compat.optional_depsr   markparametrizer8   rF   rQ   rV   r`   ri   rq   rv   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  skipifr  r%  r+  r6  rO  r\  rb  rd  rk  rm  ro  rt  rz  r|  r  r  r  r  r    r'   r$   <module>r     s         = = = = = = = =       L L L L L L L L L L 4 4 4 4 4 4                                     & 2 1 1 1 1 1 8 8 8 8 8 8 		c3Z(		dD\*		c3Z(		i34		S#J'	 	$ $	 	$ 		c3Z(		dD\*		c3Z(		i34		S#J'	 	B B	 	B 		S!		T"		S!		Y'		C 	 	" "	 	"$7 7 7,3 3 3*  &89 89 89v1 1 1 a5588eeAhh&a5588eeAhh&a5588eeAhh&a5588eeAhh&aEE!HHuuQxx'
1ajjAq111
 
 
 
< < <*C C C. . . / / / ++a"3"3\\!__jjQRTUFVFV!WXX- - YX-> > > & & &R% % %&<6 <6 <6~              -0 -0 -0`     B! ! !"        	M*:;;  <;"&' &' &'T 	M*:;;> > <;> ++a..**Q--a!QRR  SR.A& A& A&H 
	   (  &  $ 
	   &  &  &  *  &  & 	M*:;;: : <;:0 	M*:;;,: ,: <;,:^ 	M*:;;: : <;:0 	M*:;;,: ,: <;,: ,: ,:r'   