# Slow indexing of constant-valued variables in MTK

**URL:** <https://discourse.julialang.org/t/slow-indexing-of-constant-valued-variables-in-mtk/120922>\
**Category:** Modelling & Simulations\
**Tags:** question, modelingtoolkit\
**Created:** [October 4, 2024, 2:50pm UTC](https://discourse.julialang.org/t/slow-indexing-of-constant-valued-variables-in-mtk/120922 "2024-10-04T14:50:55Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![bspanoghe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bspanoghe/32/209460_2.png) [@bspanoghe](https://discourse.julialang.org/u/bspanoghe)\
**Post date:** [October 7, 2024, 12:28pm UTC](https://discourse.julialang.org/t/slow-indexing-of-constant-valued-variables-in-mtk/120922/4 "2024-10-07T12:28:41Z")

</div>

Thank you for your response!

I’ve considered forcing the system to solve directly for `B` by instantiating it with the desired values  
`@variables B(t)[1:n] = rand(n)`  
and then setting the gradient to 0.  
`[D(B[i]) ~ 0 for i in eachindex(B)]...`

This does solve the indexing time problem (at the cost of slightly higher solving time), but I was hoping for a cleaner solution.

Based on [this thread](https://discourse.julialang.org/t/modelingtoolkit-jl-intermediate-symbolic-variables-calculated-from-solutions-output-in-the-state-vector/54024) it seems there is some way to mark variables you want to track, but the closest I’ve found is the `irreducible` metadata tag mentioned [here](https://docs.sciml.ai/ModelingToolkit/stable/basics/Variable_metadata/#Irreducible):  
`@variables A(t)[1:n] = zeros(n) B(t)[1:n] [irreducible = true]`

Sadly it slows down the solver immensely, making it an even worse solution for my problem.

---

_[View the full topic](https://discourse.julialang.org/t/slow-indexing-of-constant-valued-variables-in-mtk/120922)._
