o
    `^hx                     @   s  d Z ddlZddlmZ g dZdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZG dd deZe Zdd ZG dd  d eZe Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 ZG d1d2 d2eZe ZG d3d4 d4eZ e  Z!dS )5zI Collection of Model instances for use with the odrpack fitting package.
    N)Model)r   exponentialmultilinear	unilinear	quadratic
polynomialc                 C   s:   | d | dd  }}|j d df|_ ||| jdd S Nr      axis)shapesum)Bxab r   O/home/air/shanriGPT/back/venv/lib/python3.10/site-packages/scipy/odr/_models.py_lin_fcn
   s   r   c                 C   s>   t |jd t}t || f}| jd |jd f|_|S N)nponesr   floatconcatenateravel)r   r   r   resr   r   r   _lin_fjb   s   r   c                 C   s:   | dd  }t j||jd f|jd  dd}|j|_|S )Nr	   r   r   r
   )r   repeatr   )r   r   r   r   r   r   _lin_fjd   s   "r   c                 C   s4   t | jjdkr| jjd }nd}t|d ftS N   r   r	   )lenr   r   r   r   r   )datamr   r   r   _lin_est   s   r%   c                 C   sD   | d | dd  }}|j d df|_ |tj|t|| dd S r   r   r   r   power)r   r   powersr   r   r   r   r   	_poly_fcn,   s   r)   c                 C   s@   t t |jd tt ||jf}| jd |jd f|_|S r   )r   r   r   r   r   r'   flat)r   r   r(   r   r   r   r   _poly_fjacb3   s
   r+   c                 C   sB   | dd  }|j d df|_ || }tj|t||d  ddS )Nr	   r   r
   r&   )r   r   r(   r   r   r   r   _poly_fjacd:   s   r,   c                 C   s   | d t | d |  S Nr   r	   r   expr   r   r   r   r   _exp_fcnC      r1   c                 C   s   | d t | d |  S )Nr	   r.   r0   r   r   r   _exp_fjdG   r2   r3   c                 C   sB   t t |jd t|t | d |  f}d|jd f|_|S )Nr   r	   r!   )r   r   r   r   r   r/   )r   r   r   r   r   r   _exp_fjbK   s   .r4   c                 C   s   t ddgS )N      ?)r   arrayr#   r   r   r   _exp_estQ      r8   c                           e Zd ZdZ fddZ  ZS )_MultilinearModela  
    Arbitrary-dimensional linear model

    This model is defined by :math:`y=\beta_0 + \sum_{i=1}^m \beta_i x_i`

    Examples
    --------
    We can calculate orthogonal distance regression with an arbitrary
    dimensional linear model:

    >>> from scipy import odr
    >>> import numpy as np
    >>> x = np.linspace(0.0, 5.0)
    >>> y = 10.0 + 5.0 * x
    >>> data = odr.Data(x, y)
    >>> odr_obj = odr.ODR(data, odr.multilinear)
    >>> output = odr_obj.run()
    >>> print(output.beta)
    [10.  5.]

    c              	      "   t  jttttddddd d S )NzArbitrary-dimensional Linearz y = B_0 + Sum[i=1..m, B_i * x_i]z&$y=\beta_0 + \sum_{i=1}^m \beta_i x_i$nameequTeXequ)fjacbfjacdestimatemeta)super__init__r   r   r   r%   self	__class__r   r   rF   m      
z_MultilinearModel.__init____name__
__module____qualname____doc__rF   __classcell__r   r   rI   r   r;   V   s    r;   c                 C   sx   t | }|jdkrt d|d }t|df|_t|d }|fdd}tttt||fdd|d  d|d  dd	S )
a  
    Factory function for a general polynomial model.

    Parameters
    ----------
    order : int or sequence
        If an integer, it becomes the order of the polynomial to fit. If
        a sequence of numbers, then these are the explicit powers in the
        polynomial.
        A constant term (power 0) is always included, so don't include 0.
        Thus, polynomial(n) is equivalent to polynomial(range(1, n+1)).

    Returns
    -------
    polynomial : Model instance
        Model instance.

    Examples
    --------
    We can fit an input data using orthogonal distance regression (ODR) with
    a polynomial model:

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> from scipy import odr
    >>> x = np.linspace(0.0, 5.0)
    >>> y = np.sin(x)
    >>> poly_model = odr.polynomial(3)  # using third order polynomial model
    >>> data = odr.Data(x, y)
    >>> odr_obj = odr.ODR(data, poly_model)
    >>> output = odr_obj.run()  # running ODR fitting
    >>> poly = np.poly1d(output.beta[::-1])
    >>> poly_y = poly(x)
    >>> plt.plot(x, y, label="input data")
    >>> plt.plot(x, poly_y, label="polynomial ODR")
    >>> plt.legend()
    >>> plt.show()

    r   r	   c                 S   s   t |ftS )N)r   r   r   )r#   len_betar   r   r   	_poly_est   r9   zpolynomial.<locals>._poly_estzSorta-general Polynomialz$y = B_0 + Sum[i=1..%s, B_i * (x**i)]z)$y=\beta_0 + \sum_{i=1}^{%s} \beta_i x^i$r=   )rB   rA   rC   
extra_argsrD   )	r   asarrayr   aranger"   r   r)   r,   r+   )orderr(   rR   rS   r   r   r   r   x   s   
)

r   c                       r:   )_ExponentialModela  
    Exponential model

    This model is defined by :math:`y=\beta_0 + e^{\beta_1 x}`

    Examples
    --------
    We can calculate orthogonal distance regression with an exponential model:

    >>> from scipy import odr
    >>> import numpy as np
    >>> x = np.linspace(0.0, 5.0)
    >>> y = -10.0 + np.exp(0.5*x)
    >>> data = odr.Data(x, y)
    >>> odr_obj = odr.ODR(data, odr.exponential)
    >>> output = odr_obj.run()
    >>> print(output.beta)
    [-10.    0.5]

    c              	      r<   )NExponentialzy= B_0 + exp(B_1 * x)z$y=\beta_0 + e^{\beta_1 x}$r=   rB   rA   rC   rD   )rE   rF   r1   r3   r4   r8   rG   rI   r   r   rF         
z_ExponentialModel.__init__rL   r   r   rI   r   rX          rX   c                 C   s   || d  | d  S r-   r   r0   r   r   r   _unilin   s   r]   c                 C   s   t |jt| d  S )Nr   )r   r   r   r   r0   r   r   r   _unilin_fjd   s   r^   c                 C   s(   t |t |jtf}d|j |_|S )N)r!   r   r   r   r   r   r   r   _retr   r   r   _unilin_fjb   s   rb   c                 C      dS )N)r5   r5   r   r7   r   r   r   _unilin_est      rd   c                 C   s    ||| d  | d   | d  S )Nr   r	   r!   r   r0   r   r   r   
_quadratic   s    rf   c                 C   s   d| | d  | d  S r    r   r0   r   r   r   	_quad_fjd   s   rg   c                 C   s.   t || |t |jtf}d|j |_|S )N)   r_   r`   r   r   r   	_quad_fjb   s   ri   c                 C   rc   )N)r5   r5   r5   r   r7   r   r   r   	_quad_est   re   rj   c                       r:   )_UnilinearModela  
    Univariate linear model

    This model is defined by :math:`y = \beta_0 x + \beta_1`

    Examples
    --------
    We can calculate orthogonal distance regression with an unilinear model:

    >>> from scipy import odr
    >>> import numpy as np
    >>> x = np.linspace(0.0, 5.0)
    >>> y = 1.0 * x + 2.0
    >>> data = odr.Data(x, y)
    >>> odr_obj = odr.ODR(data, odr.unilinear)
    >>> output = odr_obj.run()
    >>> print(output.beta)
    [1. 2.]

    c              	      r<   )NzUnivariate Linearzy = B_0 * x + B_1z$y = \beta_0 x + \beta_1$r=   rZ   )rE   rF   r]   r^   rb   rd   rG   rI   r   r   rF     r[   z_UnilinearModel.__init__rL   r   r   rI   r   rk      r\   rk   c                       r:   )_QuadraticModela  
    Quadratic model

    This model is defined by :math:`y = \beta_0 x^2 + \beta_1 x + \beta_2`

    Examples
    --------
    We can calculate orthogonal distance regression with a quadratic model:

    >>> from scipy import odr
    >>> import numpy as np
    >>> x = np.linspace(0.0, 5.0)
    >>> y = 1.0 * x ** 2 + 2.0 * x + 3.0
    >>> data = odr.Data(x, y)
    >>> odr_obj = odr.ODR(data, odr.quadratic)
    >>> output = odr_obj.run()
    >>> print(output.beta)
    [1. 2. 3.]

    c              	      r<   )N	Quadraticzy = B_0*x**2 + B_1*x + B_2z&$y = \beta_0 x^2 + \beta_1 x + \beta_2r=   rZ   )rE   rF   rf   rg   ri   rj   rG   rI   r   r   rF   3  rK   z_QuadraticModel.__init__rL   r   r   rI   r   rl     r\   rl   )"rP   numpyr   scipy.odr._odrpackr   __all__r   r   r   r%   r)   r+   r,   r1   r3   r4   r8   r;   r   r   rX   r   r]   r^   rb   rd   rf   rg   ri   rj   rk   r   rl   r   r   r   r   r   <module>   s@    	=
