#! /usr/bin/env python # def vandermonde_matrix_1d ( n, x ): #*****************************************************************************80 # ## VANDERMONDE_MATRIX_1D computes a Vandermonde 1D interpolation matrix. # # Discussion: # # We assume the interpolant has the form # # p(x) = c1 + c2 * x + c3 * x^2 + ... + cn * x^(n-1). # # We have n data values (x(i),y(i)) which must be interpolated: # # p(x(i)) = c1 + c2 * x(i) + c3 * x(i)^2 + ... + cn * x(i)^(n-1) = y(i) # # This can be cast as an NxN linear system for the polynomial # coefficients: # # [ 1 x1 x1^2 ... x1^(n-1) ] [ c1 ] = [ y1 ] # [ 1 x2 x2^2 ... x2^(n-1) ] [ c2 ] = [ y2 ] # [ ...................... ] [ ... ] = [ ... ] # [ 1 xn xn^2 ... xn^(n-1) ] [ cn ] = [ yn ] # # and if the x values are distinct, the matrix A is theoretically # invertible (though in fact, generally badly conditioned). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 03 July 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the number of data points. # # Input, real X(N,1), the data values. # # Output, real A(N,N), the Vandermonde matrix for X. # import numpy as np a = np.zeros ( [ n, n ] ) for i in range ( 0, n ): a[i,0] = 1.0 for j in range ( 1, n ): for i in range ( 0, n ): a[i,j] = a[i,j-1] * x[i] return a def vandermonde_matrix_1d_test ( ): #*****************************************************************************80 # ## VANDERMONDE_MATRIX_1D_TEST tests VANDERMONDE_MATRIX_1D. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 04 July 2015 # # Author: # # John Burkardt # import numpy as np import platform from r8mat_print import r8mat_print print ( '' ) print ( 'VANDERMONDE_MATRIX_1D_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' VANDERMONDE_MATRIX_1D sets the Vandermonde matrix for 1D interpolation.' ) nd = 4 xd = np.array ( [ -1.0, 2.0, 3.0, 5.0 ] ) ad = vandermonde_matrix_1d ( nd, xd ) r8mat_print ( nd, nd, ad, ' Vandermonde matrix:' ) # # Terminate. # print ( '' ) print ( 'VANDERMONDE_MATRIX_1D_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) vandermonde_matrix_1d_test ( ) timestamp ( )