在 Python 中评估多项式,并针对 x 中的每个元素评估 r 中系数的每一列。
要在点 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.arange(-2, 2).reshape(2,2)
显示数组:
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 NumPy 中的 `polynomial.polyvalfromroots()` 方法:
print("\nResult...\n",polyvalfromroots([-2, 1], c, tensor=True))示例
from numpy.polynomial.polynomial import polyvalfromroots
import numpy as np
# Create an array of multidimensional coefficients
c = np.arange(-2, 2).reshape(2,2)
# 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([-2, 1], c, tensor=True))输出
Our Array... [[-2 -1] [ 0 1]] Dimensions of our Array... 2 Datatype of our Array object... int64 Shape of our Array object... (2, 2) Result... [[-0. 3.] [ 3. 0.]]
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP