@@ -244,6 +244,45 @@ function TermInterface.arguments(x::BasicSymbolicImpl)
244
244
return args
245
245
end
246
246
247
+ """
248
+ $(TYPEDSIGNATURES)
249
+
250
+ For given `coeff` and `dict`, return arguments of type `BasicSymbolicImpl`, children's
251
+ metadata and a new dictionary with `BasicSymbolicImpl` as key's type for preparation of the
252
+ construction of either `Add` or `Mul`.
253
+ """
254
+ function get_arguments_metadata (coeff, dict:: AbstractDict , type:: ExprType )
255
+ siz = length (dict)
256
+ idcoeff = type === ADD ? iszero (coeff) : isone (coeff)
257
+ args = Vector ()
258
+ sizehint! (args, idcoeff ? siz : siz + 1 )
259
+ idcoeff || push! (args, coeff)
260
+ if type === ADD
261
+ for (k, v) in dict
262
+ if k isa BasicSymbolicImpl
263
+ k = BasicSymbolic (k, MetadataImpl ())
264
+ end
265
+ push! (args, applicable (* , k, v) ? k * v : maketerm (k, * , [k, v], nothing ))
266
+ end
267
+ else # MUL
268
+ for (k, v) in dict
269
+ if k isa BasicSymbolicImpl
270
+ k = BasicSymbolic (k, MetadataImpl ())
271
+ end
272
+ push! (args, unstable_pow (k, v))
273
+ end
274
+ end
275
+ metadata_children = map (getmetaimpl, args)
276
+ for i in 1 : length (args)
277
+ if args[i] isa BasicSymbolic
278
+ args[i] = args[i]. expr
279
+ end
280
+ end
281
+ keys = idcoeff ? args : @view args[2 : end ]
282
+ bsi_dict = Dict (zip (keys, values (dict)))
283
+ return args, metadata_children, bsi_dict
284
+ end
285
+
247
286
isexpr (s:: BasicSymbolic ) = isexpr (s. expr)
248
287
isexpr (expr:: BasicSymbolicImpl ) = ! issym (expr)
249
288
iscall (s:: BasicSymbolic ) = iscall (s. expr)
@@ -599,10 +638,15 @@ function Term{T}(f, args; metadata = NO_METADATA, kw...) where T
599
638
if eltype (args) != = Any
600
639
args = convert (Vector{Any}, args)
601
640
end
602
-
641
+ metadata_children = map (getmetaimpl, args)
642
+ for i in 1 : length (args)
643
+ if args[i] isa BasicSymbolic
644
+ args[i] = args[i]. expr
645
+ end
646
+ end
603
647
s = Term {T} (;f= f, arguments= args, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), kw... )
604
648
bsi = BasicSymbolicImpl (s)
605
- mdi = MetadataImpl (metadata, getmetadata .(args) )
649
+ mdi = MetadataImpl (metadata, metadata_children )
606
650
BasicSymbolic (bsi, mdi)
607
651
end
608
652
@@ -622,10 +666,10 @@ function Add(::Type{T}, coeff, dict; metadata=NO_METADATA, kw...) where T
622
666
return Mul (T, coeff, dict)
623
667
end
624
668
end
625
-
626
- s = Add {T} (; coeff, dict, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), arguments= [] , issorted= RefValue (false ), kw... )
669
+ arguments, metadata_children, dict = get_arguments_metadata (coeff, dict, ADD)
670
+ s = Add {T} (; coeff, dict, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), arguments, issorted= RefValue (false ), kw... )
627
671
bsi = BasicSymbolicImpl (s)
628
- mdi = MetadataImpl (metadata, getmetadata .( arguments (s)) )
672
+ mdi = MetadataImpl (metadata, metadata_children )
629
673
BasicSymbolic (bsi, mdi)
630
674
end
631
675
@@ -641,9 +685,10 @@ function Mul(T, a, b; metadata=NO_METADATA, kw...)
641
685
else
642
686
coeff = a
643
687
dict = b
644
- s = Mul {T} (; coeff, dict, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), arguments= [], issorted= RefValue (false ), kw... )
688
+ arguments, metadata_children, dict = get_arguments_metadata (coeff, dict, MUL)
689
+ s = Mul {T} (; coeff, dict, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), arguments, issorted= RefValue (false ), kw... )
645
690
bsi = BasicSymbolicImpl (s)
646
- mdi = MetadataImpl (metadata, getmetadata .( arguments (s)) )
691
+ mdi = MetadataImpl (metadata, metadata_children )
647
692
BasicSymbolic (bsi, mdi)
648
693
end
649
694
end
@@ -708,10 +753,16 @@ function Div{T}(n, d, simplified=false; metadata=NO_METADATA, kwargs...) where {
708
753
end
709
754
end
710
755
end
711
-
756
+ metadata_children = [getmetaimpl (n), getmetaimpl (d)]
757
+ if n isa BasicSymbolic
758
+ n = n. expr
759
+ end
760
+ if d isa BasicSymbolic
761
+ d = d. expr
762
+ end
712
763
s = Div {T} (; num= n, den= d, simplified, arguments= [])
713
764
bsi = BasicSymbolicImpl (s)
714
- mdi = MetadataImpl (metadata, getmetadata .( arguments (s)) )
765
+ mdi = MetadataImpl (metadata, metadata_children )
715
766
BasicSymbolic (bsi, mdi)
716
767
end
717
768
@@ -728,10 +779,17 @@ end
728
779
729
780
function Pow {T} (a, b; metadata= NO_METADATA, kwargs... ) where {T}
730
781
_iszero (b) && return 1
731
- _isone (b) && return a
782
+ _isone (b) && return a
783
+ metadata_children = [getmetaimpl (a), getmetaimpl (b)]
784
+ if a isa BasicSymbolic
785
+ a = a. expr
786
+ end
787
+ if b isa BasicSymbolic
788
+ b = b. expr
789
+ end
732
790
s = Pow {T} (; base= a, exp= b, arguments= [])
733
791
bsi = BasicSymbolicImpl (s)
734
- mdi = MetadataImpl (metadata, getmetadata .( arguments (s)) )
792
+ mdi = MetadataImpl (metadata, metadata_children )
735
793
BasicSymbolic (bsi, mdi)
736
794
end
737
795
0 commit comments