# Complex solver despite real input

**URL:** <https://community.freefem.org/t/complex-solver-despite-real-input/3956>\
**Category:** General Discussion\
**Created:** [June 2, 2025, 2:23pm UTC](https://community.freefem.org/t/complex-solver-despite-real-input/3956 "2025-06-02T14:23:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wurschtbrotwilli](https://avatars.discourse-cdn.com/v4/letter/w/6f9a4e/32.png) [@wurschtbrotwilli](https://community.freefem.org/u/wurschtbrotwilli)\
**Post date:** [June 2, 2025, 2:23pm UTC](https://community.freefem.org/t/complex-solver-despite-real-input/3956/1 "2025-06-02T14:23:05Z")

</div>

Hello,

I am dealing with Maxwell equations and want to have a vector-valued electical field whose components are complex values `[Ex,Ey,Ez]`. The imaginary part of each of the three components corresponds to the phase `phi`, which is equal for all three components of the vector `[Ex+1i*phi, Ey+1i*phi, Ez+1i*phi]`. This allows to formulate the problem depending on four real inputs `Ex,Ey,Ez,phi` but requires a complex solver due to appearing imaginary `i`’s within the integrals. They appear due to the product rule as `(dx(Ex)+1i*Ex*dx(phi))*exp(1i*phi)`. Is it possible to define a complex solver without complex inputs and correspondingly non-complex fespace? And how do I have to deal with the separation of `Ex*dx(phi)`, since from my understanding `Ex` and `dx(phi)` are not allowed as product in a single integral?

Thanks in advance!

---

<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:** [June 3, 2025, 3:41pm UTC](https://community.freefem.org/t/complex-solver-despite-real-input/3956/2 "2025-06-03T15:41:33Z")

</div>

I think you can just define the two groups of FE spaces, once corresponding to real parts, and the other to imaginary ones, and work out the complex operations by hand, i.e., exp(1i_phi)=cos(phi) + 1i_sin(phi).

---

<div class="post-metadata">

**Author:** ![wurschtbrotwilli](https://avatars.discourse-cdn.com/v4/letter/w/6f9a4e/32.png) [@wurschtbrotwilli](https://community.freefem.org/u/wurschtbrotwilli)\
**Post date:** [June 23, 2025, 11:42am UTC](https://community.freefem.org/t/complex-solver-despite-real-input/3956/3 "2025-06-23T11:42:34Z")

</div>

Thanks for the reply. I’m not sure how to incorporate this, since the problem is formulated on the mesh object, hence a differentiation of the fespaces is solely via the components of the unknown and test function possible. Is this sufficient for including inmaginary i in the problem definition despite the real inputs?  
Defining two separate real problems with real and imaginary components is possible, unfortunately, the angle phi would still be required in the imaginary part, though being real in reality. Thus I would get the components `[Exreal+i*Eximaginary, Eyreal+i*Eyimaginary, Ezreal+i*Ezimaginary]` from the two individual real-valued problems `problemreal([Exreal,Eyreal,Ezreal,phireal],[vxreal,vyreal,vzreal,psireal])=...`, and `problemimaginary([Eximaginary,Eyimaginary,Ezimaginary,phireal],[vximaginary,vyimaginary,vzimaginary,psireal])=...` but in analogy to the E-components I can not derive the angle `phi`.

---

<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:** [June 25, 2025, 2:18pm UTC](https://community.freefem.org/t/complex-solver-despite-real-input/3956/4 "2025-06-25T14:18:12Z")

</div>

I am not sure I understand the problem. With the finite element method, you can describe linear operations thus create their discrete version, matrices. The phase appears nonlinearly in the problem. It is hard to understand the problem only by the arguments of the variational form.

---

<div class="post-metadata">

**Author:** ![wurschtbrotwilli](https://avatars.discourse-cdn.com/v4/letter/w/6f9a4e/32.png) [@wurschtbrotwilli](https://community.freefem.org/u/wurschtbrotwilli)\
**Post date:** [June 26, 2025, 4:04am UTC](https://community.freefem.org/t/complex-solver-despite-real-input/3956/5 "2025-06-26T04:04:28Z")

</div>

I described my problem in [this post](https://community.freefem.org/t/time-harmonic-birefringent-maxwell-for-3d-anisotropic-medium-with-phase-shifting-light/3939)  
already and it appeared to be too difficult or off-topic for most, so I didnt get a reply. Thus, I reduced my question and formulated it more generally. In the betweentime i tried to approach my problem differently and do not want to use the unknown and test function as complex since the real valued components are of interest and the imaginary argument is equal for all spatial components.

---

<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:** [June 26, 2025, 5:28pm UTC](https://community.freefem.org/t/complex-solver-despite-real-input/3956/6 "2025-06-26T17:28:51Z")

</div>

I am still unsure I understand the problem. I think you should define everything as complex variables. I you have to set some term to be real, specify the real part, and set the imaginary part of that complex variable to zero. I think if your math is correct, you will get the result you want, i.e., the real part will be nonzero, and the imaginary part will differ from zero only up to rounding precision (ofc, if discretization/solver are set properly).
