While coding some of the procedures of quantum mechanics, I discovered the curious behavior of Python NumPy. When I multiply NumPy with more than two arrays, I get erroneous results. In the code below, I should write:
f = np.multiply(rowH,colH) A[row][col]=np.sum(np.multiply(f,w))
which gives the correct result. However, my initial wording is this:
A[row][col]=np.sum(np.multiply(rowH, colH, w))
which does not display an error message but an incorrect result. Where is my mistake that I can give three arrays for multiple chores?
Here is the complete code:
from numpy.polynomial.hermite import Hermite, hermgauss import numpy as np import matplotlib.pyplot as plt dim = 3 x,w = hermgauss(dim) A = np.zeros((dim, dim)) #build matrix for row in range(0, dim): rowH = Hermite.basis(row)(x) for col in range(0, dim): colH = Hermite.basis(col)(x) #gaussian quadrature in vectorized form f = np.multiply(rowH,colH) A[row][col]=np.sum(np.multiply(f,w)) print(A)
:: NOTE :: this code only works with NumPy 1.7.0 and higher!
source share