Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into ale/terminterface1
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Cheli committed Jun 26, 2024
2 parents db0ae46 + d009ca8 commit bfb672d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/SymbolicUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ using TermInterface
import TermInterface: iscall, isexpr, head, children,
operation, arguments, metadata, maketerm, sorted_arguments

export operation, arguments, sorted_arguments, iscall
const istree = iscalls
Base.@deprecate_binding istree iscall
export istree, operation, arguments, sorted_arguments, similarterm, iscall
# Sym, Term,
# Add, Mul and Pow
include("types.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/code.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export toexpr, Assignment, (←), Let, Func, DestructuredArgs, LiteralExpr,
import ..SymbolicUtils
import ..SymbolicUtils.Rewriters
import SymbolicUtils: @matchable, BasicSymbolic, Sym, Term, iscall, operation, arguments, issym,
symtype, sorted_arguments, metadata, isterm, term
symtype, sorted_arguments, metadata, isterm, term, maketerm

##== state management ==##

Expand Down
10 changes: 9 additions & 1 deletion src/inspect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ function AbstractTrees.nodevalue(x::BasicSymbolic)
Text(str)
end

"""
$(TYPEDSIGNATURES)
Return the children of the symbolic expression `x`, sorted by their order in
the expression.
This function is used internally for printing via AbstractTrees.
"""
function AbstractTrees.children(x::Symbolic)
iscall(x) ? sorted_arguments(x) : isexpr(x) ? children(x) : ()
iscall(x) ? sorted_arguments(x) : isexpr(x) ? sorted_children(x) : ()
end

"""
Expand Down
17 changes: 10 additions & 7 deletions src/ordering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,31 @@
<(a::T, b::S) where{T,S} = T<S
<(a::T, b::T) where{T} = a < b

"""
$(SIGNATURES)
###### A variation on degree lexicographic order ########
# find symbols and their corresponding degrees
Internal function used for printing symbolic expressions. This function determines
the degrees of symbols within a given expression, implementing a variation on
degree lexicographic order.
"""
function get_degrees(expr)
if issym(expr)
((Symbol(expr),) => 1,)
elseif iscall(expr)
op = operation(expr)
args = sorted_arguments(expr)
if operation(expr) == (^) && args[2] isa Number
if op == (^) && args[2] isa Number
return map(get_degrees(args[1])) do (base, pow)
(base => pow * args[2])
end
elseif operation(expr) == (*)
elseif op == (*)
return mapreduce(get_degrees,
(x,y)->(x...,y...,), args)
elseif operation(expr) == (+)
elseif op == (+)
ds = map(get_degrees, args)
_, idx = findmax(x->sum(last.(x), init=0), ds)
return ds[idx]
elseif operation(expr) == (getindex)
args = sorted_arguments(expr)
elseif op == (getindex)
return ((Symbol.(args)...,) => 1,)
else
return ((Symbol("zzzzzzz", hash(expr)),) => 1,)
Expand Down
3 changes: 3 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ function TermInterface.sorted_arguments(x::BasicSymbolic)
return args
end

@deprecate unsorted_arguments(x) arguments(x)

TermInterface.children(x::BasicSymbolic) = arguments(x)
TermInterface.sorted_children(x::BasicSymbolic) = sorted_arguments(x)
function TermInterface.arguments(x::BasicSymbolic)
@compactified x::BasicSymbolic begin
Term => return x.arguments
Expand Down
2 changes: 1 addition & 1 deletion test/polyform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include("utils.jl")

@testset "div and polyform" begin
@syms x y z
@test repr(PolyForm(x-y)) == "-y + x"
@test_skip repr(PolyForm(x-y)) == "-y + x"
@test repr(x/y*x/z) == "(x^2) / (y*z)"
@test repr(simplify_fractions(((x-y+z)*(x+4z+1)) /
(y*(2x - 3y + 3z) +
Expand Down

0 comments on commit bfb672d

Please sign in to comment.