标量函数是输入一个或多个自变量,输出一个标量函数值:
为了并行计算多种输入情况,可以采用Python
自带的map
函数
def add_two(x1, x2):
return x1 + x2
a = [1, 2]
b = [3, 4]
iterator = map(add_two, a, b)
res = list(iterator)
print(res)
map
还能实现类似数组广播的功能
def add_two(x1, x2):
return x1 + x2
a = [1, 2, 3, 4, 5]
iterator = map(lambda x: add2(x, 3), b)
res = list(iterator)
print(res)
根据张量网络 (tensor network) 的知识,每个自变量
下面就可以利用numpy.array
或torch.tensor
类的自动广播功能来把标量结果列表改造为张量结果。
触发自动广播的核心代码为
x1 = x1.reshape(-1, 1, 1)
x2 = x2.reshape(1, -1, 1)
x3 = x3.reshape(1, 1, -1)
本项目用高斯波包生成函数