# Variational formulation involving partial derivative and complex fields

**URL:** https://community.freefem.org/t/variational-formulation-involving-partial-derivative-and-complex-fields/2172
**Category:** General Discussion
**Created:** [December 7, 2022, 11:03am UTC](https://community.freefem.org/t/variational-formulation-involving-partial-derivative-and-complex-fields/2172 "2022-12-07T11:03:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![benjaminstamm](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/benjaminstamm/32/1444_2.png) [@benjaminstamm](https://community.freefem.org/u/benjaminstamm)
#### Post date: [December 7, 2022, 11:03am UTC](https://community.freefem.org/t/variational-formulation-involving-partial-derivative-and-complex-fields/2172/1 "2022-12-07T11:03:45Z")

</div>

Dear all,  
I have some issues involing complex functions in combination with gradients, i.e. dx() and dy(). I have a prepared a MWE (since I just registered, I can’t upload it as a file… sorry).

```auto
int numnode = 50;				
mesh Th = square(numnode,numnode);	

fespace Vh(Th,P1);				
Vh<complex> uh,vh; 						
uh = sin(pi*sqrt(x))*sin(pi*y*y) + 1i*sin(pi*x*x)*sin(pi*sqrt(y));

cout << "--> Var assembly of uh'*C*uh: " << int2d(Th)( 1i*dx(uh)'*uh ) << endl;

varf c(uh,vh)= int2d(Th)( 1i*dx(uh)*vh );	
matrix<complex> C = c(Vh,Vh); //# Assemble matrix C
complex[int] aux = C*uh[];
complex uCu = uh[]'*aux;
cout << "--> lin alg assembly of uh'*C*uh: " << uCu << endl;

```

and I get the following output:  
 → Var assembly of uh’_C_uh: (0.400063,-1.95872e-17)  
 → lin alg assembly of uh’_C_uh: (-0.400063,-9.02158e-17)

In my understanding, I should get the same value (which I do for example if I replace ‘dx(uh)’ by ‘uh’ in both expressions).

Does anyone understand what is going on?  
Best regards,  
Benjamin Stamm

---

<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: [December 7, 2022, 12:12pm UTC](https://community.freefem.org/t/variational-formulation-involving-partial-derivative-and-complex-fields/2172/2 "2022-12-07T12:12:47Z")

</div>

The line  
`complex uCu = uh[]'*aux;`  
corresponds to calculating  
`int2d(Th)( 1i*dx(uh)*uh' )`  
The difference is whether the conjugation acting on the derivative or not.

---

<div class="post-metadata">

### Author: ![benjaminstamm](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/benjaminstamm/32/1444_2.png) [@benjaminstamm](https://community.freefem.org/u/benjaminstamm)
#### Post date: [December 7, 2022, 1:24pm UTC](https://community.freefem.org/t/variational-formulation-involving-partial-derivative-and-complex-fields/2172/3 "2022-12-07T13:24:46Z")

</div>

Dear aszaboa,  
Thanks for your quick answer! Yes, indeed… I was blind towards this option, but now it seems obvious that the conjugation must be with the test and not trial function. Thanks for this helpful comment.  
Please find below the corrected MWE.  
Best, Ben

```auto
int numnode = 50;
mesh Th = square(numnode,numnode);

fespace Vh(Th,P1);
Vh<complex> uh,vh;
uh = sin(pi*sqrt(x))*sin(pi*y*y) + 1i*sin(pi*x*x)*sin(pi*sqrt(y));

cout << "--> Var assembly of uh'*C*uh: " << int2d(Th)( 1i*dx(uh)'*uh ) << endl;

varf c(uh,vh)= int2d(Th)( 1i*dx(vh)*uh );
matrix<complex> C = c(Vh,Vh); //# Assemble matrix C
complex[int] aux = C*uh[];
complex uCu = uh[]'*aux;
cout << "--> lin alg assembly of uh'*C*uh: " << uCu << endl;

```
