o
    i                     @   sH   d dl Z dededefddZdded	ed
edeeeef fddZdS )    Njneg_hreturnc                    sJ   t  dk rdt|  S t fddt| D }t |  |   S )u1  
    Compute φⱼ(z) where z = -h (negative step size in log-space)
    φ₁(z) = (e^z - 1) / z
    φ₂(z) = (e^z - 1 - z) / z²
    φⱼ(z) = (e^z - Σₖ₌₀^(j-1) zᵏ/k!) / zʲ
    These functions naturally appear when solving:
        dx/dt = A*x + g(x,t)  (linear drift + nonlinear part)
    g|=g      ?c                 3   s"    | ]} | t | V  qd S )N)math	factorial).0kr    J/home/ubuntu/LTX-2/packages/ltx-pipelines/src/ltx_pipelines/utils/res2s.py	<genexpr>   s     zphi.<locals>.<genexpr>)absr   r   sumrangeexp)r   r   	remainderr
   r	   r   phi   s   	r         ?h	phi_cachec2c                    sj   dt dtdtf fdd}|  | }|d|}|| }|  }|d|}|| }	|d|}
|
|	 }|||	fS )a  
    Compute res_2s Runge-Kutta coefficients for a given step size.
    Args:
        h: Step size in log-space = log(sigma / sigma_next)
        phi_cache: Dictionary to cache phi function results. Cache key: (j, neg_h)
        c2: Substep position (default 0.5 = midpoint)
    Returns:
        a21: Coefficient for computing intermediate x
        b1, b2: Coefficients for final combination
    r   r   r   c                    s.   | |f}| v r | S t | |}| |< |S )zGet phi value with caching.)r   )r   r   	cache_keyresultr   r
   r   get_phi%   s   
z'get_res2s_coefficients.<locals>.get_phi      )intfloat)r   r   r   r   neg_h_c2phi_1_c2a21
neg_h_full
phi_2_fullb2
phi_1_fullb1r
   r   r   get_res2s_coefficients   s   




r'   )r   )r   r   r   r   dicttupler'   r
   r
   r
   r   <module>   s    *