Zarathustra
Zarathustra

Reputation: 411

Shift arrays over non-uniform grids in Python

I would like to know if there is a Python functionality in either Numpy or SciPy that allows to shift arrays over non-uniform grids. I have created a minimal example to illustrate the procedure, but this does not seem to work in this minimal example:

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")

Note that the data points of "x_grid" are, in this case, logarithmically spaced. Is there a way to do this making use of Scipy/Numpy? Through interpolation methods or similar.

EDIT:I noted that removing the if,elif,else statements about the shift of the boundaries (where flat extrapolation was done) seems to solve the issue; but I still think this is too naive implementation for something that should already exist in Python; so the problem still persists.

Upvotes: 3

Views: 199

Answers (1)

yut23
yut23

Reputation: 3064

If I understand the question right, np.interp will just do what you want (it copies the values at the edges by default):

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

For the example input from the question, this will give something very close to

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

Upvotes: 1

Related Questions