# Edge to element

**URL:** <https://community.freefem.org/t/edge-to-element/2825>\
**Category:** General Discussion\
**Created:** [December 7, 2023, 9:12pm UTC](https://community.freefem.org/t/edge-to-element/2825 "2023-12-07T21:12:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![larismartins](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/larismartins/32/2712_2.png) [@larismartins](https://community.freefem.org/u/larismartins)\
**Post date:** [December 7, 2023, 9:12pm UTC](https://community.freefem.org/t/edge-to-element/2825/1 "2023-12-07T21:12:16Z")

</div>

Hello everyone,

how do I create an edge to element matrix using FreeFem++?  
So, basically, given the edge number, I would like to know the elements that share that edge. Is it possible?

---

<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:** [December 10, 2023, 9:57am UTC](https://community.freefem.org/t/edge-to-element/2825/2 "2023-12-10T09:57:00Z")

</div>

yes,

the finite element P0edge build a edge, and

```auto
// the corresponding edge in the adjacent triangle
  Th[k] == Th[k].adj(e) ;// non adjacent triangle return the same 
  Th[k] != Th[k].adj(e) ;// true adjacent triangle 
 

```

---

<div class="post-metadata">

**Author:** ![larismartins](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/larismartins/32/2712_2.png) [@larismartins](https://community.freefem.org/u/larismartins)\
**Post date:** [December 10, 2023, 1:51pm UTC](https://community.freefem.org/t/edge-to-element/2825/3 "2023-12-10T13:51:58Z")

</div>

yeah, i did that

```auto
//----------------Numbering edges-------------------
fespace L0(calP, P0edge);
int ndofL0K = L0.ndofK;

real[int, int] edgesinK(NbTriangles, ndofL0K);
for(int i=0; i<NbTriangles; i++){
    for(int j=0; j<ndofL0K; j++){
        edgesinK(i,j) = L0(i,j);
    }
}
cout << edgesinK << "\n";

```

but then I tried this and it’s not working

```auto
for(int i=0; i<L0.ndofK; i++){
    for(int K=0; K<NbTriangles; K++){
        // non adjacent triangle return the same
        if(calP[K]==calP[K].adj(i)){
            cout << K << "\n";
        }
        // true adjacent triangle 
        else if(calP[K] != calP[K].adj(i)){
            cout << K << "\n";
        }
        
    }
}

```

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [December 12, 2023, 2:47pm UTC](https://community.freefem.org/t/edge-to-element/2825/4 "2023-12-12T14:47:48Z")

</div>

> [@larismartins](#):
>
> `calP[K].adj(i)`

Hello,

The problem is that the value of “i” is modified when you do `calP[K].adj(i)`.

Just copy the value of `i` in a new variable `j`.

```auto
for(int i=0; i<L0.ndofK; i++){
    for(int K=0; K<NbTriangles; K++){
        // non adjacent triangle return the same
        int j = i; 
        if(calP[K]==calP[K].adj(j)){
            cout << K << "\n";
        }
        // true adjacent triangle 
        else if(calP[K] != calP[K].adj(j)){
            cout << calP[K].adj(j) << "\n";
        }
        
    }
}

```

Best regards,

Loïc
