# How to implement "dependent constants" in a module

**URL:** <https://discourse.julialang.org/t/how-to-implement-dependent-constants-in-a-module/55766>\
**Category:** New to Julia\
**Tags:** question, module\
**Created:** [February 22, 2021, 9:16am UTC](https://discourse.julialang.org/t/how-to-implement-dependent-constants-in-a-module/55766 "2021-02-22T09:16:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![triceert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/triceert/32/21896_2.png) [@triceert](https://discourse.julialang.org/u/triceert)\
**Post date:** [February 22, 2021, 9:16am UTC](https://discourse.julialang.org/t/how-to-implement-dependent-constants-in-a-module/55766/1 "2021-02-22T09:16:08Z")

</div>

So from what I learned in Julia so far, constants make everything faster. So if I have a script that calculates a value that is dependent on the input of the script but then stays constant throughout the script (e.g. a Diffusion coefficient dependent on the chemical species I want to run the script for), I could implement something like this:

```julia
fizz = 4 #dependent on input, the "constant" for the rest of the script has another value

function foo(bar)
  return bar*3
end

const buzz = foo(fizz)

#do all calculations with a constant value of buzz

```

Now I want to implement this to in a module, but it’s not clear to me how to do it. Does anybody have some suggestions or examples?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 22, 2021, 9:38am UTC](https://discourse.julialang.org/t/how-to-implement-dependent-constants-in-a-module/55766/2 "2021-02-22T09:38:46Z")

</div>

The requirement for constants associated to performance is if they are used as global variables. If you pass the values as parameters to functions that is not needed.

Thus, if the calculations are performed in a function that receive as parameter the diffusion coefficient, you don’t need it to be declared constant.

(You can do something like you are asking, but it is not a common pattern).

---

<div class="post-metadata">

**Author:** ![triceert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/triceert/32/21896_2.png) [@triceert](https://discourse.julialang.org/u/triceert)\
**Post date:** [February 22, 2021, 9:58am UTC](https://discourse.julialang.org/t/how-to-implement-dependent-constants-in-a-module/55766/3 "2021-02-22T09:58:04Z")

</div>

Thank you for your quick reply, I understand but I think I still need to achieve it. Maybe my example was too minimal however. I try to explain it better:

Actually I have an algorithm implementing an inverse advection problem using Zygote. So I actually define something like

```julia

species = "ethanol"
v = 1. #velocity
n = 100 #number of discretization points

function build_band_matrix(v,n,species)
    D = calculate_diffusion_coefficient(species)
    VB, DB = build_matrices(n) #discretization matrices for convection&diffusion
    band_matrix = v.*VB+D.*DB
    return band_matrix
end

```

In order to then use this band matrix together with the parameters to fit, I had to define

```julia
const ode_system_matrix = build_band_matrix(v,n,species)

#define and solve inverse problem with zygote

```

since Zygote does not work with mutables like my matrix building function. Now I would like to implement this kind of pipeline/sequence in a module.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 22, 2021, 10:26am UTC](https://discourse.julialang.org/t/how-to-implement-dependent-constants-in-a-module/55766/4 "2021-02-22T10:26:35Z")

</div>

Well, you _can_ do that in a module, although I am not sure if that is really what you want:

```julia
julia> module M
         x = 2
         f(x) = 3*x
         const y = f(x)
       end

julia> using .M

julia> M.y
6

```

---

<div class="post-metadata">

**Author:** ![triceert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/triceert/32/21896_2.png) [@triceert](https://discourse.julialang.org/u/triceert)\
**Post date:** [February 22, 2021, 10:41am UTC](https://discourse.julialang.org/t/how-to-implement-dependent-constants-in-a-module/55766/5 "2021-02-22T10:41:39Z")

</div>

Yes I want to do something like this, but then also to be able to do something like redefining

`M.x = 3`

to retrieve `M.y = 9` as a new constant for the rest of the functions implemented in M.  
In other words I want the module to be able to calculations for different values of x (e.g read in from a file) but for the rest of the functions to work, M.y = f(M.x) has to be a constant. (Hence the borderline nonsensical title “dependent constants”.)

Maybe I’m also doing something fundamentally wrong.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 22, 2021, 11:57am UTC](https://discourse.julialang.org/t/how-to-implement-dependent-constants-in-a-module/55766/6 "2021-02-22T11:57:22Z")

</div>

> [@triceert](#):
>
> ```julia
> const ode_system_matrix = build_band_matrix(v,n,species)
> 
> ```

I think the standard way is to define the `build_band_matrix `function in the module and let this command to be run by the user outside it, and then call the functions that use `ode_system_matrix`.

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [February 22, 2021, 12:02pm UTC](https://discourse.julialang.org/t/how-to-implement-dependent-constants-in-a-module/55766/7 "2021-02-22T12:02:13Z")

</div>

You might want to use a Struct as a container for those variables and pass it to your module functions.

```julia
struct S
    x 
    y 
    function S(x)
        y = 3*x
        new(x,y)
    end
end
function foo(myvar1,myvar2,stru)
    a = myvar2*stru.x 
    b = myvar1*stru.y
    return (a,b)
end

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 22, 2021, 2:11pm UTC](https://discourse.julialang.org/t/how-to-implement-dependent-constants-in-a-module/55766/8 "2021-02-22T14:11:26Z")

</div>

> [@triceert](#):
>
> Yes I want to do something like this, but then also to be able to do something like redefining
> 
> `M.x = 3`
> 
> to retrieve `M.y = 9` as a new constant for the rest of the functions implemented in M

Unfortunately this isn’t possible. A constant needs to be, well, constant. Moreover, a constant is just a value–it doesn’t represent some kind of implicit computation that happens every time the inputs to some function change.

It seems like what you really want is the ability to take some function `f(x, y)` where `y` is given by `build_band_matrix` and then pass it to something like Zygote which only works if your function looks like `g(x)` with `y` fixed, right?

If that’s what you want, then you don’t need a constant or a global variable at all. Instead, you need a _closure_:

```julia
function main()
  y = build_band_matrix(species, v, n)
  func_to_differentiate = x -> f(x, y) # you can also write `func_to_differentiate(x) = f(x, y)`
  do_zygote_stuff(func_to_differentiate)
end

```

You can write this more compactly as:

```julia
function main()
  do_zygote_stuff(x -> f(x, build_band_matrix(species, v, n)))
end

```

---

<div class="post-metadata">

**Author:** ![triceert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/triceert/32/21896_2.png) [@triceert](https://discourse.julialang.org/u/triceert)\
**Post date:** [February 24, 2021, 3:14pm UTC](https://discourse.julialang.org/t/how-to-implement-dependent-constants-in-a-module/55766/9 "2021-02-24T15:14:11Z")

</div>

Thank you @rdeits that was exactly what I was looking for.
