# Parameterized macro?

**URL:** https://community.freefem.org/t/parameterized-macro/3135
**Category:** General Discussion
**Created:** [April 22, 2024, 8:42am UTC](https://community.freefem.org/t/parameterized-macro/3135 "2024-04-22T08:42:06Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![bcaval](https://avatars.discourse-cdn.com/v4/letter/b/d26b3c/32.png) [@bcaval](https://community.freefem.org/u/bcaval)
#### Post date: [April 22, 2024, 8:42am UTC](https://community.freefem.org/t/parameterized-macro/3135/1 "2024-04-22T08:42:06Z")

</div>

Hello FreeFem community,

Is it possible to define a parameterized macro with a parameter inside in order to use it in varf.  
In fact, this parameter could be the index of region. For example :

real [int, int][int] c(10)  
for (i=1;i\<=3;i++) {c[i].resize(7,7);c[i] = 0.;} and after c[i] (j,k) are filled.

for (i=1;i\<=3;i++) {  
macro F…[i]…?(u,v) (c[i] (1,1) \* S1(u)+c[i] (1,2) \* S2(v)) // EOM  
}  
varf K(u,v) = int3d(Th3,1)(F…[1]…?(u,v)) + int3d(Th3,2)(F…[2]…?(u,v)) + int3d(Th3,3)(F…[3]…?(u,v))+ …

Thanks in advance,

---

<div class="post-metadata">

### Author: ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)
#### Post date: [April 25, 2024, 4:06pm UTC](https://community.freefem.org/t/parameterized-macro/3135/2 "2024-04-25T16:06:03Z")

</div>

The answer is no, but I do not understand what you want to do,  
remark, you can defined macro in macro with the new syntax

you can see this exemple

> <https://github.com/FreeFem/FreeFem-sources/blob/master/examples/tutorial/Stokes-macro2d-3d.edp>

---

<div class="post-metadata">

### Author: ![bcaval](https://avatars.discourse-cdn.com/v4/letter/b/d26b3c/32.png) [@bcaval](https://community.freefem.org/u/bcaval)
#### Post date: [April 26, 2024, 6:10pm UTC](https://community.freefem.org/t/parameterized-macro/3135/3 "2024-04-26T18:10:55Z")

</div>

Thanks for the answer.  
In fact I imagine this for attributing physical property to each material, see below, is it correct ?

real [int, int][int] z(4)  
for (i=1;i\<=3;i++) {  
z[i].resize(4,4); z[i] = 0.; // for instance 3 materials (I don’t use 0 index)  
}

// Def for a physical property to whatever i material  
macro Z(i) [[z[i] (1,1), z[i] (1,2), z[i] (1,3) ],  
[z[i] (2,1), z[i] (2,2), z[i] (2,3) ],  
[z[i] (3,1), z[i] (3,2), z[i] (3,3) ]] // EOM  
// and the varf for each material…1,2,3  
varf VZ(u,v) = int3d(Th,1)( grad(v)’ \*(Z(1)\*grad(u)) )  
+ int3d(Th,2)( grad(v)’ \*(Z(2)\*grad(u)) )  
+ int3d(Th,3)( grad(v)’ \*(Z(3)\*grad(u)) );

---

<div class="post-metadata">

### Author: ![bcaval](https://avatars.discourse-cdn.com/v4/letter/b/d26b3c/32.png) [@bcaval](https://community.freefem.org/u/bcaval)
#### Post date: [May 7, 2024, 9:54am UTC](https://community.freefem.org/t/parameterized-macro/3135/4 "2024-05-07T09:54:27Z")

</div>

Dear freefem community,

I follow my previous post, and I would like to have your advices for the best implementation of anisotropic physical property for each material.

Is the proposal of my previous post correct ?

Or should I use definition with

Ph Phy = Phy1\*(region==1)+Phy2\*(region==2) + etc. But the latter definition is well documented only for scalar values even for elasticity in isotropic conditions with E, nu or lambda, mu.

In fact, I am searching for the best way to properly model these anisotropic properties, especially when some solution fields are dependent of such region property (for instance stress field).

Best regards.

---

<div class="post-metadata">

### Author: ![Yanma](https://avatars.discourse-cdn.com/v4/letter/y/ee59a6/32.png) [@Yanma](https://community.freefem.org/u/Yanma)
#### Post date: [October 11, 2024, 5:37am UTC](https://community.freefem.org/t/parameterized-macro/3135/5 "2024-10-11T05:37:36Z")

</div>

Hello,  
It seems like I didn’t get why and how to use the macros. The book “FreeFEM documentation, 4.13” didn’t help me. I have a very non-linear equation to solve and wanted to divide it in few parts as follows:

macro grad(u1)[dx(u1),dy(u1)] //// def of grad operator  
macro w(u2) 1+(abs(grad(u2)))^2 //// in equation  
macro inter(w1) sqrt(w1-(w1-1)_(n1/n2)^2) //// intermediate  
macro F(u3,hh) (H-hh)_(inter(u3)-n1/n2)/(inter(u3)+n1/n2\*(u3-1)) // //  
problem disturb (h,v)  
=int2d(Th1)  
(  
dx(h)_dx(v)+dy(h)dy(v)  
)  
-int2d(Th1)  
(v(  
dx(F(w(h),h))_  
(  
dx(h)\*dy(dy(h))-dy(h)_dx(dy(h))  
)  
+dy(F(w(h),h))_  
(  
dx(dx(h))\*dy(h)-dx(h)_dx(dy(h))  
)  
+F(w(h),h)_(dx(dx(h))\*dy(dy(h))-(dx(dy(h)))^2)  
-grad(log(F(w(h),h)))'\*grad(h)  
-Ll2/F(w(h),h)  
)  
)  
+on(bord1,h=0);

The program falls with error at the moment when it tries to start the macro, “error operator ( \<7E\_Array\>”, and concludes with the compilation error: “before token )” for the line where the macro is called. What is my error in this part of code, maybe the syntax is wrong?

---

<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: [October 11, 2024, 7:42am UTC](https://community.freefem.org/t/parameterized-macro/3135/6 "2024-10-11T07:42:32Z")

</div>

The problem is not in the macro, but in the fact that your expression is nonlinear in `h`.  
`h` is the unknown in your `problem disturb (h,v)`. FF can only solve linear systems. For nonlinear problems you need to define an iterative scheme, each iteration containing only linear problems, as the Newton method for example.

---

<div class="post-metadata">

### Author: ![Yanma](https://avatars.discourse-cdn.com/v4/letter/y/ee59a6/32.png) [@Yanma](https://community.freefem.org/u/Yanma)
#### Post date: [October 24, 2024, 10:15am UTC](https://community.freefem.org/t/parameterized-macro/3135/7 "2024-10-24T10:15:32Z")

</div>

Thank you for the answer, but the macros still do not work. I try to use it for simple calculation of a function value, just to prove (I received the result manually). Here is a piece of code:  
Vh3 f,f1,f2,h = 0; // function f(h), first and second derivative, initial guess h0  
Vh3 htil;  
//…  
macro finter(h)  
dx(dx(h))+dy(dy(h))  
-dx(F(h))\*(dx(h)\*dy(dy(h))-dy(h)_dx(dy(h)))  
-dy(F(h))_(dy(h)\*dx(dx(h))-dx(h)_dx(dy(h)))  
-F(h)_(dx(dx(h))\*dy(dy(h))-(dx(dy(h)))^2)  
+dx(log(F(h)))\*dx(h)+dy(log(F(h)))\*dy(h)  
+Ll2/F(h)  
// //…  
htil = finter(h);

and the compiler gives a very the same error: it goes to the called macro and cannot work with it, finding an extra bracket: “before token ‘(’” It seems like the macro is described in wrong way. There is one more thing which confuses me: in the macro, the operators ‘dx’ and ‘dy’ which, as I guess, are in-built operators of space derivatives, are in some lines black as variables, and in other places brown as functions|operators.  
Sorry for the long message, I hope that it will help to someone who has the same issue

---

<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: [October 25, 2024, 10:45am UTC](https://community.freefem.org/t/parameterized-macro/3135/8 "2024-10-25T10:45:32Z")

</div>

You have to know that the operators `dx` or `dy` work only on arguments that  
are declared variables in a fespace.  
In particular you cannot write `dx(dx(h))`  
Instead you should write for example

```auto
Vh3 h=exp(x);
Wh hx;
Zh hxx;
hx=dx(h);
hxx=dx(hx);

```

Where Vh3, Wh, Zh are defined fespace.  
Same remark for `dx(F(h))`: you have to define first F(h) as a variable in a fespace.

Notice anyway that computing a second derivative of a function h in a fespace is in general problematic because the first order derivatives of h are discontinuous.
