Why won't Symbolics.jl simplify `sqrt(1)` to `1`?

@variables x
sqrt(cos(x)^2 + sin(x)^2) |> simplify

gives me sqrt(1). Why isn’t this reduced to simply 1?

OTOH

sqrt(0*x+1) |> simplify

reduces to 1, no problem.

This is clearly a bug / missing feature so would be better to open as an Issue on GitHub

Ok, created Simplify `sqrt(1)` to `1` · Issue #337 · JuliaSymbolics/Symbolics.jl · GitHub.

Note that simplify does nothing here:

julia> sqrt(0*x+1)
1.0

julia> sqrt(0*x+2) 
1.4142135623730951

However

julia> Symbolics.Term(sqrt,1) |> simplify
sqrt(1)

julia> using SymbolicUtils

julia> r = @rule sqrt(1) => 1
sqrt(1) => 1

julia> simplify(Symbolics.Term(sqrt, 1), RuleSet([r]))
1

julia> r2 = @rule cos(~x)^2+sin(~x)^2 => 1
cos(~x) ^ 2 + sin(~x) ^ 2 => 1

julia> simplify(sqrt(cos(x)^2 + sin(x)^2) , RuleSet([r,r2]))
1


1 Like

I always thought the square root of 1 was -1 :thinking:

8 Likes

This isn’t quite true:

julia> @variables x
julia> sqrt(0*x+1) |> typeof
Num

It just gets reprd as 1.0. OTOH

julia> sqrt(1) |> simplify |> typeof
Float64

Thanks for pointing out simplify with rule sets! Is there any way to simplify with all of the standard rules and then in addition some custom rules? AFAICT simplify(..., RuleSet([r])) only applies r and no other simplifications.