python - ValueError:具有多个元素的数组的真值不明确。在 If 条件中使用相等时出错
问题描述
我在 if 相等条件下犯了这个错误。我尝试了类似问题的步骤,但我仍然犯了同样的错误。
我的脚本如下,我使用的是 Jupyter Notebook:
import numpy as np
import math
f1=np.array([-59,-28])
f3=np.array([-39,76,94])
f2=np.array([75])
a=0
b=0
c=0
x = np.array([1, 1, 3, 3, 3, 2]) # x coordinates in space
y = np.array([-59, -59, -39, -39, -39, 75]) # f(x)
def getNDDCoeffs(x, y):
n = np.shape(y)[0]
pyramid = np.zeros([n, n]) # Create a square matrix to hold pyramid
pyramid[::,0] = y # first column is y
for j in range(1,n):
for i in range(n-j):
# create pyramid by updating other columns
if np.logical_and(x[i+j]==x[i],x[i]==f1):
a=a+1
pyramid[i][j] = f1[a-1]/math.factorial(a)
elif np.logical_and(x[i+j]==x[i],x[i]==f3):
b=b+1
pyramid[i][j] = f3[a-1]/math.factorial(b)
elif np.logical_and(x[i+j]==x[i],x[i]==f2):
c=c+1
pyramid[i][j] = f2[a-1]/math.factorial(c)
else:
pyramid[i][j] = (pyramid[i+1][j-1] - pyramid[i][j-1]) / (x[i+j] - x[i])
return pyramid[0] # return first row
coeff_vector = getNDDCoeffs(x, y)
print(coeff_vector)
在脚本中,我试图以牛顿除差法的金字塔分辨率的形式实现 Hermite 插值。f1[0] 表示初始值,f1[1] 是一阶导数,f1[2] 是二阶导数。这同样适用于 f2 和 f3 数组。
可能是什么问题呢?如果你能帮助我,我很高兴。祝你今天过得愉快!
解决方案
推荐阅读
- rundeck - 我的 rundeck 工作不是让我传递所有参数,只有一个,我需要所有
- jquery - 如何在同一行引导程序上水平对齐 div
- sql - 将 MS Access SQL 选择查询转换为 VBA。使用聚合求和函数选择时中断
- algorithm - 一个涵盖所有时间复杂性的问题
- matlab - 在 Matlab 中将 GUI 链接在一起
- docker - 无法连接到网络外的kafka
- javascript - 使用 JavaScript 访问外部 json 文件
- r - 如何取消过滤或清除 r dplyr 中的过滤器?
- amazon-web-services - 使用 URL 远程启动(触发)EC2 amazon windows 服务器
- python - Pandas 关联一列到多列按列范围分组