# Static vector of functions as a field of a composite type

**URL:** <https://discourse.julialang.org/t/static-vector-of-functions-as-a-field-of-a-composite-type/99083>\
**Category:** New to Julia\
**Tags:** performance, function, staticarrays, symbolics, autodiff\
**Created:** [May 18, 2023, 8:17pm UTC](https://discourse.julialang.org/t/static-vector-of-functions-as-a-field-of-a-composite-type/99083 "2023-05-18T20:17:19Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![aafsar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aafsar/32/47420_2.png) [@aafsar](https://discourse.julialang.org/u/aafsar)\
**Post date:** [May 20, 2023, 11:59am UTC](https://discourse.julialang.org/t/static-vector-of-functions-as-a-field-of-a-composite-type/99083/4 "2023-05-20T11:59:54Z")

</div>

Thank you @mikmoore and @gdalle for your great answers!

**Regarding storing functions:** The collections are indeed low-dimensional, e.g. \<10. The reason I explored using `SVector`s is to be able to access the number derivative functions for compiler to reason about when iterating over, [as discussed in this post](https://discourse.julialang.org/t/array-of-functions-is-there-a-way-to-avoid-allocations-performance-penalty/24471), but I guess one can simply use bare `Tuples` [as suggested in this post](https://discourse.julialang.org/t/get-tuple-length-from-type/32483) directly without the extra `Static Array` structure on top. I guess another way would be to use [FunctionWrappers.jl](https://github.com/yuyichao/FunctionWrappers.jl) for the `target_function`, and use the type of the output to inform compiler about the length of the `derivatives` `Tuple` (since if the codomain of the `target_function` is \mathbb{R}^n, then the number of derivatives, i.e. the length of

**Regarding Symbolic D vs AD** : I think the package would benefit from AD, since the `target_function`s are very nonlinear, and in most cases would lead to “expression swell” as you mentioned. As an example of a `target_function` (for some fixed parameters) h \colon \mathbb{R}^n \to \mathbb{R}^n:

h(y)=(h\_1(y),...,h\_n(y)), such that  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; h\_i(y) = \int\_X f\_i(x,y)g(x) dx, where  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f\_i(x,y)=\frac{e^{1 - \lVert x- y\_i \rVert}}{1+\sum\_j{e^{1 - \lVert x- y\_j \rVert}}} and g(x) is some pdf.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; and the derivatives of interest are \frac{\partial^k h\_i}{\partial y\_i^k} for k \in {1,2}.

**The use case:** The practice is a multi-objective optimization: finding local Nash equilibria in a game theoretic setting. So the aim is to find the roots of \tilde{h}(y)=(\frac{\partial h\_1}{\partial y\_1},...,\frac{\partial h\_n}{\partial y\_n}) such that \frac{\partial^2 h\_i}{\partial y\_i^2}\<0 \, \forall i (ignoring other cases for now). One approach that I have been trying is to use interval arithmetics (à la [JuliaIntervals](https://github.com/JuliaIntervals) by @dpsanders et al.), especially using their [IntervalConstraintProgramming.jl](https://github.com/JuliaIntervals/IntervalConstraintProgramming.jl)(\*), however the current version seems to be not compatible with `Dual` type propagation needed to couple it with something like [ForwardDiff.jl](https://github.com/JuliaDiff/ForwardDiff.jl), since it internally converts constraints to `ModelingToolkit.Operation` types for which the `Dual` is not defined, and I couldn’t figure out how to make it work yet. (I guess I should open a new topic for this since it’s a different issue)  
Long story short, that’s why I reverted to using symbolic differentiation using [Symbolics.jl](https://github.com/JuliaSymbolics/Symbolics.jl).

(\*) The reason that I try to implement IntervalConstraintProgramming is that each y\_i might be allowed to be vector-valued, e.g. points in 2D space, and thus the function for which the roots to be found becomes \tilde{h} \colon \mathbb{R}^{2n} \to \mathbb{R}^n, disallowing methods like Newton due to non-invertable Jacobian (as far as I understood).

---

_[View the full topic](https://discourse.julialang.org/t/static-vector-of-functions-as-a-field-of-a-composite-type/99083)._
