2626 Complex ,
2727 Expression ,
2828 Integer ,
29+ Integer1 ,
2930 Number ,
3031 Rational ,
3132 Real ,
3233 String ,
3334 Symbol ,
3435 SymbolComplexInfinity ,
3536 SymbolDirectedInfinity ,
36- SymbolFalse ,
3737 SymbolInfinity ,
3838 SymbolN ,
3939 SymbolNull ,
4040 SymbolSequence ,
4141 SymbolTrue ,
42+ SymbolFalse ,
43+ SymbolUndefined ,
4244 from_mpmath ,
4345 from_python ,
4446)
@@ -112,7 +114,7 @@ def apply(self, z, evaluation):
112114 if mpmath .isinf (result ) and isinstance (result , mpmath .mpc ):
113115 result = Symbol ("ComplexInfinity" )
114116 elif mpmath .isinf (result ) and result > 0 :
115- result = Expression ("DirectedInfinity" , Integer ( 1 ) )
117+ result = Expression ("DirectedInfinity" , Integer1 )
116118 elif mpmath .isinf (result ) and result < 0 :
117119 result = Expression ("DirectedInfinity" , Integer (- 1 ))
118120 elif mpmath .isnan (result ):
@@ -260,7 +262,7 @@ def negate(item):
260262 if item .has_form ("Times" , 1 , None ):
261263 if isinstance (item .leaves [0 ], Number ):
262264 neg = - item .leaves [0 ]
263- if neg .sameQ (Integer ( 1 ) ):
265+ if neg .sameQ (Integer1 ):
264266 if len (item .leaves ) == 1 :
265267 return neg
266268 else :
@@ -583,7 +585,7 @@ def inverse(item):
583585 item .leaves [1 ], (Integer , Rational , Real )
584586 ):
585587 neg = - item .leaves [1 ]
586- if neg .sameQ (Integer ( 1 ) ):
588+ if neg .sameQ (Integer1 ):
587589 return item .leaves [0 ]
588590 else :
589591 return Expression ("Power" , item .leaves [0 ], neg )
@@ -603,7 +605,7 @@ def inverse(item):
603605 negative .append (inverse (item ))
604606 elif isinstance (item , Rational ):
605607 numerator = item .numerator ()
606- if not numerator .sameQ (Integer ( 1 ) ):
608+ if not numerator .sameQ (Integer1 ):
607609 positive .append (numerator )
608610 negative .append (item .denominator ())
609611 else :
@@ -618,7 +620,7 @@ def inverse(item):
618620 if positive :
619621 positive = create_infix (positive , op , 400 , "None" )
620622 else :
621- positive = Integer ( 1 )
623+ positive = Integer1
622624 if negative :
623625 negative = create_infix (negative , op , 400 , "None" )
624626 result = Expression (
@@ -675,18 +677,20 @@ def apply(self, items, evaluation):
675677 Expression ("Plus" , item .leaves [1 ], leaves [- 1 ].leaves [1 ]),
676678 )
677679 elif (
678- leaves and item .has_form ("Power" , 2 ) and item .leaves [0 ].sameQ (leaves [- 1 ])
680+ leaves
681+ and item .has_form ("Power" , 2 )
682+ and item .leaves [0 ].sameQ (leaves [- 1 ])
679683 ):
680684 leaves [- 1 ] = Expression (
681- "Power" , leaves [- 1 ], Expression ("Plus" , item .leaves [1 ], Integer ( 1 ) )
685+ "Power" , leaves [- 1 ], Expression ("Plus" , item .leaves [1 ], Integer1 )
682686 )
683687 elif (
684688 leaves
685689 and leaves [- 1 ].has_form ("Power" , 2 )
686690 and leaves [- 1 ].leaves [0 ].sameQ (item )
687691 ):
688692 leaves [- 1 ] = Expression (
689- "Power" , item , Expression ("Plus" , Integer ( 1 ) , leaves [- 1 ].leaves [1 ])
693+ "Power" , item , Expression ("Plus" , Integer1 , leaves [- 1 ].leaves [1 ])
690694 )
691695 elif item .get_head ().sameQ (SymbolDirectedInfinity ):
692696 infinity_factor = True
@@ -716,9 +720,9 @@ def apply(self, items, evaluation):
716720 number = sympy .Mul (* [item .to_sympy () for item in numbers ])
717721 number = from_sympy (number )
718722 else :
719- number = Integer ( 1 )
723+ number = Integer1
720724
721- if number .sameQ (Integer ( 1 ) ):
725+ if number .sameQ (Integer1 ):
722726 number = None
723727 elif number .is_zero :
724728 if infinity_factor :
@@ -740,7 +744,7 @@ def apply(self, items, evaluation):
740744 if not leaves :
741745 if infinity_factor :
742746 return SymbolComplexInfinity
743- return Integer ( 1 )
747+ return Integer1
744748
745749 if len (leaves ) == 1 :
746750 ret = leaves [0 ]
@@ -1135,12 +1139,16 @@ class DirectedInfinity(SympyFunction):
11351139 }
11361140
11371141 def to_sympy (self , expr , ** kwargs ):
1138- if len (expr .leaves ) == 1 :
1142+ if len (expr ._leaves ) == 1 :
11391143 dir = expr .leaves [0 ].get_int_value ()
11401144 if dir == 1 :
11411145 return sympy .oo
11421146 elif dir == - 1 :
11431147 return - sympy .oo
1148+ else :
1149+ return sympy .Mul ((expr ._leaves [0 ].to_sympy ()), sympy .zoo )
1150+ else :
1151+ return sympy .zoo
11441152
11451153
11461154class Re (SympyFunction ):
@@ -1347,7 +1355,7 @@ class I(Predefined):
13471355 python_equivalent = 1j
13481356
13491357 def evaluate (self , evaluation ):
1350- return Complex (Integer (0 ), Integer ( 1 ) )
1358+ return Complex (Integer (0 ), Integer1 )
13511359
13521360
13531361class NumberQ (Test ):
@@ -2201,7 +2209,110 @@ def apply(self, expr, evaluation):
22012209 "%(name)s[expr_]"
22022210 if isinstance (expr , Symbol ):
22032211 if expr == SymbolTrue :
2204- return Integer ( 1 )
2212+ return Integer1
22052213 elif expr == SymbolFalse :
22062214 return Integer (0 )
22072215 return None
2216+
2217+
2218+ class Assumptions (Predefined ):
2219+ """
2220+ <dl>
2221+ <dt>'$Assumptions'
2222+ <dd>is the default setting for the Assumptions option used in such
2223+ functions as Simplify, Refine, and Integrate.
2224+ </dl>
2225+ """
2226+
2227+ name = "$Assumptions"
2228+ attributes = ("Unprotected" ,)
2229+ rules = {
2230+ "$Assumptions" : "True" ,
2231+ }
2232+
2233+
2234+ class Assuming (Builtin ):
2235+ """
2236+ <dl>
2237+ <dt>'Assuming[$cond$, $expr$]'
2238+ <dd>Evaluates $expr$ assuming the conditions $cond$
2239+ </dl>
2240+ >> $Assumptions = { x > 0 }
2241+ = {x > 0}
2242+ >> Assuming[y>0, $Assumptions]
2243+ = {x > 0, y > 0}
2244+ """
2245+
2246+ attributes = ("HoldRest" ,)
2247+
2248+ def apply_assuming (self , cond , expr , evaluation ):
2249+ "Assuming[cond_, expr_]"
2250+ cond = cond .evaluate (evaluation )
2251+ if cond .is_true ():
2252+ cond = []
2253+ elif cond .is_symbol () or not cond .has_form ("List" , None ):
2254+ cond = [cond ]
2255+ else :
2256+ cond = cond .leaves
2257+ assumptions = evaluation .definitions .get_definition (
2258+ "System`$Assumptions" , only_if_exists = True
2259+ )
2260+
2261+ if assumptions :
2262+ assumptions = assumptions .ownvalues
2263+ if len (assumptions ) > 0 :
2264+ assumptions = assumptions [0 ].replace
2265+ else :
2266+ assumptions = None
2267+ if assumptions :
2268+ if assumptions .is_symbol () or not assumptions .has_form ("List" , None ):
2269+ assumptions = [assumptions ]
2270+ else :
2271+ assumptions = assumptions .leaves
2272+ cond = assumptions + tuple (cond )
2273+ Expression (
2274+ "Set" , Symbol ("System`$Assumptions" ), Expression ("List" , * cond )
2275+ ).evaluate (evaluation )
2276+ ret = expr .evaluate (evaluation )
2277+ if assumptions :
2278+ Expression (
2279+ "Set" , Symbol ("System`$Assumptions" ), Expression ("List" , * assumptions )
2280+ ).evaluate (evaluation )
2281+ else :
2282+ Expression (
2283+ "Set" , Symbol ("System`$Assumptions" ), Expression ("List" , SymbolTrue )
2284+ ).evaluate (evaluation )
2285+ return ret
2286+
2287+
2288+ class ConditionalExpression (Builtin ):
2289+ """
2290+ <dl>
2291+ <dt>'ConditionalExpression[$expr$, $cond$]'
2292+ <dd>returns $expr$ if $cond$ evaluates to $True$, $Undefined$ if
2293+ $cond$ evaluates to $False$.
2294+ </dl>
2295+
2296+ >> f = ConditionalExpression[x^2, x>0]
2297+ = ConditionalExpression[x ^ 2, x > 0]
2298+ >> f /. x -> 2
2299+ = 4
2300+ >> f /. x -> -2
2301+ = Undefined
2302+ """
2303+
2304+ rules = {
2305+ "ConditionalExpression[expr_, True]" : "expr" ,
2306+ "ConditionalExpression[expr_, False]" : "Undefined" ,
2307+ }
2308+
2309+ def apply_generic (self , expr , cond , evaluation ):
2310+ "ConditionalExpression[expr_, cond_]"
2311+ cond = cond .evaluate (evaluation )
2312+ if cond is None :
2313+ return
2314+ if cond .is_true ():
2315+ return expr
2316+ if cond == SymbolFalse :
2317+ return SymbolUndefined
2318+ return
0 commit comments