File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -707,8 +707,29 @@ def _sympy_from_args(
707
707
sort : bool = True ,
708
708
is_commutative : Optional [bool ] = None ,
709
709
) -> sympy .Expr :
710
+ """
711
+ Create a sympy expression from a list of arguments, optimizing for performance.
712
+
713
+ This function creates a sympy Add or Mul expression from a list of arguments
714
+ while avoiding expensive operations like flattening. It handles sorting the
715
+ arguments appropriately based on the expression type.
716
+
717
+ Args:
718
+ cls: The sympy class to create (Add or Mul)
719
+ args: List of sympy expressions to combine
720
+ sort: Whether to sort the arguments (default: True)
721
+ is_commutative: Whether the operation is commutative (default: None)
722
+
723
+ Returns:
724
+ A sympy expression of type cls combining all arguments
725
+
726
+ Raises:
727
+ ValueError: If cls is not sympy.Add or sympy.Mul
728
+ """
729
+
710
730
if not args :
711
731
return cls .identity # type: ignore[union-attr]
732
+
712
733
# These args are already in canonical form, so we avoid calling
713
734
# Add(*args) to avoid expensive Add.flatten operation
714
735
if sort :
You can’t perform that action at this time.
0 commit comments