You don't need to re-code the factoring function; the library is not going to factor anything bigger, than, say 10.
I think libexpr\collect() collects terms in an expression specified by a matrix, but I could be wrong (its been 6 months since I've looked at the code).