# \[ANN\] Symbolics.jl: A Modern Computer Algebra System for a Modern Language

**URL:** <https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251>\
**Category:** Package Announcements\
**Created:** [March 1, 2021, 2:59pm UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251 "2021-03-01T14:59:40Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 2, 2021, 1:10pm UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/21 "2021-03-02T13:10:24Z")

</div>

Thanks for your contributions! I think it was a good choice to strip out `Symbolics` as a separate package. I can’t wait to start playing with it.

There are a few things that is not clear for me in the documentation; perhaps I am looking in the wrong place? The concept of `variables` is straightforward, but the concept of `parameters` is not really described?? The concept _parameter_ is used differently in different fields, e.g., in Modelica it means a _constant_. I seem to recall to have seen a suggestion that for `ModelingToolkit` (and probably `Symbolics`), “parameter” includes constants, independent variables, and known functions of independent variables (??).

Let me highlight the problem by including some code from the `Symbolics` documentation – there is some code that is presented both via macros and via a non-macro description… First the _macro_ description:

```julia
@parameters t α σ(..) β[1:2]
@variables w(..) x(t) y z(t, α, x)

expr = β₁* x + y^α + σ(3) * (z - t) - β₂ * w(t - 1)

```

[It is not clear to me what an _uncalled function_ implies. Here, I am _guessing_ that `α` and `β` are constants, that `t` is an independent variable (time), and that `σ` is defined to be some function; it is not clear what the argument should be.  
Furthermore, I am _guessing_ that `x(t)` and `z(t,α,x)` are \*dependent variables, it is not clear to me what type of variable `y` is – an algebraic variable for which you do not take derivatives??, while `w` again is some sort of uncalled variable – from `expr`, we can clearly make `w` a function of time.]

It is interesting to contrast this with the non-macro version…

```julia
σ = Num(Variable{Symbolics.FnType{Tuple{Any},Real}}(:σ)) # left uncalled, since it is used as a function
w = Num(Variable{Symbolics.FnType{Tuple{Any},Real}}(:w)) # unknown, left uncalled
x = Num(Variable{Symbolics.FnType{Tuple{Any},Real}}(:x))(t) # unknown, depends on `t`
y = Num(Variable(:y)) # unknown, no dependents
z = Num(Variable{Symbolics.FnType{NTuple{3,Any},Real}}(:z))(t, α, x) # unknown, multiple arguments
β₁ = Num(Variable(:β, 1)) # with index 1
β₂ = Num(Variable(:β, 2)) # with index 2

expr = β₁ * x + y^α + σ(3) * (z - t) - β₂ * w(t - 1)

```

Here, I observe the following:

1. `t` and `α` are used in `expr`, but have not been defined individually in the non-macro version. Perhaps not needed, as these are given as arguments in the definitions of `x` and `z`??
2. Quantities that are not constants or independent variables ( `y`, `β`) are defined simply using the `Num(Variable(:y))` syntax, i.e., there is no mentioning of `Symbolics` and `FnType`.
3. All functions/dependent variables are constructed with the inclusion of the `Symbolics.FnType` type.
4. _Uncalled_ functions (`σ`, `w`) are given without the specification of an argument list, while dependent variables/“called” functions are given with a list of arguments.

By contrasting the macro version and the non-macro version, it seems like there is no real difference between `parameters` and `variables`: there is no obvious difference between `σ` (“parameter”) and `w` (variable) in the no-macro description.

Also, in some of the code for symbolically defining differential equations, `t` seems sometimes to be given as a parameter, while other times it is given as a variable.

→ Anyway, some clarification of this would be helpful 🙂

---

<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:** [March 2, 2021, 1:14pm UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/22 "2021-03-02T13:14:39Z")

</div>

> [@BLI](#):
>
> The concept of `variables` is straightforward, but the concept of `parameters` is not really described?? The concept _parameter_ is used differently in different fields, e.g., in Modelica it means a _constant_ . I seem to recall to have seen a suggestion that for `ModelingToolkit` (and probably `Symbolics` ), “parameter” includes constants, independent variables, and known functions of independent variables (??).

Parameter is just a ModelingToolkit.jl thing. It shouldn’t be in the Symbolics.jl documentation at all. `Variables` can have arbitrary metadata (where there’s a neat trick for doing without allocating on operations). So a ModelingToolkit `@parameter` is just a variable with metadata attributes saying it’s a parameter for automated detection.

> [@BLI](#):
>
> By contrasting the macro version and the non-macro version, it seems like there is no real difference between `parameters` and `variables` : there is no obvious difference between `σ` (“parameter”) and `w` (variable) in the no-macro description.

Because that examples needs to update the top portion. They should all just be `@variable` there. Can you open a PR?

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 2, 2021, 1:25pm UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/23 "2021-03-02T13:25:50Z")

</div>

Will do that.

---

<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:** [March 2, 2021, 2:27pm UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/24 "2021-03-02T14:27:04Z")

</div>

Hi, thanks for this great comment!

Your first example is possible with the kind of symbolic arrays we are working on: [https://github.com/JuliaSymbolics/SymbolicUtils.jl/pull/123](https://github.com/JuliaSymbolics/SymbolicUtils.jl/pull/123)

It does a best-effort shape propagation, but you can leave out the shape if you don’t have it.

So your example could be simply encoded by something like

```julia
@variables x::Vector

maximize(sum(1//2 .* log.(x)), st=sum(x) == I)

```

Where `log.(x)` just returns a symbolic `broadcasted` term.

The second one:

```julia
differentiate(1//2 .* log.(x), x)

```

This would require adding rules for differentiating broadcast. This is tricky but possible!

Also we could easily make

```julia
@variables n::Int x[1:n]

```

Work so that you can have arrays that share dimension and match them when possible…

---

<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:** [March 2, 2021, 2:31pm UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/25 "2021-03-02T14:31:00Z")

</div>

> **How will “assumptions” be dealt with in Symbolics.jl?**

I have some priliminary work on this [GitHub - JuliaSymbolics/SymbolicSAT.jl: for you with the good questions](https://github.com/JuliaSymbolics/SymbolicSAT.jl)

It basically wraps the z3 theorem prover, calling out to it to prove expressions. 🙂 It’s a simple wrapper but the outcome is potentially really powerful!

I would appreciate any issues fleshing out people’s requirements on that package!

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 2, 2021, 3:23pm UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/26 "2021-03-02T15:23:12Z")

</div>

A couple of bugs/strange behavior:

```julia
julia> @variables x
julia> sinc(x)
TypeError: non-boolean (Num) used in boolean context

Stacktrace:
 [1] sinc(::Num) at .\special\trig.jl:944
 [2] top-level scope at In[74]:1
 [3] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

```

This problem was also found in `ModelingToolkit`.

In `ModelingToolkit`, I got around this by defining `mysinc(x) = sin(pi*x)/(pi*x)`, which worked.

However, in `Symbolics`, this triggers another error:

```julia
julia> @variables x
julia> mysinc(x) = sin(pi*x)/(pi*x)
julia> mysinc(x)
DomainError with -1:
Cannot raise an integer x to a negative power -1.
Convert input to float.

Stacktrace:
 [1] throw_domerr_powbysq(::Float64, ::Int64) at .\intfuncs.jl:225
 [2] power_by_squaring(::Irrational{:π}, ::Int64) at .\intfuncs.jl:249
....

```

Any idea what causes this problem?

---

<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:** [March 2, 2021, 3:30pm UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/27 "2021-03-02T15:30:10Z")

</div>

Missing a registered function. We should add it. Open an issue.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 2, 2021, 3:42pm UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/28 "2021-03-02T15:42:18Z")

</div>

Done.

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [March 2, 2021, 5:45pm UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/29 "2021-03-02T17:45:14Z")

</div>

This is great! What about the opposite direction (the examples here are with integrals which are not yet implemented in Symbolic.jl, but they are most ubiquitous place where an assumption has to be **generated as part of the answer** )  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/5/05d0fe0a7087fd7c8bfd1b5edf6df498224be0a6.png)

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/5/15764219e908f02c181b761c8f89203cf5154755.png)

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 2, 2021, 6:29pm UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/30 "2021-03-02T18:29:58Z")

</div>

I wish I could go back and retake my undergrad calculus sequence in light of these new tools.

---

<div class="post-metadata">

**Author:** ![pyrex41](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@pyrex41](https://discourse.julialang.org/u/pyrex41)\
**Post date:** [March 2, 2021, 6:37pm UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/31 "2021-03-02T18:37:16Z")

</div>

Is it possible yet to symbolically do integration or find expectations given variables on a known distribution? I can use Expectations.jl to numerically estimate the value, but I would love to be able to simplify these expressions analytically:

 ![Screen Shot 2021-03-02 at 1.34.44 PM](https://global.discourse-cdn.com/julialang/original/3X/c/1/c12cca58adbf36cc6795d1216ddbef18c80d7a82.png)

---

<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:** [March 2, 2021, 6:42pm UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/32 "2021-03-02T18:42:55Z")

</div>

> [@pyrex41](#):
>
> Is it possible yet to symbolically do integration or find expectations given variables on a known distribution? I can use Expectations.jl to numerically estimate the value, but I would love to be able to simplify these expressions analytically:

Not yet. That falls under integrals.

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [March 2, 2021, 8:10pm UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/33 "2021-03-02T20:10:39Z")

</div>

Thanks!!!

1. is there a place we can post some common CAS “test” problems that we would like to solve w/ Symbolics.jl from our respective fields?
2. Can we be more specific/general than `@variables x::Vector`?  
A vector can be an element of \frak{R}^{N}, \frak{C}^{N}, C\left(\frak{R}^{N}, \frak{R}\right) \dots   
Can we specify: x is a vector in \frak{R}^{N} where N is a symbolic parameter N \in Z\_{+}?
3. Btw, the slightly more general version of the above problem  
\max\_{x\_{i}} \text{ } \sum\_{i=1}^{N} \alpha\_{i} \log(x\_{i}) \text{ s.t. } \sum\_{i=1}^{N} p\_{i}x\_{i} = I  
Here: x, \alpha, p \in \frak{R}^{N}  
Assumptions (variables): N \in Z\_{+}, x\_{i}\>0 for i \in \{1,\dots, N\}  
Assumptions (parameters): \alpha\_{i} \> 0, p\_{i} \>0, I\>0 for i \in \{1,\dots, N\}  
Solution: x\_{j}^{\*}(\alpha, p, I)= \frac{\alpha\_{j}}{ \sum\_{i=1}^{N} \alpha\_{i}} \frac{I}{ p\_{j} }  
Mathematica struggles @ solving this for general parameters even if we set N=2.  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/d/cdaf551378822b5001e34fc61c0000d2c3e59ce1.png)
4. Adding one parameter \rho \in (-\infty, 1] to the utility function makes Mathematica struggle even more  
\max\_{x\_{i}} \text{ } \left( \sum\_{i=1}^{N} \alpha\_{i} x\_{i}^{\rho} \right)^{\frac{1}{\rho}} \text{ s.t. } \sum\_{i=1}^{N} p\_{i}x\_{i} = I  
Very routine solution by hand: x\_i = ![image](https://global.discourse-cdn.com/julialang/original/3X/2/1/21b983f32883ce1dd71902e8a2b106c178959c07.png)

---

<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:** [March 2, 2021, 10:04pm UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/34 "2021-03-02T22:04:58Z")

</div>

> [@Albert\_Zevelev](#):
>
> - is there a place we can post some common CAS “test” problems that we would like to solve w/ Symbolics.jl from our respective fields?

Write tests for the library and PR them. Tests don’t need to b unit tests: CI is cheap. Having a big robust test suite is always good.

> [@Albert\_Zevelev](#):
>
> Can we be more specific/general than `@variables x::Vector` ?  
> A vector can be an element of RN,CN,C(RN,R)…\frak{R}^{N}, \frak{C}^{N}, C\left(\frak{R}^{N}, \frak{R}\right) \dots  
> Can we specify: xx is a vector in RN\frak{R}^{N} where N is a symbolic parameter N∈Z+N \in Z\_{+} ?

You will be able to. Didn’t Shashi just demo that? I don’t know if he did it here though. But there’s a metadata system for arbitrary metadata. From there, the whole game is just coming up with all of the rules needed to handle all of the combinations of the metadata.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [March 3, 2021, 1:03am UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/35 "2021-03-03T01:03:34Z")

</div>

In Xmaxima, it can do basic expansion, for example  
 ![Screen Shot 2021-03-03 at 12.01.16 PM](https://global.discourse-cdn.com/julialang/original/3X/a/6/a669c775de9a8b1b90cf535be122becdf6ea6f66.png)

How do we do expansion in Symbolics.jl? The tutorials do not give any examples.

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [March 3, 2021, 5:07am UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/36 "2021-03-03T05:07:29Z")

</div>

inverse laplace transforms ?  
edit: also, this is awesome !

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [March 3, 2021, 9:29am UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/37 "2021-03-03T09:29:12Z")

</div>

Is there a reason why there is no `Sym{Complex}` in Symbolics.jl? Will it be implemented in the future?

---

<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:** [March 3, 2021, 11:03am UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/38 "2021-03-03T11:03:13Z")

</div>

One of the people working with us is handling complex numbers:

[https://github.com/JuliaSymbolics/Symbolics.jl/pull/70](https://github.com/JuliaSymbolics/Symbolics.jl/pull/70)

---

<div class="post-metadata">

**Author:** ![rfateman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfateman/32/22507_2.png) [@rfateman](https://discourse.julialang.org/u/rfateman)\
**Post date:** [March 3, 2021, 10:48pm UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/39 "2021-03-03T22:48:31Z")

</div>

This is the first note I see where someone is comparing some other computer algebra system (CAS) with the proposed Julia program.  
As an old hand in CAS programs, let me suggest that studying the previous systems, and especially what difficulties were encountered, would be time well spent.  
There are (at least) the following: Sagemath, which would seem to be close in spirit to Symbolics.jl, except using python, and borrowing other implemented stuff. There’s Maxima (continuation of Macsyma) which would seem to be close also, but in Common Lisp. There’s also Axiom, Fricas, Reduce, and commercial systems Maple and Mathematica.

The item above about assumptions might be more relevant if it addressed the real problem that is the multiplicity of roots. sqrt(9) is the set {-3,+3}.  
Even if you declare the obvious that -3\<0,  
sqrt( (-3)^2) is still not -3. It is {-3,3}. or in many of the systems mentioned above, it would be 3.  
To insist that sqrt( (-x)^2) is -x, … well, you figure out the implications of that.  
Let me voice my concern up front: Please don’t start by thinking that the various troubles in previous CAS implementations resulted from the wrong choice of an implementation language. There have been very sophisticated language designs explicitly to support the building of a CAS (see the history of Axiom). There have been strenuous efforts to elaborate on constructive versions of various algebras. There have been numerous breakthroughs in algebraic algorithms. There have been variations on memory management, big-number arithmetic, multi-programming, data-structure ideas including trees, hash-tables, cache management, etc.  
I have only a slight passing familiarity with Julia, and no familiarity with the cited precursor efforts mentioned in the notes here and there, so perhaps some of these efforts have incorporated all the major technology developed in the CAS world to date. That would be great, and I wish you much luck.  
Richard Fateman  
fateman@cs.berkeley.edu

---

<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:** [March 4, 2021, 12:03am UTC](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251/40 "2021-03-04T00:03:29Z")

</div>

The Matrix Calculus paper is pretty nice:

> **[matrixcalculus.pdf](https://www.matrixcalculus.org/matrixcalculus.pdf)**
>
> 493.54 KB

[Previous page](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251.md?page=1)

[Next page](https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251.md?page=3)
