Functional implementation of cp-als algorithm