如何在 einsum 中使用标量乘法?

How to utilize scalar multiplication in einsum?

https://numpy.org/doc/stable/reference/generated/numpy.einsum.html 据说

Broadcasting and scalar multiplication: np.einsum('..., ...', 3, c) array([[ 0, 3, 6],[ 9, 12, 15]])

似乎 einsum 可以模仿 DGEMM 中的预因子 alpha/beta http://www.netlib.org/lapack/explore-html/d1/d54/group__double__blas__level3_gaeda3cbd99c8fb834a60a6412878226e1.html

这是否意味着它(包括 einsum 中的标量乘法作为一个步骤)将比两个步骤更快:(1) A,B->C 和 (2) C*prefactor

我尝试将 https://ajcr.net/Basic-guide-to-einsum/ 扩展为

    import numpy as np
    A = np.array([0, 1, 2])
    B = np.array([[ 0,  1,  2,  3],  [ 4,  5,  6,  7], [ 8,  9, 10, 11]])
    C = np.einsum('i,ij->i', 2., A, B)
    print(C)

得到了ValueError: einstein sum subscripts string contains too many subscripts for operand.

所以,我的问题是,有什么方法可以在 einsum 中包含标量因子并加速计算吗?

我没有使用过这个 scalar 功能,但它是这样工作的:

In [422]: np.einsum('i,ij->i',A,B)
Out[422]: array([ 0, 22, 76])

In [423]: np.einsum(',i,ij->i',2,A,B)
Out[423]: array([  0,  44, 152])

节省的时间似乎很少

In [424]: timeit np.einsum(',i,ij->i',2,A,B)
11.5 µs ± 271 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

In [425]: timeit 2*np.einsum('i,ij->i',A,B)
12.3 µs ± 274 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

另一个例子:

In [427]: np.einsum(',i,,ij->i',3,A,2,B)
Out[427]: array([  0, 132, 456])