create a vander matrix in python

To create a Vandermonde matrix using numpy library in Python, we can use the numpy.vander function. The numpy.vander function accepts two parameters i.e. the input array and the number of columns to create in the output matrix.

Here is an example code snippet that creates a Vandermonde matrix using the numpy.vander function:

main.py
import numpy as np

x = np.array([1, 2, 3, 4])
n = 4

V = np.vander(x, n)
print(V)
83 chars
8 lines

This code will output the following Vandermonde matrix:

main.py
[[ 1  1  1  1]
 [ 8  4  2  1]
 [27  9  3  1]
 [64 16  4  1]]
61 chars
5 lines

In the above code snippet, we created an input array x with values [1, 2, 3, 4] and the number of columns to create in the output Vandermonde matrix is n which is also 4. We then used the numpy.vander function to create the Vandermonde matrix and stored the result in V. Finally, we printed the Vandermonde matrix using print(V).

gistlibby LogSnag