
    sg                     ^    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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         `/var/www/html/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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   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   r5   r   lamvcesols           r   _analytical_solutionrJ   p   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   g      ?   )g333333皙?        rN   rN   )g?g      g?rN   rN   )rM   rM   gٿrM   rN   )rN   g333333?ggg333333ӿ)rN   rN   rM   rM   gffffffr   c                     |    }|    \  }}}	t        |||d   |d   |d   z
  |||||	      \  }
}t        |
|       t        ||	       y Nr   r   r   )r6   r7   r8   r(   r9   r'   r:   r=   r   )idxr8   methr9   with_jacr:   r   r5   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 rP   rQ   )rR   r8   rS   r9   rT   r:   r   r5   rU   rV   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
   linspacer4   triutrilaranger   rJ   r!   r	   len	itertoolsproductr   )rU   a_reala_real_uppera_real_lowera_real_diagr   r5   rV   rY   rR   pr8   rS   r9   rT   r:   	a_complexa_complex_diagr`   r^   r_   rW   rX   s                      @@@@r   test_banded_ode_solversro      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   )r   rM   Nr[   TFF)rf   numpyr
   numpy.testingr   scipy.integrater   r   r   r   r#   r=   rJ   ro   r   r   r   <module>rs      sC      )  

 ),9=27BJ[Ir   