在Python中根据多项式的根评估x点的多项式值
为了根据根评估x点的多项式,可以使用Python NumPy中的`polynomial.polyvalfromroots()`方法。第一个参数是x。如果x是列表或元组,它将被转换为ndarray,否则它将保持不变并被视为标量。无论哪种情况,x或其元素都必须支持自身以及r的元素的加法和乘法。
第二个参数r是根的数组。如果r是多维的,则第一个索引是根索引,其余索引枚举多个多项式。例如,在二维情况下,每个多项式的根可以认为存储在r的列中。第三个参数是tensor。如果为True,则根数组的形状将在右侧扩展为1,每个x的维度一个。标量对此操作的维度为0。结果是,r中的每一列系数都将针对x的每个元素进行评估。如果为False,则x将在评估中广播到r的列上。此关键字在r是多维时很有用。默认值为True。
步骤
首先,导入所需的库:
from numpy.polynomial.polynomial import polyvalfromroots import numpy as np
创建一个系数数组:
c = np.array([1, 2, 3])
显示数组:
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)
为了根据根评估x点的多项式,可以使用Python中的`polynomial.polyvalfromroots()`方法:
print("\nResult...\n",polyvalfromroots(1, c))示例
from numpy.polynomial.polynomial import polyvalfromroots
import numpy as np
# Create an array of coefficients
c = np.array([1, 2, 3])
# 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 evaluate a polynomial specified by its roots at points x, use the polynomial.polyvalfromroots() method in Python Numpy
print("\nResult...\n",polyvalfromroots(1, c))输出
Our Array... [1 2 3] Dimensions of our Array... 1 Datatype of our Array object... int64 Shape of our Array object... (3,) Result... 0.0
广告
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP