Repository: sympy/sympy

Issue:
Rewrite factorial as a product
It would be nice that if, say,
```python
>>> k = Symbol('k', integer=True, nonnegative=True)
```
then the following would hold
```python
>>> factorial(k).rewrite(product)
Product(i, (i, 1, k))
```
(for a dummy positive integer `i`)


Discussion:
skirpichev: Product, not product!
aktech: I think you mean : symbol 'i' (for dummy variable i)

as If I use  `i = Dummy('i')` , the output would look like this:
`Product(_i, (_i, 1, k))` 


skirpichev: > I think you mean : symbol

No.  I mean Dummy.
aktech: If I use i = Dummy('i') , the output would be `Product(_i, (_i, 1, k))'   (Notice _i)
and if I put that into tests that would give an Error.
skirpichev: You can compare e.g. strings.  See other similar tests in the function (e.g. Sum rewrite).
aktech: Oh! fine, that would be good.
Thanks.
pelegm: +1.
aktech: Fixed. 
@skirpichev 

Thanks

Patch:
diff --git a/sympy/functions/combinatorial/factorials.py b/sympy/functions/combinatorial/factorials.py
index dc2ed2b5b2..185ae0d56c 100644
--- a/sympy/functions/combinatorial/factorials.py
+++ b/sympy/functions/combinatorial/factorials.py
@@ -1,6 +1,6 @@
 from __future__ import print_function, division
 
-from sympy.core import S, C, sympify
+from sympy.core import S, C, sympify, Dummy
 from sympy.core.function import Function, ArgumentIndexError
 from sympy.core.logic import fuzzy_and
 from sympy.ntheory import sieve
@@ -164,6 +164,11 @@ def eval(cls, n):
     def _eval_rewrite_as_gamma(self, n):
         return C.gamma(n + 1)
 
+    def _eval_rewrite_as_Product(self, n):
+        if n.is_nonnegative and n.is_integer:
+            i = Dummy('i', integer=True)
+            return C.Product(i, (i, 1, n))
+
     def _eval_is_integer(self):
         if self.args[0].is_integer and self.args[0].is_nonnegative:
             return True


Test patch:
diff --git a/sympy/functions/combinatorial/tests/test_comb_factorials.py b/sympy/functions/combinatorial/tests/test_comb_factorials.py
index eba850cbb8..727c5fc999 100644
--- a/sympy/functions/combinatorial/tests/test_comb_factorials.py
+++ b/sympy/functions/combinatorial/tests/test_comb_factorials.py
@@ -1,6 +1,6 @@
 from sympy import (Symbol, symbols, factorial, factorial2, binomial,
                    rf, ff, gamma, polygamma, EulerGamma, O, pi, nan,
-                   oo, zoo, simplify, expand_func, C, S)
+                   oo, zoo, simplify, expand_func, C, S, Product)
 from sympy.functions.combinatorial.factorials import subfactorial
 from sympy.utilities.pytest import XFAIL, raises
 
@@ -146,8 +146,10 @@ def test_factorial_series():
 
 def test_factorial_rewrite():
     n = Symbol('n', integer=True)
+    k = Symbol('k', integer=True, nonnegative=True)
 
     assert factorial(n).rewrite(gamma) == gamma(n + 1)
+    assert str(factorial(k).rewrite(Product)) == 'Product(_i, (_i, 1, k))'
 
 
 def test_factorial2():