我们可以利用np.ix_来创建输入数组的开放网格版本,然后将其馈送到np.multiply.reduce以便对所有这些元素进行逐元素乘法减少,如下所示-
A = np.multiply.reduce(np.ix_(*[x]*M))
扩展到其他支持的功能
我们可以将其扩展到其他具有reduce方法的ufunc.因此,例如执行外部加法,则将是-
np.add.reduce(np.ix_(*[x]*M))
等等.
有关支持此功能的ufunc的完整列表,请参考docs.在对它们进行了Grep-ing之后,我得到了以下支持reduce方法的ufunc,因此可以利用先前发布的方法:
add, subtract, multiply, divide, logaddexp, logaddexp2, true_divide,
floor_divide, negative, positive, power, remainder, mod, fmod, divmod,
absolute, fabs, rint, sign, heaviside, conj, exp, exp2, log, log2,
log10, expm1, log1p, sqrt, square, cbrt, reciprocal, gcd, lcm, sin,
cos, tan, arcsin, arccos, arctan, arctan2, hypot, sinh, cosh, tanh,
arcsinh, arccosh, arctanh, deg2rad, rad2deg, bitwise_and, bitwise_or,
bitwise_xor, invert, left_shift, right_shift, greater, greater_equal,
less, less_equal, not_equal, equal, logical_and, logical_or,
logical_xor, logical_not, maximum, minimum, fmax, fmin, isfinite,
isinf, isnan, isnat, fabs, signbit, copysign, nextafter, spacing,
modf, ldexp, frexp, fmod, floor, ceil, trunc