factorial

68 views
Skip to first unread message

Henri Girard

unread,
Oct 28, 2020, 8:56:24 AM10/28/20
to sage-s...@googlegroups.com
Hi,

I would like to know if it is possible to use ! instead factorial like
in maths ?

factorial(1/2) = (1/2)!

Any help welcome

Henri

Emmanuel Charpentier

unread,
Oct 28, 2020, 1:29:09 PM10/28/20
to sage-support
Nope. This syntactic sugar is provided by `Maxima`'s and `Mathematica`'s readers, but not by Sage preparser.

BTW, I'd write `gamma(3/2)` rather than `factorial(/2)`...

Simon King

unread,
Nov 29, 2020, 12:10:16 PM11/29/20
to sage-s...@googlegroups.com
Hi Emmanuel,

On 2020-10-28, Emmanuel Charpentier <emanuel.c...@gmail.com> wrote:
> Nope. This syntactic sugar is provided by `Maxima`'s and `Mathematica`'s
> readers, but not by Sage preparser.

Would it be nice (and easy) to have in Sage? What prevents the preparser
from understanding "!"?

Best regards,
Simon

Simon King

unread,
Nov 29, 2020, 1:32:30 PM11/29/20
to sage-s...@googlegroups.com
To be more precise: If I understand correctly, the preparser can be made
to accept any syntax, provided that this syntax is in no conflict with
valid Python syntax.

There is a special meaning of "?" and "??" in Python, but I think there
isn't a special meaning to "!". Sage accepts "!" at the beginning of a
line, meaning that it executes a shell command, such as
sage: !ls
But it should be possible that the preparser could turn "(expression)!" into
"gamma(expression+1)".

Do people agree? Unfortunately I do not volonteer to implement it, I'
not familiar with the innards of the preparser.

Best regards,
Simon

Nils Bruin

unread,
Nov 29, 2020, 9:02:03 PM11/29/20
to sage-support
With the current regexp-based rewriting we'd need a pattern of the kind '<argument>!' -> 'factorial(<argument)'. That will be very tricky to do. We are doing an operator substitution already '^' -> '**" but that's very basic and doesn't need any context. To change an implicit unary postfix operator to an explicitly parenthesized prefix operator need almost complete parsing. Had the factorial been explicitly parenthesized as well, it would be a little easier, since now you could do it with parenthesis counting -- still not regex territory, but possibly reasonably efficiently done if one concentrates on the case where the expected string before the ! is short.

Insisting on parens does not satisfy the people who'd request this, I imagine. Writing (5)! instead of 5! doesn't really do the trick.

I consider this weird postfix notation as a peculiarity that works somewhat OK in written math but, just as implicit multiplication, as bad in CAS use. So I'm not in favour of it. I estimate that the number of arguments against it is at least 3!

Simon King

unread,
Nov 29, 2020, 9:13:54 PM11/29/20
to sage-s...@googlegroups.com
Hi Nils,

fair enough. I didn't deeply think about it, my naive impression was
that what the preparser does to
R.<a,b> = QQ[]
is at least as complicated as dealing with the exclamation mark. But
I guess you're right: It isn't.

Best regards,
Simon

G. M.-S.

unread,
Nov 29, 2020, 9:23:36 PM11/29/20
to sage-s...@googlegroups.com

Hi Nils.

I agree with you, but at the same time I wonder what would be wrong with replacing
'!' → '.factorial()'

Best regards,

Guillermo

--
You received this message because you are subscribed to the Google Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/0f70ad8a-bf21-4352-a863-0b8d596e509cn%40googlegroups.com.

slelievre

unread,
Nov 29, 2020, 11:48:27 PM11/29/20
to sage-support
2020-11-29 21:23:36 UTC, Guillermo:
>
> I wonder what would be wrong with replacing '!' → '.factorial()'

Interesting idea. Just be careful with `!=` of course.

Nils Bruin

unread,
Nov 30, 2020, 8:42:39 AM11/30/20
to sage-support
On Sunday, November 29, 2020 at 1:23:36 PM UTC-8 list...@gmail.com wrote:

I agree with you, but at the same time I wonder what would be wrong with replacing
'!' → '.factorial()'

Good observation: python already has postfix operators! I still don't like factorial notation; not even in math writing because it clashes with the custom of punctuation on math expressions that are part of a sentence, but technically that should cover most (all?) cases.

Vincent Delecroix

unread,
Nov 30, 2020, 9:16:37 AM11/30/20
to sage-s...@googlegroups.com
Indeed the expression "3!=3" is ambiguous... Of course one can choose
a priority (3!)=(3) or (3)!=(3) but that would be much more error
prone than using .factorial() from the start.

Dima Pasechnik

unread,
Nov 30, 2020, 12:37:08 PM11/30/20
to sage-support
On Mon, Nov 30, 2020 at 9:16 AM Vincent Delecroix
I think .factorial() will do. Perhaps this can be combined with
looking for deprecation issues vs factorial() and Python 3.9.

From python 3.9 docs:

Currently math.factorial() accepts float instances with non-negative
integer values (like 5.0).
It raises a ValueError for non-integral and negative floats. It is now
deprecated.
In future Python versions it will raise a TypeError for all floats.
(Contributed by Serhiy Storchaka in bpo-37315.)
https://bugs.python.org/issue37315

Samuel Lelièvre

unread,
Nov 30, 2020, 2:18:19 PM11/30/20
to Sage-support
Dealing with that deprecation is the object of:

- Sage Trac ticket 30764
Adapt to float factorial deprecation in Python 3.9
https://trac.sagemath.org/ticket/30764

in case anyone wants to give it a go.

Preparsing `!` to `.factorial()` is orthogonal enough
that I think it deserves its own ticket. I opened

- Sage Trac ticket 30982
Preparse ! for factorial
https://trac.sagemath.org/ticket/30982
Reply all
Reply to author
Forward
0 new messages