|
| 1 | +module SymEngineSymbolicUtilsExt |
| 2 | + |
| 3 | +using SymEngine |
| 4 | +using SymbolicUtils |
| 5 | +import SymEngine: SymbolicType |
| 6 | + |
| 7 | +# |
| 8 | +function is_number(a::SymEngine.Basic) |
| 9 | + cls = SymEngine.get_symengine_class(a) |
| 10 | + any(==(cls), SymEngine.number_types) && return true |
| 11 | + false |
| 12 | +end |
| 13 | + |
| 14 | + |
| 15 | +λ(x::SymEngine.SymbolicType) = λ(Val(SymEngine.get_symengine_class(x))) |
| 16 | +λ(::Val{T}) where {T} = getfield(Main, Symbol(lowercase(string(T)))) |
| 17 | + |
| 18 | +λ(::Val{:Add}) = +; λ(::Val{:Sub}) = - |
| 19 | +λ(::Val{:Mul}) = *; λ(::Val{:Div}) = / |
| 20 | +λ(::Val{:Pow}) = ^ |
| 21 | +λ(::Val{:re}) = real; λ(::Val{:im}) = imag |
| 22 | +λ(::Val{:Abs}) = abs |
| 23 | +λ(::Val{:Log}) = log |
| 24 | +λ(::Val{:Sin}) = sin; λ(::Val{:Cos}) = cos; λ(::Val{:Tan}) = tan |
| 25 | +λ(::Val{:Csc}) = csc; λ(::Val{:Sec}) = sec; λ(::Val{:Cot}) = cot |
| 26 | +λ(::Val{:Asin}) = asin; λ(::Val{:Acos}) = acos; λ(::Val{:Atan}) = atan |
| 27 | +λ(::Val{:Acsc}) = acsc; λ(::Val{:Asec}) = asec; λ(::Val{:Acot}) = acot |
| 28 | +λ(::Val{:Sinh}) = sinh; λ(::Val{:Cosh}) = cosh; λ(::Val{:Tanh}) = tanh |
| 29 | +λ(::Val{:Csch}) = csch; λ(::Val{:Sech}) = sech; λ(::Val{:Coth}) = coth |
| 30 | +λ(::Val{:Asinh}) = asinh; λ(::Val{:Acosh}) = acosh; λ(::Val{:Atanh}) = atanh |
| 31 | +λ(::Val{:Acsch}) = acsch; λ(::Val{:Asech}) = asech; λ(::Val{:Acoth}) = acoth |
| 32 | +λ(::Val{:Gamma}) = gamma; λ(::Val{:Zeta}) = zeta; λ(::Val{:LambertW}) = lambertw |
| 33 | + |
| 34 | +#== |
| 35 | +Check if x represents an expression tree. If returns true, it will be assumed that operation(::T) and arguments(::T) methods are defined. Definining these three should allow use of SymbolicUtils.simplify on custom types. Optionally symtype(x) can be defined to return the expected type of the symbolic expression. |
| 36 | +==# |
| 37 | +function SymbolicUtils.istree(x::SymEngine.SymbolicType) |
| 38 | + cls = SymEngine.get_symengine_class(x) |
| 39 | + cls == :Symbol && return false |
| 40 | + any(==(cls), SymEngine.number_types) && return false |
| 41 | + return true |
| 42 | +end |
| 43 | + |
| 44 | +SymbolicUtils.issym(x::SymEngine.SymbolicType) = SymEngine.get_symengine_class(x) == :Symbol |
| 45 | +Base.nameof(x::SymEngine.SymbolicType) = Symbol(x) |
| 46 | + |
| 47 | +# no metadata(x), metadata(x, data) |
| 48 | + |
| 49 | +#== |
| 50 | +Returns the head (a function object) performed by an expression tree. Called only if istree(::T) is true. Part of the API required for simplify to work. Other required methods are arguments and istree |
| 51 | +==# |
| 52 | +function SymbolicUtils.operation(x::SymEngine.SymbolicType) |
| 53 | + istree(x) || error("$(typeof(x)) doesn't have an operation!") |
| 54 | + return λ(x) |
| 55 | +end |
| 56 | + |
| 57 | + |
| 58 | +#== |
| 59 | +Returns the arguments (a Vector) for an expression tree. Called only if istree(x) is true. Part of the API required for simplify to work. Other required methods are operation and istree |
| 60 | +==# |
| 61 | +function SymbolicUtils.arguments(x::SymEngine.SymbolicType) |
| 62 | + get_args(x) |
| 63 | +end |
| 64 | + |
| 65 | +#== |
| 66 | +Construct a new term with the operation f and arguments args, the term should be similar to t in type. if t is a SymbolicUtils.Term object a new Term is created with the same symtype as t. If not, the result is computed as f(args...). Defining this method for your term type will reduce any performance loss in performing f(args...) (esp. the splatting, and redundant type computation). T is the symtype of the output term. You can use SymbolicUtils.promote_symtype to infer this type. The exprhead keyword argument is useful when creating Exprs. |
| 67 | +==# |
| 68 | +function SymbolicUtils.similarterm(t::SymEngine.SymbolicType, f, args, symtype=nothing; |
| 69 | + metadata=nothing, exprhead=:call) |
| 70 | + f(args...) # default |
| 71 | +end |
| 72 | + |
| 73 | +# Needed for some simplification routines |
| 74 | +# a total order <ₑ |
| 75 | +import SymbolicUtils: <ₑ, isterm, isadd, ismul, issym, cmp_mul_adds, cmp_term_term |
| 76 | +function SymbolicUtils.:<ₑ(a::SymEngine.Basic, b::SymEngine.Basic) |
| 77 | + if isterm(a) && !isterm(b) |
| 78 | + return false |
| 79 | + elseif isterm(b) && !isterm(a) |
| 80 | + return true |
| 81 | + elseif (isadd(a) || ismul(a)) && (isadd(b) || ismul(b)) |
| 82 | + return cmp_mul_adds(a, b) |
| 83 | + elseif issym(a) && issym(b) |
| 84 | + nameof(a) < nameof(b) |
| 85 | + elseif !istree(a) && !istree(b) |
| 86 | + T = typeof(a) |
| 87 | + S = typeof(b) |
| 88 | + if T == S |
| 89 | + is_number(a) && is_number(b) && return N(a) < N(b) |
| 90 | + return hash(a) < hash(b) |
| 91 | + else |
| 92 | + return name(T) < nameof(S) |
| 93 | + end |
| 94 | + #return T===S ? (T <: Number ? isless(a, b) : hash(a) < hash(b)) : nameof(T) < nameof(S) |
| 95 | + elseif istree(b) && !istree(a) |
| 96 | + return true |
| 97 | + elseif istree(a) && istree(b) |
| 98 | + return cmp_term_term(a,b) |
| 99 | + else |
| 100 | + return !(b <ₑ a) |
| 101 | + end |
| 102 | +end |
| 103 | + |
| 104 | +end |
0 commit comments