11export SymReal, SafeReal, TreeReal, vartype
22
33abstract type SymVariant end
4+ """
5+ $TYPEDEF
6+
7+ One of three possible values of the [`vartype`](@ref). This variant is the default and
8+ behaves as a typical ideal scalar algebra would be expected to.
9+ """
410abstract type SymReal <: SymVariant end
11+ """
12+ $TYPEDEF
13+
14+ One of three possible values of the [`vartype`](@ref). This variant is identical to
15+ [`SymReal`](@ref) except common terms in the numerator and denominator of a division are
16+ not cancelled out.
17+ """
518abstract type SafeReal <: SymVariant end
19+ """
20+ $TYPEDEF
21+
22+ One of three possible values of the [`vartype`](@ref). This variant does not assume anything
23+ about the algebra and always uses the default tree form for expressions.
24+ """
625abstract type TreeReal <: SymVariant end
726
827# ##
3554
3655const SCALARS = [Bool, Int, Int32, BigInt, Float64, Float32, BigFloat, Rational{Int}, Rational{Int32}, Rational{BigInt}, ComplexF32, ComplexF64, Complex{BigFloat}]
3756
57+ """
58+ $TYPEDEF
3859
60+ Type of metadata field for symbolics.
61+ """
3962const MetadataT = Union{Base. ImmutableDict{DataType, Any}, Nothing}
63+ """
64+ $TYPEDEF
65+
66+ A custom vector type which does not allocate for small numbers of elements. If the number of elements is
67+ known at compile time, it should be passed as a `Tuple` to the constructor.
68+ """
4069const SmallV{T} = SmallVec{T, Vector{T}}
4170const ShapeVecT = SmallV{UnitRange{Int}}
71+ """
72+ $TYPEDEF
73+
74+ Type that represents the [`SymbolicUtils.shape`](@ref) of symbolics.
75+ """
4276const ShapeT = Union{Unknown, ShapeVecT}
4377const IdentT = Union{IDType, Nothing}
4478const MonomialOrder = MP. Graded{MP. Reverse{MP. InverseLexOrder}}
@@ -50,6 +84,11 @@ const _PolynomialT{T} = DP.Polynomial{PolyVarOrder, MonomialOrder, T}
5084# we can't actually print a zero polynomial of this type, since it attempts to call
5185# `zero(Any)` but that doesn't matter because we shouldn't ever store a zero polynomial
5286const PolynomialT = _PolynomialT{PolyCoeffT}
87+ """
88+ $TYPEDEF
89+
90+ Allowed types for the [`SymbolicUtils.symtype`](@ref) of symbolics.
91+ """
5392const TypeT = DataType
5493const MonomialT = DP. Monomial{PolyVarOrder, MonomialOrder}
5594const MonomialVecT = DP. MonomialVector{PolyVarOrder, MonomialOrder}
@@ -81,6 +120,12 @@ function onepoly()
81120 PolynomialT (PolyCoeffT[1 ], mv)
82121end
83122
123+ """
124+ $(TYPEDEF)
125+
126+ An EnumX.jl enum used to distinguish between addition and multiplication in
127+ [`SymbolicUtils.BSImpl.AddMul`](@ref).
128+ """
84129@enumx AddMulVariant:: Bool begin
85130 ADD
86131 MUL
89134"""
90135 $(TYPEDEF)
91136
92- Core ADT for `BasicSymbolic`
137+ Core ADT for symbolic expressions.
93138"""
94139@data mutable BasicSymbolicImpl{T <: SymVariant } begin
95140 struct Const
@@ -171,12 +216,38 @@ Core ADT for `BasicSymbolic`
171216 end
172217end
173218
219+ """
220+ Alias for `SymbolicUtils.BasicSymbolicImpl`.
221+ """
174222const BSImpl = BasicSymbolicImpl
223+ """
224+ Alias for `SymbolicUtils.BasicSymbolicImpl.Type`.
225+ """
175226const BasicSymbolic = BSImpl. Type
227+ """
228+ The type of a mutable buffer containing symbolic arguments. Passing this to the
229+ [`SymbolicUtils.Term`](@ref) constructor will avoid allocating a new array.
230+ """
176231const ArgsT{T} = SmallV{BasicSymbolic{T}}
232+ """
233+ The type of a read-only buffer containing symbolic arguments. Passing this to the
234+ [`SymbolicUtils.Term`](@ref) constructor will avoid allocating a new array. This is
235+ the type returned from [`TermInterface.arguments`](@ref).
236+ """
177237const ROArgsT{T} = ReadOnlyVector{BasicSymbolic{T}, ArgsT{T}}
238+ """
239+ The type of the dictionary stored in [`BSImpl.AddMul`](@ref). Passing this to the
240+ [`SymbolicUtils.Add`](@ref) or [`SymbolicUtils.Mul`](@ref) constructors will avoid
241+ allocating a new dictionary.
242+ """
178243const ACDict{T} = Dict{BasicSymbolic{T}, Number}
244+ """
245+ The type of the `output_idxs` field in [`BSImpl.ArrayOp`](@ref).
246+ """
179247const OutIdxT{T} = SmallV{Union{Int, BasicSymbolic{T}}}
248+ """
249+ The type of the `ranges` field in [`BSImpl.ArrayOp`](@ref).
250+ """
180251const RangesT{T} = Dict{BasicSymbolic{T}, StepRange{Int, Int}}
181252
182253"""
423494# ##
424495
425496"""
426- operation(expr)
497+ $TYPEDSIGNATURES
427498
428499Extract the operation (function) from a symbolic function call expression.
429500Only valid for expressions where `iscall(expr)` returns `true`.
@@ -454,7 +525,7 @@ operation(expr4) # returns sin
454525operation(arguments(expr4)[1]) # returns +
455526```
456527
457- See also: [`iscall`](@ref), [`arguments`](@ref)
528+ See also: [`TermInterface. iscall`](@ref), [`arguments`](@ref)
458529"""
459530@inline function TermInterface. operation (x:: BSImpl.Type{T} ) where {T}
460531 @nospecialize x
612683"""
613684 $TYPEDSIGNATURES
614685
615- Check if a `BasicSymbolic` is an expression (not a `Sym` or `Const`).
616-
617- # Arguments
618- - `s`: A `BasicSymbolic` value to check.
619-
620- # Returns
621- `true` if `s` is a compound expression.
622- """
623- function isexpr (s:: BSImpl.Type )
624- ! MData. isa_variant (s, BSImpl. Sym) && ! MData. isa_variant (s, BSImpl. Const)
625- end
626-
627- """
628- iscall(expr)
629-
630686Check if a symbolic expression `expr` represents a function call. Returns `true` if the
631687expression is a composite expression with an operation and arguments, `false` otherwise.
632688
@@ -653,7 +709,9 @@ iscall(x * y) # true
653709
654710See also: [`operation`](@ref), [`arguments`](@ref)
655711"""
656- iscall (s:: BSImpl.Type ) = isexpr (s)
712+ function TermInterface. iscall (s:: BSImpl.Type )
713+ ! MData. isa_variant (s, BSImpl. Sym) && ! MData. isa_variant (s, BSImpl. Const)
714+ end
657715
658716"""
659717 $TYPEDSIGNATURES
@@ -2690,9 +2748,9 @@ end
26902748# ##
26912749
26922750"""
2693- promote_symtype(f, Ts...)
2751+ $TYPEDSIGNATURES
26942752
2695- The result of applying `f` to arguments of [`symtype`](#symtype ) `Ts...`
2753+ The result of applying `f` to arguments of [`SymbolicUtils. symtype`](@ref ) `Ts...`
26962754
26972755```julia
26982756julia> promote_symtype(+, Real, Real)
@@ -2708,15 +2766,22 @@ julia> promote_symtype(f, Number)
27082766Complex
27092767```
27102768
2711- When constructing [`Term`](#Term)s without an explicit symtype,
2769+ When constructing expressions without an explicit symtype,
27122770`promote_symtype` is used to figure out the symtype of the Term.
2771+
2772+ It is recommended that all type arguments be annotated with [`SymbolicUtils.TypeT`](@ref)
2773+ and one method be implemented for any combination of `f` and the number of arguments. For
2774+ example, one method is implemented for unary `-` and one method for binary `-`. Each method
2775+ has an `if..elseif` chain to handle possible types. Any call to `promote_type` should be
2776+ typeasserted with `::TypeT`.
27132777"""
27142778promote_symtype (f, Ts... ) = Any
27152779
27162780"""
27172781 promote_shape(f, shs::ShapeT...)
27182782
27192783The shape of the result of applying `f` to arguments of [`shape`](@ref) `shs...`.
2784+ It is recommended that implemented methods `@nospecialize` all the shape arguments.
27202785"""
27212786promote_shape (f, szs:: ShapeT... ) = Unknown (- 1 )
27222787
@@ -2956,6 +3021,12 @@ Base.empty!(awb::AddWorkerBuffer) = empty!(awb.dict)
29563021const SYMREAL_ADDBUFFER = TaskLocalValue {AddWorkerBuffer{SymReal}} (AddWorkerBuffer{SymReal})
29573022const SAFEREAL_ADDBUFFER = TaskLocalValue {AddWorkerBuffer{SafeReal}} (AddWorkerBuffer{SafeReal})
29583023
3024+ """
3025+ $METHODLIST
3026+
3027+ Add an indexable list or tuple of terms `terms` with the given vartype. Applicable only for
3028+ symbolic expressions with numeric or array of numeric symtype.
3029+ """
29593030add_worker (:: Type{SymReal} , terms) = SYMREAL_ADDBUFFER[](terms)
29603031add_worker (:: Type{SafeReal} , terms) = SAFEREAL_ADDBUFFER[](terms)
29613032
@@ -3428,6 +3499,12 @@ function (mwb::MulWorkerBuffer{T})(terms) where {T}
34283499 return Term {T} (* , new_arrterms; type, shape = newshape)
34293500end
34303501
3502+ """
3503+ $METHODLIST
3504+
3505+ Multiply an indexable list or tuple of terms `terms` with the given vartype. Applicable
3506+ only for symbolic expressions with numeric or array of numeric symtype.
3507+ """
34313508mul_worker (:: Type{SymReal} , terms) = SYMREAL_MULBUFFER[](terms)
34323509mul_worker (:: Type{SafeReal} , terms) = SAFEREAL_MULBUFFER[](terms)
34333510
0 commit comments