首页 > 解决方案 > 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 数组。

可能是什么问题呢?如果你能帮助我,我很高兴。祝你今天过得愉快!

标签: pythonnumpy

解决方案


推荐阅读