Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Interpolation basis

1074 views


6�`W� �@s�ddlmZmZmZmZmZmZmZmZm	Z	ddl
mZddlm
Z
ddlmZmZmZddljZddljZddlmZGdd�d�ZGd	d
�d
e�ZdS)�)	�Function�S�symbols�And�	Piecewise�lambdify�Matrix�pi�latex)�
parse_expr)�jacobi)�array�float32�linspaceN)�dbgc@s�eZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zddddd�Z
dd�Zddd�ZdS)�LeastSquaresa�
    find aproximation use least squares method

    func must be set while initialisation
    basis and points added directly or 
    use pointsSet for point
        and createBasisH or createBasisJ for Head or Jacoby basis
    see tests for examples
    used as
    l=LeastSquares(func='cos(x)')
    l.points=list of points or
    l.points=lg.choosingPoints(x0=0,n=10)
    l.createBasisH()
    l.leastSquares(weightFunction=1)
    l.plot()
    

    zsin(x)cCsdtd�|_tjdddd�|_g|_g|_d|_||_d|_	d|_
dS)N�x�x0r�n�
)rr�lg�choosingPoints�points�basis�texBasis�texF�f�fLam�iF)�self�func�r!�J/projects/91f280f0-b3af-42e1-a2e0-ed2660b96906/jupyter/src/leastSquares.py�__init__,s					zLeastSquares.__init__cCsZ|j}d|d|}ddg|_|jd|dddd�|jd|�dS)N�r�alpha�beta�weightFunction�����)rr�createBasisJ�leastSquares)rrr�Wr!r!r"�forEps6s
	zLeastSquares.forEpscslt�jd�jdd�}t�fdd�|D��}t�fdd�|D��}t||�S)Nrr$�csg|]}�j|��qSr!)r)�.0�p)rr!r"�
<listcomp>?s	z$LeastSquares.eps.<locals>.<listcomp>csg|]}�j|��qSr!)r)r.r/)rr!r"r0@s	r()rrr
�max)r�a�ff�iFFr!)rr"�eps=s zLeastSquares.epscCs'dd�|jD�|_|j�dS)NcSsg|]}t|��qSr!)r
)r.�br!r!r"r0Ds	z/LeastSquares.showInNotebook.<locals>.<listcomp>)rr�plot)rr!r!r"�showInNotebookCszLeastSquares.showInNotebookcCsCtjdddd�|_|j�|jdd�|j�dS)z�
        find interplated function iF for function sin(x) (if you create LeastSquares object
        as default) 
        in Chebyshev points
        used Head function basis
        rrrrr'r$N)rrr�createBasisHr*r7)rr!r!r"�test1HGs
zLeastSquares.test1Hc
Cs�dttd�ttd�ttd�ttd�tdtd�tdtd�tdtd�tt�g	|_|j�|jd	d
�|j�dS)z�
        find interplated function iF for function sin(x) (if you create LeastSquares object
        as default)
        in points from interval 0,\pi
        used Head function basis
        rg@g@g@g@���r'r$N)�floatr	rr9r*r7)rr!r!r"�test2HTs|
zLeastSquares.test2HcCsd|j}d|d|}ddg|_|jdddddd�|jd|�|j�dS)	z�
        find interplated function iF for function sin(x) (if you create LeastSquares object
        as default)
        in Chebyshev points
        used Jacoby basis
        r$rr=r%r&r'Nr()rrr)r*r7)rrr+r!r!r"�test1Jbs	zLeastSquares.test1Jcs��j}t�j�}t�j�}t||��t||d�}t|d|d
d�}t�fdd�|D��}t�fdd�|D��}tj||||d�tj	ddd�tj
�dS)N�numpyrr$r-csg|]}�|��qSr!r!)r.r/)�f1r!r"r0�s	z%LeastSquares.plot.<locals>.<listcomp>csg|]}�j|��qSr!)r)r.r/)rr!r"r0�s	�o�f(x)�interp f(x)�locz
upper leftr()rDrE)rr
rrrrr�pltr7�legend�show)rrrrr2r3r4r!)rBrr"r7rs	zLeastSquares.plotcCs
||_dS)N)r)rrr!r!r"�	pointsSet�szLeastSquares.pointsSetr=rcs;|j����fdd�t|�D�}||_|S)zK
        DESCRIPTION:
        create basis from Jacobi polynomials
        cs%g|]}t|�����qSr!)r)r.�i)r%r&rr!r"r0�s	z-LeastSquares.createBasisJ.<locals>.<listcomp>)r�ranger)rrr%r&rr!)r%r&rr"r)�s	%	zLeastSquares.createBasisJc	Csft|j�}g}|j}|dd�|dd�dd�}|dd�|dd�}x�tt|��dd	�D]�}g}|j|||d||||dt|||dk|||k�f�|j||d|||d||t|||k|||dk�f�|jd
�|jt|��q�W||_|S)a�
        DESCRIPTION:
        create Head functions for points:
                 (x-p_{i-1})/(p_{i}-p_{i-1}) ,x \in [p_{i-1},p_{i}] 
        H_{i}(x)=(p_{i+1}-x)/(p_{i+1}-p_{i}) ,x \in [p_{i},p_{i+1}] 
                 0,otherwise
        
        it use sympy.Piecewise for conditions
        every conditions must be rigorus (<,>), otherwise uninfinite integrals
        will be wrong
        
        r$Nr;rTr(r(r(r()rT)	r
rrrL�len�appendrrr)rr�HrZdeltaPointsZddeltaPointsrK�condr!r!r"r9�s
	(#TT
	zLeastSquares.createBasisHr$cs9|j�|j�|j�|j}t|��t���|_td�d�d	fi�|�t����fdd��D��}t����fdd��D��}|d
|�t	��fdd�t
t���D��}td||�|fi�t|�|_
t�|�}||_|S)a~
        DECRIPTIONS:
        find a_{i} that satisfy
        \integral(g(x)-\sum a_{j}*H_{j}(x))^2)->min
        i.e:
        find best aproximation of function func in form
        L_{2} g= \sum a_{j}*H_{j}(x)
        
        INPUT:
        weight function:
            for Head functions =1
            for Jacobi functions=(1-x)**alpha*(1+x)**beta
        self.points:
            for Head functions same as for createBasisH
            for Jacobi functions points=[-1,1]
        self.func:
            is function (as string)  whom will be aproximated 
            when you useing Jacoby func mast be from -1 to 1 
            (for that use subset: f(x)=F((a+b)/2+(b-a)*x/2))
        EXAMPLES:
         forHead:
          see self.test1H and test2H methods
          l=LeastSquares(func='cos(x)')
          l.points=lg.choocingPoints(x0=0,n=10)
          l.createBasisH()
          l.leastSquares()
          l.plot()
         for Jacoby:
          see self.test1J methods
          x=symbols('x')
          W=(1-x)*(1+x)
          l.points=[-1,1]
          l.createBasisJ(n=5,alpha=1,beta=1)
          l.leastSquares(weightFunction=W)
        zleastSquares|part1rr$cs2g|](�����fdd��D��qS)cs8g|].}��|j��d�df��qS)rr$r()�	integrate)r.ZHj)�Hir+rrr!r"r0�s	z8LeastSquares.leastSquares.<locals>.<listcomp>.<listcomp>r!)r.)r+rrr)rRr"r0�s	z-LeastSquares.leastSquares.<locals>.<listcomp>cs8g|].}��|j��d�df��qS)rr$r()rQ)r.rR)r+rrrr!r"r0�s	cs"g|]}�|�|�qSr!r!)r.rK)r2rr!r"r0�s	zleastSquares|part2r(r()rrrrrrrrr�sumrLrMr
rr)rr'r �HHr6�expr�expr1r!)r+r2rrrrr"r*�s"$				((.	zLeastSquares.leastSquaresN)�__name__�
__module__�__qualname__�__doc__r#r,r5r8r:r?r@r7rJr)r9r*r!r!r!r"rs


rc@s(eZdZdZedd��ZdS)�my_functr$cCsSt|�tt|��|jrO|tjkr9tjS|tjkrOtjSdS)N)�print�type�	is_Numberr�Zero�One�Infinity)�clsrr!r!r"�evals
	z
my_funct.evalN)rWrXrY�nargs�classmethodrcr!r!r!r"r[
sr[)�sympyrrrrrrrr	r
�sympy.parsing.sympy_parserr�#sympy.functions.special.polynomialsrrAr
rr�src.lagrange�lagranger�matplotlib.pyplot�pyplotrG�src.dbgrrr[r!r!r!r"�<module>s@�