Derivation for computing sum of squares function using Matrix method