在Python中使用NumPy在点x上广播评估一个多项式的系数列

在Python中使用NumPy在点x上广播评估一个多项式的系数列

在这篇文章中,我们将看到如何在Python中使用NumPy在点x上广播评估一个多项式的系数列。

numpy.polyval() 方法

numpy numpy.polynomial.polynomial.polyval()方法用于在python中评估一个多项式在x点播过系数的列。计算一个多项式在x位置的值。如果参数x是一个元组或一个列表,它就会变成一个数组;否则,它就被视为一个标量。在这两种情况下,x或其元素必须能够在它们之间以及在c的元素之间进行乘法和加法。如果c是一个一维数组,p(x)的形状将与x相同。如果c是多维的,结果的形状是由张量值决定的。如果张量为真,其形状将是c.shape[1:] + x.shape。如果张量为假,则形状将是c.shape[1:]。值得注意的是,标量有形状(,)。因为在评估中采用了系数中的尾部零,如果效率是优先考虑的,应该避免。

语法: polynomial.polynomial.polyval(x, c, tensor=True)

参数:

  • x:类似数组的对象。如果x是一个列表或元组,它将被转换为一个ndarray;否则,它将被视为一个标量。在任何情况下,x或其元素必须能够在它们之间以及与c的元素进行加法和乘法。
  • c:类似数组的对象。度数为n的项的系数存储在c[n]中,c[n]是一个系数数组,经过排序,度数为n的项的系数都包含在c[n]中。如果c是多维的,其余的索引列举了无数的多项式。二维例子中的系数可以认为是存储在c的列中。
  • tensor:布尔值。如果为真,系数数组的结构被向右拉伸,用1表示x的每个维度。对于这个动作,标量没有维度。因此,x的每个元素都被评估为c中的每一列系数。如果是假的,对于评估,x被分散在c的各列中。True是默认值。

返回:value:返回数组的形状是上面指定的。

示例 1:

使用numpy.array()方法创建一个数组。使用.shape , .dimension和.dtype属性找到数组的形状、尺寸和数据类型。polynomial.polynomial.polyval用于评估x点的多项式,并将其作为tensor设置为true在行中广播。

# import packages
from numpy.polynomial.polynomial import polyval
import numpy as np
  
# Creating an array
array = np.array([[3, 4], [5, 6]])
print(array)
  
# shape of the array is
print("Shape of the array is : ", array.shape)
  
# dimension of the array
print("The dimension of the array is : ", array.ndim)
  
# Datatype of the array
print("Datatype of our Array is : ", array.dtype)
  
# evaluating a polynomial at point x
print(polyval([2, 2], array))

输出:

[[3 4]
[5 6]]
Shape of the array is :  (2, 2)
The dimension of the array is :  2
Datatype of our Array is :  int64
[[13. 13.]
[16. 16.]]

示例 2:

在这个例子中,polynomial.polynomial.polyval被用来评估x点的多项式,并在列中广播,因为tensor被设置为False。

# import packages
from numpy.polynomial.polynomial import polyval
import numpy as np
  
# Creating an array
array = np.array([[3, 4], [5, 6]])
print(array)
  
# shape of the array is
print("Shape of the array is : ", array.shape)
  
# dimension of the array
print("The dimension of the array is : ", array.ndim)
  
# Datatype of the array
print("Datatype of our Array is : ", array.dtype)
  
# evaluating a polynomial at point x
print(polyval([2, 2], array, tensor=False))

输出:

[[3 4]
[5 6]]
Shape of the array is :  (2, 2)
The dimension of the array is :  2
Datatype of our Array is :  int64
[13. 16.]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Numpy 多项式