Working with External Symbolics Packages: SymPy, Mathematica, Oscar, and Beyond
Symbolics.jl takes an inclusive development philosophy: yes there are other computer algebra systems such as SymPy and Mathematica, yes they can do some things that Symbolics.jl cannot natively do, and no that is not a bad thing. Instead, we maintain bridges to these other languages in order to encourage users to take full advantage of other tools as part of their workflow!
The following are the wrappers and interactions users should be aware of.
Using Symbolics.jl with SymPy
SymPy is a Python-based symbolic library. While it is generally not computationally performant, it is good in its completeness of features and as such it can be helpful to call its functions from time to time. Because of this, Symbolics.jl maintains extensions for SymPy.jl which facilitates translations. The extension is triggered by adding the SymPy.jl packages doing using SymPy
. When this is done, Symbolics.jl gives functions which allow for translating to and from SymPy expressions:
Symbolics.symbolics_to_sympy
— Functionsymbolics_to_sympy(expr)
Converts a Symbolics.jl expression to a SymPy expression. It is represented in SymPy.jl format and thus can use all of the wrapped functionality from SymPy.jl.
For conversion back to Symbolics, see sympy_to_symbolics
.
Arguments
expr
: A Symbolics.jl expression
Example
using Symbolics
@variables x y
expr = x^2 + y
sympy_expr = symbolics_to_sympy(expr)
Symbolics.sympy_to_symbolics
— Functionsympy_to_symbolics(sympy_expr, vars)
Converts a SymPy expression to Symbolics.jl.
Arguments
sympy_expr
: SymPy expression.vars
: List or dictionary of Symbolics variables.
Example
@variables x y
sympy_expr = SymPy.Sym("x")^2 + SymPy.Sym("y")
symbolics_expr = sympy_to_symbolics(sympy_expr, [x, y])
In addition, many of the features pages include docstrings for functionality given by this bridge. These functions all have sympy
in the name, and includes:
sympy_simplify
sympy_linear_solve
sympy_algebraic_solve
sympy_integrate
sympy_limit
sympy_simplify
sympy_ode_solve
Using Symbolics.jl with Mathematica / Wolfram's MathLink
The library SymbolicsMathLink.jl is capable of converting Symbolics.jl expressions for use with Mathematica calls via Wolfram's MathLink. The package requires an installation of either Mathematica or the free Wolfram Engine. It will attempt to find the installation at build time; if this fails, please see the installation troubleshoot on the MathLink.jl README.
Round-trip conversion is given by the functions:
expr_to_mathematica(juliaSymbolicExpression)
mathematica_to_expr(W`Some Mathematica expression`)
Some examples in action include using Mathematica for equation solving:
julia> using SymbolicsMathLink
julia> @variables x;
julia> expr = x^2 + x - 1;
julia> result = wcall("Solve", expr~0)
2-element Array{Num,1}:
-1 + x
1 + x
and solving differential equations:
julia> @variables vars(x)[1:2];
julia> expr = Differential(x)(vars[1]) + 2
2 + Differential(x)((vars(x))[1])
julia> result = wcall("DSolveValue", expr~0, vars[1], x)
C_1 - 2x
Groebner Basis via Groebner.jl
Groebner.jl is a very efficient system for computing Groebner basis written in Julia. Its publication details how it is much more efficient than Maple, msolve, Mathematica, and Singular for these computing a Groebner basis. As such, it is highly recommended that one use this system for the computation. Symbolics.jl provides an extension which automatically converts Symbolics.jl polynomials into its specialized form for the computation and converts the result back, and thus no work is required on the users end for integrating.
Symbolics.groebner_basis
— Functiongroebner_basis(polynomials; kwargs...)
Computes a Groebner basis of the ideal generated by the given polynomials
using Groebner.jl as the backend.
The basis is guaranteed to be unique. The algorithm is randomized, and the output is correct with high probability.
If a coefficient in the resulting basis becomes too large to be represented exactly, DomainError
is thrown.
Optional Arguments
The Groebner.jl backend provides a number of useful keyword arguments, which are also available for this function. See ?Groebner.groebner
.
Example
julia> using Symbolics, Groebner
julia> @variables x y;
julia> groebner_basis([x*y^2 + x, x^2*y + y])
Nemo.jl
Nemo.jl's abstract algebra functionality (and by extension FLINT) is used as part of many of the symbolic solver routines. If Nemo is required an error message specifying the requirement to load Nemo.jl will be given. For more information, see the symbolic solver page