Skip to content

Symbolics.jl | Interfacing - building an expression tree to use custom rules #687

@Audrius-St

Description

@Audrius-St

Hello,

I would like to use the SymbolicUtils composing rewriter Fixpoint(rw) which

returns a rewriter which applies rw repeatedly until there are no changes to be made

to apply a custom rule to a Symbolics.jl expression until nothing is returned.

However, rereading the SymbolicUtils.jl and TermInterface.jl documentation it is not clear to me how to do this - specifically how to set up the interface between Symbolics.jl and SymbolicUtils.jl.

The MNWE is

# test_symbolics_main.jl

using Symbolics

function main()

    @variables x, t
    @variables Φ(x, t)

    Dx = Differential(x)
    D2x = Dx * Dx

    Dt = Differential(t)

    # The rule that I would like to apply
    r = @rule Dt(Dx(Dx(~Φ))) => Dx(Dx(Dt(~Φ)))

    # The test expression to which I would like to apply the rule
    ∂2Φ∂t2 = Dt(Dx(Dx(Φ))) - 2*Dt(Φ)*Φ

    # The rewrite I would like to perform
    γ2 = Fixpoint(r)(∂2Φ∂t2)

    # The problem
    @show istree(∂2Φ∂t2) # returns "false" . Not an expression tree.

end

begin
    main()

end

The SymbolicUtils.jl documentation states

In particular, you should define methods from TermInterface.jl for an expression tree type T with symbol types S to work with SymbolicUtils.jl

and the TermInterface.jl documentation states

You should define the following methods for an expression tree type T with symbol types S to work with TermInterface.jl . . .

istree(x::T) or istree(x::Type{T})

Without an example, it is not clear to me, a Symbolics neophyte, what is meant and how to proceed.

In the above MNWE,
should @variables be replaced by @syms?
should ∂2Φ∂t2 be defined as ∂2Φ∂t2::Symbolics.Term?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions