|
33 | 33 | Symbol, |
34 | 34 | SymbolComplexInfinity, |
35 | 35 | SymbolDirectedInfinity, |
36 | | - SymbolFalse, |
37 | 36 | SymbolInfinity, |
38 | 37 | SymbolN, |
39 | 38 | SymbolNull, |
40 | 39 | SymbolSequence, |
41 | 40 | SymbolTrue, |
| 41 | + SymbolFalse, |
| 42 | + SymbolUndefined, |
42 | 43 | from_mpmath, |
43 | 44 | from_python, |
44 | 45 | ) |
@@ -675,7 +676,9 @@ def apply(self, items, evaluation): |
675 | 676 | Expression("Plus", item.leaves[1], leaves[-1].leaves[1]), |
676 | 677 | ) |
677 | 678 | elif ( |
678 | | - leaves and item.has_form("Power", 2) and item.leaves[0].sameQ(leaves[-1]) |
| 679 | + leaves |
| 680 | + and item.has_form("Power", 2) |
| 681 | + and item.leaves[0].sameQ(leaves[-1]) |
679 | 682 | ): |
680 | 683 | leaves[-1] = Expression( |
681 | 684 | "Power", leaves[-1], Expression("Plus", item.leaves[1], Integer(1)) |
@@ -2209,3 +2212,106 @@ def apply(self, expr, evaluation): |
2209 | 2212 | elif expr == SymbolFalse: |
2210 | 2213 | return Integer(0) |
2211 | 2214 | return None |
| 2215 | + |
| 2216 | + |
| 2217 | +class Assumptions(Predefined): |
| 2218 | + """ |
| 2219 | + <dl> |
| 2220 | + <dt>'$Assumptions' |
| 2221 | + <dd>is the default setting for the Assumptions option used in such |
| 2222 | + functions as Simplify, Refine, and Integrate. |
| 2223 | + </dl> |
| 2224 | + """ |
| 2225 | + |
| 2226 | + name = "$Assumptions" |
| 2227 | + attributes = ("Unprotected",) |
| 2228 | + rules = { |
| 2229 | + "$Assumptions": "True", |
| 2230 | + } |
| 2231 | + |
| 2232 | + |
| 2233 | +class Assuming(Builtin): |
| 2234 | + """ |
| 2235 | + <dl> |
| 2236 | + <dt>'Assuming[$cond$, $expr$]' |
| 2237 | + <dd>Evaluates $expr$ assuming the conditions $cond$ |
| 2238 | + </dl> |
| 2239 | + >> $Assumptions = { x > 0 } |
| 2240 | + = {x > 0} |
| 2241 | + >> Assuming[y>0, $Assumptions] |
| 2242 | + = {x > 0, y > 0} |
| 2243 | + """ |
| 2244 | + |
| 2245 | + attributes = ("HoldRest",) |
| 2246 | + |
| 2247 | + def apply_assuming(self, cond, expr, evaluation): |
| 2248 | + "Assuming[cond_, expr_]" |
| 2249 | + cond = cond.evaluate(evaluation) |
| 2250 | + if cond.is_true(): |
| 2251 | + cond = [] |
| 2252 | + elif cond.is_symbol() or not cond.has_form("List", None): |
| 2253 | + cond = [cond] |
| 2254 | + else: |
| 2255 | + cond = cond.leaves |
| 2256 | + assumptions = evaluation.definitions.get_definition( |
| 2257 | + "System`$Assumptions", only_if_exists=True |
| 2258 | + ) |
| 2259 | + |
| 2260 | + if assumptions: |
| 2261 | + assumptions = assumptions.ownvalues |
| 2262 | + if len(assumptions) > 0: |
| 2263 | + assumptions = assumptions[0].replace |
| 2264 | + else: |
| 2265 | + assumptions = None |
| 2266 | + if assumptions: |
| 2267 | + if assumptions.is_symbol() or not assumptions.has_form("List", None): |
| 2268 | + assumptions = [assumptions] |
| 2269 | + else: |
| 2270 | + assumptions = assumptions.leaves |
| 2271 | + cond = assumptions + tuple(cond) |
| 2272 | + Expression( |
| 2273 | + "Set", Symbol("System`$Assumptions"), Expression("List", *cond) |
| 2274 | + ).evaluate(evaluation) |
| 2275 | + ret = expr.evaluate(evaluation) |
| 2276 | + if assumptions: |
| 2277 | + Expression( |
| 2278 | + "Set", Symbol("System`$Assumptions"), Expression("List", *assumptions) |
| 2279 | + ).evaluate(evaluation) |
| 2280 | + else: |
| 2281 | + Expression( |
| 2282 | + "Set", Symbol("System`$Assumptions"), Expression("List", SymbolTrue) |
| 2283 | + ).evaluate(evaluation) |
| 2284 | + return ret |
| 2285 | + |
| 2286 | + |
| 2287 | +class ConditionalExpression(Builtin): |
| 2288 | + """ |
| 2289 | + <dl> |
| 2290 | + <dt>'ConditionalExpression[$expr$, $cond$]' |
| 2291 | + <dd>returns $expr$ if $cond$ evaluates to $True$, $Undefined$ if |
| 2292 | + $cond$ evaluates to $False$. |
| 2293 | + </dl> |
| 2294 | +
|
| 2295 | + >> f = ConditionalExpression[x^2, x>0] |
| 2296 | + = ConditionalExpression[x ^ 2, x > 0] |
| 2297 | + >> f /. x -> 2 |
| 2298 | + = 4 |
| 2299 | + >> f /. x -> -2 |
| 2300 | + = Undefined |
| 2301 | + """ |
| 2302 | + |
| 2303 | + rules = { |
| 2304 | + "ConditionalExpression[expr_, True]": "expr", |
| 2305 | + "ConditionalExpression[expr_, False]": "Undefined", |
| 2306 | + } |
| 2307 | + |
| 2308 | + def apply_generic(self, expr, cond, evaluation): |
| 2309 | + "ConditionalExpression[expr_, cond_]" |
| 2310 | + cond = cond.evaluate(evaluation) |
| 2311 | + if cond is None: |
| 2312 | + return |
| 2313 | + if cond.is_true(): |
| 2314 | + return expr |
| 2315 | + if cond == SymbolFalse: |
| 2316 | + return SymbolUndefined |
| 2317 | + return |
0 commit comments