# Creating a struct with symbolic (Symbolics package) functions and variables

**URL:** <https://discourse.julialang.org/t/creating-a-struct-with-symbolic-symbolics-package-functions-and-variables/98851>\
**Category:** General Usage\
**Created:** [May 14, 2023, 9:52pm UTC](https://discourse.julialang.org/t/creating-a-struct-with-symbolic-symbolics-package-functions-and-variables/98851 "2023-05-14T21:52:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [May 14, 2023, 9:52pm UTC](https://discourse.julialang.org/t/creating-a-struct-with-symbolic-symbolics-package-functions-and-variables/98851/1 "2023-05-14T21:52:15Z")

</div>

I am trying to write a struct with members to manipulate symbolic variables. I am a newbie to Julia. I have programmed in python using sympy for symbolic algebra. Here is the code I have so far -

```julia
using Symbolics, SymbolicUtils

mutable struct SYMBOLS
    num_vars::Int
    latex_map::Dict
    vars::Vector{SymbolicUtils.Sym{Real}}
    add
    latex
    dump
end

function Symbols_add(s::String)
    syms = split(s," ")
    n = length(syms)
    for i in 1:n
        Symbols.latex_map[Symbols.vars[i]] = syms[i+Symbols.num_vars]
    end
    Symbols.num_vars = Symbols.num_vars+n
    return Symbols.var[Symbols.num_vars:Symbols.num_vars-n]
end

function Symbols_latex(sym::SymbolicUtils.Sym{Real})
    return Symbols.latex_map(sym)
end

function Symbols_dump()
    for s in Symbols.var_lst
        println(Symbols.latex_map[s])
    end
end

Symbols = SYMBOLS(0,Dict(),@variables X[1:50],Symbols_add,Symbols_latex,Symbols_dump)

(x,y,z) = Symbols.add("x y z")
(a,b) = Symbols.add(raw"\alpha \beta")

Symbols.dump()

```

and this is my error message -

ERROR: LoadError: invalid redefinition of constant Symbols\_add  
Stacktrace:  
[1] top-level scope  
@ ~/MyJulia/MySymbols.jl:32  
in expression starting at /home/brombo/MyJulia/MySymbols.jl:32

I feel that I must be declaring the types of the members of the SYMBOLS struct or the the types of the functions I entered into the SYMBOLS constructor incorrectly but can find no examples of how to do it correctly. Documentation of struct with members that a functions that are not one-line functions seems to be almost non-existent. Any suggestions would be appreciated.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [May 14, 2023, 10:09pm UTC](https://discourse.julialang.org/t/creating-a-struct-with-symbolic-symbolics-package-functions-and-variables/98851/2 "2023-05-14T22:09:30Z")

</div>

Never used Symbolics before, but the expression `@variables X[1:50], Symbols_add, Symbols_latex, Symbols_dump` is making the macro operate on a tuple of all those, and it’s erroring at trying to reassign the implicitly `const` function name `Symbols_add`. It’s covered in the docs but doesn’t cover implicit tuple parentheses, so here’s an example:

```julia
julia> macro id(args...) args end
@id (macro with 1 method)

julia> @id 1 2 3 # typical one-liner, 3 Ints
(1, 2, 3)

julia> @id(1, 2, 3) # equivalent call expression
(1, 2, 3)

julia> @id (1, 2, 3) # spacing does one-liner, now 1 tuple
(:((1, 2, 3)),)

julia> @id 1, 2, 3 # still 1 tuple argument
(:((1, 2, 3)),)

```

To fix the macro call, you would just change to `@variables(X[1:50])` or `(@variables X[1:50])`. But I’m not sure it wouldn’t cause another error, could give it a try.

---

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [May 14, 2023, 10:21pm UTC](https://discourse.julialang.org/t/creating-a-struct-with-symbolic-symbolics-package-functions-and-variables/98851/3 "2023-05-14T22:21:42Z")

</div>

Thanks for you efforts. By suggestions did cause a whole slew of new errors. I think I need help from someone familiar with the `Symbolics` package.

---

<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:** [May 15, 2023, 4:27am UTC](https://discourse.julialang.org/t/creating-a-struct-with-symbolic-symbolics-package-functions-and-variables/98851/4 "2023-05-15T04:27:39Z")

</div>

> [@brombo](#):
>
> ERROR: LoadError: invalid redefinition of constant Symbols\_add  
> Stacktrace:  
> [1] top-level scope  
> @ ~/MyJulia/MySymbols.jl:32  
> in expression starting at /home/brombo/MyJulia/MySymbols.jl:32

This is of course unrelated and fixed by not having a non-function version of that.

> [@brombo](#):
>
> I feel that I must be declaring the types of the members of the SYMBOLS struct or the the types of the functions I entered into the SYMBOLS constructor incorrectly but can find no examples of how to do it correctly. Documentation of struct with members that a functions that are not one-line functions seems to be almost non-existent. Any suggestions would be appreciated.

They are all just `Num{SymbolicUtils.BasicSymbolic}`

---

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [May 15, 2023, 12:49pm UTC](https://discourse.julialang.org/t/creating-a-struct-with-symbolic-symbolics-package-functions-and-variables/98851/5 "2023-05-15T12:49:56Z")

</div>

Thank you that worked. Sometimes the simplest things are the hardest to see!
