def calc_l_r_roots(self, n): """Returns the matrix square roots (and inverses) needed to calculate B. Hermiticity of l[n] and r[n] is used to speed this up. If an exception occurs here, it is probably because these matrices are not longer Hermitian (enough). """ l_sqrt, evd = m.sqrtmh(self.l[n - 1], ret_evd=True) l_sqrt_inv = m.invmh(l_sqrt, evd=evd) r_sqrt, evd = m.sqrtmh(self.r[n], ret_evd=True) r_sqrt_inv = m.invmh(r_sqrt, evd=evd) return l_sqrt, r_sqrt, l_sqrt_inv, r_sqrt_inv
def calc_l_r_roots(self, n): """Returns the matrix square roots (and inverses) needed to calculate B. Hermiticity of l[n] and r[n] is used to speed this up. If an exception occurs here, it is probably because these matrices are no longer Hermitian (enough). If l[n] or r[n] are diagonal or the identity, further optimizations are used. """ try: l_sqrt = self.l[n - 1].sqrt() except AttributeError: l_sqrt, evd = mm.sqrtmh(self.l[n - 1], ret_evd=True) try: l_sqrt_inv = l_sqrt.inv() except AttributeError: l_sqrt_inv = mm.invmh(l_sqrt, evd=evd) try: r_sqrt = self.r[n].sqrt() except AttributeError: r_sqrt, evd = mm.sqrtmh(self.r[n], ret_evd=True) try: r_sqrt_inv = r_sqrt.inv() except AttributeError: r_sqrt_inv = mm.invmh(r_sqrt, evd=evd) if self.sanity_checks: if not sp.allclose(mm.mmul(l_sqrt, l_sqrt), self.l[n - 1]): print "Sanity Fail in calc_l_r_roots: Bad l_sqrt_%u" % (n - 1) if not sp.allclose(mm.mmul(r_sqrt, r_sqrt), self.r[n]): print "Sanity Fail in calc_l_r_roots: Bad r_sqrt_%u" % (n) if not sp.allclose(mm.mmul(l_sqrt, l_sqrt_inv), sp.eye(l_sqrt.shape[0])): print "Sanity Fail in calc_l_r_roots: Bad l_sqrt_inv_%u" % (n - 1) if not sp.allclose(mm.mmul(r_sqrt, r_sqrt_inv), sp.eye(r_sqrt.shape[0])): print "Sanity Fail in calc_l_r_roots: Bad r_sqrt_inv_%u" % (n) return l_sqrt, r_sqrt, l_sqrt_inv, r_sqrt_inv
def calc_l_r_roots(self): try: self.l_sqrt = self.l.sqrt() self.l_sqrt_i = self.l_sqrt.inv() except AttributeError: self.l_sqrt, evd = m.sqrtmh(self.l, ret_evd=True) self.l_sqrt_i = m.invmh(self.l_sqrt, evd=evd) try: self.r_sqrt = self.r.sqrt() self.r_sqrt_i = self.r_sqrt.inv() except AttributeError: self.r_sqrt, evd = m.sqrtmh(self.r, ret_evd=True) self.r_sqrt_i = m.invmh(self.r_sqrt, evd=evd) if self.sanity_checks: if not np.allclose(self.l_sqrt.dot(self.l_sqrt), self.l): print "Sanity check failed: l_sqrt is bad!" if not np.allclose(self.l_sqrt.dot(self.l_sqrt_i), np.eye(self.D)): print "Sanity check failed: l_sqrt_i is bad!" if not np.allclose(self.r_sqrt.dot(self.r_sqrt), self.r): print "Sanity check failed: r_sqrt is bad!" if (not np.allclose(self.r_sqrt.dot(self.r_sqrt_i), np.eye(self.D))): print "Sanity check failed: r_sqrt_i is bad!"