美文网首页
11-05:Python 插值

11-05:Python 插值

作者: zengw20 | 来源:发表于2020-11-05 20:53 被阅读0次

1.函数式

import numpy as np

'''
(X,Y):需要插值的一系列点,array数据类型
point:需要计算插值的点
'''

def _Lagrange(X,Y,point):
    '''挑战一行代码实现Lagrange插值'''
    return np.sum([(((np.prod(point-X)/(point-X[i]))/np.prod(np.delete((X[i]-X),i)))*Y[i]) for i in range(len(X)) if point != X[i]])

def Lagrange(X,Y,point):
    '''正常写法'''
    result = 0.0
    for i in range(len(X)):
        if point != X[i]:
            Li = (np.prod(point-X)/(point-X[i])) / np.prod(np.delete((X[i]-X),i))
            result = result + Li*Y[i]
        else:
            result = Y[i]
    return result

def Difference(X,Y):
    '''差分函数'''
    return np.sum([Y[i]/np.prod(np.delete((X[i]-X),i)) for i in range(len(X))])

def Newton(X,Y,point):
    '''牛顿插值'''
    result = Y[0]
    for i in range(1,len(X)):
        result = result + Difference(X[:i+1],Y[:i+1])*np.prod(point-X[:i])
    return result
X = np.arange(10)
Y = X**2
point = 3.5
print('真实值 ',point**2)
print('拉格朗日插值1:',_Lagrange(X,Y,point))
print('拉格朗日插值2:',Lagrange(X,Y,point))
print('牛顿插值',Newton(X,Y,point))

2.面向对象式

import numpy as np
class Interpolation(object):
    def __init__(self,X,Y,point):
        '''
        @author:zengwei
        (X,Y):给定的用于插值的点
        point:需要用插值函数计算值的点
        '''
        self.X = np.array(X)
        self.Y = Y
        self.point = point
        
    def _Lagrange(self):
        '''挑战一行代码实现Lagrange插值'''
        return np.sum([(((np.prod(self.point-self.X)/(self.point-self.X[i]))/np.prod(np.delete((self.X[i]-self.X),i)))*self.Y[i]) for i in range(len(self.X)) if self.point!=self.X[i]])
        
    def Lagrange(self):
        '''Lagrange插值的正常写法'''
        result = 0.0
        for i in range(len(self.X)):
            if self.point != self.X[i]:
                Li = (np.prod(self.point-self.X)/(self.point-self.X[i]))/np.prod(np.delete((self.X[i]-self.X),i))
                result = result + Li*self.Y[i]
            else:
                result = self.Y[i]
        return result
    
    def Difference(X,Y):
        return np.sum([Y[i]/np.prod(np.delete((X[i]-X),i)) for i in range(len(X))])
    
    def Newton(self):
        '''牛顿插值'''
        result = self.Y[0]
        for i in range(1,len(self.X)):
            result = result + Interpolation.Difference(self.X[:i+1],self.Y[:i+1])*np.prod(self.point-self.X[:i])
        return result
        
    def Hermite(self):
        '''Hermite插值'''
        pass
    
    def CubicSpline(self):
        '''三次样条插值'''
        pass
X = np.arange(10)
Y = X**2
point = 3.5
myop = Interpolation(X,Y,point)
print('真实值 ',point**2)
print('拉格朗日插值1:',myop._Lagrange())
print('拉格朗日插值2:',myop.Lagrange())
print('牛顿插值',myop.Newton())
print('计算差分',Interpolation.Difference(X,Y))

相关文章

  • 11-05:Python 插值

    1.函数式 2.面向对象式

  • 缺失值处理-拉格朗日插值

    常用的插值法有:一维插值法:拉格朗日插值、牛顿插值、分段低次插值、埃尔米特插值、样条插值。二维插值法:双线性插值、...

  • 28. 图像缩放

    插值方法 四种插值,最近邻域插值 双线性插值 像素关系重采样 立方插值其中最近邻域插值、双线性插值原理如下: 1)...

  • 数值分析之插值

    插值 一.基本概念 1.1插值需要研究的问题 插值函数是否存在? 如何构造插值函数? 如何评估误差? 1.2插值法...

  • Less_变量插值

    选择器名插值 属性名插值 URL插值 import插值 媒体查询插值 less的作用域,就近原则,如果自己有这个变...

  • vue.js权威指南第2章——数据绑定

    一: 插值(Mustache标签)1: {{}},插值为数据;变体{{*text}},2: {{{}}} , 插值...

  • python线性插值解析

    在缺失值填补上如果用前后的均值填补中间的均值,比如,0,空,1,[https://www.bilibili.com...

  • 【图像缩放算法】双立方(三次)插值

    当我们进行图像缩放的时候,我们就需要用到插值算法。常见的插值有: 最邻近插值 双线性插值 双立方(三次)插值在这三...

  • AngularJS学习之路——表达式(2)

    插值字符串 插值字符串就是拥有插值标记的字符串 举个栗子 这里的 {{ expr }} 就是插值标记,str 就是...

  • Android动画中篇(插值器、估值器)

    插值器(Interpolator)&估值器(TypeEvaluator) 插值器(Interpolator) 定义...

网友评论

      本文标题:11-05:Python 插值

      本文链接:https://www.haomeiwen.com/subject/hrwavktx.html