Repository: sympy/sympy

Issue:
Unevaluated integral for truncated Gaussian
The following results in an unevaluated integral (versions 0.7.4.1 and 0.7.5-git):

```
>>> c = symbols('c',real=True)
>>> integrate(Max(c,x)*exp(-x**2),(x,-oo,oo))
```

but expressing it slightly differently avoids any problems:

```
>>> integrate((c+Max(0,x-c))*exp(-x**2),(x,-oo,oo))
```


Discussion:

Patch:
diff --git a/sympy/integrals/integrals.py b/sympy/integrals/integrals.py
index b202af230d..74931be911 100644
--- a/sympy/integrals/integrals.py
+++ b/sympy/integrals/integrals.py
@@ -21,6 +21,7 @@
 from sympy.polys import Poly, PolynomialError
 from sympy.functions import Piecewise, sqrt, sign, piecewise_fold
 from sympy.functions.elementary.exponential import log
+from sympy.functions.elementary.miscellaneous import Min, Max
 from sympy.series import limit
 from sympy.series.order import Order
 from sympy.series.formal import FormalPowerSeries
@@ -443,6 +444,8 @@ def doit(self, **hints):
                     function = factored_function
                 continue
 
+            if function.has(Min, Max):
+                function = function.rewrite(Piecewise)
             if function.has(Piecewise) and \
                 not isinstance(function, Piecewise):
                     function = piecewise_fold(function)


Test patch:
diff --git a/sympy/integrals/tests/test_integrals.py b/sympy/integrals/tests/test_integrals.py
index 07aced7260..0397939ee6 100644
--- a/sympy/integrals/tests/test_integrals.py
+++ b/sympy/integrals/tests/test_integrals.py
@@ -4,7 +4,7 @@
     Expr, factor, Function, I, Integral, integrate, Interval, Lambda,
     LambertW, log, Matrix, O, oo, pi, Piecewise, Poly, Rational, S, simplify,
     sin, tan, sqrt, sstr, Sum, Symbol, symbols, sympify, trigsimp, Tuple, nan,
-    And, Eq, Ne, re, im, polar_lift, meijerg, SingularityFunction
+    And, Eq, Ne, re, im, polar_lift, meijerg, SingularityFunction, Max, Min
 )
 from sympy.functions.elementary.complexes import periodic_argument
 from sympy.integrals.risch import NonElementaryIntegral
@@ -529,6 +529,20 @@ def test_integrate_returns_piecewise():
         (z, Eq(y,0)), (exp(y*z)/y - 1/y, True))
 
 
+def test_max_min():
+    assert integrate(Min(x, 2), (x, 0, 3)) == 4
+    assert integrate(Max(x**2, x**3), (x, 0, 2)) == S(49)/12
+    assert integrate(Min(exp(x), exp(-x))**2, x) == Piecewise( \
+        (exp(2*x)/2, x <= 0), (1 - exp(-2*x)/2, True))
+    # issue 7907
+    c = symbols('c', real=True)
+    int1 = integrate(Max(c, x)*exp(-x**2), (x, -oo, oo))
+    int2 = integrate(c*exp(-x**2), (x, -oo, c))
+    int3 = integrate(x*exp(-x**2), (x, c, oo))
+    assert int1 == int2 + int3 == sqrt(pi)*c*erf(c)/2 + \
+        sqrt(pi)*c/2 + exp(-c**2)/2
+
+
 def test_subs1():
     e = Integral(exp(x - y), x)
     assert e.subs(y, 3) == Integral(exp(x - 3), x)