def comp_proj_lyap_res_norm(Z, amat=None, mmat=None, wmat=None, jmat=None, umat=None, vmat=None, Sinv=None): """compute the squared f norm of projected lyap residual res = Pt*[ Ft*ZZt*M + Mt*ZZt*M + W*Wt ]*P """ if Z.shape[1] >= Z.shape[0]: raise Warning('TODO: catch cases where Z has more cols than rows') if Sinv is None: Mlu = spsla.factorized(mmat) MinvJt = lau.app_luinv_to_spmat(Mlu, jmat.T) Sinv = np.linalg.inv(jmat * MinvJt) def _app_pt(Z, jmat, MinvJt, Sinv): return Z - jmat.T * np.dot(Sinv, np.dot(MinvJt.T, Z)) if umat is None and vmat is None: amattZ = amat.T * Z else: amattZ = amat.T*Z - lau.comp_uvz_spdns(vmat.T, umat.T, Z) PtFtZ = _app_pt(amattZ, jmat, MinvJt, Sinv) PtMtZ = _app_pt(mmat.T * Z, jmat, MinvJt, Sinv) PtW = _app_pt(wmat, jmat, MinvJt, Sinv) return lau.comp_sqfnrm_factrd_lyap_res(PtMtZ, PtFtZ, PtW)
def setUp(self): self.NV = 200 self.NP = 40 self.NY = 5 self.NU = self.NY + 3 self.verbose = False self.comprthresh = 1e-6 # threshhold for SVD trunc. for compr. of Z self.nwtn_adi_dict = dict(adi_max_steps=300, adi_newZ_reltol=1e-11, nwtn_max_steps=24, nwtn_upd_reltol=4e-7, nwtn_upd_abstol=4e-7, full_upd_norm_check=True, verbose=self.verbose) # -F, M spd -- coefficient matrices self.F = -sps.eye(self.NV) - \ sps.rand(self.NV, self.NV) * sps.rand(self.NV, self.NV) self.M = sps.eye(self.NV) + \ sps.rand(self.NV, self.NV) * sps.rand(self.NV, self.NV) try: self.Mlu = spsla.factorized(self.M.tocsc()) except RuntimeError: print 'M is not full rank' # bmatrix that appears in the nonliner ric term X*B*B.T*X self.bmat = np.random.randn(self.NV, self.NU) # right-handside: C= -W*W.T self.W = np.random.randn(self.NV, self.NY) # smw formula Asmw = A - UV self.U = 1e-4 * np.random.randn(self.NV, self.NY) self.Usp = 1e-4 * sps.rand(self.NV, self.NY) self.V = np.random.randn(self.NY, self.NV) self.uvs = sps.csr_matrix(np.dot(self.U, self.V)) self.uvssp = sps.csr_matrix(self.Usp * self.V) # initial value for newton adi self.Z0 = np.random.randn(self.NV, self.NY) # we need J sparse and of full rank for auxk in range(10): try: self.J = sps.rand(self.NP, self.NV, density=0.03, format='csr') spsla.splu((self.J * self.J.T).tocsc()) break except RuntimeError: if self.verbose: print 'J not full row-rank.. I make another try' try: spsla.splu((self.J * self.J.T).tocsc()) except RuntimeError: raise Warning('Fail: J is not full rank') # the Leray projector MinvJt = lau.app_luinv_to_spmat(self.Mlu, self.J.T) Sinv = np.linalg.inv(self.J * MinvJt) self.P = np.eye(self.NV) - np.dot(MinvJt, Sinv * self.J)
def test_luinv_to_spmat_complex(self): """check the application of the inverse of a lu-factored matrix to a sparse mat""" alusolve = spsla.factorized(self.A) Z = sps.csr_matrix(self.U+1j*self.V.T) AinvZ = lau.app_luinv_to_spmat(alusolve, Z) self.assertTrue(np.allclose(Z.todense(), self.A * AinvZ))
def test_luinv_to_spmat_complex(self): """check the application of the inverse of a lu-factored matrix to a sparse mat""" alusolve = spsla.factorized(self.A) Z = sps.csr_matrix(self.U + 1j * self.V.T) AinvZ = lau.app_luinv_to_spmat(alusolve, Z) self.assertTrue(np.allclose(Z.todense(), self.A * AinvZ))
def setUp(self): self.NV = 200 self.NP = 40 self.NY = 5 self.NU = self.NY+3 self.verbose = False self.comprthresh = 1e-6 # threshhold for SVD trunc. for compr. of Z self.nwtn_adi_dict = dict(adi_max_steps=300, adi_newZ_reltol=1e-11, nwtn_max_steps=24, nwtn_upd_reltol=4e-7, nwtn_upd_abstol=4e-7, full_upd_norm_check=True, verbose=self.verbose) # -F, M spd -- coefficient matrices self.F = -sps.eye(self.NV) - \ sps.rand(self.NV, self.NV) * sps.rand(self.NV, self.NV) self.M = sps.eye(self.NV) + \ sps.rand(self.NV, self.NV) * sps.rand(self.NV, self.NV) try: self.Mlu = spsla.factorized(self.M.tocsc()) except RuntimeError: print 'M is not full rank' # bmatrix that appears in the nonliner ric term X*B*B.T*X self.bmat = np.random.randn(self.NV, self.NU) # right-handside: C= -W*W.T self.W = np.random.randn(self.NV, self.NY) # smw formula Asmw = A - UV self.U = 1e-4 * np.random.randn(self.NV, self.NY) self.Usp = 1e-4 * sps.rand(self.NV, self.NY) self.V = np.random.randn(self.NY, self.NV) self.uvs = sps.csr_matrix(np.dot(self.U, self.V)) self.uvssp = sps.csr_matrix(self.Usp * self.V) # initial value for newton adi self.Z0 = np.random.randn(self.NV, self.NY) # we need J sparse and of full rank for auxk in range(10): try: self.J = sps.rand(self.NP, self.NV, density=0.03, format='csr') spsla.splu((self.J * self.J.T).tocsc()) break except RuntimeError: if self.verbose: print 'J not full row-rank.. I make another try' try: spsla.splu((self.J * self.J.T).tocsc()) except RuntimeError: raise Warning('Fail: J is not full rank') # the Leray projector MinvJt = lau.app_luinv_to_spmat(self.Mlu, self.J.T) Sinv = np.linalg.inv(self.J * MinvJt) self.P = np.eye(self.NV) - np.dot(MinvJt, Sinv * self.J)