# Define Lagrangian using struct

**URL:** https://discourse.julialang.org/t/define-lagrangian-using-struct/95193
**Category:** New to Julia
**Tags:** question
**Created:** [February 25, 2023, 4:51pm UTC](https://discourse.julialang.org/t/define-lagrangian-using-struct/95193 "2023-02-25T16:51:57Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![khaledharizb](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@khaledharizb](https://discourse.julialang.org/u/khaledharizb)
#### Post date: [February 25, 2023, 4:51pm UTC](https://discourse.julialang.org/t/define-lagrangian-using-struct/95193/1 "2023-02-25T16:51:57Z")

</div>

I’m new in Julia and I want to define a dynamical Lagrangian which is a difference between the kinetic energy and the potential energy L(x,v)=K(v)-U(x). I use this code in order to do trick for example if we but `a=Lagrangian` and `a.K` give me the kinetic energy.

```julia
mutable struct Lagrangian{T<:Float64}
          x::T
           v::T
            function K{T}(v::T) where T<:Float64
             return 0.5*v^2
            end
     function U{T}(x::T) where T<:Float64
             return 0.5*x^2
            end
        end

```

UndefVarError: K not defined

Stacktrace:  
[1] top-level scope  
@ In[3]:1

---

<div class="post-metadata">

### Author: ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)
#### Post date: [February 25, 2023, 6:02pm UTC](https://discourse.julialang.org/t/define-lagrangian-using-struct/95193/2 "2023-02-25T18:02:46Z")

</div>

In Julia, structs are only really for data, and all functions are defined separately (maybe an exception for a constructor). So for your example:

```julia
mutable struct Lagrangian{T}
    x::T
    v::T
end

kintetic(L::Lagrangian) = L.v^2/2
potential(L::Lagrangian) = L.x^2/2

```

There is an option to override some functions like `getproperty` to do what you want but I would recommend the above method instead.

---

<div class="post-metadata">

### Author: ![khaledharizb](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@khaledharizb](https://discourse.julialang.org/u/khaledharizb)
#### Post date: [February 25, 2023, 7:00pm UTC](https://discourse.julialang.org/t/define-lagrangian-using-struct/95193/3 "2023-02-25T19:00:59Z")

</div>

Thank you @jmair for your reply. That was clear, but how can I evaluate `kintetic(L::Lagrangian) = L.v^2/2` let say for `v=1.5`

---

<div class="post-metadata">

### Author: ![khaledharizb](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@khaledharizb](https://discourse.julialang.org/u/khaledharizb)
#### Post date: [February 25, 2023, 7:08pm UTC](https://discourse.julialang.org/t/define-lagrangian-using-struct/95193/4 "2023-02-25T19:08:54Z")

</div>

I see in this example that it could use a function with structs like

```julia
struct Point{T<:Real}
           x::T
           y::T
           Point{T}(x,y) where {T<:Real} = x^2+y^2
       end

```

and then use `Point{Int}(1,1)` for the evaluation which gives `2`.

---

<div class="post-metadata">

### Author: ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)
#### Post date: [February 25, 2023, 7:22pm UTC](https://discourse.julialang.org/t/define-lagrangian-using-struct/95193/5 "2023-02-25T19:22:07Z")

</div>

Just call like a regular function:

```julia
julia> L = Lagrangian(0.0, 2.0);
julia> kinetic(L)==2.0
true

```

> [@khaledharizb](#):
>
> ```julia
> struct Point{T<:Real}
> x::T
> y::T
> Point{T}(x,y) where {T<:Real} = x^2+y^2
> end
> 
> ```

This is what I would call a “functor” but again, I would avoid this unless there is a good reason.

```julia
mutable struct Point{T}
    x::T
    y::T
end
function (point::Point)()
    return point.x^2+point.y^2
end

```

Although I am not sure what reason you have for the struct at all, these are all probably better done with normal plain functions.

---

<div class="post-metadata">

### Author: ![khaledharizb](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@khaledharizb](https://discourse.julialang.org/u/khaledharizb)
#### Post date: [February 27, 2023, 8:45am UTC](https://discourse.julialang.org/t/define-lagrangian-using-struct/95193/6 "2023-02-27T08:45:32Z")

</div>

Great @jmair thank you!
