
    Џkh#                         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                  j                  d
        Zd Zd Zd Zej                  j                  d        Zd Zy)    N)assert_allclose)odec                 ,   | j                   \  }}d}t        | dz   d      D ]+  }t        j                  | |      j	                         s(| } n d}t        |dz
  dd      D ]-  }t        j                  | |      j	                         s(|} ||fS  ||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         i/var/www/teggl/fontify/venv/lib/python3.12/site-packages/scipy/integrate/tests/test_banded_ode_solvers.py_band_countr      s    77LE5	
BE6!8Q 771a=B 
B57Ar" 771a=Br6M	 r6M    c                 $    |j                  |      S )zLinear system dy/dt = a * y)dottyr   s      r   _linear_funcr      s    558Or   c                     |S )zJacobian of a * y is a. r   s      r   _linear_jacr      s    Hr   c           	         t        |      \  }}t        |dd      D cg c]/  }t        j                  dg|z  t        j                  ||      f   1 }}|j                  t        j                  |             t        d| dz
  d      D ]?  }|j                  t        j                  t        j                  ||      dg| z  f          A |S c c}w )zBanded Jacobian.r   r   r   )r   r	   r
   r_r   append)r   r   r   r   r   r   bjacs          r   _linear_banded_jacr#   "   s    ^FB38Q3CDaBEE1#'2771a=()DDDKK
2s1ub! 6BEE"''!Q-!3456K	 Es   4Cc	           	         |rt        |       \  }	}
nd}	d}
|r,|rt        t        t              }n$t        t        t              }nt        t              }|t        j                  |       rd}nd}|j                  ||||	|
dd       d}|j                  ||       |j                  |        |j                  |        |g}|g}|j                         r|j                  |k  rt|j                  |j                  |z          |j                  |j                         |j                  |j                         |j                         r|j                  |k  rtt        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
successfulr   	integrater!   r   array)r   y0tenddtsolverr*   use_jacr)   bandedr+   r,   rt0r   r   s                  r   _solve_linear_sysr?   ,   sE   4 "1~uL"45AL+.A~??1FFV#0" U	   
BBNN1Q	A	A
,,.QSS4Z	ACC"H		 ,,.QSS4Z
 	A
Aa4Kr   c                    t         j                  j                  |       \  }}t         j                  j                  ||      }|t        j                  ||j                  dd      z        z  }|j                  |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   r7   r   lamvcesols           r   _analytical_solutionrL   q   sg     YY]]1FC
		2A	BFF32q))**A
%%*CJr   c            
         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 ]I  }t        j
                  d	|j                  d   d	z         }t        |||       }j                  || |f       K 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 ]L  }t        j
                  d	|j                  d   d	z         dz   }t        |||       }j                  || |f       N fd}t        t                    D ]9  }	ddgddgddgddgg}
t        j                  |
 D ]  \  }}}} ||	d||||        ; y )Nr         ?   )g333333皙?        rQ   rQ   )g?g      g?rQ   rQ   )rP   rP   gٿrP   rQ   )rQ   g333333?ggg333333ӿ)rQ   rQ   rP   rP   gffffffr   c                     |    }|    \  }}}	t        |||d   |d   |d   z
  |||||	      \  }
}t        |
|       t        ||	       y Nr   r   r   )r8   r9   r:   r*   r;   r)   r<   r?   r   )idxr:   methr;   with_jacr<   r   r7   t_exacty_exactr   r   real_matricesreal_solutionss               r   
check_realz+test_banded_ode_solvers.<locals>.check_real   sl    #-c2GW B&-bk$+AJ$;(.(,)0/7(.01 	7#7#r   r&   lsodabdfadamsFTy              ?y              ?c                     |    }|    \  }}}	t        |||d   |d   |d   z
  |||||	      \  }
}t        |
|       t        ||	       y rS   rT   )rU   r:   rV   r;   rW   r<   r   r7   rX   rY   r   r   complex_matricescomplex_solutionss               r   check_complexz.test_banded_ode_solvers.<locals>.check_complex   sl    S!05GW B&-bk$+AJ$;(.(,)0/7(.01 	7#7#r   r%   )r
   linspacer6   triutrilaranger   rL   r!   r	   len	itertoolsproductr   )rX   a_reala_real_uppera_real_lowera_real_diagr   r7   rY   r\   rU   pr:   rV   r;   rW   r<   	a_complexa_complex_diagrc   ra   rb   rZ   r[   s                      @@@@r   test_banded_ode_solversrr      sa    kk!S!$G
 XX11131	3 4F 776?L 776?L '','K\<EMN 6YYq!''!*q.)&q"g6r7G456
$ S'( EgWT]T]T]	
 8A7H7H!7L 	E3FD'8VsFD'8VD	EE &I WWRWWY/0N!>2 9YYq!''!*q.)B.&q"g6  "gw!789
$ S)*+ IWT]T]T] 09/@/@!/D 	I+D'8V#wgxH	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
   r6   r   r   s     r   stiff_fr|      s    88	!!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   rt   ru   rw   rv   rx            8A)r   r   r   r   r   rz   r{   s     r   	stiff_jacr      s}    88>	
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   ru   rv   rw   r~   r   rt   r   rx   rz   r{   s     r   banded_stiff_jacr      s    88	
a#ad(A6	
s1Q4xadA6	
E4!9U1Q4Z'A6	
TU1Q4ZA6	  r   c                      t        dd      \  } }dD ])  }t        |d      \  }}t        ||        t        ||       + y )NTF)r;   r<   )TF)_solve_robertson_lsodar   )tfullyfullr;   r   r   s        r   test_banded_lsodar      sG     *$uELE5  "%gt415!5!"r   c                    | r|rt         }n	t        }nd }|rd}d}nd }d }t        t        |      }|j	                  d||dd       d}d}d}t        j                  g d	      }	|j                  |	|       |g}
|	g}|j                         r|j                  |k  rt|j                  |j                  |z          |
j                  |j                         |j                  |j                         |j                         r|j                  |k  rt|j                  j                  d
   dkD  sJ t        j                  |
      }
t        j                  |      }|
|fS )Nr   rv   r]   r'   r(   )r+   r,   r-   r.   r   
   )rN   rN   rQ   rQ   rN      )r   r   r   r|   r0   r
   r6   r1   r4   r   r5   r!   r   _integratoriwork)r;   r<   jacr+   r,   r=   r>   r9   r8   r7   r   r   s               r   r   r     s3   "CCGSAW U   
B	
BD	+	,BB	A	A
,,.QSS4Z	ACC"H		 ,,.QSS4Z ==r"Q&&&
A
Aa4Kr   )r   rP   Nr^   TFF)ri   pytestnumpyr
   numpy.testingr   scipy.integrater   r   r   r   r#   r?   rL   markthread_unsaferr   r|   r   r   r   r   r   r   r   <module>r      s       )  

 ),9=27BJ ^I ^IF " ")r   