# Several problem with complex number

**URL:** https://community.freefem.org/t/several-problem-with-complex-number/3691
**Category:** General Discussion
**Created:** [January 12, 2025, 5:41pm UTC](https://community.freefem.org/t/several-problem-with-complex-number/3691 "2025-01-12T17:41:16Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![zezi-yang](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/zezi-yang/32/2944_2.png) [@zezi-yang](https://community.freefem.org/u/zezi-yang)
#### Post date: [January 12, 2025, 5:41pm UTC](https://community.freefem.org/t/several-problem-with-complex-number/3691/1 "2025-01-12T17:41:16Z")

</div>

Hello everyone, I have encountered several problems in the computing of complex numbers.

1. The definition of inner product in my question is as follows.  
 ![QQ20250113-003006](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/e/e36c9df9131865f5c59da8f2a99446fce242845f.png)  
My question is whether my program needs to add conjugate when using the problem/solve/varf like this?

```auto
problem Test1(u,v) = int2d(Th)(u*conj(v))

```

After completing the variation, if the test function of the inner product contains complex numbers, is it possible to do :

```auto
problem Test1(u,v) = int2d(Th)(1i*u*conj(1i*v))

```

2.The second problem is that I noticed that when two complex vectors are multiplied, Freefem will calculate like this :

> conj(A)'\*A

which means that when I do not need to calculate in this way, it is a better choice to split the vector multiplication. When I write variables [ux, uy] as such a complex function, is the second method more appropriate ?

```auto
problem Test2([ux,uy],[vx,vy]) 
= int2d(Th)([ux,uy]'*[vx,vy])

```

```auto
problem Test2([ux,uy],[vx,vy]) 
= int2d(Th)(dx(ux)*dx(vx)+dy(uy)+dy(vy))

```

3.My third problem is that this expression appears in the GL equation：  
 ![QQ20250113-012323](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/f/fbd91fa6222ed4ea3d756dec2d0b1b7b3de8f499.png)

When calculating in the Freefem, A is real vector and Psi is complex number. But various operations of complex Psi are involved in the calculation of A, which will generate errors:

> Compile error : Error: Problem a complex problem with no complex FE function

My solution is to also set the variable type of A as a complex vector, and A will not have any complex value in the calculation. Is this appropriate ?

Any help/suggestion would be greatly appreciated ! 😊

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [January 14, 2025, 10:41am UTC](https://community.freefem.org/t/several-problem-with-complex-number/3691/2 "2025-01-14T10:41:11Z")

</div>

Hello,  
For question (1), your proposal `problem Test1(u,v) = int2d(Th)(u*conj(v))` is correct for u,v scalar complex valued (not vector of complex).  
Your second proposal with 1i in it is also correct, this is just standard rules for complex numbers!

For question (2), the important thing is that `'` (prime) applied to a vector or a matrix of complex numbers gives the transpose of the conjugate:

```auto
complex[int,int] a(2,2),b(2,2);
a=[[1+2i,3+4i],[5+6i,7+8i]];
b=a';
cout << "a " << a << endl;
cout << "b " << b << endl;

```

gives

| a 2 2 | |
| --- | --- |
| | (1,2) (3,4) |
| | (5,6) (7,8) |
| | |
| b 2 2 | |
| | (1,-2) (5,-6) |
| | (3,-4) (7,-8) |

However the `*` operator for complex numbers never puts conjugates.  
If you prefer, as you suggest you can always replace a complex number by two real numbers (standing for real and imaginary part).

For your question (3), I suggest you to represent \psi by two real numbers (real and imaginary parts), and keep A real.

---

<div class="post-metadata">

### Author: ![zezi-yang](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/zezi-yang/32/2944_2.png) [@zezi-yang](https://community.freefem.org/u/zezi-yang)
#### Post date: [January 16, 2025, 6:19am UTC](https://community.freefem.org/t/several-problem-with-complex-number/3691/3 "2025-01-16T06:19:22Z")

</div>

Thank you so much for your suggestions and for your time !

At present, my equation is divided into two parts where Psi is complex scalar and A is real vector :

 ![QQ20250116-135804](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/3/3e75159c1c6803761513f2699cbdbd8988d8d42e.png)  
Should I define Psi as you suggest :

```auto
Wh<complex> psi;
Wh psii,psir;
psir = real(psi); psii = imag(psi);

```

and bring in two equations for calculation ? Or I can only use Psi itself when calculating Psi, and then use real and imaginary parts when calculating A.

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [January 16, 2025, 11:18am UTC](https://community.freefem.org/t/several-problem-with-complex-number/3691/4 "2025-01-16T11:18:44Z")

</div>

There is no unique way of doing it. You can try and see what you prefer!

---

<div class="post-metadata">

### Author: ![zezi-yang](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/zezi-yang/32/2944_2.png) [@zezi-yang](https://community.freefem.org/u/zezi-yang)
#### Post date: [January 16, 2025, 6:35pm UTC](https://community.freefem.org/t/several-problem-with-complex-number/3691/5 "2025-01-16T18:35:14Z")

</div>

Okay, I’ll choose a way to do it. Thank you again for your suggestion

---

<div class="post-metadata">

### Author: ![julienG](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/julieng/32/222_2.png) [@julienG](https://community.freefem.org/u/julienG)
#### Post date: [January 16, 2025, 9:22pm UTC](https://community.freefem.org/t/several-problem-with-complex-number/3691/6 "2025-01-16T21:22:37Z")

</div>

You solve Time-dependent Ginzburg-Landau equations.

- first of all you can work with complex fields, but in my experience it is always much better to separate the gap \psi into real and imaginary parts. In any case modulus and phase in never an option as they are not continuous and differentiable
- now your fe-space (in 2d) will be [RePsi, ImPsi,Ax,Ay]
- to evolve TDGL, you can do naive implementation with all degrees of freedom P2, this should work. If you want something better I recommend Crank-Nicolson algorithm and deal with nonlinearity with richardson extrapolation. An alternative is Semi-implicit (Backward)-Euler Galerkin-mixed FEM… If necessary I have references …

---

<div class="post-metadata">

### Author: ![zezi-yang](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/zezi-yang/32/2944_2.png) [@zezi-yang](https://community.freefem.org/u/zezi-yang)
#### Post date: [January 17, 2025, 7:53am UTC](https://community.freefem.org/t/several-problem-with-complex-number/3691/7 "2025-01-17T07:53:18Z")

</div>

Thank you for your suggestion, but I have one question.

If Psi is divided into real and imaginary parts, how should the problem be expressed? I am now using P1 element and second-order time difference scheme. I intend to finish the program to see the results.

If you have any reference, I’d be grateful if you could share them. And here is my email: [894494510@qq.com](mailto:894494510@qq.com)
