# Implementing discrete factor products

**URL:** <https://discourse.julialang.org/t/implementing-discrete-factor-products/44564>\
**Category:** Numerics\
**Tags:** arrays\
**Created:** [August 8, 2020, 11:47am UTC](https://discourse.julialang.org/t/implementing-discrete-factor-products/44564 "2020-08-08T11:47:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mroavi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mroavi/32/8804_2.png) [@mroavi](https://discourse.julialang.org/u/mroavi)\
**Post date:** [August 8, 2020, 11:47am UTC](https://discourse.julialang.org/t/implementing-discrete-factor-products/44564/1 "2020-08-08T11:47:29Z")

</div>

I have an algorithm that involves computations over multidimensional arrays. I came across this blog post by Tim Holy: [Multidimensional algorithms and iteration](https://julialang.org/blog/2016/02/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.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/e/7e2befc7ef70af5139a0a32af13f5fd37a775edb.png)

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!
