
    Pi$                     
   d dl Z d dlZd dlZd dlmZ d dlmZ d Zd Z	d Z
d Z	 	 	 ddZd Zej                            d          d             Zd Zd Zd Zej                            d          d             Zd ZdS )    N)assert_allclose)odec                 0   | j         \  }}d}t          | dz   d          D ].}t          j        | |                                          r| } n/d}t          |dz
  dd          D ]-}t          j        | |                                          r|} n.||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mus         /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/scipy/integrate/tests/test_banded_ode_solvers.py_band_countr      s    7LE5	
BE6!8Q  71a== 	BE	 
B57Ar""  71a== 	BE	 r6M    c                 ,    |                     |          S )zLinear system dy/dt = a * y)dottyr   s      r   _linear_funcr      s    5588Or   c                     |S )zJacobian of a * y is a. r   s      r   _linear_jacr      s    Hr   c                 b   t                    \  }}fdt          |dd          D             }|                    t          j                             t          d| dz
  d          D ]A}|                    t          j        t          j        |          dg| z  f                    B|S )zBanded Jacobian.c                 f    g | ]-}t           j        d g|z  t          j        |          f         .S )r   )r
   r_r   ).0r   r   s     r   
<listcomp>z&_linear_banded_jac.<locals>.<listcomp>%   s5    DDDaBE1#'271a==()DDDr   r   r   r   )r   r	   appendr
   r   r!   )r   r   r   r   r   bjacr   s     `    r   _linear_banded_jacr&   "   s    ^^FBDDDD5Q3C3CDDDDKK

2s1ub!! 6 6BE"'!Q--!345555Kr   r   皙?bdfTFc	           	         |rt          |           \  }	}
nd}	d}
|r8|rt          t          t                    }n/t          t          t                    }nt          t                    }|t          j        |           rd}nd}|                    ||||	|
dd           d}|                    ||           |	                    |            |
                    |            |g}|                                g}|                                r|j        |k     r|                    |j        |z              |                    |j                   |                    |j                                                   |                                r|j        |k     t          j        |          }t          j        |          }||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vode&.>绽|=)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copy
successfulr   	integrater$   r   array)r   y0tenddtsolverr/   use_jacr.   bandedr0   r1   rt0r   r   s                  r   _solve_linear_sysrE   ,   s   4  "1~~uu  	/L"455AAL+..AA~?1 	FFFV#0" U	     
BBNN1Q	A	A
,,.. QS4ZZ	AC"H		 ,,.. QS4ZZ
 	A
Aa4Kr   c                    t           j                            |           \  }}t           j                            ||          }|t          j        ||                    dd          z            z  }|                    |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   r=   r   lamvcesols           r   _analytical_solutionrR   q   sm     Y]]1FC
	2A	BF32q)))***A
%%**CJr   z"vode integrator is not thread-safe)reasonc            
      &   t          j        ddd          } t          j        g dg dg dg dg dg          }t          j        |          }t          j        |          }t          j        |          }||||gg D ]N}t          j        d	|j        d         d	z             }t          |||           }                    || |f           Ofd
}t          t                              D ]:}	ddgddgddgddgddgg}
t          j        |
 D ]\  }}}}} ||	|||||           ;|d|z  z
  }t          j        t          j        |                    }||gg D ]Q}t          j        d	|j        d         d	z             dz   }t          |||           }                    || |f           Rfd}t          t                              D ]6}	ddgddgddgddgg}
t          j        |
 D ]\  }}}} ||	d||||           7d S )Nr         ?   )g333333r'           rW   rW   )g?g      g?rW   rW   )r'   r'   gٿr'   rW   )rW   g333333?ggg333333ӿ)rW   rW   r'   r'   gffffffr   c                     |          }|          \  }}}	t          |||d         |d         |d         z
  |||||	  	        \  }
}t          |
|           t          ||	           d S Nr   r   r   )r>   r?   r@   r/   rA   r.   rB   rE   r   )idxr@   methrA   with_jacrB   r   r=   t_exacty_exactr   r   real_matricesreal_solutionss               r   
check_realz+test_banded_ode_solvers.<locals>.check_real   s    #-c2GW B&-bk$+AJ$;(.(,)0/7(.0 0 01 	7###7#####r   r+   lsodar(   adamsFTy              ?y              ?c                     |          }|          \  }}}	t          |||d         |d         |d         z
  |||||	  	        \  }
}t          |
|           t          ||	           d S rY   rZ   )r[   r@   r\   rA   r]   rB   r   r=   r^   r_   r   r   complex_matricescomplex_solutionss               r   check_complexz.test_banded_ode_solvers.<locals>.check_complex   s    S!05GW B&-bk$+AJ$;(.(,)0/7(.0 0 01 	7###7#####r   r*   )r
   linspacer<   triutrilaranger   rR   r$   r	   len	itertoolsproductr   )r^   a_reala_real_uppera_real_lowera_real_diagr   r=   r_   rb   r[   pr@   r\   rA   r]   rB   	a_complexa_complex_diagrh   rf   rg   r`   ra   s                      @@@@r   test_banded_ode_solversrw      s    k!S!$$G
 X111111111333111	3 4 4F 76??L 76??L ',''K\<EMN 6 6Yq!'!*q.))&q"g66r7G45555$ $ $ $ $ $ S''(( E EgWT]T]T]	
 8A7H!7L 	E 	E3FD'8VJsFD'8VDDDD	E &I WRWY//00N!>2 9 9Yq!'!*q.))B.&q"g66  "gw!78888$ $ $ $ $ $ S)**++ I IWT]T]T] 09/@!/D 	I 	I+D'8VM#wgxHHHH	II Ir   c                     t          j        |d         d|d         z  d|d         z  |d         z  z   d|d         z  d|d         z  |d         z  z
  d|d         dz  z  z
  d|d         dz  z  |d	         g          S )
Nr   {Gzr        @      {Gz?g    8|A   r
   r<   r   r   s     r   stiff_fr      s    8	!!sQqTzAaD((qtcAaDj1Q4''#!a-7adAg	!   r   c           
          t          j        g dddd|d         z  d|d         z  dgddd|d         z  d	|d         z  z
  d|d         z  dgddd	|d         z  ddgg d
g          S )N)r   r   r   r   r   r   ry   rz   r|   r{   r}            8A)r   r   r   r   r   r   r   s     r   	stiff_jacr      s    8>>>	
Es1Q4x#ad(A>	
T4!A$;1Q4/adA>	
U1Q4ZA>>>>   r   c           
          t          j        dddd|d         z  dgddd|d         z  d|d         z  dgddd|d         z  d|d         z  z
  ddgdd	d|d         z  ddgg          S )
Nr   rz   r{   r|   r   r   ry   r   r}   r   r   s     r   banded_stiff_jacr      s    8	
a#ad(A6	
s1Q4xadA6	
E4!9U1Q4Z'A6	
TU1Q4ZA6	   r   z#lsoda integrator is not thread-safec                      t          dd          \  } }dD ]7}t          |d          \  }}t          ||            t          ||d           8d S )NTF)rA   rB   )TFgH׊>)r2   )_solve_robertson_lsodar   )tfullyfullrA   r   r   s        r   test_banded_lsodar      sp     *$uEEELE5  - -%gt4415!!! 	5t,,,,,- -r   c                     | r|rt           }n
t          }nd }|rd}d}nd }d }t          t          |          }|                    d||dd           d}d}d}t          j        g d	          }	|                    |	|           |g}
|	                                g}|	                                r|j
        |k     r|                    |j
        |z              |
                    |j
                   |                    |j                                                   |	                                r|j
        |k     |j        j        d
         dk    sJ t          j        |
          }
t          j        |          }|
|fS )Nr   r{   rc   r,   r-   )r0   r1   r2   r3   r   
   )rU   rU   rW   rW   rU      )r   r   r   r   r5   r
   r<   r6   r9   r:   r   r;   r$   r   _integratoriwork)rA   rB   jacr0   r1   rC   rD   r?   r>   r=   r   r   s               r   r   r   
  s     	"CCCC GSAW U     
B	
BD	+++	,	,BB	A	A
,,.. QS4ZZ	AC"H		 ,,.. QS4ZZ =r"Q&&&&
A
Aa4Kr   )r   r'   Nr(   TFF)rn   pytestnumpyr
   numpy.testingr   scipy.integrater   r   r   r   r&   rE   rR   markthread_unsaferw   r   r   r   r   r   r   r   r   <module>r      sh            ) ) ) ) ) )           
  
   ),9=27B B B BJ   "FGG^I ^I HG^IF       "GHH	- 	- IH	-) ) ) ) )r   