Symbolics.jl
Symbolics.jl copied to clipboard
Assumptions
julia> using Symbolics;
julia> @variables x1 x2 ρ;
julia> z = (x1^(ρ) + x2^(ρ) )^(1/ρ);
julia> Dx1 = Differential(x1);
julia> Dx2 = Differential(x2);
julia> expand_derivatives(Dx1(z))
(x1^(ρ - 1))*(((x1^ρ) + (x2^ρ))^((ρ^-1) - 1))
julia> expand_derivatives(Dx2(z))
(x2^(ρ - 1))*(((x1^ρ) + (x2^ρ))^((ρ^-1) - 1))
Q1: how do I make assumptions about the variables?
x1 ∈ Real ∩ Positive
ρ ∈ Real ∩ (-Inf, 1]
Q2: should the following be equal?
julia> [expand_derivatives(Dx1(z)) expand_derivatives(Dx2(z))]
1×2 Array{Num,2}:
(x1^(ρ - 1))*(((x1^ρ) + (x2^ρ))^((ρ^-1) - 1)) (x2^(ρ - 1))*(((x1^ρ) + (x2^ρ))^((ρ^-1) - 1))
julia> Symbolics.jacobian([z],[x1,x2])
1×2 Array{Num,2}:
(x1^(ρ - 1))*(((x1^ρ) + (x2^ρ))^((ρ^-1) - 1)) (x2^(ρ - 1))*(((x1^ρ) + (x2^ρ))^((ρ^-1) - 1))
julia> Symbolics.jacobian([z],[x1,x2]) == [expand_derivatives(Dx1(z)) expand_derivatives(Dx2(z))]
ERROR: MethodError: no method matching !(::Num)
Closest candidates are:
!(::Missing) at missing.jl:100
!(::Bool) at bool.jl:33
!(::Function) at operators.jl:896
...
Stacktrace:
[1] ==(::Array{Num,2}, ::Array{Num,2}) at .\abstractarray.jl:1868
[2] top-level scope at REPL[352]:1
┆Issue is synchronized with this Trello card by Unito
Try isequal
instead of ==
. That's usually my problem when I run into that error.
This isn't all implemented yet, so it might be good to use this issue to start talking design. For the PDESystem work https://github.com/SciML/ModelingToolkit.jl/issues/822 we've been discussing using DomainSets.jl for representing domains: we might want to make sure the representation is the same for assumptions.
https://github.com/JuliaSymbolics/Symbolics.jl/issues/97 showcased an interesting example of branch elimination via assumptions in SymPy.
@shashi could you do a bit of a lit review on how other CAS's handle assumptions? I think it would make sense to do it through the metadata system, but comments from https://discourse.julialang.org/t/ann-symbolics-jl-a-modern-computer-algebra-system-for-a-modern-language/56251 say we should look at the details here.
Q1: how do I make assumptions about the variables? x1 ∈ Real ∩ Positive ρ ∈ Real ∩ (-Inf, 1]
Is there any update on satisfying assumptions? I have been using SymPy
, where a simple positive=true
flag gives positive numbers, but I cannot seem to find similar functionality for Symbolics
. For my purposes I would like to check positive elements of a matrix (or array), which currently throws an error:
julia> using Symbolics
julia> @variables x[1:3]::Real
julia> x = substitute(Symbolics.scalarize(x), Dict([x[1] => 0]))
julia> indices = findall(x .> 0)
ERROR: TypeError: non-boolean (Num) used in boolean context