
    5[g=                         d dl mZ d dlmZ d dlZd dlmZmZ  ej                  e
      j                  Zd Zd Zd Zd Zd	 Zd
 Z G d d      Zedz  Zedz  Zedz  Zdez  ZdZdZddZd Zd Zy)    )groupby)warnN)find
coo_matrixc                 r    | dk  rt        d      | t        j                  ||z
        kD  rt        d      | S )z.Assert that first_step is valid and return it.r   z`first_step` must be positive.z`first_step` exceeds bounds.)
ValueErrornpabs)
first_stept0t_bounds      V/var/www/html/bid-api/venv/lib/python3.12/site-packages/scipy/integrate/_ivp/common.pyvalidate_first_stepr   
   s<    Q9::BFF7R<((788    c                 &    | dk  rt        d      | S )z,Assert that max_Step is valid and return it.r   z`max_step` must be positive.)r   )max_steps    r   validate_max_stepr      s    1}788Or   c                 n    | r3t        dj                  dj                  d | D                    d       yy)ae  Display a warning for extraneous keyword arguments.

    The initializer of each solver class is expected to collect keyword
    arguments that it doesn't understand and warn about them. This function
    prints a warning for each key in the supplied dictionary.

    Parameters
    ----------
    extraneous : dict
        Extraneous keyword arguments
    z?The following arguments have no effect for a chosen solver: {}.z, c              3   (   K   | ]
  }d | d   yw)`N ).0xs     r   	<genexpr>z"warn_extraneous.<locals>.<genexpr>(   s     <A1#Qxs      
stacklevelN)r   formatjoin)
extraneouss    r   warn_extraneousr!      s3     NfTYY<<<=	 r   c                 v   t        j                  | dt        z  k        r5t        ddt        z   dd       t        j                  | dt        z        } t        j
                  |      }|j                  dkD  r|j                  |fk7  rt        d      t        j                  |dk        rt        d      | |fS )	zValidate tolerance values.d   zNAt least one element of `rtol` is too small. Setting `rtol = np.maximum(rtol, z)`.r   r   r   z`atol` has wrong shape.z`atol` must be positive.)	r	   anyEPSr   maximumasarrayndimshaper   )rtolatolns      r   validate_tolr-   ,   s     
vvdS3Y 114s3@	 zz$c	*::dDyy1}t+233	vvdQh344:r   c                 `    t         j                  j                  |       | j                  dz  z  S )zCompute RMS norm.      ?)r	   linalgnormsize)r   s    r   r1   r1   ?   s"    99>>!qvv},,r   c
                    |j                   dk(  rt        j                  S t        ||z
        }
|
dk(  ry|	t        j                  |      |z  z   }t	        ||z        }t	        ||z        }|dk  s|dk  rd}nd|z  |z  }t        ||
      }|||z  |z  z   } | |||z  z   |      }t	        ||z
  |z        |z  }|dk  r|dk  rt        d|dz        }ndt        ||      z  d|dz   z  z  }t        d	|z  ||
|      S )
a  Empirically select a good initial step.

    The algorithm is described in [1]_.

    Parameters
    ----------
    fun : callable
        Right-hand side of the system.
    t0 : float
        Initial value of the independent variable.
    y0 : ndarray, shape (n,)
        Initial value of the dependent variable.
    t_bound : float
        End-point of integration interval; used to ensure that t0+step<=tbound 
        and that fun is only evaluated in the interval [t0,tbound]
    max_step : float
        Maximum allowable step size.
    f0 : ndarray, shape (n,)
        Initial value of the derivative, i.e., ``fun(t0, y0)``.
    direction : float
        Integration direction.
    order : float
        Error estimator order. It means that the error controlled by the
        algorithm is proportional to ``step_size ** (order + 1)`.
    rtol : float
        Desired relative tolerance.
    atol : float
        Desired absolute tolerance.

    Returns
    -------
    h_abs : float
        Absolute value of the suggested initial step.

    References
    ----------
    .. [1] E. Hairer, S. P. Norsett G. Wanner, "Solving Ordinary Differential
           Equations I: Nonstiff Problems", Sec. II.4.
    r   g        gh㈵>gư>g{Gz?gV瞯<gMbP?   r#   )r2   r	   infr
   r1   minmax)funr   y0r   r   f0	directionorderr*   r+   interval_lengthscaled0d1h0y1f1d2h1s                      r   select_initial_steprF   D   s   R 
ww!|vv'B,'O#266":$$E	b5j	B	b5j	B	DyBIBY^	R	!B	b9nr!	!B	R"y. "	%B	rBw%	 2	%B	U{rU{rDy!SR[ a519o6sRx_h77r   c                   $    e Zd ZdZddZd Zd Zy)OdeSolutiona  Continuous ODE solution.

    It is organized as a collection of `DenseOutput` objects which represent
    local interpolants. It provides an algorithm to select a right interpolant
    for each given point.

    The interpolants cover the range between `t_min` and `t_max` (see
    Attributes below). Evaluation outside this interval is not forbidden, but
    the accuracy is not guaranteed.

    When evaluating at a breakpoint (one of the values in `ts`) a segment with
    the lower index is selected.

    Parameters
    ----------
    ts : array_like, shape (n_segments + 1,)
        Time instants between which local interpolants are defined. Must
        be strictly increasing or decreasing (zero segment with two points is
        also allowed).
    interpolants : list of DenseOutput with n_segments elements
        Local interpolants. An i-th interpolant is assumed to be defined
        between ``ts[i]`` and ``ts[i + 1]``.
    alt_segment : boolean
        Requests the alternative interpolant segment selection scheme. At each
        solver integration point, two interpolant segments are available. The
        default (False) and alternative (True) behaviours select the segment
        for which the requested time corresponded to ``t`` and ``t_old``,
        respectively. This functionality is only relevant for testing the
        interpolants' accuracy: different integrators use different
        construction strategies.

    Attributes
    ----------
    t_min, t_max : float
        Time range of the interpolation.
    c                 f   t        j                  |      }t        j                  |      }|j                  dk(  r|d   |d   k(  s;t        j                  |dkD        s#t        j                  |dk        st        d      t        |      | _        |j                  | j                  dz   fk7  rt        d      || _	        || _
        |d   |d   k\  r.|d   | _        |d   | _        d| _        |rdnd	| _        || _        y |d   | _        |d   | _        d
| _        |rd	nd| _        |d d d   | _        y )N   r   z/`ts` must be strictly increasing or decreasing.r4   z4Numbers of time stamps and interpolants don't match.TrightleftF)r	   r'   diffr2   allr   len
n_segmentsr)   tsinterpolantst_mint_max	ascendingside	ts_sorted)selfrR   rS   alt_segmentds        r   __init__zOdeSolution.__init__   s   ZZ^GGBKA"Q%2b6/66!a%=BFF1q5MNOOl+88!+-- , - - (b6RU?ADJBDJ!DN#.FDIDNBDJADJ"DN"-7DI"XDNr   c                    t        j                  | j                  || j                        }t	        t        |dz
  d      | j                  dz
        }| j                  s| j                  dz
  |z
  } | j                  |   |      S )NrW   r4   r   )	r	   searchsortedrX   rW   r6   r7   rQ   rV   rS   )rY   tindsegments       r   _call_singlezOdeSolution._call_single   so     oodnnadii@c#'1ot':;~~oo)G3G)t  )!,,r   c                    t        j                  |      }|j                  dk(  r| j                  |      S t        j                  |      }t        j
                  |      }t        j                  |j                  d         ||<   ||   }t        j                  | j                  || j                        }|dz  }d||dk  <   | j                  dz
  ||| j                  dz
  kD  <   | j                  s| j                  dz
  |z
  }g }d}t        |      D ]G  \  }}	|t        t        |	            z   }
 | j                   |   |||
       }|j#                  |       |
}I t        j$                  |      }|dd|f   }|S )ab  Evaluate the solution.

        Parameters
        ----------
        t : float or array_like with shape (n_points,)
            Points to evaluate at.

        Returns
        -------
        y : ndarray, shape (n_states,) or (n_states, n_points)
            Computed values. Shape depends on whether `t` is a scalar or a
            1-D array.
        r   r^   r4   N)r	   r'   r(   rc   argsort
empty_likearanger)   r_   rX   rW   rQ   rV   r   rP   listrS   appendhstack)rY   r`   r<   reverset_sortedsegmentsysgroup_startrb   group	group_endys               r   __call__zOdeSolution.__call__   sX    JJqM66Q;$$Q''

1--&5;;q>2U8 ??4>>8$))LA!"A37??Q3FDOOa//0~~*X5H%h/NGU#c$u+&66I*!!'*8K	+JKAIIaL#K	 0 YYr]7
^	r   N)F)__name__
__module____qualname____doc__r\   rc   rs   r   r   r   rH   rH      s    #H&8
-+r   rH   g      ?g      ?g      ?g     @@
   g?c                    t        j                  |      }|j                  d   }|dk(  rt        j                  d      |fS |t        j                  |t
        dz        }n|j                         }dt        j                  |      dk\  j                  t              z  dz
  }|t        j                  |t        j                  |            z  }	|||	z  z   |z
  }
t        j                  |
dk(        d   D ];  }|
|   dk(  s||xx   dz  cc<   ||   ||   |	|   z  z   ||   z
  |
|<   |
|   dk(  r0= |t        | ||||
||	      S |\  }}t        | ||||
||	||	      S )aA  Finite differences Jacobian approximation tailored for ODE solvers.

    This function computes finite difference approximation to the Jacobian
    matrix of `fun` with respect to `y` using forward differences.
    The Jacobian matrix has shape (n, n) and its element (i, j) is equal to
    ``d f_i / d y_j``.

    A special feature of this function is the ability to correct the step
    size from iteration to iteration. The main idea is to keep the finite
    difference significantly separated from its round-off error which
    approximately equals ``EPS * np.abs(f)``. It reduces a possibility of a
    huge error and assures that the estimated derivative are reasonably close
    to the true values (i.e., the finite difference approximation is at least
    qualitatively reflects the structure of the true Jacobian).

    Parameters
    ----------
    fun : callable
        Right-hand side of the system implemented in a vectorized fashion.
    t : float
        Current time.
    y : ndarray, shape (n,)
        Current state.
    f : ndarray, shape (n,)
        Value of the right hand side at (t, y).
    threshold : float
        Threshold for `y` value used for computing the step size as
        ``factor * np.maximum(np.abs(y), threshold)``. Typically, the value of
        absolute tolerance (atol) for a solver should be passed as `threshold`.
    factor : ndarray with shape (n,) or None
        Factor to use for computing the step size. Pass None for the very
        evaluation, then use the value returned from this function.
    sparsity : tuple (structure, groups) or None
        Sparsity structure of the Jacobian, `structure` must be csc_matrix.

    Returns
    -------
    J : ndarray or csc_matrix, shape (n, n)
        Jacobian matrix.
    factor : ndarray, shape (n,)
        Suggested `factor` for the next evaluation.
    r   )r   r   r/   rJ   r4   rx   )r	   r'   r)   emptyfullr%   copyrealastypefloatr&   r
   nonzero_dense_num_jac_sparse_num_jac)r8   r`   rr   f	thresholdfactorsparsityr,   f_signy_scalehi	structuregroupss                 r   num_jacr     sk   V 	

1A	
AAvxx''~C3J'
 "''!*/))%0014Frzz)RVVAY77G	
Vg	"A ZZQ"dai1IOIaD6!9wqz11QqT9AaD dai #
 c1aAvw??$	6sAq!Q(&2 	2r   c                    |j                   d   }t        j                  |      } | ||d d d f   |z         }	|	|d d d f   z
  }
t        j                  t        j                  |
      d      }t        j
                  |      }t        j                  |
||f         }t        j                  t        j                  ||         t        j                  |	||f               }|t        |z  k  }t        j                  |      rt        j                  |      \  }t        ||   z  }||   |||   z  z   ||   z
  }||||f<    | ||d d d f   |d d |f   z         }	|	|d d d f   z
  }t        j                  t        j                  |      d      }t        j
                  |j                   d         }t        j                  |||f         }t        j                  t        j                  ||         t        j                  |	||f               }||   |z  |||   z  k  }t        j                  |      rLt        j                  |      \  }||   }||   ||<   ||   ||<   |d d |f   |
d d |f<   ||   ||<   ||   ||<   |
|z  }
||t        |z  k  xx   t        z  cc<   ||t        |z  kD  xx   t        z  cc<   t        j                  |t              }|
|fS )Nr   axis)r)   r	   diagargmaxr
   rg   r&   NUM_JAC_DIFF_REJECTr$   r   NUM_JAC_FACTOR_INCREASENUM_JAC_DIFF_SMALLNUM_JAC_DIFF_BIGNUM_JAC_FACTOR_DECREASENUM_JAC_MIN_FACTOR)r8   r`   rr   r   r   r   r   r,   h_vecsf_newrN   max_indrmax_diffr>   diff_too_smallra   
new_factorh_newdiff_newmax_diff_new	scale_newupdate
update_inds                           r   r   r   W  s   	
AWWQZF1QW:&'E1QW:Diit1-G
		!Avvd7A:&'HJJrvvaj)266%
2C+DEE 3e ;;N	vvnzz.),vc{:
3*ws|33qv= sCxAqDzF1c6N231QW:%))BFF8,15IIciil#vvhwz23JJrvvaj1266%
:K3LM	#*\E#J-FF66&>jj(GFVJ!+F!3F:!&MAjM"*1f9"5DJ )& 1E*#/#7HZ AID
8(50015LL1
8&../3JJ/ZZ 23F<r   c	           
      
   |j                   d   }	t        j                  |      dz   }
t        j                  |
|	f      }t	        |
      D ]   }t        j
                  ||      }||z  ||<   " |j                  } | ||d d d f   |z         }||d d d f   z
  }t        |      \  }}}t        ||||   f   ||ff|	|	f      j                         }t        j                  t        |      j                  d            j                         }t        j                  |	      }t        j                  t        j                  |||f               j                         }t        j                   t        j                  ||         t        j                  ||||   f               }|t"        |z  k  }t        j$                  |      rt        j&                  |      \  }t(        ||   z  }||   |||   z  z   ||   z
  }t        j*                  |	      }|||<   t        j,                  ||         }t        j                  |
t.              }t        j                  |j                   d   |	f      }t1        |      D ](  \  }}t        j
                  ||      }||z  ||<   |||<   * |j                  } | ||d d d f   |z         }||d d d f   z
  }t        |d d |f         \  }}}t        ||||||         f   ||ff|	|j                   d   f      j                         } t        j                  t        |       j                  d            j                         }!t        j                  |j                   d         }t        j                  t        j                  | |!|f               j                         }"t        j                   t        j                  ||!         t        j                  ||!|||      f               }#||   |#z  |"||   z  k  }$t        j$                  |$      rLt        j&                  |$      \  }$||$   }%||$   ||%<   ||$   ||%<   | d d |$f   |d d |%f<   |#|$   ||%<   |"|$   ||%<   |xj2                  t        j4                  |t        j6                  |j8                              z  c_        ||t:        |z  k  xx   t(        z  cc<   ||t<        |z  kD  xx   t>        z  cc<   t        j                   |t@              }||fS )Nr   r4   )r)   r   )dtype)!r)   r	   r7   rz   rangeequalTr   r   tocscarrayr
   r   ravelrg   r'   r&   r   r$   r   r   zerosuniqueint	enumeratedatarepeatrN   indptrr   r   r   r   )&r8   r`   rr   r   r   r   r   r   r   r,   n_groupsr   rp   er   dfr   j_rN   r   r   r   r>   r   ra   r   r   	h_new_allgroups_unique
groups_mapkr   max_ind_newr   r   r   r   s&                                         r   r   r     s   	
Avvf~!HXXxm$FxHHUF#Au ! XXF1QW:&'E	1d7	B9oGAq!r!VAY,'!Q0A?EEGDhhs4y''Q'/0668G
		!Azz"&&gqj!12399;HJJrvvaj)vveGVAY$678:E  3e ;;N	vvnzz.),vc{:
3*ws|33qv=HHQK		#		&+.XXhc2
=..q1156!-0HAu'A!AF1I !Ju 1 AqDzF*+Qq$wZyC()1ar!Zs1v%?"?@ !1v'/0#))A,.?AAF 	 hhs8}333;<BBDIIciil#zz"&&+q.)A"BCIIKJJFF1[>"FF5j&==>?A	 #*\E#J-FF66&>jj(GFVJ!+F!3F:!&MAjM"*1f9"5DJ )& 1E*#/#7HZ II1bggdkk233I
8(50015LL1
8&../3JJ/ZZ 23F<r   )N)	itertoolsr   warningsr   numpyr	   scipy.sparser   r   finfor   epsr%   r   r   r!   r-   r1   rF   rH   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s       ) bhhuo$&-
B8Jx xv Ul D[ $; 3Y   H2V'TBr   