import numpy as np
def multiplier (first_vector, second_vector, size, index, total):
if index < size:
addendum = first_vector[index]*second_vector[index]
total = total + addendum
index = index + 1
# ongoing job
if index < size:
multiplier(first_vector, second_vector, size, index, total)
# job done
else:
print("dot product = " + str(total))
def main():
a = np.array([1.5, 2, 3.7])
b = np.array([3, 4.3, 5])
print(a, b)
i = 0
total_sum = 0
# check needed if the arrays are not hardcoded
if a.size == b.size:
multiplier(a, b, a.size, i, total_sum)
else:
print("impossible dot product for arrays with different size")
if __name__== "__main__":
main()
2条答案
按热度按时间vcudknz31#
一种可能的解决方案是使用递归
n8ghc7c12#
可能被认为是欺骗,但是Python 3.5添加了一个矩阵乘法运算符,
numpy
使用它来计算点积,而实际上并没有调用np.dot
:问题解决了!