@@ -16,7 +16,8 @@ const NO_METADATA = nothing
1616sdict (kv... ) = Dict {Any, Any} (kv... )
1717
1818using Base: RefValue
19- const EMPTY_ARGS = []
19+ const SmallV{T} = SmallVec{T, Vector{T}}
20+ const EMPTY_ARGS = SmallV {Any} ()
2021const EMPTY_HASH = RefValue (UInt (0 ))
2122const EMPTY_DICT = sdict ()
2223const EMPTY_DICT_T = typeof (EMPTY_DICT)
@@ -31,7 +32,7 @@ const ENABLE_HASHCONSING = Ref(true)
3132 end
3233 struct Term{T} <: BasicSymbolic{T}
3334 f:: Any = identity # base/num if Pow; issorted if Add/Dict
34- arguments:: Vector {Any} = EMPTY_ARGS
35+ arguments:: SmallV {Any} = EMPTY_ARGS
3536 hash:: RefValue{UInt} = EMPTY_HASH
3637 hash2:: RefValue{UInt} = EMPTY_HASH
3738 end
@@ -40,25 +41,25 @@ const ENABLE_HASHCONSING = Ref(true)
4041 dict:: EMPTY_DICT_T = EMPTY_DICT
4142 hash:: RefValue{UInt} = EMPTY_HASH
4243 hash2:: RefValue{UInt} = EMPTY_HASH
43- arguments:: Vector {Any} = EMPTY_ARGS
44+ arguments:: SmallV {Any} = EMPTY_ARGS
4445 end
4546 struct Add{T} <: BasicSymbolic{T}
4647 coeff:: Any = 0 # exp/den if Pow
4748 dict:: EMPTY_DICT_T = EMPTY_DICT
4849 hash:: RefValue{UInt} = EMPTY_HASH
4950 hash2:: RefValue{UInt} = EMPTY_HASH
50- arguments:: Vector {Any} = EMPTY_ARGS
51+ arguments:: SmallV {Any} = EMPTY_ARGS
5152 end
5253 struct Div{T} <: BasicSymbolic{T}
5354 num:: Any = 1
5455 den:: Any = 1
5556 simplified:: Bool = false
56- arguments:: Vector {Any} = EMPTY_ARGS
57+ arguments:: SmallV {Any} = EMPTY_ARGS
5758 end
5859 struct Pow{T} <: BasicSymbolic{T}
5960 base:: Any = 1
6061 exp:: Any = 1
61- arguments:: Vector {Any} = EMPTY_ARGS
62+ arguments:: SmallV {Any} = EMPTY_ARGS
6263 end
6364end
6465
@@ -564,17 +565,8 @@ function Sym{T}(name::Symbol; kw...) where {T}
564565 BasicSymbolic (s)
565566end
566567
567- function unwrap_arr! (arr)
568- for i in eachindex (arr)
569- arr[i] = unwrap (arr[i])
570- end
571- end
572-
573568function Term {T} (f, args; kw... ) where T
574- if eltype (args) != = Any
575- args = convert (Vector{Any}, args)
576- end
577- unwrap_arr! (args)
569+ args = SmallV {Any} (args)
578570
579571 s = Term {T} (;f= f, arguments= args, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), kw... )
580572 BasicSymbolic (s)
@@ -606,7 +598,7 @@ function Add(::Type{T}, coeff, dict; metadata=NO_METADATA, kw...) where T
606598 end
607599 end
608600
609- s = Add {T} (; coeff, dict, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), metadata, arguments= [] , kw... )
601+ s = Add {T} (; coeff, dict, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), metadata, arguments= SmallV {Any} () , kw... )
610602 BasicSymbolic (s)
611603end
612604
@@ -624,7 +616,7 @@ function Mul(T, a, b; metadata=NO_METADATA, kw...)
624616 else
625617 coeff = a
626618 dict = b
627- s = Mul {T} (; coeff, dict, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), metadata, arguments= [] , kw... )
619+ s = Mul {T} (; coeff, dict, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), metadata, arguments= SmallV {Any} () , kw... )
628620 BasicSymbolic (s)
629621 end
630622end
@@ -645,7 +637,7 @@ ratio(x::Rat,y::Rat) = x//y
645637function maybe_intcoeff (x)
646638 if ismul (x)
647639 if x. coeff isa Rational && isone (x. coeff. den)
648- Mul {symtype(x)} (; coeff= x. coeff. num, dict= x. dict, x. metadata, arguments= [] )
640+ Mul {symtype(x)} (; coeff= x. coeff. num, dict= x. dict, x. metadata, arguments= SmallV {Any} () )
649641 else
650642 x
651643 end
@@ -692,7 +684,7 @@ function Div{T}(n, d, simplified=false; metadata=nothing, kwargs...) where {T}
692684 end
693685 end
694686
695- s = Div {T} (; num= n, den= d, simplified, arguments= [] , metadata)
687+ s = Div {T} (; num= n, den= d, simplified, arguments= SmallV {Any} () , metadata)
696688 BasicSymbolic (s)
697689end
698690
@@ -712,7 +704,7 @@ function Pow{T}(a, b; metadata=NO_METADATA, kwargs...) where {T}
712704 b = unwrap (b)
713705 _iszero (b) && return 1
714706 _isone (b) && return a
715- s = Pow {T} (; base= a, exp= b, arguments= [] , metadata)
707+ s = Pow {T} (; base= a, exp= b, arguments= SmallV {Any} () , metadata)
716708 BasicSymbolic (s)
717709end
718710
@@ -728,13 +720,13 @@ function toterm(t::BasicSymbolic{T}) where T
728720 args = Any[]
729721 push! (args, t. coeff)
730722 for (k, coeff) in t. dict
731- push! (args, coeff == 1 ? k : Term {T} (E === MUL ? (^ ) : (* ), Any[ coeff, k] ))
723+ push! (args, coeff == 1 ? k : Term {T} (E === MUL ? (^ ) : (* ), SmallV { Any} (( coeff, k)) ))
732724 end
733725 Term {T} (operation (t), args)
734726 elseif E === DIV
735- Term {T} (/ , Any[ t. num, t. den] )
727+ Term {T} (/ , SmallV { Any} (( t. num, t. den)) )
736728 elseif E === POW
737- Term {T} (^ , [ t. base, t. exp] )
729+ Term {T} (^ , SmallV {Any} (( t. base, t. exp)) )
738730 else
739731 error_on_type ()
740732 end
@@ -808,13 +800,13 @@ function makepow(a, b)
808800end
809801
810802function term (f, args... ; type = nothing )
811- args = map (unwrap, args)
803+ args = SmallV {Any} ( args)
812804 if type === nothing
813805 T = _promote_symtype (f, args)
814806 else
815807 T = type
816808 end
817- Term {T} (f, Any[ args... ] )
809+ Term {T} (f, args)
818810end
819811
820812"""
@@ -826,7 +818,7 @@ function unflatten(t::Symbolic{T}) where{T}
826818 f = operation (t)
827819 if f == (+ ) || f == (* ) # TODO check out for other n-ary --> binary ops
828820 a = arguments (t)
829- return foldl ((x,y) -> Term {T} (f, Any[ x, y] ), a)
821+ return foldl ((x,y) -> Term {T} (f, SmallV { Any} (( x, y)) ), a)
830822 end
831823 end
832824 return t
@@ -1192,7 +1184,7 @@ promote_symtype(f, Ts...) = Any
11921184
11931185struct FnType{X<: Tuple ,Y,Z} end
11941186
1195- (f:: Symbolic{<:FnType} )(args... ) = Term {promote_symtype(f, symtype.(args)...)} (f, Any[ args... ] )
1187+ (f:: Symbolic{<:FnType} )(args... ) = Term {promote_symtype(f, symtype.(args)...)} (f, SmallV { Any} ( args) )
11961188
11971189function (f:: Symbolic )(args... )
11981190 error (" Sym $f is not callable. " *
0 commit comments