Matrix product optimization