o
    hH                     @   s|   d dl Z d dlZd dlZd dlmZ d dlmZ dd Zdd Z	dd	 Z
d
d Z			dddZdd Zejjdd ZdS )    N)assert_allclose)odec                 C   s~   | j \}}d}t| d dD ]}t| | r| } nqd}t|d ddD ]}t| | r:|} ||fS q)||fS )z7Returns ml and mu, the lower and upper band sizes of a.r      )shaperangenpdiagany)anrowsncolsmlkmu r   a/var/www/vscode/kcb/lib/python3.10/site-packages/scipy/integrate/tests/test_banded_ode_solvers.py_band_count   s   
r   c                 C   s
   | |S )zLinear system dy/dt = a * y)dottyr   r   r   r   _linear_func   s   
r   c                 C   s   |S )zJacobian of a * y is a.r   r   r   r   r   _linear_jac   s   r   c                    sv   t  \}} fddt|ddD }|t  td| d dD ]}|tjt |dg|  f  q$|S )zBanded Jacobian.c                    s(   g | ]}t jd g| t  |f qS )r   )r   r_r	   ).0r   r   r   r   
<listcomp>%   s   ( z&_linear_banded_jac.<locals>.<listcomp>r   r   r   )r   r   appendr   r	   r   )r   r   r   r   r   bjacr   r   r   r   _linear_banded_jac"   s   &r    r   皙?bdfTFc	              	   C   s  |r	t | \}	}
nd}	d}
|r|rttt}n
ttt}ntt}|du r/t| r-d}nd}|j||||	|
ddd d}||| |	|  |
|  |g}|g}| ry|j|k ry||j|  ||j ||j | ry|j|k s\t|}t|}||fS )a  Use scipy.integrate.ode to solve a linear system of ODEs.

    a : square ndarray
        Matrix of the linear system to be solved.
    y0 : ndarray
        Initial condition
    tend : float
        Stop time.
    dt : float
        Step size of the output.
    solver : str
        If not None, this must be "vode", "lsoda" or "zvode".
    method : str
        Either "bdf" or "adams".
    use_jac : bool
        Determines if the jacobian function is passed to ode().
    with_jacobian : bool
        Passed to ode.set_integrator().
    banded : bool
        Determines whether a banded or full jacobian is used.
        If `banded` is True, `lband` and `uband` are determined by the
        values in `a`.
    Nzvodevodeg&.>g|=)with_jacobianmethodlbandubandrtolatolr   )r   r   r   r    r   r   iscomplexobjset_integratorset_initial_valueset_f_paramsset_jac_params
successfulr   	integrater   r   array)r   y0tenddtsolverr&   use_jacr%   bandedr'   r(   rt0r   r   r   r   r   _solve_linear_sys,   sB   




r;   c                 C   sH   t j| \}}t j||}|t ||dd  }||j}|S )z
    Analytical solution to the linear differential equations dy/dt = a*y.

    The solution is only valid if `a` is diagonalizable.

    Returns a 2-D array with shape (len(t), len(y0)).
    r   r   )r   linalgeigsolveexpreshaper   T)r   r3   r   lamvcesolr   r   r   _analytical_solutionq   s
   rG   c               	      s  t ddd} t g dg dg dg dg dg}t |}t |}t |}||||gg D ]}t d	|jd d	 }t||| }|| |f q4fd
d}t	t
D ])}	ddgddgddgddgddgg}
tj|
 D ]\}}}}}||	||||| quq]|d|  }t t |}||g g  D ]}t d	|jd d	 d }t||| }|| |f q fdd}t	t
 D ]%}	ddgddgddgddgg}
tj|
 D ]\}}}}||	d|||| qqd S )Nr   g      ?   )g333333r!           rI   rI   )g?g      g?rI   rI   )r!   r!   gٿr!   rI   )rI   g333333?ggg333333ӿ)rI   rI   r!   r!   gffffffr   c                    \    |  }|  \}}}	t |||d |d |d  |||||d	\}
}t|
| t||	 d S Nr   r   r   )r4   r5   r6   r&   r7   r%   r8   r;   r   idxr6   methr7   with_jacr8   r   r3   t_exacty_exactr   r   )real_matricesreal_solutionsr   r   
check_real      

z+test_banded_ode_solvers.<locals>.check_realr$   lsodar"   adamsFTy              ?y              ?c                    rJ   rK   rL   rM   )complex_matricescomplex_solutionsr   r   check_complex   rV   z.test_banded_ode_solvers.<locals>.check_complexr#   )r   linspacer2   triutrilaranger   rG   r   r   len	itertoolsproductr	   )rQ   a_reala_real_uppera_real_lowera_real_diagr   r3   rR   rU   rN   pr6   rO   r7   rP   r8   	a_complexa_complex_diagr[   r   )rY   rZ   rS   rT   r   test_banded_ode_solvers   s\   



rj   )r   r!   Nr"   TFF)ra   pytestnumpyr   numpy.testingr   scipy.integrater   r   r   r   r    r;   rG   markthread_unsaferj   r   r   r   r   <module>   s     

E