Implementing discrete factor products

I have an algorithm that involves computations over multidimensional arrays. I came across this blog post by Tim Holy: Multidimensional algorithms and iteration). I’m wondering whether the CartesianIndices data structure can help me compute factor products efficiently.

A factor product is a product of multidimensional functions. Each variable can appear in one of the two operand functions or in both. I’m interested in factor products that involve functions of discrete variables only. The scope (set of variables) of the resulting product is the union of the scopes of the operands. The image below shows what actual multiplications need to be performed.

The application I’m working on involves many of these types of operations. The factors have different dimensionalities and each dimension can have different cardinality.

I have a gut feeling that CartesianIndices is the way to efficiently implement these types of operations. I played around with CartesianIndices and they seem to be really fast. However, I still cannot get my head around how to implement this. I’ve made a few attempts but I feel I haven’t gotten very far. I would really appreciate hearing what the experts think an efficient way of implementing this would be. Does Julia offer a builtin method for this type of operation? Thanks in advance!