U
    »mœdx  ã                   @   s$  d Z ddlZddlmZ dddddd	g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/d0„ Zd1d2„ Zd3d4„ ZG d5d6„ d6eƒZeƒ ZG d7d8„ d8eƒZ e ƒ Z!dS )9zI Collection of Model instances for use with the odrpack fitting package.
é    N)ÚModelr   ÚexponentialÚmultilinearÚ	unilinearÚ	quadraticÚ
polynomialc                 C   s:   | d | dd …  }}|j d df|_ ||| jdd� S ©Nr   é   ©Zaxis)ÚshapeÚsum)ÚBÚxÚaÚb© r   úJ/home/sam/Atlas/atlas_env/lib/python3.8/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ÚconcatenateZ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%   Z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   s    r.   c                 C   s   | d t  | d | ¡ S )Nr	   r+   r-   r   r   r   Ú_exp_fjdG   s    r/   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    .r0   c                 C   s   t  ddg¡S )Nç      ð?)r   Úarray©r!   r   r   r   Ú_exp_estQ   s    r4   c                       s    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              	      s"   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$©ÚnameZequZTeXequ)ÚfjacbÚfjacdÚestimateÚmeta)ÚsuperÚ__init__r   r   r   r#   ©Úself©Ú	__class__r   r   r=   m   s       þþz_MultilinearModel.__init__©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r=   Ú__classcell__r   r   r@   r   r5   V   s   r5   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©   s    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$r6   )r9   r8   r:   Ú
extra_argsr;   )	r   Zasarrayr   Zaranger    r   r'   r)   r(   )Úorderr&   rH   rI   r   r   r   r   x   s     )

 
ÿþþc                       s    e Zd ZdZ‡ fdd„Z‡  ZS )Ú_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              	      s"   t ƒ jttttddddœd� d S )NZExponentialzy= B_0 + exp(B_1 * x)z$y=\beta_0 + e^{\beta_1 x}$r6   ©r9   r8   r:   r;   )r<   r=   r.   r/   r0   r4   r>   r@   r   r   r=   Ë   s    þþz_ExponentialModel.__init__rB   r   r   r@   r   rL   µ   s   rL   c                 C   s   || d  | d  S r*   r   r-   r   r   r   Ú_unilinÖ   s    rN   c                 C   s   t  |jt¡| d  S )Nr   )r   r   r   r   r-   r   r   r   Ú_unilin_fjdÚ   s    rO   c                 C   s(   t  |t  |jt¡f¡}d|j |_|S )N)r   ©r   r   r   r   r   ©r   r   Z_retr   r   r   Ú_unilin_fjbÞ   s    rR   c                 C   s   dS )N)r1   r1   r   r3   r   r   r   Ú_unilin_estå   s    rS   c                 C   s    ||| d  | d   | d  S )Nr   r	   r   r   r-   r   r   r   Ú
_quadraticé   s    rT   c                 C   s   d| | d  | d  S r   r   r-   r   r   r   Ú	_quad_fjdí   s    rU   c                 C   s.   t  || |t  |jt¡f¡}d|j |_|S )N)é   rP   rQ   r   r   r   Ú	_quad_fjbñ   s    rW   c                 C   s   dS )N)r1   r1   r1   r   r3   r   r   r   Ú	_quad_estø   s    rX   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )Ú_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              	      s"   t ƒ jttttddddœd� d S )NzUnivariate Linearzy = B_0 * x + B_1z$y = \beta_0 x + \beta_1$r6   rM   )r<   r=   rN   rO   rR   rS   r>   r@   r   r   r=     s    þþz_UnilinearModel.__init__rB   r   r   r@   r   rY   ü   s   rY   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )Ú_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              	      s"   t ƒ jttttddddœd� d S )NZ	Quadraticzy = B_0*x**2 + B_1*x + B_2z&$y = \beta_0 x^2 + \beta_1 x + \beta_2r6   rM   )r<   r=   rT   rU   rW   rX   r>   r@   r   r   r=   3  s       þþz_QuadraticModel.__init__rB   r   r   r@   r   rZ     s   rZ   )"rF   Únumpyr   Zscipy.odr._odrpackr   Ú__all__r   r   r   r#   r'   r(   r)   r.   r/   r0   r4   r5   r   r   rL   r   rN   rO   rR   rS   rT   rU   rW   rX   rY   r   rZ   r   r   r   r   r   Ú<module>   sB   
ÿ	=