Generate a Vandermonde matrix of given degree in Python

PythonNumpyServer Side ProgrammingProgramming

To generate a Vandermonde matrix of given degree, use the polynomial.polyvander() in Python Numpy. The method returns rhe Vandermonde matrix. The shape of the returned matrix is x.shape + (deg + 1,), where the last index is the power of x. The dtype will be the same as the converted x. The parameter, a is Array of points. The dtype is converted to float64 or complex128 depending on whether any of the elements are complex. If x is scalar it is converted to a 1-D array. The parameter, deg is the degree of the resulting matrix.

Steps

At first, import the required libraries −

import numpy as np
from numpy.polynomial.polynomial import polyvander

Create an array −

x = np.array([0, 1, -1, 2])

Display the array −

print("Our Array...\n",x)

Check the Dimensions −

print("\nDimensions of our Array...\n",x.ndim)

Get the Datatype −

print("\nDatatype of our Array object...\n",x.dtype)

Get the Shape −

print("\nShape of our Array object...\n",x.shape)

To generate a Vandermonde matrix of given degree, use the polynomial.polyvander() in Python Numpy −

print("\nResult...\n",polyvander(x, 2))

Example

import numpy as np
from numpy.polynomial.polynomial import polyvander

# Create an array
x = np.array([0, 1, -1, 2])

# Display the array
print("Our Array...\n",x)

# Check the Dimensions
print("\nDimensions of our Array...\n",x.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",x.dtype)

# Get the Shape
print("\nShape of our Array object...\n",x.shape)

# To generate a Vandermonde matrix of given degree, use the polynomial.polyvander() in Python Numpy
print("\nResult...\n",polyvander(x, 2))

Output

Our Array...
[ 0 1 -1 2]

Dimensions of our Array...
1

Datatype of our Array object...
int64

Shape of our Array object...
(4,)

Result...
[[ 1. 0. 0.]
[ 1. 1. 1.]
[ 1. -1. 1.]
[ 1. 2. 4.]]
raja
Updated on 28-Feb-2022 10:55:43

Advertisements