在Python中删除Legendre多项式中的小尾数

在Python中删除Legendre多项式中的小尾数

要从Legendre多项式中去除小尾数,在Python的numpy库中使用legendre.legtrim()方法。该方法返回一个删除尾部零的一维数组。如果结果序列为空,则返回包含一个零的序列。

“Small”表示“绝对值小”,由参数tol控制;“trailing”表示最高阶系数,例如,在[0,1,1,0,0]中(表示0 + x + x**2 + 0*x**3 + 0*x**4),3和4阶系数都会被“修剪”。参数c是一个按从低阶到高阶排序的一维数组。参数tol是尾部元素的绝对值小于等于tol时会被删除。

步骤

首先,导入所需的库−

import numpy as np
from numpy.polynomial import legendre as L

使用numpy.array()方法创建一个数组。这是一维系数数组 –

c = np.array([0,5,0, 0,9,0])

显示数组 –

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

检查尺寸 –

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

获取数据类型 −

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

获取形状 –

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

从Legendre多项式中去除小的尾数系数,使用Python numpy中的legendre.legtrim()方法

print("\nResult...\n",L.legtrim(c))

示例

import numpy as np
from numpy.polynomial import legendre as L

# Create an array using the numpy.array() method
# This is the 1-d array of coefficients
c = np.array([0,5,0, 0,9,0])

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

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

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

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

# To remove small trailing coefficients from Legendre polynomial, use the legendre.legtrim() method in Python numpy
print("\nResult...\n",L.legtrim(c))

输出

Our Array...
   [0 5 0 0 9 0]

Dimensions of our Array...
1

Datatype of our Array object...
int64

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

Result...
   [0. 5. 0. 0. 9.]

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程