Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
restrepo
GitHub Repository: restrepo/ComputationalMethods
Path: blob/master/material/__pycache__/LagrangePolynomial.cpython-34.pyc
934 views
�
*��Z�
�@s�dZddlmZmZmZmZddlmZmZddl	Z	ddl
mZddlm
Z
mZddlmZed�Zd	d
�Zd d"dd#d$gZdd�eD�Zed�dd��Zdd�Zdd�Zdd�Zdd�Zdd�Zedkr}eee�Zeee�Zx<eee�D](\ZZ ee�e dksNt!�qNWndS)%a;
From: https://gist.github.com/folkertdev/084c53887c49a6248839
A sympy-based Lagrange polynomial constructor. 
Given a set of function inputs and outputs, the lagrangePolynomial function will construct an 
expression that for every input gives the corresponding output. For intermediate values,
the polynomial interpolates (giving varying results based on the shape of your input). 
This is useful when the result needs to be used outside of Python, because the 
expression can easily be copied. To convert the expression to a python function object,
use sympy.lambdify.
�)�symbols�expand�lambdify�solve_poly_system)�tan�eN)�mul)�reduce�	lru_cache)�chain�xcGsttt|�dg�S)N�)r	r�list)�args�r�I/home/restrepo/Cursos/ComputationalMethods/material/LagrangePolynomial.py�<lambda>sr���cCsg|]}tj|��qSr)�mathr)�.0�vrrr�
<listcomp>s	r�cCs%dd�}tt|||���S)NcssNt|�}||}x1|D])}||kr5qnt|||VqWdS)N)�lenr)�labels�j�k�current�mrrr�gen"s

zl.<locals>.gen)r�product)rrr!rrr�l sr#cCst|�}t||�S)z�
    Lagrange polynomials as sympy polynomial
    xs: the n+1 nodes of the intepolation polynomial in the Lagrange Form
    j: Is the j-th Lagrange polinomial for the specific xs.
    )�tupler#)�xsrrrr�polyL+sr&cCsttt||��S)z�
    Lagrange polynomials as python function
    xs: the n+1 nodes of the intepolation polynomial in the Lagrange Form
    j: Is the j-th Lagrange polinomial for the specific xs.
    )rrr&)r%rrrr�L4sr'cCs\t|�}d}t|�}x7t|�D])\}}|t||�}||7}q+W|S)Nr)rr$�	enumerater#)r%�ysr�totalrr�trrr�lagrangePolynomial<sr,cGs&dd�tjt|g|��D�S)z%Finds all x for which function(x) = 0cSsg|]}|jr|�qSr)Zis_real)r�varrrrrPs	z#x_intersections.<locals>.<listcomp>)r�
from_iterabler)�functionrrrr�x_intersectionsMsr0cCstjtt|�S)zScale function on the x-axis)�	functions�subsr)r/�factorrrr�x_scaleRsr4�__main__g�����ư>�����g��r6g�g�?g�?)"�__doc__ZsympyrrrrZmpmathrrr�operatorr�	functoolsr	r
�	itertoolsrrr"r�pointsr#r&r'r,r0r4�__name__�func�pyfunc�zip�a�b�AssertionErrorrrrr�<module>
s*"