# How to move scalars to left side?

For example,

{\mu,\nu}::Indices(vector).
{a^{\mu},b^{\mu}}::NonCommuting.
ex:=a b a^{\mu} a b_{\mu} b+a b b^{\mu} b a_{\mu} a;
canonicalise(_);


I want to move 'a' & 'b' to left side, but I don't know how to do it ('factor_out' is not simple enough to do it).

Add a SortOrder property to determine how you want symbols to be sorted, and then use the sort_product algorithm:

{\mu,\nu}::Indices(vector).
{a,b,a{#},b{#}}::SortOrder.
{a^{\mu},b^{\mu}}::NonCommuting.
ex:=a b a^{\mu} a b_{\mu} b+a b b^{\mu} b a_{\mu} a;
sort_product(_);


which produces $$aabb a^{\mu} b_{\mu} + aabb b^{\mu} a_{\mu} .$$ If you want to do it, instead, with factor_out(_, $a,b$), then you will need a call to distribute(_) afterwards to get rid of the brackets again.