[sympy] add subfactorial function (#1692)

17 views
Skip to first unread message

Christopher Smith

unread,
Dec 14, 2012, 11:00:45 PM12/14/12
to sympy/sympy

Why doesn't this get loaded into the sympy namespace along with all the other factorial frunctions? I don't understand how the __init__ file is working in this module.


You can merge this Pull Request by running:

  git pull https://github.com/smichr/sympy subfact

Or view, comment on, or merge it at:

  https://github.com/sympy/sympy/pull/1692

Commit Summary

  • add subfactorial function

File Changes

  • M sympy/functions/combinatorial/factorials.py (62)
  • M sympy/functions/combinatorial/tests/test_comb_factorials.py (10)
  • M sympy/utilities/iterables.py (4)

Patch Links


Reply to this email directly or view it on GitHub.

Aaron Meurer

unread,
Dec 15, 2012, 1:04:25 AM12/15/12
to sympy/sympy

In sympy/functions/combinatorial/factorials.py:

> @@ -161,6 +163,64 @@ class MultiFactorial(CombinatorialFunction):
>      pass
>  
>  
> +class subfactorial(CombinatorialFunction):
> +    """The subfactorial counts the derangements of n items and is
> +    defined for non-negative integers as::
> +
> +                 ,
> +                |  1                             for n = 0
> +        !n =    |  0                             for n = 1

This should be a { or < (and by the way, pprint(Piecewise, use_unicode=False) looks worse than this).

Aaron Meurer

unread,
Dec 15, 2012, 1:05:58 AM12/15/12
to sympy/sympy

You need to add it to sympy/functions/__init__.py. I also don't understand the purpose of the sympy/functions/combinatorial/__init__.py function.

Aaron Meurer

unread,
Dec 15, 2012, 1:07:17 AM12/15/12
to sympy/sympy

Add the standard links to Wikipedia and/or MathWorld (or any other good reference that you know of). The Wikipedia article seems to be pretty good.

Aaron Meurer

unread,
Dec 15, 2012, 1:08:42 AM12/15/12
to sympy/sympy

In sympy/functions/combinatorial/factorials.py:

> +    @classmethod
> +    def eval(cls, arg):
> +        try:
> +            arg = as_int(arg)
> +            if arg < 0:
> +                raise ValueError
> +            return C.Integer(cls._eval(arg))
> +        except ValueError:
> +            if sympify(arg).is_Number:
> +                raise ValueError("argument must be a nonnegative integer")
> +
> +    def _sympystr(self, p):
> +        if self.args[0].is_Atom:
> +            return "!%s" % p.doprint(self.args[0])
> +        else:
> +            return "!(%s)" % p.doprint(self.args[0])

This needs other printers too, but I wonder if you can somehow just set the function name to ! somewhere, and have all printers automatically work.

Aaron Meurer

unread,
Dec 15, 2012, 1:10:27 AM12/15/12
to sympy/sympy

In sympy/functions/combinatorial/factorials.py:

> +    @classmethod
> +    def eval(cls, arg):
> +        try:
> +            arg = as_int(arg)
> +            if arg < 0:
> +                raise ValueError
> +            return C.Integer(cls._eval(arg))
> +        except ValueError:
> +            if sympify(arg).is_Number:
> +                raise ValueError("argument must be a nonnegative integer")
> +
> +    def _sympystr(self, p):
> +        if self.args[0].is_Atom:
> +            return "!%s" % p.doprint(self.args[0])
> +        else:
> +            return "!(%s)" % p.doprint(self.args[0])

Except it doesn't use parentheses for atom arguments. So you still have to special case it. So please add the other printers. You need pretty print (ascii and unicode, should be the same) and LaTeX (and any others that might be useful).

Aaron Meurer

unread,
Dec 15, 2012, 10:08:47 PM12/15/12
to sympy/sympy

SymPy Bot Summary: :red_circle: Failed after merging smichr/subfact (afec347) into master (14452a4).
@smichr: Please fix the test failures.
:red_circle:Python 2.5.0-final-0: fail
:red_circle:Python 2.6.6-final-0: fail
:red_circle:Python 2.7.2-final-0: fail
:red_circle:Python 2.6.8-final-0: fail
:red_circle:Python 2.7.3-final-0: fail
:red_circle:PyPy 2.0.0-beta-1; 2.7.3-final-42: fail
:red_circle:Python 3.2.2-final-0: fail
:red_circle:Python 3.3.0-final-0: fail
:red_circle:Python 3.2.3-final-0: fail
:red_circle:Python 3.3.0-final-0: fail
:red_circle:Python 3.3.0-final-0: fail
:eight_spoked_asterisk:**Sphinx 1.1.3:** pass

Aaron Meurer

unread,
Dec 18, 2012, 1:36:34 AM12/18/12
to sympy/sympy

SymPy Bot Summary: :red_circle: Failed after merging smichr/subfact (797dd34) into master (501534b).


@smichr: Please fix the test failures.
:red_circle:Python 2.5.0-final-0: fail
:red_circle:Python 2.6.6-final-0: fail
:red_circle:Python 2.7.2-final-0: fail
:red_circle:Python 2.6.8-final-0: fail
:red_circle:Python 2.7.3-final-0: fail
:red_circle:PyPy 2.0.0-beta-1; 2.7.3-final-42: fail
:red_circle:Python 3.2.2-final-0: fail
:red_circle:Python 3.3.0-final-0: fail
:red_circle:Python 3.2.3-final-0: fail
:red_circle:Python 3.3.0-final-0: fail
:red_circle:Python 3.3.0-final-0: fail
:eight_spoked_asterisk:**Sphinx 1.1.3:** pass


Reply to this email directly or view it on GitHub.

Christopher Smith

unread,
Dec 18, 2012, 6:02:02 AM12/18/12
to sympy/sympy

@asmeurer , printers and reference has been added.

Aaron Meurer

unread,
Dec 18, 2012, 4:42:12 PM12/18/12
to sympy/sympy

In sympy/functions/combinatorial/factorials.py:

>  class factorial2(CombinatorialFunction):
>      """The double factorial n!!, not to be confused with (n!)!
>  
>      The double factorial is defined for integers >= -1 as::
>  
> -                 ,
> -                |  n*(n - 2)*(n - 4)* ... * 1    for n odd
> -        n!! =  -|  n*(n - 2)*(n - 4)* ... * 2    for n even
> -                |  1                             for n = 0, -1
> -                 '
> +                {  n*(n - 2)*(n - 4)* ... * 1    for n odd
> +        n!! =   {  n*(n - 2)*(n - 4)* ... * 2    for n even

That's not quite what I meant. I was thinking more

  ,
 |
 {
 |
  '

Really, though, none of them look very good. But we really should figure out what looks the best, and change the Piecewise ASCII pretty printer to use it.

Christopher Smith

unread,
Dec 19, 2012, 1:55:43 AM12/19/12
to sympy/sympy

I modified the docstring representation of the piecewise definition.

Julien Rioux

unread,
Dec 19, 2012, 11:28:45 AM12/19/12
to sympy/sympy

SymPy Bot Summary: :eight_spoked_asterisk: Passed after merging smichr/subfact (dda5947) into master (d503614).
:eight_spoked_asterisk:PyPy 2.0.0-beta-1; 2.7.3-final-42: pass
:eight_spoked_asterisk:Python 2.7.2-final-0: pass
:eight_spoked_asterisk:Python 3.2.1-final-0: pass
:eight_spoked_asterisk:Sphinx 1.1.3: pass
Docs build command: make clean && make html-errors && make latex && cd _build/latex && xelatex sympy-*.tex

Aaron Meurer

unread,
Dec 19, 2012, 4:04:12 PM12/19/12
to sympy/sympy

This looks fine.

Christopher Smith

unread,
Dec 19, 2012, 4:10:19 PM12/19/12
to sympy/sympy

Thanks for looking this over. It's in.

Christopher Smith

unread,
Dec 24, 2012, 4:01:25 AM12/24/12
to sympy/sympy

Merged #1692.

Reply all
Reply to author
Forward
0 new messages