# \[ANN\] SymbolicUtils.jl -- Groundwork for a symbolic ecosystem in Julia

**URL:** https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455
**Category:** Package Announcements
**Tags:** announcement
**Created:** [April 29, 2020, 10:56pm UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455 "2020-04-29T22:56:36Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![shashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shashi/32/1824_2.png) [@shashi](https://discourse.julialang.org/u/shashi)
#### Post date: [April 29, 2020, 10:56pm UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/1 "2020-04-29T22:56:37Z")

</div>

Hello everyone, we (Mason Protter, Yingbo Ma, and I) are pleased to present to you [SymbolicUtils.jl](https://github.com/JuliaSymbolics/SymbolicUtils.jl). This is the first of hopefully many packages to live in the JuliaSymbolics Github organization, and we hope that SymbolicUtils.jl can lay the groundwork for a vibrant, interacting symbolic programming ecosystem in julia.

Building symbolic programming systems is **hard** and there are various reasons to think it is unlikely that one person, or even a small, coordinated group of people, will be able to to build a monolithic symbolic programming system in Julia that can compete with something like Mathematica or SymPy. Hence, we think it’s important to build flexible infrastructure that others can build on, and interoperate with. Other packages are welcome to build on top of our symbolic types but do not have to. We provide infrastructure for [interfacting with SymbolicUtils](https://juliasymbolics.github.io/SymbolicUtils.jl/interface/) so that packages can use their own types if they wish, but plug into our rule rewriting infrastructure.

We’re happy to say that [ModelingToolkit.jl](https://github.com/SciML/ModelingToolkit.jl) from the SciML organization is the first package to depend on and interoperate with SymbolicUtils.jl.

SymbolicUtils.jl is on the general registry and can be added the usual way:

```julia
pkg> add SymbolicUtils

```

or

```julia
julia> using Pkg; pkg"add SymbolicUtils"

```

Here’s a quick and unexplained code sample:

```julia
julia> using SymbolicUtils

julia> @syms x::Real y::Real z::Complex f(::Number)::Real
(x, y, z)

julia> 2x^2 - y + x^2
(3 * (x ^ 2)) + (-1 * y)

julia> f(sin(x)^2 + cos(x)^2) + z
f(1) + z

julia> r = @rule sinh(im * ~x) => sin(~x)
sinh(im * ~x) => sin(~x)

julia> r(sinh(im * y))
sin(y)

julia> simplify(cos(y)^2 + sinh(im*y)^2, [r])
1

```

We have a [manual](https://juliasymbolics.github.io/SymbolicUtils.jl/) with more info.

Collaboration and documentation improvements / suggestions are quite welcome! If you want to chat about SymbolicUtils.jl, you can find us here, on GitHub, or in the [symbolic programming](https://julialang.zulipchat.com/#narrow/stream/236639-symbolic-programming) Zulip stream.

We want to give a big thanks to Harrison Grodin for doing a lot of pioneering work in this approach with [Rewrite.jl](https://github.com/HarrisonGrodin/Rewrite.jl) and [Simplify.jl](https://github.com/HarrisonGrodin/Simplify.jl) which we hybridized with a more [scmutils](https://groups.csail.mit.edu/mac/users/gjs/6946/refman.txt) like approach and added some flavour of our own.

~ @Mason, @YingboMa and @shashi

---

<div class="post-metadata">

### Author: ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)
#### Post date: [April 30, 2020, 12:13am UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/2 "2020-04-30T00:13:38Z")

</div>

This looks very cool!

I noticed there are errors in the [manual](https://juliasymbolics.github.io/SymbolicUtils.jl) that probably weren’t intended:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/a/8a899528ce164b9448323e6950bf0e27a2be9b73.png)

---

<div class="post-metadata">

### Author: ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)
#### Post date: [April 30, 2020, 12:20am UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/3 "2020-04-30T00:20:37Z")

</div>

This might be controversial and offtopic (feel free to make this a separate thread), but the first thing that most people will say when they see a Symbolic package is that:  
**Well, yet another symbolic package.**

No one likes redoing the stuff, and code reusability is the king when it comes to sustainable software libraries.

How do you justify the existence of another symbolic package, instead of developing and growing all the previous ones that exist?

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [April 30, 2020, 12:31am UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/4 "2020-04-30T00:31:50Z")

</div>

> [@yurivish](#):
>
> I noticed there are errors in the [manual](https://juliasymbolics.github.io/SymbolicUtils.jl) that probably weren’t intended:

uh oh, looks like I broke something earlier when I tried to `publish` some documentation fixes. I’m a total Franklin.jl noob. ☹ hoepfully you can follow along by running the code and we can get this fixed asap.

* * *

**Edit:** Looks like it’s now fixed. If you find any other problems, opening an issue on [our github](https://github.com/JuliaSymbolics/SymbolicUtils.jl/issues/new) would be much appreciated!

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [April 30, 2020, 12:32am UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/5 "2020-04-30T00:32:05Z")

</div>

I think exactly that was quite well explained in the OP?

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [April 30, 2020, 12:38am UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/6 "2020-04-30T00:38:39Z")

</div>

> [@Amin\_Yahyaabadi](#):
>
> How do you justify the existence of another symbolic package, instead of developing and growing all the previous ones that exit?

The reason basically boils down to the fact that revamping an existing package to meet the needs we saw appeared to be a greater investment of effort than just writing this from scratch.

We hope that the design decisions we made will make future people less likely to do that and more interested in building on top of what we have. I think the fact that [ModelingToolKit.jl now uses SymbolicUtils.jl for it’s simplification pipeline](https://github.com/SciML/ModelingToolkit.jl/pulls?q=is%3Apr+is%3Aclosed) is an encouraging sign that we might be on the right track.

I should also add that we would likely not have been able to write this package if we didn’t learn from existing previous attempts at similar things. This is a hard problem and we shouldn’t expect anyone to ‘get it right’ on the first try. We’re optimistic about this attempt, but only time will tell how it turns out.

---

<div class="post-metadata">

### Author: ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)
#### Post date: [April 30, 2020, 1:06am UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/7 "2020-04-30T01:06:44Z")

</div>

> [@Mason](#):
>
> The reason basically boils down to the fact that revamping an existing package to meet the needs we saw appeared to be a greater investment of effort than just writing this from scratch.

That’s why I am afraid that the efforts put in the previous packages are ignored.

I hope the interfaces of this new package make it the basis for the future packages, and somehow the features of the previous packages move to this new ecosystem.

If you could provide a table with the available and missing features, people can make extensions easier. For instance, the example in the interface section seems like a feature that I expect to see from SymbolicUtils by default. The interface example is better to be a real-world problem.

---

<div class="post-metadata">

### Author: ![shashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shashi/32/1824_2.png) [@shashi](https://discourse.julialang.org/u/shashi)
#### Post date: [April 30, 2020, 1:21am UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/8 "2020-04-30T01:21:47Z")

</div>

Thanks Yuri! I fixed this now. I’m still getting to know Franklin 🙂

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [April 30, 2020, 1:23am UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/9 "2020-04-30T01:23:23Z")

</div>

> [@Amin\_Yahyaabadi](#):
>
> That’s why I am afraid that the efforts put in the previous packages are ignored.

I assure you that we did not ignore them. Shashi and I both put serious effort towards trying to make [Rewrite.jl](https://github.com/HarrisonGrodin/Rewrite.jl) work for this before Shashi started SymbolicUtils.jl. [ModelingToolkit.jl](https://github.com/SciML/ModelingToolkit.jl/) was revamped to rely on SymbolicUtils and I also plan to revive my old package, [Symbolics.jl](https://github.com/MasonProtter/Symbolics.jl) but ripping out most of its guts and replacing them with the infrastructure provided by SymbolicUtils.jl.

I think it could be interesting to eventually try and take over the role SymPy takes in providing simplification for other packages, i.e. things like [Symata.jl](https://github.com/jlapeyre/Symata.jl) and if @jlapeyre wants to collaborate on such a project I’d certainly be interested.

The whole idea here is to bring all these packages closer together, not push them away!

---

<div class="post-metadata">

### Author: ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)
#### Post date: [April 30, 2020, 1:57am UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/10 "2020-04-30T01:57:42Z")

</div>

This is a off-topic question. Is there any Julia package that offers full functionality of Matlab Symbolic Math Toolbox?

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [April 30, 2020, 2:15am UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/11 "2020-04-30T02:15:38Z")

</div>

SymPy.jl is probably the closest

---

<div class="post-metadata">

### Author: ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)
#### Post date: [April 30, 2020, 3:51am UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/12 "2020-04-30T03:51:33Z")

</div>

I find it strange that the symbol is not a subtype of `Number`.  
Is the Idea for another package, using `SymbolicUtils`, to provide that interface?

```julia
julia> using SymbolicUtils

julia> @syms x::Real
(x,)

julia> f(x::Number) = 2*x
f (generic function with 1 method)

julia> f(x)
ERROR: MethodError: no method matching f(::SymbolicUtils.Sym{Real})
Closest candidates are:
  f(::Number) at REPL[7]:1
Stacktrace:
 [1] top-level scope at REPL[8]:1

```

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [April 30, 2020, 4:01am UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/13 "2020-04-30T04:01:54Z")

</div>

> [@favba](#):
>
> I find it strange that the symbol is not a subtype of `Number` .

This is something we agonized over quite a bit. I think `Sym <: Number` is just plain not good enough. We want to be able to support symbolic `Float64`s, `Matrix`s, `String`s or really _any_ datatype. One approach would be to just create a billion `SymbolicX` types which subtype `X`, but that has many downsides.

What we really want is `Sym{T} <: T`, but that’s not supported by Julia’s semantics, however it can actually be done inside an IRTools.jl pass like I show in [this proof of concept](https://github.com/JuliaSymbolics/SymbolicUtils.jl/issues/16#issuecomment-618577041). I plan to investigate this approach further.

One thing I feel somewhat strongly about is that this package can’t just be for math on numbers. It should have wider applicability than that. Mathematica has many flaws, but I think it demonstrates the value of symbolic programming in all sorts of domains outside simple math.

---

<div class="post-metadata">

### Author: ![shashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shashi/32/1824_2.png) [@shashi](https://discourse.julialang.org/u/shashi)
#### Post date: [April 30, 2020, 4:02am UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/14 "2020-04-30T04:02:03Z")

</div>

Yes it is! I think we should mention this in the docs, thank you!

We are looking into tracing tools like Cassette or IRTools to make this kinda thing work for all types.

The basic problem is we need something that behaves like `Sym{T} <: T` for Sym to take on the role of any type but be symbolic.

**Edit:** I just added it to the [docs](https://juliasymbolics.github.io/SymbolicUtils.jl/#creating_symbolic_expressions)!

---

<div class="post-metadata">

### Author: ![datnamer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datnamer/32/3471_2.png) [@datnamer](https://discourse.julialang.org/u/datnamer)
#### Post date: [April 30, 2020, 11:28am UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/15 "2020-04-30T11:28:50Z")

</div>

Is this similar to the wrapper array problem so it would be helpful if T were a trait ?

---

<div class="post-metadata">

### Author: ![shashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shashi/32/1824_2.png) [@shashi](https://discourse.julialang.org/u/shashi)
#### Post date: [April 30, 2020, 11:59am UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/16 "2020-04-30T11:59:36Z")

</div>

It’s similar, but it wouldn’t help much if `T` is a trait since existing code does not dispatch on the trait.

---

<div class="post-metadata">

### Author: ![Ratingulate](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ratingulate/32/9242_2.png) [@Ratingulate](https://discourse.julialang.org/u/Ratingulate)
#### Post date: [April 30, 2020, 12:56pm UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/17 "2020-04-30T12:56:15Z")

</div>

> [@shashi](#):
>
> trait sin

Right. Hopefully at some point traits will get first class language support and become more pervasive 🙂

I think this is a type system limitation and it would be great if we didn’t have to rely on compiler transforms to work around it.

However, there are some packages that use traits for dispatch/abstract type. Can this interop with those as well?

---

<div class="post-metadata">

### Author: ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)
#### Post date: [May 1, 2020, 1:32pm UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/18 "2020-05-01T13:32:06Z")

</div>

This looks great!

One glaring omission from most symbolic systems is a nice way to reason about array manipulations. Any chance there’s anything in the works specific to this? An interface to one of the tensor manipulation libraries would be especially exciting.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [May 1, 2020, 3:34pm UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/19 "2020-05-01T15:34:57Z")

</div>

Yes, this is indeed one major reason I got involved with this whole project, and why we are pushing so hard for parameterized types, despite the headaches they induce.

Symbolically manipulating tensor expressions instead of arrays of symbolic numbers is an important distinction and something I’d really like to explore.

I think there’s still a bit more ‘plumbing’ that I’d like to work on before getting into a high level application like this, but it’s certainly on my radar and if anyone would be interested in collaborating on it, I’m all ears!

---

<div class="post-metadata">

### Author: ![fkguo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fkguo/32/16866_2.png) [@fkguo](https://discourse.julialang.org/u/fkguo)
#### Post date: [August 13, 2020, 8:49am UTC](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455/20 "2020-08-13T08:49:12Z")

</div>

I just started using your package, pretty nice. Thanks!

In version `v0.4.3`, `@syms a; 0* a` gives zero, while in the new version `v0.5.0`, it gives `0 * a`. How can I force it to be zero?

Also I observed the following behavior:

```julia
@syms a
@syms b::Real

```

then `0a` gives `0 * a`, but `0b` gives the following error:

```julia
ERROR: syntax: invalid numeric constant "0b"
Stacktrace:
 [1] top-level scope at none:1

```

[Next page](https://discourse.julialang.org/t/ann-symbolicutils-jl-groundwork-for-a-symbolic-ecosystem-in-julia/38455.md?page=2)
