# Automatic generation of parametric functions

**URL:** <https://discourse.julialang.org/t/automatic-generation-of-parametric-functions/38151>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [April 24, 2020, 3:02pm UTC](https://discourse.julialang.org/t/automatic-generation-of-parametric-functions/38151 "2020-04-24T15:02:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isaia\_Nisoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isaia_nisoli/32/12594_2.png) [@Isaia\_Nisoli](https://discourse.julialang.org/u/Isaia_Nisoli)\
**Post date:** [April 24, 2020, 3:02pm UTC](https://discourse.julialang.org/t/automatic-generation-of-parametric-functions/38151/1 "2020-04-24T15:02:07Z")

</div>

Dear all,  
I have a parametric Type, for simplicity suppose something like

```julia
struct Foo{T}
      x::T
end 

```

a list of function symbols (i.e. :sin, :cos, etc) and I would like to generate a function for each element in my list, i.e., something like

```julia
for funcsymb in funclyst
      @eval $funcsymb(x::T) where T = $funcsymb(x) 
end

```

but it does not seem to work.

Any idea in how to solve this kind of issue?

Thanks  
Isaia

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 24, 2020, 3:07pm UTC](https://discourse.julialang.org/t/automatic-generation-of-parametric-functions/38151/2 "2020-04-24T15:07:01Z")

</div>

Why not

```julia
for funcsymb in funclyst
      @eval $funcsymb(x::Foo) = $funcsymb(x.x) 
end

```

? Or why do you ask about the type-parameter?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 24, 2020, 3:15pm UTC](https://discourse.julialang.org/t/automatic-generation-of-parametric-functions/38151/3 "2020-04-24T15:15:50Z")

</div>

Please provide a minimal working example where you eg give example values of funcsymb and funclyst so people can copy and paste.

---

<div class="post-metadata">

**Author:** ![Isaia\_Nisoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isaia_nisoli/32/12594_2.png) [@Isaia\_Nisoli](https://discourse.julialang.org/u/Isaia_Nisoli)\
**Post date:** [April 24, 2020, 3:25pm UTC](https://discourse.julialang.org/t/automatic-generation-of-parametric-functions/38151/4 "2020-04-24T15:25:28Z")

</div>

I’m working on this project for sparse automatic differentiation:

> **[GitHub - orkolorko/DiffCheckNumbers.jl: Sparse Differentiation Arithmetics...](https://github.com/orkolorko/DiffCheckNumbers.jl)**
>
> Sparse Differentiation Arithmetics for Julia. Contribute to orkolorko/DiffCheckNumbers.jl development by creating an account on GitHub.

The structs involved are defined like this:

```julia
const StaticTuple{T,S,N} = NamedTuple{S, NTuple{N,T}}

struct DiffCheck{T<:ReComp, S, N} <: Number
    value::T
    der::StaticTuple{T,S,N} # maybe not the fastest structure

   function DiffCheck{T, S}(x::T, der::StaticTuple{T,S,N}) where {T<:ReComp, S, N}
        return new{T,S,N}(x, der)
    end   
end

```

where S is a tuple of Symbols, N is the size of the Tuple.  
The symbols are a part of the parameters so that a lot of things are static and work is done at compile time.  
I’m currently trying to implement function calls

I would like the following to work (in alternative I will generate a source file and the include it)

```julia
(funsym, der) = (:(sin), :(cos))

@eval function Base.$funsym(x::DiffCheck{T,S,N}) where {T, S, N}
    return DiffCheck{T, S}($funsym(value(x)), StaticTuple{T,S,N}(($der(value(x)) .* values(derivatives(x)))))
end

```

---

<div class="post-metadata">

**Author:** ![Isaia\_Nisoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isaia_nisoli/32/12594_2.png) [@Isaia\_Nisoli](https://discourse.julialang.org/u/Isaia_Nisoli)\
**Post date:** [April 24, 2020, 4:31pm UTC](https://discourse.julialang.org/t/automatic-generation-of-parametric-functions/38151/5 "2020-04-24T16:31:20Z")

</div>

I think a similar design pattern may be implemented for Taylor series.

I’m also implementing a dynamical version of these Sparse Automatic Differentiation types for performance comparison.  
Allocating a big quantity of heterogeneous objects seems expensive.

---

<div class="post-metadata">

**Author:** ![Isaia\_Nisoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isaia_nisoli/32/12594_2.png) [@Isaia\_Nisoli](https://discourse.julialang.org/u/Isaia_Nisoli)\
**Post date:** [April 24, 2020, 6:24pm UTC](https://discourse.julialang.org/t/automatic-generation-of-parametric-functions/38151/6 "2020-04-24T18:24:08Z")

</div>

Indeed, the code was working fine. It was an import problem, the following code works fine

```julia
@eval function $(funsym)(z::DiffCheck{T,S,N}) where {T, S, N}
            x = value(z)
            xp = values(derivatives(z))
            DiffCheck{T, S}($(funsym)(x), StaticTuple{T,S,N}($exp .*values(derivatives(z))))
        end

```
