python - 在 Python 中的非均匀网格上移动数组

标签 python numpy scipy

我想知道 Numpy 或 SciPy 中是否有允许在非均匀网格上移动数组的 Python 功能。我已经创建了一个最小示例来说明该过程,但这在这个最小示例中似乎不起作用:

import numpy as np
import matplotlib.pyplot as pyt

def roll_arrays( a, shift_values,x_grid ):
    #from scipy.interpolate import interp1d
    
    x_max         = np.amax(x_grid)    
    total_items   = a.shape[0]  
    the_ddtype    = a.dtype 
       
    result = np.zeros( (a.shape[0], a.shape[1] ), dtype=the_ddtype )    
   
    
    for k in range( total_items ):        
        edge_val_left  = a[k,0]
        edge_val_right = a[k,-1]     
       
        #extend grid to edges with boundary values (flat extrapolation)
        extended_boundary = np.abs( shift_values[k] )#positive or negative depending on shift 
        
        if( shift_values[k] != 0.0 ):
                   
            x0_right          = np.linspace( x_max +1e-3, x_max + 1e-3 + extended_boundary, 10 )
            x0_left           = np.linspace( -x_max - 1e-3 -extended_boundary, -x_max - 1e-3, 10 )
            if( shift_values[k]>0.0 ):
                #we fill left values
                x_dense_grid  = np.concatenate( ( x0_left, x_grid + shift_values[k] ) ) 
                ynew          = np.concatenate(  ( edge_val_left*np.ones( 10 ), a[k,:] )  )            
                
            elif( shift_values[k]<0.0 ):
                x_dense_grid  = np.concatenate( ( x_grid + shift_values[k], x0_right ) )               
                ynew          = np.concatenate(  ( a[k,:], edge_val_right*np.ones( 10 ) )  ) 
            
                                             
            ###
            #return on the original grid                       
            f_interp    = np.interp( x_grid, x_dense_grid, ynew )                
            result[k,:] = f_interp  
        
        else:
            #no shift
            result[k,:] = a[k,:]
             
    
    return result


x_geom     = np.array( [ 100*( 1.5**(-0.5*k) ) for k in range(1000)] )
x_geom_neg =-( x_geom )
x_geom = np.concatenate( (np.array([0.0]), np.flip(x_geom)) )
x_geom = np.concatenate( (x_geom_neg, x_geom) )

shifts = np.array([-1.0,-2.0,1.0])
f      = np.array( [ k**2/( x_geom**2 + k**4 ) for k in range(1,shifts.shape[0]+1)  ] )
fs     = roll_arrays( f, shifts, x_geom)

pyt.plot( x_geom, f[0,:], marker='.' )
pyt.plot( x_geom, fs[0,:], marker='.' )


print("done")

请注意,在这种情况下,“x_grid”的数据点是对数间隔的。有没有办法利用 Scipy/Numpy 来做到这一点?通过插值法或类似方法。

编辑:我注意到删除关于边界移动的if,elif,else 语句(进行平坦外推的地方)似乎可以解决问题;但我仍然认为对于 Python 中应该已经存在的东西来说,这种实现太天真了;所以问题仍然存在。

最佳答案

如果我对问题的理解正确,np.interp 将按照您的意愿执行(默认情况下它会复制边缘的值):

def roll_arrays(a, shift_values, x_grid):
    total_items = a.shape[0]
    result = np.zeros_like(a)

    for k in range(total_items):
        if shift_values[k] != 0.0:
            # shift the x values
            x_grid_shifted = x_grid + shift_values[k]
            # interpolate back to the original grid
            f_interp = np.interp(x_grid, x_grid_shifted, a[k, :])
            result[k, :] = f_interp
        else:
            # no shift
            result[k, :] = a[k, :]

    return result

对于问题的示例输入,这将给出非常接近的东西

fs_expected = np.array([k ** 2 / ((x_geom - shift) ** 2 + k ** 4) for k, shift in enumerate(shifts, start=1)])

关于python - 在 Python 中的非均匀网格上移动数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71144456/

相关文章:

python - 如何让 .to_datetime() 停止在字符串中按 UTC 偏移

python - 中位数周围范围内的随机数

python - 如何在 2x2 矩阵上应用cramer V

python - scipy.sparse.csr.csr_matrix :Matrix extension

python - 将 Vabinary(Max) 更改回文本<MS SQL "2005">

python - Redis 缓存不适用于 Django

python - 如何使用 numpy 创建一个从数据数组返回二维值的函数?

python - 如何在 Python/Matplotlib 中根据特征值和特征向量绘制椭圆?

python - Scipy:高效生成一系列积分(积分函数)

python - pyOpenGL 远面渲染在近面之上