I originally asked a question on the Mathematica stack exchange on a similar topic here.
But it seems like my question actually extends beyond Mathematica. The issue is the following. Let $a,b,d$ be elements of some vector space $V$ over $\mathbb{R}$, and let $c_1,c_2 \in \mathbb{R}$. I have some tensor product expression $a \otimes b$ that is non commutative, but is distributive over addition and linear and such: $$(c_1 a + c_2 b) \otimes d = c_1 a \otimes b + c_2 a \otimes d.$$
Given some long expression involving the sum of many tensor products. Something like:
$$a_1 \otimes b_1 + a_2 \otimes b_2 + a_1 \otimes d + a_2 \otimes d$$
I would like an algorithm that will find a factored expression with the minimum number of tensor products. For the above, that would be just two:
$$a_1 \otimes (b_1 + d) + a_2 \otimes (b_2 + d).$$
At the moment, my 'algorithm' is to:
- Expand all tensor products so I do truly have a sum of single products
- Recursively apply rules like $c_1 a\otimes b + c_2 a \otimes d \to a \otimes (c_1 b + c_2 d)$
- Hope for the best.
This is working fine-ish, but is definitely not guaranteed to arrive at the factorization with the fewest terms. Because of the order that my program implements things it can get stuck on, for example:
$$a_1 \otimes b_1 + a_2 \otimes b_2 + (a_1+a_2) \otimes d$$
and doesn't realize this can be simplified further by instead grouping the $a$ terms together to get:
$$a_1 \otimes (b_1 + d) + a_2 \otimes (b_2 + d).$$
This is because I "group from the right" before I "group from the left" but regardless of which way I do it, there will be an analagous case to above where that runs into trouble.
This really feels like a problem that is common enough there should be a procedure for handling it. In fact, perhaps even one in Mathematica. I would be curious about a general thought as to a good algorithm, if I were to implement such a thing homebrew, or something specific to Mathematica.
In essence my problem is that my current algorithm is greedy, and not looking at the big picture