Adding php logic for matrix operation