# Inconsistent build\_function behavior in Symbolics

**URL:** <https://discourse.julialang.org/t/inconsistent-build-function-behavior-in-symbolics/80230>\
**Category:** Numerics\
**Tags:** symbolics\
**Created:** [April 28, 2022, 10:01pm UTC](https://discourse.julialang.org/t/inconsistent-build-function-behavior-in-symbolics/80230 "2022-04-28T22:01:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)\
**Post date:** [April 28, 2022, 10:01pm UTC](https://discourse.julialang.org/t/inconsistent-build-function-behavior-in-symbolics/80230/1 "2022-04-28T22:01:44Z")

</div>

Hi,

is there a good way to make sure that the output function from `build_function` in Symbolics returns output in the same dimensions as the symbolic function used to generate it?

In the simple example below, if `N = 5`, running `myf` returns a sparse matrix (as expected). However, if `N=100`, it returns a sparse vector. If I reshape it, it becomes a dense vector, defeating the purpose of trying to keep everything sparse. Does anybody have a work around?

```julia
using Symbolics, SparseArrays

N = 5 # try with N = 5 and N = 100
_S = sprand(N, N, 0.1) 
_Q = Array(sprand(N, N, 0.1))

F(z) = [
    _S * z 
    _Q * z.^2
]

Symbolics.@variables z[1:N]

sj = Symbolics.sparsejacobian(F(z), z)

f_expr = build_function(sj, z)
myf = eval(first(f_expr))
myf(rand(N))

reshape(myf(rand(N)), size(sj)) # dense!

```

---

<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:** [April 28, 2022, 10:04pm UTC](https://discourse.julialang.org/t/inconsistent-build-function-behavior-in-symbolics/80230/2 "2022-04-28T22:04:05Z")

</div>

What if you do `sj = Symbolics.sparsejacobian(F(z), collect(z))`?

---

<div class="post-metadata">

**Author:** ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)\
**Post date:** [April 28, 2022, 10:08pm UTC](https://discourse.julialang.org/t/inconsistent-build-function-behavior-in-symbolics/80230/3 "2022-04-28T22:08:04Z")

</div>

That doesn’t change the inconsistent output behavior. I think the problem is in `build_function` because `sj` seem to look good no matter the size of `N`. However, `myf` is a sparse matrix for small N, and a sparse vector for large `N` (ideally they should both be sparse matrices).
