Replace some object copies for shared_ptr. Make variable and contant matrices MatrixMatrixFunc global to the expression.