Compute partition function