Python中的逐元素张量乘法 [英] Element by element tensor multiplication in python
问题描述
我正在尝试使用python解决计算代数中的问题.
I am trying to solve a problem in computational algebra using python.
基本上给定两个集合,例如A={a,b}
和B={e}
,我需要按元素张量积计算元素,并得到包含这些元素乘积的最终集合,例如C={a\tensor{e},b\tensor{e}}
.
Basically given two sets, say A={a,b}
and B={e}
, I need to compute the element by element tensor products and get a final set say C={a\tensor{e},b\tensor{e}}
containing these products of elements.
我可以使用带有数字的数组来逐元素相乘,但是不能执行字母而不是数字的逐元素张量相乘.
I can do an element by element multiplication using arrays with numbers but I can't do an element by element tensor multiplication of letters instead of numbers.
推荐答案
不确定我是否正确理解,下面的代码将一个集合的每个字母与另一个集合的每个字母相乘
Not sure if I understood correctly, this below code multiplies each letter of one set with each letter of the the other set
def getProduct(A,B):
prod=[]
for a in A:
for b in B:
prod.append(a+b)
return prod
A=['a','b']
B=['e']
print(getProduct(A,B))
Output: ['ae', 'be']
这篇关于Python中的逐元素张量乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!