
    ukik              	          U d Z ddlmZ ddlmZ ddlmZ ddlZddl	m
Z
 ddl	mZ ddlmZ ddlmZmZmZmZmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm
Z  ddl!m"Z"m#Z#m$Z$ d[dZ% ee%d      Z&d Z'ejP                  d        Z)ejP                  d        Z*ejV                  d        Z, G d dejZ                        Z. G d dej^                        Z0 G d d      Z1 e1       Z2 ee1d d         G d  d!      Z3 e3       Z4 ee3d" d#        i Z5e6ejn                  ed$ef   f   e8d%<   i Z9d& Z:d' Z; e:e
jx                          e:e
jz                          e:e
j|                          e:e
j~                          e:e
j                          e:e
j                          e:e
j                          e:e
j                          e:e
j                          e:e
j                          e:e
j                          e:e
j                          e:e
j                          e:e
j                          e:ej                          e:e
j                          e:e
j                          e:e
j                          e:e
j                          e:e
j                         d( ZPd) ZQ ePe
j                          ePe
j                          ePe
j                          ePe
j                          ePe
j                          ePe
j                          ePej                          ePe
j                          ePe
j                          ePe
j                          ePe
j                          ePe
j                          ePe
j                          ePe
j                          ePe
j                          ePe
j                          ePe
j                          ePe
j                          ePe
j                          ePe
j                          ePe
j                          ePe
j                          ePej                         d* Zieie9e
j                  <   d+ Zkeke9e
j                  <   d,emd-end.efd/Zo ePe
j                          eeoe
j                  0      e9e
j                  <    eeoe
j                  0      e9e
j                  <    eeoe
j                  0      e9e
j                  <   d1 Zwd2 Zx ewe
j                  d3        d4 Zz eze
j                  d5         eze
j                  d6         eze
j                  d7         eze
j                  d8         eze
j                  d9         eze
j                   d:         eze
j                  d;         eze
j                  d<         eze
j                  d=         eze
j                  d>         eze
j
                  d?        d@ Zee9e
j                  <   dA Zee9e
j                  <   dB Zee9e
j                  <   dC ZdD Zee9e
j                  <   dE Zee9e
j                   <   dF Zee9e
j$                  <   dG Zee9e
j(                  <   dH Zee9e
j,                  <   dI Zee9e
j0                  <   dJ ZdK Z e eedLe
j6                  e
j8                  f      d      e9e
j:                  <    e eedLe
j6                  e
j8                  f      dM      e9e
j<                  <    e eedMe
j>                  e
j@                  f      d      e9e
jB                  <    e eedMe
j>                  e
j@                  f      dM      e9e
jD                  <   dN Z eee
jH                        e9e
jH                  <    eee
jJ                        e9e
jJ                  <    eee
jL                        e9e
jL                  <   dO Zee9e
jP                  <   dP Z ee
jT                        e9e
jV                  <    ee
jX                        e9e
jZ                  <   dQ Zee9e
j^                  <   dR Zee9e
jb                  <   dS Zee9e
jf                  <   dT Zee9e
jj                  <   dU Zee9e
jn                  <   e#dVejp                  dWemdXeejp                  ef   fdY       ZdZ Zee9ejx                  <   y)\a  Jet is an experimental module for higher-order automatic differentiation
  that does not rely on repeated first-order automatic differentiation.

  How? Through the propagation of truncated Taylor polynomials.
  Consider a function :math:`f = g \circ h`, some point :math:`x`
  and some offset :math:`v`.
  First-order automatic differentiation (such as :func:`jax.jvp`)
  computes the pair :math:`(f(x), \partial f(x)[v])` from the pair
  :math:`(h(x), \partial h(x)[v])`.

  :func:`jet` implements the higher-order analogue:
  Given the tuple

  .. math::
    (h_0, ... h_K) :=
    (h(x), \partial h(x)[v], \partial^2 h(x)[v, v], ..., \partial^K h(x)[v,...,v]),

  which represents a :math:`K`-th order Taylor approximation
  of :math:`h` at :math:`x`, :func:`jet` returns a :math:`K`-th order
  Taylor approximation of :math:`f` at :math:`x`,

  .. math::
    (f_0, ..., f_K) :=
    (f(x), \partial f(x)[v], \partial^2 f(x)[v, v], ..., \partial^K f(x)[v,...,v]).

  More specifically, :func:`jet` computes

  .. math::
    f_0, (f_1, . . . , f_K) = \texttt{jet} (f, h_0, (h_1, . . . , h_K))

  and can thus be used for high-order
  automatic differentiation of :math:`f`.
  Details are explained in
  `these notes <https://github.com/jax-ml/jax/files/6717197/jet.pdf>`__.

  Note:
    Help improve :func:`jet` by contributing
    `outstanding primitive rules <https://github.com/jax-ml/jax/issues/2431>`__.
    )Callable)Any)partialN)lax)api_util)register_pytree_nodetree_structuretreedef_is_leaftree_flattentree_unflatten)ad_util)core)dispatch)linear_util)pjit)sharding_impls)partial_eval)unzip2weakref_lru_cachesafe_zipc                    	 t        t        t        |            \  }t	        t        ||            D ]h  \  }\  }}	t        |      }
t        |
      st        d| d      t	        |	      D ]-  \  }}t        |      }
t        |
      rt        d| d| d       j t        j                  d        } |t        j                  | t        j                  d| |i       	            \  }}|r>|D 	cg c]/  }	t	        |	      D cg c]  \  }}|t        |d
z         z   c}}1 }}}	}t        t        |            j!                  ||      \  }}|r>|D 	cg c]/  }	t	        |	      D cg c]  \  }}|t        |d
z         z   c}}1 }}}	}t#         |       |      t#         |       |      fS # t        $ r d}t        |      dw xY wc c}}w c c}}}	w c c}}w c c}}}	w )uU	  Taylor-mode higher-order automatic differentiation.

  Args:
    fun: Function to be differentiated. Its arguments should be arrays, scalars,
      or standard Python containers of arrays or scalars. It should return an
      array, scalar, or standard Python container of arrays or scalars.
    primals: The primal values at which the Taylor approximation of ``fun`` should be
      evaluated. Should be either a tuple or a list of arguments,
      and its length should be equal to the number of positional parameters of
      ``fun``.
    series: Higher order Taylor-series-coefficients.
      Together, `primals` and `series` make up a truncated Taylor polynomial.
      Should be either a tuple or a list of tuples or lists,
      and its length dictates the degree of the truncated Taylor polynomial.
    factorial_scaled: If True, each term in both the input and output series is scaled
      by the factorial of its order, so that the input and output series is a
      Taylor series. This is the default behavior so that the n-th order term
      in the input and output series is the n-th order derivative of the function.
      If False, the input and output series are the non-factorial scaled Taylor
      coefficients (i.e., the constant coefficients for each term in the Taylor
      series).

  Returns:
    A ``(primals_out, series_out)`` pair, where ``primals_out`` is ``fun(*primals)``,
    and together, ``primals_out`` and ``series_out`` are a
    truncated Taylor polynomial of :math:`f(h(\cdot))`.
    The ``primals_out`` value has the same Python tree structure as ``primals``,
    and the ``series_out`` value the same Python tree structure as ``series``.

  For example:

  >>> import jax
  >>> import jax.numpy as np

  Consider the function :math:`h(z) = z^3`, :math:`x = 0.5`,
  and the first few Taylor coefficients
  :math:`h_0=x^3`, :math:`h_1=3x^2`, and :math:`h_2=6x`.
  Let :math:`f(y) = \sin(y)`.

  >>> h0, h1, h2 = 0.5**3., 3.*0.5**2., 6.*0.5
  >>> f, df, ddf = np.sin, np.cos, lambda *args: -np.sin(*args)

  :func:`jet` returns the Taylor coefficients of :math:`f(h(z)) = \sin(z^3)`
  according to Faà di Bruno's formula:

  >>> f0, (f1, f2) =  jet(f, (h0,), ((h1, h2),))
  >>> print(f0,  f(h0))
  0.12467473 0.12467473

  >>> print(f1, df(h0) * h1)
  0.74414825 0.74414825

  >>> print(f2, ddf(h0) * h1 ** 2 + df(h0) * h2)
  2.9064636 2.9064634
  z;jet terms have inconsistent lengths for different argumentsNzprimal value at position z is not an arrayzterm z for argument c                 N     | | }t        |      \  }}|j                  |       |S N)r   store)fr   argsanstrees        O/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/experimental/jet.pyflatten_fun_outputzjet.<locals>.flatten_fun_output   s*    
T(CS!IC	KKJ    jet
debug_info   )setmaplen
ValueError	enumeratezipr	   r
   lutransformation_with_aux2	wrap_initr   r$   factjet_funjet_subtracecall_wrappedr   )funprimalsseriesfactorial_scaled_ordermsgixtermstreedefjtr    r   out_treetermout_primals	out_termss                      r   r"   r"   O   s   p$S&!"FE !Wf!56 Gma!UQG7#21#5EFGG%  G1q!gW%5>!4DEFFG	G   #ll3&11%grJLM+!X  
 <EU;KLKE4tEAI&L F "<?E:GGQWX+y 
 =Fe<LM[UD$eai(M I 	
K	0.Y2W	WW? 
 $
GC
S/t#$2 M  N s;   F3 1GGGG!,GG!3GGG!F)r6   c                 X    t        j                  t        j                  | dz               S )N      ?)r   explgamma)ns    r   r/   r/      s    	AbD!	""r!   c                     t        j                         } | ||||      \  }}t        ||      D cg c](  \  }}|t        u rt	        j
                  |      g|z  n|* }}}||fS c c}}w r   )r   TraceTagr+   zero_seriesjnp
zeros_like)	r   r8   r4   r5   tagrB   rC   pss	            r   r0   r0      sw    #S%&9+y{I681a /0;.>q!"U*AE 8) 8	i	8s   -A&c                 P   t        j                         5 }t        |||      }t        t	        t
        |      ||      }t        j                  |      5   | | }d d d        t        t        |j                              \  }	}
|	|
fcd d d        S # 1 sw Y   8xY w# 1 sw Y   y xY wr   )	r   take_current_traceJetTracer'   r   	JetTracerset_current_tracer   to_primal_terms_pair)r   rN   r8   r4   r5   parent_tracetrace
in_tracersr   rB   rC   s              r   r1   r1      s      "LS,.EWY.@J				& 
^s $C(B(BC$HIK	!" " " "s#   >BB-BB	BB%c                 ~    t        ||      \  }} | ||      \  }}t        ||f      \  }}	|j                  |	       |S r   )r   r   r   )
r   r   in_tree_defprimals_and_series
primals_in	series_inprimals_out
series_outout_flatout_tree_defs
             r   	traceablerc      sJ    (6HI*ij)4+z'j(AB(L++l	/r!   c                   0    e Zd ZddgZd Zed        Zd Zy)rT   primalr<   c                 j    t        |      t        t        t        fv sJ || _        || _        || _        y r   )type
ZeroSerieslisttuple_tracere   r<   )selfrX   re   r<   s       r   __init__zJetTracer.__init__   s0    ;:tU3333DKDKDJr!   c                 @    t        j                  | j                        S r   )r   get_avalre   rl   s    r   avalzJetTracer.aval   s    ==%%r!   c                     | j                   t        u st        d | j                   D              rt        j                  | j
                        S | S )Nc              3   ,   K   | ]  }|t         u   y wr   	zero_term.0r?   s     r   	<genexpr>z'JetTracer.full_lower.<locals>.<genexpr>   s     'K1Y'K   )r<   rK   allr   
full_lowerre   rp   s    r   r{   zJetTracer.full_lower   s7    zz[ C'K

'K$K__T[[))kr!   N)__name__
__module____qualname__	__slots__rm   propertyrq   r{    r!   r   rT   rT      s*    !) & &r!   rT   c                   @     e Zd ZdZ fdZd Zd Zd Zd Zd Z	 xZ
S )rS   )rN   rW   r8   c                 L    t         |           || _        || _        || _        y r   )superrm   rN   rW   r8   )rl   rN   rW   r8   	__class__s       r   rm   zJetTrace.__init__   s$    	GDH$DDJr!   c                     t        |t              r:|j                  j                  | j                  u r|j                  |j
                  fS |t        fS r   )
isinstancerT   rk   rN   re   r<   rK   )rl   vals     r   rV   zJetTrace.to_primal_terms_pair   s=    #y!cjjnn&@ZZ""+r!   c                    | j                   }t        t        | j                  |            \  }}t	        d |D              rY|j                  | j                  ||      }|j                  r|D cg c]  }t        | |t               c}S t        | |t              S |D 	cg c]  }	|	t        u r
t        g|z  n|	 }}	t        j                  | j                        5  t        ||      D 
cg c]Z  \  }
}|D cg c]I  }|t        u r=t        j                  t!        j"                  |
      t        j$                  |
            n|K c}\ }}}
}t&        |   } |||fi |\  }}d d d        |j                  st        |       S t              D cg c]  \  }}t        | ||       c}}S c c}w c c}	w c c}w c c}}}
w # 1 sw Y   bxY wc c}}w )Nc              3   ,   K   | ]  }|t         u   y wr   rK   rv   s     r   rx   z-JetTrace.process_primitive.<locals>.<genexpr>   s     
/1
/ry   )dtype)r8   r   r'   rV   rz   bind_with_tracerW   multiple_resultsrT   rK   ru   r   rU   r+   rL   zerosnpshaperesult_type	jet_rules)rl   	primitivetracersparamsr8   r]   r^   
primal_outrO   rP   r;   r5   r?   rule	terms_outtss                   r   process_primitivezJetTrace.process_primitive   s   JJE"3t'@'@'#JKJ	

/Y
//,,T->->
FSj		#	#9CDA	$;/DDz;77 $% )*[(8)u$a? %I %			 1 1	2 D %(
I$>@ @ q& 7=>12qI~ IIbhhqk1CD+,- > @i @ y!d":yCFCj)D %%tZ3325j)2LMBia$MM! E%> @D D NsC   .F/F4G+F>
7AF9F>

GG9F>
>GGc           	         t        t        | j                  |            \  }}t        ||f      \  }}t	        t        || j                        |      \  }	}
t        j                  |      }|r ||t        |            n|} |j                  |	g|i |}t         |
       |      \  }}t        ||      D cg c]  \  }}t        | ||       c}}S c c}}w r   )r   r'   rV   r   rc   r1   maincall_param_updatersgetr(   bindr   r+   rT   )rl   call_primitiver   r   r   r]   r^   r\   r[   f_jetrb   update_params
new_paramsresultr_   r`   rO   r   s                     r   process_callzJetTrace.process_call  s    "3t'@'@'#JKJ	&2J	3J&K##LDII$>LE<'++N;M"  ,>(?@(.  ^  J);JzJF,\^VDK03K0LMuq"IdAr"MMMs   :Cc                $    ~~ |j                   | S r   r2   )rl   r   r3   jvpr   symbolic_zeross         r   process_custom_jvp_callz JetTrace.process_custom_jvp_call  s     	33W%%r!   c                 (    ~~~~ |j                   | S r   r   )rl   r   r3   fwdbwdr   	out_treess          r   process_custom_vjp_callz JetTrace.process_custom_vjp_call  s    3Y3W%%r!   )r|   r}   r~   r   rm   rV   r   r   r   r   __classcell__)r   s   @r   rS   rS      s(    .)N2	N&&r!   rS   c                       e Zd Zy)ZeroTermNr|   r}   r~   r   r!   r   r   r         r!   r   c                      yN)r   Nr   zs    r   <lambda>r         r!   c                     t         S r   rt   r7   xss     r   r   r     s    9 r!   c                       e Zd Zy)rh   Nr   r   r!   r   rh   rh      r   r!   rh   c                      yr   r   r   s    r   r   r   "  r   r!   c                     t         S r   r   r   s     r   r   r   "  s    [ r!   .r   c                 2    t        t        |       t        | <   y r   )r   	zero_propr   prims    r   defzeror   ,  s    It,)D/r!   c                 6     | j                   |i |}|t        fS r   )r   rK   )r   r]   r^   r   r   s        r   r   r   /  s"    tyy*//*	[	  r!   c                 2    t        t        |       t        | <   y r   )r   linear_propr   r   s    r   	deflinearr   I  s    K.)D/r!   c                      | j                   |i |}t        | D cg c]  } | j                   |i | }}| j                  rt        | }||fS c c}w r   )r   r+   r   r   )r   r]   r^   r   r   terms_inr`   s          r   r   r   L  sb    tyy*//*?BIO8			8.v.O*O	:&J	Z	 Ps   Ac                     | ^}}t        j                  j                  |g|i |}t        | D cg c](  }t        j                  j                  |d   g|i |* }}||fS c c}w )Nr   )r   dynamic_slice_pr   r+   )r]   r^   r   operandstart_indicesr   r   r`   s           r   _dynamic_slice_jet_ruler   k  s|    &'M""''J-J6J* #Y1 ##((!O}OO 1* 1	Z	1s   -A%c           	          | ^}}}t        j                  j                  ||g| }t        | D cg c])  }t        j                  j                  g |d d |i |+ }}||fS c c}w )N   )r   dynamic_update_slice_pr   r+   )	r]   r^   r   r   updater   r   r   r`   s	            r   _dynamic_update_slice_jet_ruler   t  s    $.!'6M))..wOO* #Y1 **//X"1XXQWX 1* 1	Z	1s   .A%axisreverse
combine_fnc                R    t        t        t        j                  |||      | |      S )N)r   r   )jet2r   r   associative_scan)r]   r^   r   r   r   s        r   _cumulative_jet_ruler   }  s,    
 
gc**JT$&
$ $r!   )r   c                 4    t        t        | |      t        | <   y)zK
  Define the jet rule for a primitive in terms of its first derivative.
  N)r   
deriv_propr   )r   derivs     r   	def_derivr     s     Je4)D/r!   c           	      .   |\  }|\  }| j                  |      }t        |||      \  }}|g|z   |g|z   |gd gt        |      z  z   }	t        dt        |	            D ]+  t	        fdt        ddz         D              z  |	<   - |	^}}
||
fS )Nr%   c              3   @   K   | ]  }||z
     z  |   z    y wr   r   rw   r>   ckus     r   rx   zderiv_prop.<locals>.<genexpr>  &     :Qq1QqS6zAaD :   )r   r   r(   rangesum)r   r   r]   r^   r;   r5   r   c0csvr`   r   r   r   s              @@@r   r   r     s    "!'&yy|*z9-&"b	dRi!cFl!ldVc&k))!CF ?a:%1q5/::Q>AaD?*z	Z	r!   c           
         t        j                  t        j                  | dt	        j
                  t        j                        z        t        j                  t        j                  t        j                  |                         S )N       @)
r   mullax_internal_constr   sqrtpirF   negsquarer;   s    r   r   r     sM    CGG!!!R"''"%%.%89ggcggcjjm,-/ r!   c                 4    t        t        |fi |t        | <   y)zZ
  Define the jet rule for a primitive in terms of a composition of simpler primitives.
  N)r   r   r   )r   compkwargss      r   def_compr     s     D$1&1)D/r!   c                 2    t        j                  |       dz
  S Nr%   )r   rF   r   s    r   r   r     s    
Q r!   c                 2    t        j                  d| z         S r   )r   logr   s    r   r   r     s    A r!   c                     | dz  S )N      ?r   r   s    r   r   r     s
    qCx r!   c                     | | z  S r   r   r   s    r   r   r     s
    Q r!   c                     | dz  S )Ng      r   r   s    r   r   r     s
    T	 r!   c                     t        j                  | t        j                  t        j                  |       dz         z         S r   r   r   r   r   r   s    r   r   r     *    CHHSZZ]Q5F,G(G H r!   c                     t        j                  | t        j                  t        j                  |       dz
        z         S r   r  r   s    r   r   r     r  r!   c                 f    dt        j                  t        j                  d| z   d| z
              z  S )Nr  r%   )r   r   divr   s    r   r   r     s'    cggcgga!eQU.C&D D r!   c                 2    dt        j                  |       z
  S r   )r   erfr   s    r   r   r     s    q3771:~ r!   c                 >    | |t        j                  | |z        z  z
  S r   )r   floorr;   ys     r   r   r     s    Q1q5)9%9!9 r!   c                 V    t        j                  t        j                  | |      |      S r   )r   minmax)ar;   bs      r   r   r     s    cggcggamQ&? r!   c           	      h  	
 | \  }|\  }|g|z   t        j                  |      }|gd gt        |      z  z   t        j                  t        j
                        dz  fd} ||      gd gt        |      dz
  z  z   t        j                  d         gd gt        |      dz
  z  z   t        j                  d         gd gt        |      dz
  z  z   
t        dt        |            D ]  	t        	fdt        d	dz         D              	z  	<   t        	fdt        	dz         D              	<   t        	
fdt        d	dz         D              	z  
	<   
	   z  	<    t        |      	t        	fdt        d	dz         D              	z  	<   ^}}||fS )	Nr   c                 |    t        j                  t        j                  t        j                  |                   S r   )r   r   rF   r   )r  deriv_consts    r   r   z_erf_inv_rule.<locals>.<lambda>  s#    cggk3773::a=+AB r!   r%   r   c              3   @   K   | ]  }||z
     z  |   z    y wr   r   r   s     r   rx   z _erf_inv_rule.<locals>.<genexpr>  r   r   c              3   :   K   | ]  }|z
     |   z    y wr   r   rw   r>   r   r   s     r   rx   z _erf_inv_rule.<locals>.<genexpr>  s!     9qQqsVad]9   c              3   @   K   | ]  }||z
     z  |   z    y wr   r   )rw   r>   r   tmp_exptmp_sqs     r   rx   z _erf_inv_rule.<locals>.<genexpr>  s'     KaQ1%q	1Kr   c              3   @   K   | ]  }||z
     z  |   z    y wr   r   r   s     r   rx   z _erf_inv_rule.<locals>.<genexpr>  s&     81Q1Q3Z!A$8r   )
r   erf_invr(   r   r   r   r   rF   r   r   )r]   r^   r;   r5   r   deriv_yr`   r   r  r   r  r  r   r   s          @@@@@@@r   _erf_inv_ruler#    s   "!'&cFl!{{1~*ldVc&k))! #+B' ztfFa88!JJqt#f+/ ::&WWVAY D6S[1_#=='CK  $a :%1q5/::Q>AaD
 9E!a%L99F1I K5AE?KKaOGAJ #AaD$$ 
&k!	8aQ8	81	<!A$*z	Z	r!   c           	        	 | \  }|\  }|g|z   t        j                  |      gd gt        |      z  z   	t        dt        	            D ]+  t	        	fdt        ddz         D              z  	<   - 	^}}||fS )Nr%   c              3   @   K   | ]  }||z
     z  |   z    y wr   r   rw   r>   r   r   r   s     r   rx   z_exp_taylor.<locals>.<genexpr>  &     8Qq1QqS6zAaD 8r   )r   rF   r(   r   r   )
r]   r^   r7   r;   r5   r   r`   r   r   r   s
          @@@r   _exp_taylorr(    s    "!'&cFl!
wwqzldVc&k))!3q6? =a8%1Q3-881<AaD=*z	Z	r!   c           	        	
 | \  }}t        d | |      \  }}|g|z   	||z  gd gt        |      z  z   
t        dt        
            D ]+  t        	
fdt        ddz         D              z  
<   - 
^}}||fS )Nc                 T    t        j                  |t        j                  |             S r   )r   r   r   r  s     r   r   z_pow_taylor.<locals>.<lambda>  s    3771: 6 r!   r%   c              3   @   K   | ]  }||z
     z  |   z    y wr   r   r&  s     r   rx   z_pow_taylor.<locals>.<genexpr>  r'  r   )r   r(   r   r   )r]   r^   u_r_r;   r5   r   r`   r   r   r   s           @@@r   _pow_taylorr.    s    &"b6
IN)!VcFl!	RxjD6CK''!CF =a8%1Q3-881<AaD=*z	Z	r!   c                     |dk  rt        d| z  |       S |dk(  ry|dz  dk(  rt        | | z  |dz        S |dz  dk(  r| t        | | z  |dz
  dz        z  S y )Nr   r%   r   _pow_by_squaring)r;   rH   s     r   r1  r1    su    UAEA2&&Av1uzAE1q5))1uzAA{333 r!   c                d    dk(  rt        t        j                  | |      S t        fd| |      S )Nr   c                     t        |       S r   r0  r  s    r   r   z%_integer_pow_taylor.<locals>.<lambda>  s    *1a0 r!   )r   rL   	ones_like)r]   r^   r  s     `r   _integer_pow_taylorr5    s-    !Vz9550*iHHr!   c           
        	
 | \  }|\  }|g|z   	t        j                  |      gd gt        |      z  z   

d   d
d   z
  z  gd gt        |      z  z   t        dt        
            D ]_  t	        	fdt        ddz         D              z  
<   d
d   z
  
   z  t	        
fdt        ddz         D              z
  <   a 
^}}||fS )Nr   r%   c              3   @   K   | ]  }||z
     z  |   z    y wr   r   )rw   r>   er   r   s     r   rx   z#_logistic_taylor.<locals>.<genexpr>  r'  r   c              3   :   K   | ]  }|   |z
     z    y wr   r   r  s     r   rx   z#_logistic_taylor.<locals>.<genexpr>  s!     "HQ1Q4!AaC&="Hr  )r   logisticr(   r   r   )r]   r^   r7   r;   r5   r   r`   r8  r   r   r   s          @@@@r   _logistic_taylorr;    s    "!'&cFl!
||A4&3v;..!tq1Q4xTFS[00!CF Ia8%1Q3-881<AaD!H!s"H%1Q3-"HHHAaDI *z	Z	r!   c                     | \  }|\  }d|z  g|D cg c]  }d|z  	 c}z   }|^} }t        | f|f      \  }}|D cg c]  }d|z  	 }}d|z  dz
  |fS c c}w c c}w )Nr   r%   )r;  )	r]   r^   r7   r;   r5   series_r   r   r`   s	            r   _tanh_taylorr>  %  s    "!'&se&1wq7{11!*y+ZNYMJ*j+56G6*6	
Z!	Z	''	 2 7s
   AAc                   	
 | \  }|\  }|g|z   	t        j                  |      gd gt        |      z  z   
t        dt        
            D ]6  t	        	
fdt        d      D              }	   |z  z
  	d   z  
<   8 
^}}||fS )Nr%   c              3   @   K   | ]  }||   z  |z
     z    y wr   r   r&  s     r   rx   z_log_taylor.<locals>.<genexpr>5  s&     6Qq1Q4x!AaC& 6r   r   )r   r   r(   r   r   )r]   r^   r7   r;   r5   convr   r`   r   r   r   s           @@@r   _log_taylorrB  /  s    "!'&cFl!
wwqzldVc&k))!CF $a6%1+66DaD4!8Oqt#AaD$ *z	Z	r!   c           	        
 | \  }}t        j                  ||      }t        t         j                  | |      \  }}t	        j
                  |d      t        fd|f|f      \  }}|g|z   
|g|z   |gd gt        |      z  z   }t        dt        |            D ]+  t        
fdt        ddz         D              z  |<   - |^}}	||	fS )Nr%   c                 \    t        j                  dt        j                  |       z         S r   )r   r
  r   )r;   ones    r   r   z_atan2_taylor.<locals>.<lambda>A  s    #''#q3::a='89 r!   c              3   @   K   | ]  }||z
     z  |   z    y wr   r   r   s     r   rx   z _atan2_taylor.<locals>.<genexpr>F  r   r   )	r   atan2r   r
  r   r   r(   r   r   )r]   r^   r;   r  r   r5   r   r   r   r`   r   r   rE  r   s             @@@@r   _atan2_taylorrH  ;  s    	$!QyyA*377J	2)!VAq!#9A56*M&"b	dRi!cFl!ldVc&k))!CF ?a:%1q5/::Q>AaD?*z	Z	r!   c                    
 | \  }}|\  }}|g|z   }|g|z   d gt        |      z  t        dt                    D ]3  
t        
fdt        d
      D              }|
   |z
  d   z  
<   5 ^}}	||	fS )Nr   c              3   :   K   | ]  }|   |z
     z    y wr   r   )rw   r>   r   r   ws     r   rx   z#_div_taylor_rule.<locals>.<genexpr>S  s!     2qta!f}2r  r(   r   r   )r]   r^   r;   r  x_termsy_termsr   rA  r   r`   r   r   rK  s             @@@r   _div_taylor_rulerO  K  s    	$!Q'7cGm!cGm!fs1vo!CF  a2eAqk22DaD4K1Q4AaD  *z	Z	r!   c           	        	
 |\  }|\  }|g|z   
|\  	 	|      gd gt        |      z  z   	 |      gd gt        |      z  z   t        dt        	            D ]W  t        
fdt        ddz         D              z  	<   t        	
fdt        ddz         D              z  | z  <   Y 	d   	dd  fd   dd  ffS )Nr%   c              3   @   K   | ]  }||   z  |z
     z    y wr   r   r   s     r   rx   z#_sinusoidal_rule.<locals>.<genexpr>a  &     :Qq1Q4x!AaC& :r   c              3   @   K   | ]  }||   z  |z
     z    y wr   r   )rw   r>   r   rP   r   s     r   rx   z#_sinusoidal_rule.<locals>.<genexpr>b  rR  r   r   rL  )signprimsr]   r^   r7   r;   r5   r   r   rP   r   s          @@@@r   _sinusoidal_rulerV  Y  s    "!'&cFl!	$!QtfvF##!tfvF##!CF Fa:%1q5/::Q>AaD:%1q5/::Q>EAaDF A$!"1qu	%%r!   c                       fdS )Nc                       | i |   S r   r   )r   r   r   inds     r   r   z_get_ind.<locals>.<lambda>f  s    D!3F!3C!8 r!   r   )r   rY  s   ``r   _get_indrZ  e  s	    	88r!   r%   c           	         |\  }}|\  }}|g|z   |g|z   d gt              z  }t        | j                  fi |t        dt        |            D ])  t	        fdt        ddz         D              |<   + |^}	}
|	|
fS )Nr   c              3   B   K   | ]  } |   |z
             y wr   r   )rw   r>   r   opr   rK  s     r   rx   z(_bilinear_taylor_rule.<locals>.<genexpr>u  s$     7Ar!A$!A#7s   r%   )r(   r   r   r   r   )r   r]   r^   r   r;   r  rM  rN  r   r   r`   r   r^  r   rK  s              @@@@r   _bilinear_taylor_ruler_  m  s    	$!Q'7cGm!cGm!fs1vo!tyy#F#"CF 8a7q!A#77AaD8*z	Z	r!   c                     | \  }}|\  }}t        j                  j                  ||fi |}|D cg c]#  }t        j                  j                  ||fi |% }	}||	fS c c}w r   )r   gather_pr   )
r]   r^   r   r   r   gsr7   r   gr`   s
             r   _gather_taylor_rulerd  |  sj    %'=
%"a||  -B6B*GIJ!!!!]=f=J*J	Z	 Ks   (Ac                       fd}|S )Nc           	         | \  }|\  } |fi |}|j                  dd       |d   j                  }t        |j                        D cg c]  \  }}|v rdn| }	}}t	        j
                  t        j                  |t	        j                  ||	            |      t	        j                        fd}
|D cg c]
  } |
|       }}||fS c c}}w c c}w )Naxesr   r%   c                     t        j                  t        j                  t        j                  |                   S r   )r   r
  
reduce_sumr   )rc  rg  countslocation_indicatorss    r   _reduce_chooser_taylor_rulez`_gen_reduce_choose_taylor_rule.<locals>.chooser_taylor_rule.<locals>._reduce_chooser_taylor_rule  s1    WW
..$78$
?
 r!   )
popr   r*   r   r   convert_element_typer   _eq_meetreshaperi  )r]   r^   r   r   rb  r   primal_dtyper:   dr   rl  rc  r`   rg  rj  rk  chooser_funs                @@@r   chooser_taylor_rulez;_gen_reduce_choose_taylor_rule.<locals>.chooser_taylor_rule  s    HG
CBW//J::fd#Da5;;L/8/GHtq!!t)Q"HEH22gs{{:u'EF ^^/6F ;==Q-a0=J=z!! I >s   CCr   )rs  rt  s   ` r   _gen_reduce_choose_taylor_ruleru    s    "" 
r!   c                 t   | \  } t        j                  | dd      }t        j                  j                  | fi |}t        j                  t        j
                  | |      t        j                  | d      t        j                  | d            fd}t        | D cg c]
  } ||i | }}||fS c c}w )Nr   r   )r   r[  rE   c                     | z  S r   r   )r  negss    r   r   z"_abs_taylor_rule.<locals>.<lambda>  s    tax r!   )r   	full_likeabs_pr   selectltr+   )	r;   r^   r   zeror   fix_signr   r`   rx  s	           @r   _abs_taylor_ruler    s    "!	q!2	&$yy~~a*6**	CFF1dOS]]1b%93==C;P	Q$(>A9oN((-f-N*N	Z	 Os    B5c                     | ^}t        j                  g| }fd}t        | D cg c]  } || 	 }}||fS c c}w )Nc                 0    t        j                  g| S r   )r   select_n)r7   r   r  s     r   r   z'_select_n_taylor_rule.<locals>.<lambda>  s    s||A++ r!   )r   r  r+   )		primal_inr^   r   casesr   selr   r`   r  s	           @r   _select_n_taylor_ruler    sP    )!e||A&&*+#/2I?8X?*?	Z	 @s   =c                    	 t        j                  |  \  }}||kD  	||k(  t        j                  	||      }	fd}t	        | D cg c]  } |t        j                  |   }}||fS c c}w )Nc                 p    t        j                  | |      }t        j                  | |z   dz  |      }|S z'Select x where x>y or average when x==yr   r   r{  )x_iy_imax_ixeyxgys      r   select_max_and_avg_eqz3_lax_max_taylor_rule.<locals>.select_max_and_avg_eq  4    

3S)

3sAu5r!   )rL   broadcast_arraysr   r{  r+   )
r  r^   r;   r  r   r  r   r`   r  r  s
           @@r   _lax_max_taylor_ruler    s    +DAq
a%C
q&CCA&J [^_hZijh')=)=x)HIjJjz!! ks   	A+c                    	 | \  }}||k  	||k(  t        j                  	||      }	fd}t        | D cg c]  } || 	 }}||fS c c}w )Nc                 p    t        j                  | |      }t        j                  | |z   dz  |      }|S r  r  )r  r  min_ir  r  s      r   select_min_and_avg_eqz3_lax_min_taylor_rule.<locals>.select_min_and_avg_eq  r  r!   )r   r{  r+   )
r  r^   r;   r  r   r  r   r`   r  r  s
           @@r   _lax_min_taylor_ruler    sg    DAq
a%C
q&CCA&J DG	?Sx'2SJSz!! Ts   Ac          
          t        t        j                  j                  ||||||      }| \  }	}
} ||	|
|      }t	        | D cg c]  \  }}} |||
|       }}}}||fS c c}}}w )N)update_jaxprupdate_constsdimension_numbersindices_are_sortedunique_indicesmode)r   r   scatter_add_pr   r+   )r]   r^   r  r  r  r  r  r  r   r   scatter_indicesupdatesr   d1r7   d2r`   s                    r   _scatter_add_ruler    s     
""''l,@Q$6 .T
;$ '1#'?GG_g6*?BIOO)"aR"-O*O	Z	 Ps   A$jaxprr8   returnc                 B   t        j                  t        j                  |       | j                  j
                  j                               }t        t        t        |      |      |      \  }}t        j                  ||      \  }}}	t        j                  ||	      |fS )Nr#   )r,   r.   r   jaxpr_as_funr  r$   with_unknown_namesrc   r0   r1   petrace_to_jaxpr_dynamicClosedJaxpr)
r  r8   primals_and_series_avalsr[   r   r   rb   	jaxpr_jetr7   constss
             r   
_jet_jaxprr    s     	ll4$$U+#kk44GGIK!!',q/5"A;O%22%')Q			)V	,l	::r!   c           	         t        | |f      \  }}t        |d         }t        d |D              }t        |d   |||      \  }}t        |       |z  }	t        |d         |z  }
i |||d   t        j
                  f|	z  z   |d   t        j
                  f|
z  z   |d   d|	z  z   |d   d|
z  z   |d	   d
|	z  z   d}t        j                  j                  |i |}t         |       |      S )Nr   c              3   F   K   | ]  }t        j                  |        y wr   )r   shaped_abstractify)rw   r;   s     r   rx   z!_pjit_jet_rule.<locals>.<genexpr>  s     "Z!4#:#:1#="Zs   !r  out_shardingsin_shardings
in_layoutsr   out_layoutsdonated_invars)F)r  r  r  r  r  r  )
r   r(   rj   r  r   UNSPECIFIEDr   jit_pr   r   )r]   r^   r   r\   r[   r8   r  r  rb   num_series_innum_series_outr   r   s                r   _pjit_jet_ruler    s*   $0*i1H$I!k
il
%""ZGY"ZZ&vg'?N)\j/E)-vo./%7.

 N$>$>#@=#P
P 
!'')N:; <(7]+BBM*W~-EE/08m3KK* ::??.=*=&		//r!   )T)__doc__collections.abcr   typingr   	functoolsr   numpyr   jaxr   r   	jax.numpyrL   jax.tree_utilr   r	   r
   r   r   jax._srcr   r   r   r   r,   r   r   jax._src.interpretersr   r  jax._src.laxr   jax._src.utilr   r   r   r"   r   r/   transformation2r0   r1   r-   rc   TracerrT   TracerS   r   ru   rh   rK   r   dict	Primitive__annotations__r   r   r   le_plt_pgt_pge_peq_pne_pnot_pand_por_pxor_pfloor_pceil_pround_psign_pstop_gradient_pis_finite_pshift_left_pshift_right_arithmetic_pshift_right_logical_pbitcast_convert_type_pr   r   neg_preal_p	complex_pconj_pimag_padd_padd_jaxvals_psub_pconvert_element_type_pbroadcast_in_dim_pconcatenate_psplit_ppad_p	reshape_p	squeeze_prev_ptranspose_pslice_preduce_sum_preduce_window_sum_pfft_pcopy_pdevice_put_pr   r   r   r   intboolr   cumsum_pr   	cumprod_pr  cummax_pr  cummin_pr   r   erf_pr   expm1_plog1p_psqrt_psquare_prsqrt_pasinh_pacosh_patanh_perfc_prem_pclamp_pr#  	erf_inv_pr(  exp_pr.  pow_pr1  r5  integer_pow_pr;  
logistic_pr>  tanh_prB  log_prH  atan2_prO  div_prV  rZ  sincossin_pcos_psinhcoshsinh_pcosh_pr_  dot_general_pmul_pconv_general_dilated_prd  ra  ru  
reduce_maxreduce_max_p
reduce_minreduce_min_pr  rz  r  
select_n_pr  max_pr  min_pr  r  r  rj   r  r  r  r   r!   r   <module>r)     s  &P %      K K    &  # 4 , = =YXv sU+#     " "   &;&tzz ;&|  J	 X35L M  l Z!57P Q AC T$..(38*<<= B
 	-!             		  		    		    

    

         $$ % !! " "" #/  
#))  	#**  	#--  	#**  	#**  	#))  	'

   	#))  	#
$
$ % 	#
 
  ! 	#

  	#++  	#))  	#--  	#--  	#))  	#//  	#++  	#

  	#
!
! " 	#))  	#**  	(

    "9	#

   )G	#
$
$ %$ $t $%-$ 
#,, "#7;>77D	#-- !"6:=''C	#,, !"6:=''C	#,, 5  
#))/02 	. / . / ' (  ' ) * H I H I D E - . 9 : ? @' P )	#--   #	#)) 
  #	#)) 4I  3	#

   -	#.. ( %	#** 	  #	#))   '	#++   (	#)) 
&9  (8"sww>P QSTU	#)) (8"sww>P QSTU	#))  )91sxx>R!SUVW	#**  )91sxx>R!SUVW	#** 
   ''<c>O>OP	#

 4cii@	#)) (/0EsGaGa(b	#
$
$ %  .	#,, & =NN	#

 <NN	#

   (	#))   2	#.. " ,	#)) " ,	#)) 
   1	#

  ;;$';
4S !; ;02 '	$** r!   