3
$\begingroup$

The compiler does not have support for tensor product, are there any packages that implements the tensor product in a way that can be compiled? I need to take the tensor product of several numeric tensors, and the other operations can all be compiled, so I was looking for a way to do the tensor product inside the compiler as well.

Edit1: Mathematica does not compile TensorProduct but it does compile Outer, and when you call Outer[Times, array,array] is equivalent to a tensor product. One can check this by compiling this function and using CompilePrint as Domen commented.

$\endgroup$
5
  • $\begingroup$ Do you happen to have any code so far? $\endgroup$ Commented Jan 12 at 14:13
  • $\begingroup$ I do, but it is a little extensive. The point is that I have several lists each one containing several tensors with the same rank and all "rectangular" (same number of dimensions for all tensor indices). It is something like {rank1 tensors},{rank2 tensors },.... I will be taking the tensor product of the tensors with the highest rank with all the others of lowest rank. I don't have any constrain on the number of dimensions or the highest tensor rank at first. $\endgroup$
    – Felipe
    Commented Jan 12 at 14:20
  • 3
    $\begingroup$ Please provide a minimum example, then. $\endgroup$ Commented Jan 12 at 14:21
  • $\begingroup$ special case, dimension3 and up to rank 2 only, the general case would be higher dimensions and continuing this pattern for higher ranks. All lists have the same number of tensors. The number of tensors per list would increase as well list1 = RandomReal[{0,1}, {3, 3}] list2 = RandomReal[{0,1}, {3,3,3}] MapThread[(#1\[TensorProduct]#2)&, {list2, list1}] MapThread[(#1\[TensorProduct]#2)&, {list2,list2}] $\endgroup$
    – Felipe
    Commented Jan 12 at 14:31
  • $\begingroup$ @Felipe, regarding your edit: just because something seemingly gets compiled, doesn't necessarily mean it actually got compiled. You can check the compiled function with Needs["CompiledFunctionTools`"]; CompilePrint[fun] If you see a line containing MainEvaluate, it means this piece of code isn't actually compiled, but it calls back the main kernel evaluator. $\endgroup$
    – Domen
    Commented Jan 13 at 18:17

1 Answer 1

5
$\begingroup$

Here is a working example using FunctionCompile. This example works for two lists. I think it can be generalized to tensors with higher dimensions.

compiledTP = 
 FunctionCompile[
  Function[{Typed[arg1, "PackedArray"::["Real64", 1]], 
    Typed[arg2, "PackedArray"::["Real64", 1]] , 
    Typed[len1, "Integer64"], Typed[len2, "Integer64"]}, 
   Table[arg1[[i]]  arg2[[j]], {i, 1, len1}, {j, 1, len2}]]
  ]

Example:

a1 = RandomReal[1, 4];
a2 = RandomReal[1, 7];

compiledTP[a1, a2, Length[a1], Length[a2]]

I had trouble getting FunctionCompile to cooperate with Length and a PackedArray. So, one could wrap the above into a down-values function:

tensorProd[a1_List, a2_List] := 
 compiledTP[a1, a2, Length[a1], Length[a2]]

If you generalize, perhaps you can post your code here.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.