4646from mathics .builtin .lists import _IterationFunction
4747from mathics .core .convert import from_sympy , SympyExpression
4848
49+
4950@lru_cache (maxsize = 1024 )
5051def call_mpmath (mpmath_function , mpmath_args ):
5152 try :
@@ -687,15 +688,14 @@ def apply(self, items, evaluation):
687688 )
688689 elif item .get_head ().same (SymbolDirectedInfinity ):
689690 infinity_factor = True
690- direction = item .leaves [ 0 ]
691- if isinstance ( direction , Number ):
692- numbers . append (direction )
693- else :
694- leaves . append ( direction )
695- item . leaves [ 0 ]
696- elif ( item .same (SymbolInfinity ) or item .same (SymbolComplexInfinity ) ):
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 ):
697698 infinity_factor = True
698- item .leaves [0 ]
699699 else :
700700 leaves .append (item )
701701
@@ -720,7 +720,7 @@ def apply(self, items, evaluation):
720720 number = None
721721 elif number .is_zero :
722722 if infinity_factor :
723- return Symbol (' Indeterminate' )
723+ return Symbol (" Indeterminate" )
724724 return number
725725 elif number .same (Integer (- 1 )) and leaves and leaves [0 ].has_form ("Plus" , None ):
726726 leaves [0 ] = Expression (
@@ -733,16 +733,14 @@ def apply(self, items, evaluation):
733733 leaf .clear_cache ()
734734
735735 if number is not None :
736- if infinity_factor :
737- number = Expression (SymbolDirectedInfinity ,number / Expression ("Abs" ,number ))
738736 leaves .insert (0 , number )
739737
740738 if not leaves :
741739 if infinity_factor :
742740 return SymbolInfinity
743741 return Integer (1 )
744-
745- elif len (leaves ) == 1 :
742+
743+ if len (leaves ) == 1 :
746744 ret = leaves [0 ]
747745 else :
748746 ret = Expression ("Times" , * leaves )
@@ -855,7 +853,7 @@ class Power(BinaryOperator, _MPMathFunction):
855853
856854 #> 1/0
857855 : Infinite expression 1 / 0 encountered.
858- = ComplexInfinity
856+ = Infinity
859857 #> 0 ^ -2
860858 : Infinite expression 1 / 0 ^ 2 encountered.
861859 = ComplexInfinity
@@ -883,7 +881,7 @@ class Power(BinaryOperator, _MPMathFunction):
883881 #> (3/2+1/2I)^2
884882 = 2 + 3 I / 2
885883 #> I ^ I
886- = I ^ I
884+ = -1 ^ (I / 2)
887885
888886 #> 2 ^ 2.0
889887 = 4.
@@ -916,7 +914,6 @@ class Power(BinaryOperator, _MPMathFunction):
916914 }
917915
918916 formats = {
919-
920917 Expression (
921918 "Power" ,
922919 Expression ("Pattern" , Symbol ("x" ), Expression ("Blank" )),
@@ -934,7 +931,7 @@ class Power(BinaryOperator, _MPMathFunction):
934931 ),
935932 ("" , "x_?Negative ^ y_" ): (
936933 'Infix[{HoldForm[(x)], HoldForm[y]},"^", 590, Right]'
937- ),
934+ ),
938935 }
939936
940937 rules = {
@@ -963,8 +960,8 @@ def apply_check(self, x, y, evaluation):
963960 return Symbol ("ComplexInfinity" )
964961 if isinstance (x , Complex ) and x .real .is_zero :
965962 yhalf = Expression ("Times" , y , Rational (1 , 2 ))
966- factor = self .apply (Expression ("Sequence" , x .imag , y ), evaluation )
967- return Expression ("Times" , factor , Expression ("Power" , Integer (- 1 ), yhalf ))
963+ factor = self .apply (Expression ("Sequence" , x .imag , y ), evaluation )
964+ return Expression ("Times" , factor , Expression ("Power" , Integer (- 1 ), yhalf ))
968965
969966 result = self .apply (Expression ("Sequence" , x , y ), evaluation )
970967 if result is None or result != SymbolNull :
@@ -1083,6 +1080,10 @@ class DirectedInfinity(SympyFunction):
10831080 : Indeterminate expression -Infinity + Infinity encountered.
10841081 = Indeterminate
10851082
1083+ >> DirectedInfinity[0]
1084+ : Indeterminate expression 0 Infinity encountered.
1085+ = Indeterminate
1086+
10861087 #> DirectedInfinity[1+I]+DirectedInfinity[2+I]
10871088 = (2 / 5 + I / 5) Sqrt[5] Infinity + (1 / 2 + I / 2) Sqrt[2] Infinity
10881089
@@ -1091,15 +1092,16 @@ class DirectedInfinity(SympyFunction):
10911092 """
10921093
10931094 rules = {
1095+ "DirectedInfinity[Indeterminate]" :"Indeterminate" ,
10941096 "DirectedInfinity[args___] ^ -1" : "0" ,
10951097 "0 * DirectedInfinity[args___]" : "Message[Infinity::indet, Unevaluated[0 DirectedInfinity[args]]]; Indeterminate" ,
10961098 "DirectedInfinity[a_?NumericQ] /; N[Abs[a]] != 1" : "DirectedInfinity[a / Abs[a]]" ,
10971099 "DirectedInfinity[a_] * DirectedInfinity[b_]" : "DirectedInfinity[a*b]" ,
10981100 "DirectedInfinity[] * DirectedInfinity[args___]" : "DirectedInfinity[]" ,
10991101 "DirectedInfinity[0]" : "DirectedInfinity[]" ,
1100- # Rules already implemented in Times.apply
1101- # "z_?NumberQ * DirectedInfinity[]": "DirectedInfinity[]",
1102- # "z_?NumberQ * DirectedInfinity[a_]": "DirectedInfinity[z * a]",
1102+ # Rules already implemented in Times.apply
1103+ # "z_?NumberQ * DirectedInfinity[]": "DirectedInfinity[]",
1104+ # "z_?NumberQ * DirectedInfinity[a_]": "DirectedInfinity[z * a]",
11031105 "DirectedInfinity[a_] + DirectedInfinity[b_] /; b == -a" : (
11041106 "Message[Infinity::indet,"
11051107 " Unevaluated[DirectedInfinity[a] + DirectedInfinity[b]]];"
@@ -1111,12 +1113,23 @@ class DirectedInfinity(SympyFunction):
11111113 "Indeterminate"
11121114 ),
11131115 "DirectedInfinity[args___] + _?NumberQ" : "DirectedInfinity[args]" ,
1116+ "DirectedInfinity[0]" : (
1117+ "Message[Infinity::indet,"
1118+ " Unevaluated[DirectedInfinity[0]]];"
1119+ "Indeterminate"
1120+ ),
1121+ "DirectedInfinity[0.]" : (
1122+ "Message[Infinity::indet,"
1123+ " Unevaluated[DirectedInfinity[0.]]];"
1124+ "Indeterminate"
1125+ ),
11141126 }
11151127
11161128 formats = {
11171129 "DirectedInfinity[1]" : "HoldForm[Infinity]" ,
11181130 "DirectedInfinity[-1]" : "HoldForm[-Infinity]" ,
11191131 "DirectedInfinity[]" : "HoldForm[ComplexInfinity]" ,
1132+ "DirectedInfinity[DirectedInfinity[z_]]" : "DirectedInfinity[z]" ,
11201133 "DirectedInfinity[z_?NumericQ]" : "HoldForm[z Infinity]" ,
11211134 }
11221135
@@ -1304,19 +1317,12 @@ class Sign(SympyFunction):
13041317 def apply (self , x , evaluation ):
13051318 "%(name)s[x_]"
13061319 # Sympy and mpmath do not give the desired form of complex number
1307- print (x )
13081320 if isinstance (x , Complex ):
13091321 return Expression ("Times" , x , Expression ("Power" , Expression ("Abs" , x ), - 1 ))
13101322
13111323 sympy_x = x .to_sympy ()
13121324 if sympy_x is None :
1313- print (x , " does not have a sympy form" )
1314- if x .is_zero ():
1315- return Real (0 )
1316- return Expression ("Times" , x ,
1317- Expression ("Power" ,
1318- Expression ("Abs" , x ), - 1 )).evaluate (evaluation )
1319- print (sympy_x )
1325+ return None
13201326 return super ().apply (x )
13211327
13221328 def apply_error (self , x , seqs , evaluation ):
@@ -1407,7 +1413,7 @@ def apply(self, expr, evaluation):
14071413 sympy_expr = expr .to_sympy ()
14081414 result = _iszero (sympy_expr )
14091415 if result is None :
1410- # try expanding the expression
1416+ # try expanding the expression
14111417 exprexp = Expression ("ExpandAll" , expr ).evaluate (evaluation )
14121418 exprexp = exprexp .to_sympy ()
14131419 result = _iszero (exprexp )
0 commit comments