def check_ExpCM_matrix_exponentials(self):
for r in range(self.nsites):<EOL><INDENT>fromdiag = scipy.dot(self.expcm_divpressure.A[r], scipy.dot(scipy.diag(<EOL>self.expcm_divpressure.D[r]), self.expcm_divpressure.Ainv[r]))<EOL>self.assertTrue(scipy.allclose(self.expcm_divpressure.Prxy[r], fromdiag,<EOL>atol=<NUM_LIT>), "<STR_LIT>".format(<EOL>(self.expcm_divpressure.Prxy[r] - fromdiag).max()))<EOL>for t in [<NUM_LIT>, <NUM_LIT>, <NUM_LIT:0.5>]:<EOL><INDENT>direct = scipy.linalg.expm(self.expcm_divpressure.Prxy[r] * self.expcm_divpressure.mu * t)<EOL>self.assertTrue(scipy.allclose(self.expcm_divpressure.M(t)[r], direct, atol=<NUM_LIT>),<EOL>"<STR_LIT>".format((self.expcm_divpressure.M(t)[r] - direct).max()))<EOL><DEDENT><DEDENT>def funcM(paramvalue, paramname, t, expcm, r, x, y, storedvalues):<EOL><INDENT>"""<STR_LIT>"""<EOL>key = ('<STR_LIT:M>', tuple(paramvalue), paramname, t)<EOL>if key not in storedvalues:<EOL><INDENT>if len(paramvalue) == <NUM_LIT:1>:<EOL><INDENT>expcm.updateParams({paramname:paramvalue[<NUM_LIT:0>]})<EOL><DEDENT>else:<EOL><INDENT>expcm.updateParams({paramname:paramvalue})<EOL><DEDENT>storedvalues[key] = expcm.M(t)<EOL><DEDENT>if len(storedvalues[key].shape) == <NUM_LIT:3>:<EOL><INDENT>return storedvalues[key][r][x][y]<EOL><DEDENT>else:<EOL><INDENT>return storedvalues[key][:,r,x,y]<EOL><DEDENT><DEDENT>def funcdM(paramvalue, paramname, t, expcm, r, x, y, storedvalues):<EOL><INDENT>"""<STR_LIT>"""<EOL>key = ('<STR_LIT>', tuple(paramvalue), paramname, t)<EOL>if key not in storedvalues:<EOL><INDENT>if len(paramvalue) == <NUM_LIT:1>:<EOL><INDENT>expcm.updateParams({paramname:paramvalue[<NUM_LIT:0>]})<EOL><DEDENT>else:<EOL><INDENT>expcm.updateParams({paramname:paramvalue})<EOL><DEDENT>storedvalues[key] = expcm.dM(t, paramname, expcm.M(t))<EOL><DEDENT>if len(storedvalues[key].shape) == <NUM_LIT:3>:<EOL><INDENT>return storedvalues[key][r][x][y]<EOL><DEDENT>else:<EOL><INDENT>return storedvalues[key][:,r,x,y]<EOL><DEDENT><DEDENT>for (pname, pvalue) in sorted(self.params.items()):<EOL><INDENT>storedvalues = {} <EOL>if isinstance(pvalue, float):<EOL><INDENT>pvalue = [pvalue]<EOL><DEDENT>for t in [<NUM_LIT>, <NUM_LIT>]:<EOL><INDENT>for r in random.sample(range(self.expcm_divpressure.nsites), <NUM_LIT:2>):<EOL><INDENT>for x in random.sample(range(N_CODON), <NUM_LIT:3>):<EOL><INDENT>for y in range(N_CODON):<EOL><INDENT>if x == y:<EOL><INDENT>continue<EOL><DEDENT>diff = scipy.optimize.check_grad(funcM, funcdM, pvalue,<EOL>pname, t, self.expcm_divpressure, r, x, y, storedvalues,<EOL>epsilon=<NUM_LIT>)<EOL>self.assertTrue(diff < <NUM_LIT>, ("<STR_LIT>" +<EOL>"<STR_LIT>" +<EOL>"<STR_LIT>" +<EOL>"<STR_LIT>"<EOL>).format(diff, pname, r, x, y,<EOL>self.params['<STR_LIT>'], self.expcm_divpressure.pi_codon[r][x],<EOL>self.expcm_divpressure.pi_codon[r][y], t, self.expcm_divpressure.mu,<EOL>funcdM(pvalue, pname, t, self.expcm_divpressure,<EOL>r, x, y, {})))<EOL><DEDENT><DEDENT><DEDENT>self.expcm_divpressure.updateParams(self.params)<EOL><DEDENT><DEDENT>
Makes sure matrix exponentials are as expected.