Discussion and poll: should Sage Integers have a backslash operator?

56 views
Skip to first unread message

John H Palmieri

unread,
Sep 27, 2023, 2:32:30 PM9/27/23
to sage-devel
The github issue #36060 (https://github.com/sagemath/sage/issues/36060) proposes adding a backslash operator for Sage integers, so that "2 \ 3" will return the same as "3 / 2". Do you support this?

BackslashOperator is defined in "sage/misc/misc.py", and the preparser converts "A \ b" to the appropriate Python code. The docstring for BackslashOperator says "Implements Matlab-style backslash operator for solving systems A \ b".

It seems pretty innocuous to me — in fact I don't care much either way — but since Sage integers are so ubiquitous, it seems like a good idea to ask before implementing it.

--
John

David Joyner

unread,
Sep 27, 2023, 3:16:57 PM9/27/23
to sage-...@googlegroups.com
On Wed, Sep 27, 2023 at 2:32 PM John H Palmieri <jhpalm...@gmail.com> wrote:
The github issue #36060 (https://github.com/sagemath/sage/issues/36060) proposes adding a backslash operator for Sage integers, so that "2 \ 3" will return the same as "3 / 2". Do you support this?


I'm not for or against. However, I don't see the problem that implementing this "\" is going to solve. The ticket suggests that users will naturally type 2\3 instead of 3/2, if I am reading between the lines correctly. 
 
BackslashOperator is defined in "sage/misc/misc.py", and the preparser converts "A \ b" to the appropriate Python code. The docstring for BackslashOperator says "Implements Matlab-style backslash operator for solving systems A \ b".

It seems pretty innocuous to me — in fact I don't care much either way — but since Sage integers are so ubiquitous, it seems like a good idea to ask before implementing it.

--
John

--
You received this message because you are subscribed to the Google Groups "sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/3f157aa4-74cd-4c5c-bc76-f198c6417432n%40googlegroups.com.

Nils Bruin

unread,
Sep 27, 2023, 5:44:19 PM9/27/23
to sage-devel
I'm quite strongly against because it collides with a well-established meaning of `\` in python: escape character. It's used to avoid command termination by newline in things like formulas (as "\<newline", which gets parsed as whitespace rather than command termination outside of brackets).

I'm in fact rather shocked that `\` in sage already doesn't work as it's supposed to in python but instead gets substituted as "BackslashOperator()".

Searching the codebase currently only shows "_backslash_" implemented on matroid, matrix, and binary_tree, so extinguishing it should be doable. We should definitely not entrench its use further.

If you want to write your denominator first, you can already do ~2 * 3 . I think that's already sufficiently perverse that we don't need another way to spell that.

John H Palmieri

unread,
Sep 27, 2023, 6:09:19 PM9/27/23
to sage-devel
Okay, so maybe we should open this to other options: should we get rid of preparsing "\" into "BackslashOperator"?

For what it's worth, I removed the line defining `_backslash_` in binary_tree.py and and I only say one doctest failure in any obvious places (combinat and thematic_tutorials). So it's not even used much. Same for matroids.

I think that using \ to escape characters in strings still works, but line-continuation does not: in Sage, these fail:

    sage: a = 3 + \

and

    sage: \

whereas in pure Python, such backslashes would be treated as line-continuations. It should be possible to fix this while keeping the current preparser behavior for "\" in the middle of a line. Or as Nils proposes, getting rid of the current preparser behavior should also solve it.

William Stein

unread,
Sep 27, 2023, 6:19:16 PM9/27/23
to sage-...@googlegroups.com
Hi,

I'm the guilty party who added \ to Sage notation in the first place,
and I would definitely vote to *remove* it. I wish I had
never added it in the first place. Nils has some very good points!

In retrospect, I wish we had a "fully supported" way of using Sage
entirely without the preparser, but that's another discussion.
It's important in the longterm for interoperability with the rest of
the Python ecosystem. By "fully supported", I perhaps just
mean better documentation (e.g., a switch for the reference and other
docs that shows all examples in a way that works without
the preparser).

-- William
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/941e7314-5f78-454a-9017-fe752c630233n%40googlegroups.com.



--
William (http://wstein.org)

Michael Orlitzky

unread,
Sep 27, 2023, 7:58:14 PM9/27/23
to sage-...@googlegroups.com
On Wed, 2023-09-27 at 14:44 -0700, Nils Bruin wrote:

> Searching the codebase currently only shows "_backslash_" implemented on
> matroid, matrix, and binary_tree, so extinguishing it should be doable. We
> should definitely not entrench its use further.
>
> If you want to write your denominator first, you can already do ~2 * 3 . I
> think that's already sufficiently perverse that we don't need another way
> to spell that.

To keep the flamebait to a minimum, I will just say: +1.

Dima Pasechnik

unread,
Sep 27, 2023, 8:21:17 PM9/27/23
to sage-...@googlegroups.com
I'd even say, to keep the back(s)lash to a minimum...
+1

>
> --
> You received this message because you are subscribed to the Google Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/a4d3e780ff4ba16eb2f11cc096b81816b72914aa.camel%40orlitzky.com.

kcrisman

unread,
Sep 28, 2023, 6:37:23 AM9/28/23
to sage-devel
On Wednesday, September 27, 2023 at 6:19:16 PM UTC-4 William Stein wrote:
Hi,

I'm the guilty party who added \ to Sage notation in the first place,
and I would definitely vote to *remove* it. I wish I had
never added it in the first place. Nils has some very good points!


I do think it's worth considering whether people outside of the doctests actually use the backslash operator in the Matlab way.  This is a well-known shortcut in a lot of the practical computational world, as I've seen when people have tried to type it in other contexts and wonder why it doesn't "just work".  For instance, one could look at any linear algebra textbooks that use Sage and see whether they use it.  Keeping the mission in mind!

That said, this is pretty clearly the only strong reason to implement it, to keep learning new syntax to a minimum for people coming from Matlab/Octave.  And maybe those people are not using Sage for this purpose, as it seems to focus more on advanced math(s) capabilities.  But one should at least raise the question, and if it's not actively hurting anything, then John's suggestion regarding adding the "usual" behavior outside of the very narrow matroid/ix-related contexts seems best.

Eric Gourgoulhon

unread,
Sep 28, 2023, 11:34:37 AM9/28/23
to sage-devel
+ 1 for getting rid of the preparsing of \

It is particularly annoying when typing (usually long) expressions like

f(x) = x + \ 
           1
in an input cell of a Jupyter notebook. If the \ is followed by a blank space, one gets 

Cell In [22], line 1 
__tmp__=var("x"); f = symbolic_expression(x + * BackslashOperator() * ).function(x)
SyntaxError: invalid syntax

which is particularly disturbing for a new user!

Eric

Reply all
Reply to author
Forward
0 new messages