3434 SymbolFalse ,
3535 SymbolNull ,
3636 SymbolTrue ,
37+ SymbolList ,
38+ SymbolInfinity ,
39+ SymbolDirectedInfinity ,
40+ SymbolComplexInfinity ,
3741 from_python ,
3842 from_mpmath ,
3943)
4246from mathics .builtin .lists import _IterationFunction
4347from mathics .core .convert import from_sympy , SympyExpression
4448
49+
4550@lru_cache (maxsize = 1024 )
4651def call_mpmath (mpmath_function , mpmath_args ):
4752 try :
@@ -642,10 +647,10 @@ def format_outputform(self, items, evaluation):
642647
643648 def apply (self , items , evaluation ):
644649 "Times[items___]"
645-
646650 items = items .numerify (evaluation ).get_sequence ()
647651 leaves = []
648652 numbers = []
653+ infinity_factor = False
649654
650655 prec = min_prec (* items )
651656 is_machine_precision = any (item .is_machine_precision () for item in items )
@@ -681,6 +686,16 @@ def apply(self, items, evaluation):
681686 leaves [- 1 ] = Expression (
682687 "Power" , item , Expression ("Plus" , Integer (1 ), leaves [- 1 ].leaves [1 ])
683688 )
689+ elif item .get_head ().same (SymbolDirectedInfinity ):
690+ infinity_factor = True
691+ if len (item .leaves )> 1 :
692+ direction = item .leaves [0 ]
693+ if isinstance (direction , Number ):
694+ numbers .append (direction )
695+ else :
696+ leaves .append (direction )
697+ elif item .same (SymbolInfinity ) or item .same (SymbolComplexInfinity ):
698+ infinity_factor = True
684699 else :
685700 leaves .append (item )
686701
@@ -704,6 +719,8 @@ def apply(self, items, evaluation):
704719 if number .same (Integer (1 )):
705720 number = None
706721 elif number .is_zero :
722+ if infinity_factor :
723+ return Symbol ("Indeterminate" )
707724 return number
708725 elif number .same (Integer (- 1 )) and leaves and leaves [0 ].has_form ("Plus" , None ):
709726 leaves [0 ] = Expression (
@@ -719,11 +736,18 @@ def apply(self, items, evaluation):
719736 leaves .insert (0 , number )
720737
721738 if not leaves :
739+ if infinity_factor :
740+ return SymbolComplexInfinity
722741 return Integer (1 )
723- elif len (leaves ) == 1 :
724- return leaves [0 ]
742+
743+ if len (leaves ) == 1 :
744+ ret = leaves [0 ]
725745 else :
726- return Expression ("Times" , * leaves )
746+ ret = Expression ("Times" , * leaves )
747+ if infinity_factor :
748+ return Expression (SymbolDirectedInfinity , ret )
749+ else :
750+ return ret
727751
728752
729753class Divide (BinaryOperator ):
@@ -857,7 +881,7 @@ class Power(BinaryOperator, _MPMathFunction):
857881 #> (3/2+1/2I)^2
858882 = 2 + 3 I / 2
859883 #> I ^ I
860- = I ^ I
884+ = -1 ^ (I / 2)
861885
862886 #> 2 ^ 2.0
863887 = 4.
@@ -905,6 +929,9 @@ class Power(BinaryOperator, _MPMathFunction):
905929 ("" , "x_ ^ y_?Negative" ): (
906930 "HoldForm[Divide[1, #]]&[If[y==-1, HoldForm[x], HoldForm[x]^-y]]"
907931 ),
932+ ("" , "x_?Negative ^ y_" ): (
933+ 'Infix[{HoldForm[(x)], HoldForm[y]},"^", 590, Right]'
934+ ),
908935 }
909936
910937 rules = {
@@ -931,6 +958,10 @@ def apply_check(self, x, y, evaluation):
931958 elif py_y < 0 :
932959 evaluation .message ("Power" , "infy" , Expression ("Power" , x , y_err ))
933960 return Symbol ("ComplexInfinity" )
961+ if isinstance (x , Complex ) and x .real .is_zero :
962+ yhalf = Expression ("Times" , y , Rational (1 , 2 ))
963+ factor = self .apply (Expression ("Sequence" , x .imag , y ), evaluation )
964+ return Expression ("Times" , factor , Expression ("Power" , Integer (- 1 ), yhalf ))
934965
935966 result = self .apply (Expression ("Sequence" , x , y ), evaluation )
936967 if result is None or result != SymbolNull :
@@ -1049,6 +1080,10 @@ class DirectedInfinity(SympyFunction):
10491080 : Indeterminate expression -Infinity + Infinity encountered.
10501081 = Indeterminate
10511082
1083+ >> DirectedInfinity[0]
1084+ : Indeterminate expression 0 Infinity encountered.
1085+ = Indeterminate
1086+
10521087 #> DirectedInfinity[1+I]+DirectedInfinity[2+I]
10531088 = (2 / 5 + I / 5) Sqrt[5] Infinity + (1 / 2 + I / 2) Sqrt[2] Infinity
10541089
@@ -1057,14 +1092,15 @@ class DirectedInfinity(SympyFunction):
10571092 """
10581093
10591094 rules = {
1095+ "DirectedInfinity[Indeterminate]" :"Indeterminate" ,
10601096 "DirectedInfinity[args___] ^ -1" : "0" ,
10611097 "0 * DirectedInfinity[args___]" : "Message[Infinity::indet, Unevaluated[0 DirectedInfinity[args]]]; Indeterminate" ,
10621098 "DirectedInfinity[a_?NumericQ] /; N[Abs[a]] != 1" : "DirectedInfinity[a / Abs[a]]" ,
10631099 "DirectedInfinity[a_] * DirectedInfinity[b_]" : "DirectedInfinity[a*b]" ,
10641100 "DirectedInfinity[] * DirectedInfinity[args___]" : "DirectedInfinity[]" ,
1065- "DirectedInfinity[0]" : "DirectedInfinity[]" ,
1066- "z_?NumberQ * DirectedInfinity[]" : "DirectedInfinity[]" ,
1067- "z_?NumberQ * DirectedInfinity[a_]" : "DirectedInfinity[z * a]" ,
1101+ # Rules already implemented in Times.apply
1102+ # "z_?NumberQ * DirectedInfinity[]": "DirectedInfinity[]",
1103+ # "z_?NumberQ * DirectedInfinity[a_]": "DirectedInfinity[z * a]",
10681104 "DirectedInfinity[a_] + DirectedInfinity[b_] /; b == -a" : (
10691105 "Message[Infinity::indet,"
10701106 " Unevaluated[DirectedInfinity[a] + DirectedInfinity[b]]];"
@@ -1076,12 +1112,23 @@ class DirectedInfinity(SympyFunction):
10761112 "Indeterminate"
10771113 ),
10781114 "DirectedInfinity[args___] + _?NumberQ" : "DirectedInfinity[args]" ,
1115+ "DirectedInfinity[0]" : (
1116+ "Message[Infinity::indet,"
1117+ " Unevaluated[DirectedInfinity[0]]];"
1118+ "Indeterminate"
1119+ ),
1120+ "DirectedInfinity[0.]" : (
1121+ "Message[Infinity::indet,"
1122+ " Unevaluated[DirectedInfinity[0.]]];"
1123+ "Indeterminate"
1124+ ),
10791125 }
10801126
10811127 formats = {
10821128 "DirectedInfinity[1]" : "HoldForm[Infinity]" ,
10831129 "DirectedInfinity[-1]" : "HoldForm[-Infinity]" ,
10841130 "DirectedInfinity[]" : "HoldForm[ComplexInfinity]" ,
1131+ "DirectedInfinity[DirectedInfinity[z_]]" : "DirectedInfinity[z]" ,
10851132 "DirectedInfinity[z_?NumericQ]" : "HoldForm[z Infinity]" ,
10861133 }
10871134
@@ -1365,7 +1412,7 @@ def apply(self, expr, evaluation):
13651412 sympy_expr = expr .to_sympy ()
13661413 result = _iszero (sympy_expr )
13671414 if result is None :
1368- # try expanding the expression
1415+ # try expanding the expression
13691416 exprexp = Expression ("ExpandAll" , expr ).evaluate (evaluation )
13701417 exprexp = exprexp .to_sympy ()
13711418 result = _iszero (exprexp )
0 commit comments