def main(): """Solve a generalized Lyapunov Equation using glyap.""" # generate matrices n = 100 a = rand(n, n) e = rand(n, n) y1 = rand(n, n) y1 = y1 + y1.T y2 = rand(n, n) y2 = y2 + y2.T # solve standard and generalized Lyapunov Equation x1, ahat, qa = glyap(a, None, y1, MESS_OP_NONE) x2, ahat2, ehat2, qa2, qe2 = glyap(a, e, y1, MESS_OP_NONE) # solve again using schur decomposition x3 = glyap(a, None, y2, MESS_OP_NONE, ahat, None, qa, None) x4 = glyap(a, e, y2, MESS_OP_NONE, ahat2, ehat2, qa2, qe2) # compute residual res1, _, relres1 = res2_glyap(a, None, y1, x1, MESS_OP_NONE) res2, _, relres2 = res2_glyap(a, e, y1, x2, MESS_OP_NONE) res3, _, relres3 = res2_glyap(a, None, y2, x3, MESS_OP_NONE) res4, _, relres4 = res2_glyap(a, e, y2, x4, MESS_OP_NONE) print("n = {0:d}".format(n)) print("(1): residual = {0:e}\t rel. residual = {1:e}".format( res1, relres1)) print("(2): residual = {0:e}\t rel. residual = {1:e}".format( res2, relres2)) print("(3): residual = {0:e}\t rel. residual = {1:e}".format( res3, relres3)) print("(4): residual = {0:e}\t rel. residual = {1:e}".format( res4, relres4)) print("\n")
def solve_lyap_dense(A, E, B, trans=False, options=None): """Compute the solution of a Lyapunov equation. See :func:`pymor.algorithms.lyapunov.solve_lyap_dense` for a general description. This function uses `pymess.glyap`. Parameters ---------- A The operator A as a 2D |NumPy array|. E The operator E as a 2D |NumPy array| or `None`. B The operator B as a 2D |NumPy array|. trans Whether the first operator in the Lyapunov equation is transposed. options The solver options to use (see :func:`lyap_dense_solver_options`). Returns ------- X Lyapunov equation solution as a |NumPy array|. """ _solve_lyap_dense_check_args(A, E, B, trans) options = _parse_options(options, lyap_lrcf_solver_options(), 'pymess_glyap', None, False) if options['type'] == 'pymess_glyap': Y = B.dot(B.T) if not trans else B.T.dot(B) op = pymess.MESS_OP_NONE if not trans else pymess.MESS_OP_TRANSPOSE X = pymess.glyap(A, E, Y, op=op)[0] X = np.asarray(X) else: raise ValueError( f'Unexpected Lyapunov equation solver ({options["type"]}).') return X
def solve_lyap_dense(A, E, B, trans=False, options=None): """Compute the solution of a Lyapunov equation. See :func:`pymor.algorithms.lyapunov.solve_lyap_dense` for a general description. This function uses `pymess.glyap`. Parameters ---------- A The operator A as a 2D |NumPy array|. E The operator E as a 2D |NumPy array| or `None`. B The operator B as a 2D |NumPy array|. trans Whether the first operator in the Lyapunov equation is transposed. options The solver options to use (see :func:`lyap_dense_solver_options`). Returns ------- X Lyapunov equation solution as a |NumPy array|. """ _solve_lyap_dense_check_args(A, E, B, trans) options = _parse_options(options, lyap_lrcf_solver_options(), 'pymess_glyap', None, False) if options['type'] == 'pymess_glyap': Y = B.dot(B.T) if not trans else B.T.dot(B) op = pymess.MESS_OP_NONE if not trans else pymess.MESS_OP_TRANSPOSE X = pymess.glyap(A, E, Y, op=op)[0] else: raise ValueError(f'Unexpected Lyapunov equation solver ({options["type"]}).') return X