# Minimum working example for ModelingToolkit's CTarget

**URL:** https://discourse.julialang.org/t/minimum-working-example-for-modelingtoolkits-ctarget/53303
**Category:** General Usage
**Tags:** question, package
**Created:** [January 13, 2021, 7:28pm UTC](https://discourse.julialang.org/t/minimum-working-example-for-modelingtoolkits-ctarget/53303 "2021-01-13T19:28:34Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![cadojo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cadojo/32/25328_2.png) [@cadojo](https://discourse.julialang.org/u/cadojo)
#### Post date: [January 13, 2021, 7:28pm UTC](https://discourse.julialang.org/t/minimum-working-example-for-modelingtoolkits-ctarget/53303/1 "2021-01-13T19:28:34Z")

</div>

I’m trying to use `ModelingToolkit.build_function` to generate a C function with “vector-like” inputs, and “matrix-like” outputs. I would prefer **not** to have the left-hand-side of the equations in the state vector, but I can’t figure out how to build a `CTarget` function without including the left-hand-side in the “variables” argument.

**My Attempt**

```nohighlight
import Pkg
Pkg.activate(tempdir())
Pkg.add("ModelingToolkit")
using ModelingToolkit

@variables x[1:2,1:2] y[1:4]
eqs = x[:] .~ y

build_function(eqs, [y..., x...]; 
               target=ModelingToolkit.CTarget())

```

**Output**

```nohighlight
"void diffeqf(double* du, double* RHS1) {\n du[4] = RHS1[0];\n du[5] = RHS1[2];\n du[6] = RHS1[1];\n du[7] = RHS1[3];\n}\n"

```

The output above has `du` first indexed at `4`, because it is the _4th through 8th_ element in the state vector. I’d prefer for `du` and `RHS1` to be entirely separate. **How can I best accomplish this?**

---

<div class="post-metadata">

### Author: ![cadojo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cadojo/32/25328_2.png) [@cadojo](https://discourse.julialang.org/u/cadojo)
#### Post date: [January 13, 2021, 7:55pm UTC](https://discourse.julialang.org/t/minimum-working-example-for-modelingtoolkits-ctarget/53303/2 "2021-01-13T19:55:17Z")

</div>

If I leave out the `x...` in the state vector argument to `build_function`…

```nohighlight
julia> @variables x[1:2,1:2] y[1:4]
julia> eqs = x[:] .~ y

julia> build_function(eqs, y, target=ModelingToolkit.CTarget())
ERROR: MethodError: no method matching +(::Nothing, ::Int64)
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at operators.jl:538
  +(::ChainRulesCore.One, ::Any) at /home/joe/.julia/packages/ChainRulesCore/cpHLu/src/differential_arithmetic.jl:94
  +(::ChainRulesCore.Zero, ::Any) at /home/joe/.julia/packages/ChainRulesCore/cpHLu/src/differential_arithmetic.jl:63

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [January 13, 2021, 8:17pm UTC](https://discourse.julialang.org/t/minimum-working-example-for-modelingtoolkits-ctarget/53303/3 "2021-01-13T20:17:07Z")

</div>

It’s just not supported yet. CTarget would need more work.

---

<div class="post-metadata">

### Author: ![cadojo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cadojo/32/25328_2.png) [@cadojo](https://discourse.julialang.org/u/cadojo)
#### Post date: [January 13, 2021, 8:40pm UTC](https://discourse.julialang.org/t/minimum-working-example-for-modelingtoolkits-ctarget/53303/4 "2021-01-13T20:40:51Z")

</div>

I _might_ have time to look into this in 2021. I think all that would be required is some top-level parsing changes, since the equations produced are correct in this simple Matrix case, the indices just need to be offset.

Should I file an issue on GitHub? Or hold off for now, since this isn’t a supported case?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [January 13, 2021, 9:20pm UTC](https://discourse.julialang.org/t/minimum-working-example-for-modelingtoolkits-ctarget/53303/5 "2021-01-13T21:20:29Z")

</div>

file an issue

---

<div class="post-metadata">

### Author: ![cadojo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cadojo/32/25328_2.png) [@cadojo](https://discourse.julialang.org/u/cadojo)
#### Post date: [January 14, 2021, 7:34pm UTC](https://discourse.julialang.org/t/minimum-working-example-for-modelingtoolkits-ctarget/53303/6 "2021-01-14T19:34:50Z")

</div>

Update: [#736](https://github.com/SciML/ModelingToolkit.jl/pull/736)
