# Factoring commutative coefficients to the left

Is it possible to make ncalgebra factorize commutative coefficients to the left?

For example, if I define a non-commutative object W and a commutative object a by

SetNonCommutative[W];
SetCommutative[a];


and then write

a W


the output is, as expected, just a W. Same for

W a


However, if I write

a[t] W


instead, for some reason a[t] lands on the right and the output is

W a[t]


Even if I apply Simplify to the latter, it still remains the same.

Finally, if I write

a[t] W + a[t] tp[W] // Simplify


then the output looks as I would want it to:

a[t] (W + tp[W])


I found two similar questions on the forum, but the approach with NCCollect doesn't work here.

Here's a more real life example: The commutative expression in a in the second parentheses got factored out to the left, but not the expression in the first.

By the way, the LeafCount of the expression factored as I want it to be is one less than the LeafCount of the output.

P.S. I found the following approach to my real life example: First, it feels more like a kludge than a solution, but it does the job. Secondly, the commutative factor is still not on the left, but at least the formula it's taken out.

Is there a better way to do it?

I am not sure about what you want to do here but be careful. Even though you set a to be commutative, a[t] is still non commutative. This is because t, which is a small letter is still non commutative. If you set a and t to be commutative then a[t] would be treated as commutative. The way you have it now you have a commutative product (Times) of two non commutative expressions a[t] and W, and that is bound to cause you trouble, such as things don't factor as you would expect.
Regarding the ordering in a[t] W, this is controller by Times, which is commutative and sorted. There is nothing you can do about that.