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 ]
@@ -1351,7 +1355,7 @@ class I(Predefined):
13511355 python_equivalent = 1j
13521356
13531357 def evaluate (self , evaluation ):
1354- return Complex (Integer (0 ), Integer ( 1 ) )
1358+ return Complex (Integer (0 ), Integer1 )
13551359
13561360
13571361class NumberQ (Test ):
@@ -2210,7 +2214,110 @@ def apply(self, expr, evaluation):
22102214 "%(name)s[expr_]"
22112215 if isinstance (expr , Symbol ):
22122216 if expr == SymbolTrue :
2213- return Integer ( 1 )
2217+ return Integer1
22142218 elif expr == SymbolFalse :
22152219 return Integer (0 )
22162220 return None
2221+
2222+
2223+ class Assumptions (Predefined ):
2224+ """
2225+ <dl>
2226+ <dt>'$Assumptions'
2227+ <dd>is the default setting for the Assumptions option used in such
2228+ functions as Simplify, Refine, and Integrate.
2229+ </dl>
2230+ """
2231+
2232+ name = "$Assumptions"
2233+ attributes = ("Unprotected" ,)
2234+ rules = {
2235+ "$Assumptions" : "True" ,
2236+ }
2237+
2238+
2239+ class Assuming (Builtin ):
2240+ """
2241+ <dl>
2242+ <dt>'Assuming[$cond$, $expr$]'
2243+ <dd>Evaluates $expr$ assuming the conditions $cond$
2244+ </dl>
2245+ >> $Assumptions = { x > 0 }
2246+ = {x > 0}
2247+ >> Assuming[y>0, $Assumptions]
2248+ = {x > 0, y > 0}
2249+ """
2250+
2251+ attributes = ("HoldRest" ,)
2252+
2253+ def apply_assuming (self , cond , expr , evaluation ):
2254+ "Assuming[cond_, expr_]"
2255+ cond = cond .evaluate (evaluation )
2256+ if cond .is_true ():
2257+ cond = []
2258+ elif cond .is_symbol () or not cond .has_form ("List" , None ):
2259+ cond = [cond ]
2260+ else :
2261+ cond = cond .leaves
2262+ assumptions = evaluation .definitions .get_definition (
2263+ "System`$Assumptions" , only_if_exists = True
2264+ )
2265+
2266+ if assumptions :
2267+ assumptions = assumptions .ownvalues
2268+ if len (assumptions ) > 0 :
2269+ assumptions = assumptions [0 ].replace
2270+ else :
2271+ assumptions = None
2272+ if assumptions :
2273+ if assumptions .is_symbol () or not assumptions .has_form ("List" , None ):
2274+ assumptions = [assumptions ]
2275+ else :
2276+ assumptions = assumptions .leaves
2277+ cond = assumptions + tuple (cond )
2278+ Expression (
2279+ "Set" , Symbol ("System`$Assumptions" ), Expression ("List" , * cond )
2280+ ).evaluate (evaluation )
2281+ ret = expr .evaluate (evaluation )
2282+ if assumptions :
2283+ Expression (
2284+ "Set" , Symbol ("System`$Assumptions" ), Expression ("List" , * assumptions )
2285+ ).evaluate (evaluation )
2286+ else :
2287+ Expression (
2288+ "Set" , Symbol ("System`$Assumptions" ), Expression ("List" , SymbolTrue )
2289+ ).evaluate (evaluation )
2290+ return ret
2291+
2292+
2293+ class ConditionalExpression (Builtin ):
2294+ """
2295+ <dl>
2296+ <dt>'ConditionalExpression[$expr$, $cond$]'
2297+ <dd>returns $expr$ if $cond$ evaluates to $True$, $Undefined$ if
2298+ $cond$ evaluates to $False$.
2299+ </dl>
2300+
2301+ >> f = ConditionalExpression[x^2, x>0]
2302+ = ConditionalExpression[x ^ 2, x > 0]
2303+ >> f /. x -> 2
2304+ = 4
2305+ >> f /. x -> -2
2306+ = Undefined
2307+ """
2308+
2309+ rules = {
2310+ "ConditionalExpression[expr_, True]" : "expr" ,
2311+ "ConditionalExpression[expr_, False]" : "Undefined" ,
2312+ }
2313+
2314+ def apply_generic (self , expr , cond , evaluation ):
2315+ "ConditionalExpression[expr_, cond_]"
2316+ cond = cond .evaluate (evaluation )
2317+ if cond is None :
2318+ return
2319+ if cond .is_true ():
2320+ return expr
2321+ if cond == SymbolFalse :
2322+ return SymbolUndefined
2323+ return
0 commit comments