# Complex eigenvalues and eigenvectors of a real problem

**URL:** https://community.freefem.org/t/complex-eigenvalues-and-eigenvectors-of-a-real-problem/3843
**Category:** General Discussion
**Created:** [April 9, 2025, 4:02am UTC](https://community.freefem.org/t/complex-eigenvalues-and-eigenvectors-of-a-real-problem/3843 "2025-04-09T04:02:43Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)
#### Post date: [April 9, 2025, 4:02am UTC](https://community.freefem.org/t/complex-eigenvalues-and-eigenvectors-of-a-real-problem/3843/1 "2025-04-09T04:02:43Z")

</div>

Hello everyone,

I am trying to solve an eigenvalue problem in which the matricx is real, but I expect complex eigenvalues. When I run PETSc with real, I see in the convergence history that SLEPc indeed calculates real eigenvalues. However, `EPSSolve` only accepts a real vector/FE variable for the eigenvalues and eigenvectors in its argument. Is this the current implementation of the FreeFEM-PETSc interface, or is there something more fundamental behind this related to the separation of the separation of the real and complex versions of PETSc?

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [April 9, 2025, 5:48am UTC](https://community.freefem.org/t/complex-eigenvalues-and-eigenvectors-of-a-real-problem/3843/2 "2025-04-09T05:48:15Z")

</div>

See notes in [EPSGetEigenvector](https://slepc.upv.es/documentation/current/docs/manualpages/EPS/EPSGetEigenvector.html).

---

<div class="post-metadata">

### Author: ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)
#### Post date: [April 16, 2025, 9:09pm UTC](https://community.freefem.org/t/complex-eigenvalues-and-eigenvectors-of-a-real-problem/3843/3 "2025-04-16T21:09:45Z")

</div>

Thanks for the reply. If I understand correctly, the complex eigenvalues/eigenvector could accessed, but it would require modification of [FreeFem-sources/plugin/mpi/SLEPc-code.hpp at master · FreeFem/FreeFem-sources · GitHub](https://github.com/FreeFem/FreeFem-sources/blob/master/plugin/mpi/SLEPc-code.hpp). If I understand correctly, in the current implementation whether the complex part is requested depends on whether real or complex PETSc is requrested. I looked into the source code, and I think I could try modifying the interface to extract the complex part of the eigenvalues; however, for the eigenvectors, the [corresponding code](https://github.com/FreeFem/FreeFem-sources/blob/master/plugin/mpi/SLEPc-code.hpp#L437-L490) seems very spooky and I do not think I can do it.

If I would check all the corresponding FreeFEM examples, and implement the extraction of the eigenvalues, would you consider implementing the extraction of the complex part of the eigenvalues?

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [April 17, 2025, 5:11am UTC](https://community.freefem.org/t/complex-eigenvalues-and-eigenvectors-of-a-real-problem/3843/4 "2025-04-17T05:11:48Z")

</div>

I don’t understand the problem, the complex eigenpairs are already split in both real and imaginary part. I’m not sure of what you need or what is not working.

---

<div class="post-metadata">

### Author: ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)
#### Post date: [April 17, 2025, 2:39pm UTC](https://community.freefem.org/t/complex-eigenvalues-and-eigenvectors-of-a-real-problem/3843/5 "2025-04-17T14:39:16Z")

</div>

I mean if in [FreeFem-sources/examples/hpddm/laplace-2d-SLEPc.edp at master · FreeFem/FreeFem-sources · GitHub](https://github.com/FreeFem/FreeFem-sources/blob/master/examples/hpddm/laplace-2d-SLEPc.edp), I modify it so that

```auto
complex[int] EigenVAL(0); // array to store eigenvalues
int k = EPSSolve
(DistA, // matrix OP = A − sigma*B
 DistB, //
 values = EigenVAL, // Array to store the EigenValues
 vectors = EigenVEC, // Array to store the FEM-EigenFunctions
 sparams = ssparams, // Parameters for the distributed EigenValue solver
 deflation = space
);

```

then, I get an error that `values` cannot be cast - I assume - to be a real vector. The same applies for the eigenvectors.

I want to us real PETSc/SLEPc to solve a problem with real matrices, and find its complex eigenvalues and eigenvectors. I do not want to use complex PETSc/SLEPc since it would mean an increased memory and computational effort.

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [April 17, 2025, 4:40pm UTC](https://community.freefem.org/t/complex-eigenvalues-and-eigenvectors-of-a-real-problem/3843/6 "2025-04-17T16:40:59Z")

</div>

> I want to us real PETSc/SLEPc to solve a problem with real matrices, and find its complex eigenvalues and eigenvectors

As written in the SLEPc documentation, these complex eigenmodes are stored as real eigenmodes, with first the real part, then the imaginary part. Again, I don’t understand the issue, sorry.

---

<div class="post-metadata">

### Author: ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)
#### Post date: [April 17, 2025, 5:16pm UTC](https://community.freefem.org/t/complex-eigenvalues-and-eigenvectors-of-a-real-problem/3843/7 "2025-04-17T17:16:50Z")

</div>

Yes, I understand that. But there is no way to access complex eigenvalues through the current FreeFEM interface with `load "PETSc" ` and not `load "PETSc-complex"` if I understand correctly.

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [April 17, 2025, 5:21pm UTC](https://community.freefem.org/t/complex-eigenvalues-and-eigenvectors-of-a-real-problem/3843/8 "2025-04-17T17:21:39Z")

</div>

Could you share an example?

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [April 17, 2025, 5:34pm UTC](https://community.freefem.org/t/complex-eigenvalues-and-eigenvectors-of-a-real-problem/3843/9 "2025-04-17T17:34:29Z")

</div>

OK, I have one.

```auto
load "PETSc"
matrix Aff(2, 2);
Aff(0, 0) = 1;
Aff(0, 1) = -2;
Aff(1, 0) = 2;
Aff(1, 1) = 1;
Mat A(Aff);
real[int] nv;
real[int, int] nV(1, 1);
int nev = EPSSolve(A, sparams = "-eps_type lapack -eps_view_values -eps_view_vectors", values = nv, array = nV);
cout << nv << endl;
cout << nV << endl;

```

`nv` and `nV` should be of size 4, not 2. That’s a bug indeed.

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [April 17, 2025, 5:39pm UTC](https://community.freefem.org/t/complex-eigenvalues-and-eigenvectors-of-a-real-problem/3843/10 "2025-04-17T17:39:20Z")

</div>

Or there could be another parameter `valuesi` and `arrayi` as with ARPACK.

---

<div class="post-metadata">

### Author: ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)
#### Post date: [April 18, 2025, 3:42pm UTC](https://community.freefem.org/t/complex-eigenvalues-and-eigenvectors-of-a-real-problem/3843/11 "2025-04-18T15:42:13Z")

</div>

Yes, that is what I meant. I think `valuesi` and `arrayi` would be more a convenient way to access them. But as I told you previously - even if I think I could implement `valuesi` in the SLEPc-FreeFEM interface, I do not think I could make `arrayi` work.
