Automatic generation of parametric functions

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

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

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

1 Like

Why not

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

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

1 Like

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

1 Like

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

The structs involved are defined like this:

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)

(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

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.

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

@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