Expression manipulation in Symbolics

Playing with Symbolics like so:

julia> @variables a b c
(a, b, c)

julia> par(x,y) = 1/(1/x+1/y)
par (generic function with 1 method)

julia> ex1 = par(a, par(b,c))
(a^-1 + ((b^-1 + c^-1)^-1)^-1)^-1

julia> simplify(ex1)
(a^-1 + b^-1 + c^-1)^-1

julia> par2(x,y) = x*y/(x+y)
par2 (generic function with 1 method)

julia> ex2 = par2(a, par2(b, c))
a*b*c*((a + b*c*((b + c)^-1))^-1)*((b + c)^-1)

julia> simplify(ex2)
a*b*c*((a + b*c*((b + c)^-1))^-1)*((b + c)^-1)

julia>

ex1 and ex2 are equivalent expressions but don’t simplify to the same expression. Are there tools coming to let one guide what simplify is doing?

P.S. I did build_functions from both of these and the function derived from ex2 computes slightly faster, although not as fast as if I make one from a manually created ex3 as follows:

julia> ex3 = a*b*c/(a*b+a*c+b*c)
a*b*c*((a*b + a*c + b*c)^-1)

Open an issue. This is more of an issue than a Discourse post.

1 Like

Will do.

https://github.com/JuliaSymbolics/Symbolics.jl/issues/132

2 Likes

in matlab (2020b), we get similar differences. maybe they are less “differing”…
executing the script simp.m:

clear ;
syms a b c x y ;

fun1(x,y) = 1/(1/x+1/y) ;
ex1 = fun1(a,fun1(b,c)) ;
ex1=simplify(ex1)
 
fun2(x,y)=x*y/(x+y) ;
ex2 = fun2(a,fun2(b,c)) ;
ex2=simplify(ex2)

gives:

>> simp
ex1 =
1/(1/a + 1/b + 1/c)

ex2 =
(a*b*c)/(a*b + a*c + b*c)

Thanks. Matlab seems to do a better job with the second one. ex1 is most pleasing to my eye, but ex2 seems to be faster on Float64s and Complexes. I assume that’s because division is much slower than multiplication or addition.