#! /usr/bin/env python # def pwl_basis_1d ( nd, xd, ni, xi ): #*****************************************************************************80 # ## PWL_BASIS_1D evaluates a 1D piecewise linear basis function. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 02 July 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer ND, the number of data points. # # Input, real XD(ND), the data points. # # Input, integer NI, the number of interpolation points. # # Input, real XI(NI), the interpolation points. # # Output, real BK(NI,ND), the basis functions at the interpolation points. # import numpy as np if ( nd == 1 ): bk = np.ones ( [ ni, nd ] ) return bk bk = np.zeros ( [ ni, nd ] ) for i in range ( 0, ni ): for j in range ( 0, nd ): if ( j == 0 and xi[i] <= xd[j] ): t = ( xi[i] - xd[j] ) / ( xd[j+1] - xd[j] ) bk[i,j] = 1.0 - t elif ( j == nd - 1 and xd[j] <= xi[i] ): t = ( xi[i] - xd[j-1] ) / ( xd[j] - xd[j-1] ) bk[i,j] = t elif ( xd[j-1] < xi[i] and xi[i] <= xd[j] ): t = ( xi[i] - xd[j-1] ) / ( xd[j] - xd[j-1] ) bk[i,j] = t elif ( xd[j] <= xi[i] and xi[i] < xd[j+1] ): t = ( xi[i] - xd[j] ) / ( xd[j+1] - xd[j] ) bk[i,j] = 1.0 - t return bk def pwl_basis_1d_test ( ): #*****************************************************************************80 # ## PWL_BASIS_1D_TEST tests PWL_BASIS_1D. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 02 July 2015 # # Author: # # John Burkardt # import numpy as np import platform from r8mat_print import r8mat_print nd = 4 ni = 21 xd = np.array ( [ 0.0, 2.0, 5.0, 10.0 ] ) print ( '' ) print ( 'PWL_BASIS_1D_TEST:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' PWL_BASIS_1D evaluates the piecewise linear 1D basis' ) print ( ' functions.' ) x_min = 0.0 x_max = 10.0 xi = np.linspace ( x_min, x_max, ni ) lb = pwl_basis_1d ( nd, xd, ni, xi ) r8mat_print ( ni, nd, lb, ' The PWL basis functions:' ) # # Terminate. # print ( '' ) print ( 'PWL_BASIS_1D_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) pwl_basis_1d_test ( ) timestamp ( )