# Create Generic Function Without Macros From String and Symbolic Variable

**URL:** <https://discourse.julialang.org/t/create-generic-function-without-macros-from-string-and-symbolic-variable/117535>\
**Category:** General Usage\
**Tags:** symbolics\
**Created:** [July 27, 2024, 12:03am UTC](https://discourse.julialang.org/t/create-generic-function-without-macros-from-string-and-symbolic-variable/117535 "2024-07-27T00:03:36Z")\
**Posts on this page:** 14\
**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:** [July 27, 2024, 12:03am UTC](https://discourse.julialang.org/t/create-generic-function-without-macros-from-string-and-symbolic-variable/117535/1 "2024-07-27T00:03:36Z")

</div>

I know you can create a function, say `f(t)` where `t` is a symbolic variable with a macro. How can you do it without using a macro where I have previously defined `t`. More specifically I wish to define several different functions, `f(t)`, in a function where the name `f` is specified by a string. For example I have a function `MultiVector("f",t)` and I wish to return `[f(t),fx(t),fy(t),fz(t),fxy(t),fyz(t),fzx(t), fxyz(t)]`. Is there a way to do this. Also if `t` is replaces by multiple variables such as `f(x,y,z)` where the generating function could be `MultiVector(“f”,[x,y,z]). Thanks for any help.

---

<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:** [July 27, 2024, 1:07am UTC](https://discourse.julialang.org/t/create-generic-function-without-macros-from-string-and-symbolic-variable/117535/2 "2024-07-27T01:07:59Z")

</div>

> [@brombo](#):
>
> create a function, say `f(t)` where `t` is a symbolic variable with a macro.

Do you have a working code example of this? Not sure if you’re using atypical terminology for base Julia or talking about Symbolics.jl, in which case the post could use a more specific tag.

---

<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:** [July 27, 2024, 1:26am UTC](https://discourse.julialang.org/t/create-generic-function-without-macros-from-string-and-symbolic-variable/117535/3 "2024-07-27T01:26:17Z")

</div>

I am using Julia Symbolics. My previous experience is with sympy. I am able to follow Symbolics but I don’t see how to use macros to create general functions say `f(t)` whose names are defined by strings such as `"f"`.

---

<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:** [July 27, 2024, 1:54am UTC](https://discourse.julialang.org/t/create-generic-function-without-macros-from-string-and-symbolic-variable/117535/4 "2024-07-27T01:54:56Z")

</div>

Hm, my Symbolics is rather rusty, but it seems like you want to do something with a value known only at runtime, but the macro `@variables` only works on values known when you write the line. Maybe you’re looking for the functions `variable` and `variables`? Those strictly take `Symbol`s (from base Julia, not the same as symbolic expressions in Symbolics), so you’ll need to convert somewhere like `Symbol("f")`. Doing the same for function call expressions escape my memory at the moment, so I’ll add the tag and maybe someone who uses it on a regular basis can help.

---

<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:** [July 27, 2024, 2:07am UTC](https://discourse.julialang.org/t/create-generic-function-without-macros-from-string-and-symbolic-variable/117535/5 "2024-07-27T02:07:22Z")

</div>

Thank you. I will repost on the forum for Symbolics. Note I have a routine that uses `Symbolics.variables("t")` to create a variable `t`. I don’t know how to make a function with anything other than `@variables t,f(t)` and that doesn’t let me pass the name of the function from a string anymore than `@variables t` does. I am not particularly fond of the macros in Symbolics.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 27, 2024, 2:07am UTC](https://discourse.julialang.org/t/create-generic-function-without-macros-from-string-and-symbolic-variable/117535/6 "2024-07-27T02:07:45Z")

</div>

> [@Benny](#):
>
> the macro `@variables` only works on values known when you write the line. Maybe you’re looking for the functions `variable` and `variables`

It should also be possible to interpolate into the macro with `$`. Like this, I think:

```julia-repl
julia> f = :f
:f

julia> t = :t
:t

julia> @variables $t, $f($t)
2-element Vector{Num}:
 t
  f(t)

```

---

<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:** [July 27, 2024, 2:34am UTC](https://discourse.julialang.org/t/create-generic-function-without-macros-from-string-and-symbolic-variable/117535/7 "2024-07-27T02:34:49Z")

</div>

Definitely slipped my mind that interpolation was supported. I think brombo is saying a function needs to take those values though, so are runtime-known values supported, e.g. `foo(f, t) = @variables $t, $f($t)` allows `foo(:sin, :x)`?

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [July 27, 2024, 7:55am UTC](https://discourse.julialang.org/t/create-generic-function-without-macros-from-string-and-symbolic-variable/117535/8 "2024-07-27T07:55:23Z")

</div>

Interpolation seems to work fine for me

```julia-repl
julia> foo(:sin, :t)
2-element Vector{Num}:
      t
 sin(t)

```

Another option seems to be

```julia
let f = :sin, x = :t
    Symbolics.variable(f, T=Symbolics.FnType)(Symbolics.variable(x))
end

```

---

<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:** [July 27, 2024, 12:11pm UTC](https://discourse.julialang.org/t/create-generic-function-without-macros-from-string-and-symbolic-variable/117535/9 "2024-07-27T12:11:30Z")

</div>

I am using

```julia
str = "x"
xvar = Symbolics.variable(str)

```

and it works fine. Would the following work?

```julia
xstr = "x"
fstr = "f"
xvar = Symbolics.variable(xstr)
fvar = Symbolics.variable(fstr, T=Symbolics.FnType)(xvar)

```

or if `f` was a function of say an `xvar` and `yvar` then

```julia
xstr = "x"
ystr = "y"
fstr = "f"
xvar = Symbolics.variable(xstr)
yvar = Symbolics.variable(ystr)
fvar = Symbolics.variable(fstr, T=Symbolics.FnType)(xvar,yvar)

```

for `f(x,y)`?

---

<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:** [July 27, 2024, 12:38pm UTC](https://discourse.julialang.org/t/create-generic-function-without-macros-from-string-and-symbolic-variable/117535/10 "2024-07-27T12:38:16Z")

</div>

Thank you. I tried it where `f` and `x` are strings and it worked fine and also for function with multiple arguments. Symbolics is great except that it generates lousy LaTeX strings which actually id probably the fault of LaTeXStrings and not Symbolics. LaTeXStrings needs to do a lot more post processing than it does.

---

<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:** [July 27, 2024, 1:39pm UTC](https://discourse.julialang.org/t/create-generic-function-without-macros-from-string-and-symbolic-variable/117535/11 "2024-07-27T13:39:00Z")

</div>

Are you looking for `build_function`?

---

<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:** [July 27, 2024, 2:18pm UTC](https://discourse.julialang.org/t/create-generic-function-without-macros-from-string-and-symbolic-variable/117535/12 "2024-07-27T14:18:14Z")

</div>

I have it working (thanks to advice on the forum) for a single variable. The question is how to implement my functions function for functions of multiple variables.

```julia
function functions(s::String,X::Vector{Num})
S = split(s," ")
F = []
for str in S
f = Symbolics.variable(str, T=Symbolics.FnType)(X)
push!(F,f)
end
return F
end

x,y = symbols("x y") #My function using Symbolics.variable(mystring)
F = functions("f fx fy fxy",[x,y]) #Equivalent of symbols but for functions
display(F)

```

Output -

4-element Vector{Any}:  
f(Num[x,y])  
fx(Num[x,y])  
fy(Num[x,y])  
fxy(Num[x,y])

How do I get -

4-element Vector{Any}:  
f(x,y)  
fx(x,y)  
fy(x,y)  
fxy(x,y)

Essentially how to convert -

([X[1],X[2]]) to (X[1],X[2]) in Symbolics argument

But for arbitrary length vector of variables?

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [July 27, 2024, 9:03pm UTC](https://discourse.julialang.org/t/create-generic-function-without-macros-from-string-and-symbolic-variable/117535/13 "2024-07-27T21:03:34Z")

</div>

Splatting

```julia
f = Symbolics.variable(str, T=Symbolics.FnType)(X...)

```

---

<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:** [July 28, 2024, 12:15am UTC](https://discourse.julialang.org/t/create-generic-function-without-macros-from-string-and-symbolic-variable/117535/14 "2024-07-28T00:15:57Z")

</div>

Than you. That was what I needed.
