ex5.py: Simple quadratic eigenvalue problem =========================================== This example solves a polynomial eigenvalue problem of degree 2, which is the most commonly found in applications. Larger degree polynomials are handled similarly. The coefficient matrices in this example do not come from an application, they are just simple matrices that are easy to build, just to illustrate how it works. The eigenvalues in this example are purely imaginary and come in conjugate pairs. The full source code for this demo can be `downloaded here <../_static/ex5.py>`__. Initialization is similar to previous examples. :: import sys, slepc4py slepc4py.init(sys.argv) from petsc4py import PETSc from slepc4py import SLEPc Print = PETSc.Sys.Print A function to build the matrices. The lowest degree coefficient is the 2-D Laplacian, the highest degree one is the identity matrix, and the other matrix is set to zero (which means that this problem could have been solved as a linear eigenproblem). :: def construct_operators(m,n): Print("Quadratic Eigenproblem, N=%d (%dx%d grid)"% (m*n, m, n)) # K is the 2-D Laplacian K = PETSc.Mat().create() K.setSizes([n*m, n*m]) K.setFromOptions() Istart, Iend = K.getOwnershipRange() for I in range(Istart,Iend): v = -1.0; i = I//n; j = I-i*n; if i>0: J=I-n; K[I,J] = v if i0: J=I-1; K[I,J] = v if j 0: Print("") Print(" k ||(k^2M+Ck+K)x||/||kx|| ") Print("-------------------- -------------------------") for i in range(nconv): k = Q.getEigenpair(i, xr, xi) error = Q.computeError(i) if k.imag != 0.0: Print("%9f%+9f j %12g" % (k.real, k.imag, error)) else: Print("%12f %12g" % (k.real, error)) Print("") The main program simply processes two user-defined command-line options (the dimensions of the mesh) and calls the other two functions. :: if __name__ == '__main__': opts = PETSc.Options() m = opts.getInt('m', 32) n = opts.getInt('n', m) M, C, K = construct_operators(m,n) solve_eigensystem(M, C, K) M = C = K = None