Nemocas/AbstractAlgebra.jl

Usability of non-standard index ranges in variables of polynomial rings

joschmitt opened this issue · 7 comments

This came up in the discussion of #1360 ; requires either #1360 or Oscar:

julia> R, x = polynomial_ring(ZZ, :x => (-3:4));

julia> x[1]
x[-3]

julia> x[5]
x[1]

So x[5] gives x[1], but if I type x[1] I get x[-3]...
@fingolfin explained in #1360 (comment) what would be necessary to make indexing by -3 etc. work. To me personally, this sounds like 'too much magic' and would require constant effort to make new functions fit in the pattern (see point 3 and 4 on the list).

I think the main problem is that the variables print like something one should be able to type in the REPL, but when one does one gets unexpected results. So maybe we should print the variables differently? Like x(-3)? x_{-3}? I feel like we should at least comment on this odd behaviour in the documentation somewhere.

In general, I am wondering what the use of index ranges other than 1:n is. I would think they are mainly so that I can type in the variable names as in some paper without needing to think. But as soon as I have to access anything, I have to think again. So is it just to make the output look nice? Note that the @polynomial_ring macro introduced in #1360 cannot circumvent this because it cannot produce variable names with illegal characters (like -).

I think the main problem is that the variables print like something one should be able to type in the REPL, but when one does one gets unexpected results.

Indeed, using a plain "x" instead of "x#" only prints sensible variable names if we use standard indexing. We could give a warning in the other cases.

So maybe we should print the variables differently? Like x(-3)? x_{-3}?

If you want variables showing as x_{-3} you can already use x_{#}. At least if you have only one index. If you need this for more indices, tell us and we can support it. But until complex use cases show up, we decided to keep our interface simple.

In general, I am wondering what the use of index ranges other than 1:n is.

They are mainly for interactive usage and then typically should be created with R = @polynomial_ring(ZZ, "x#" => [-3,1,3,7]), so you can access them as var"x-3", x1, x3, and x7.

Note that the @polynomial_ring macro introduced in #1360 cannot circumvent this because it cannot produce variable names with illegal characters (like -).

Ways to circumvent this that I see are …

  1. use var"x-3";
  2. magically replace - by something else like _, m, or minus with all the downsides of magic usage;
  3. manually replace - by something else, like in @polynomial_ring(ZZ, "x#" => ["minus3", 0, 3]);
  4. provide some other access method like R("x-3").

Probably this is a non-issue because anybody can use "x#" etc.
To be clear: I don't really want variables to print like x_{3}, I just think they should not print by default in a way that one could type in the REPL and will produce different results.
I did not know of this var"x-3" option and it does not really sound like a good solution to me, to be honest (but probably it is the best one can do).

Anyway, I guess it would be more helpful if people who want to index variables by non-negative (or non-consecutive) numbers, would state what they want or need.

Fine, I added a warning. So your example now yields

julia> R, x = polynomial_ring(ZZ, :x => (-3:4));
┌ Warning: Indexing with -3:4 can lead to confusion, since the entry at index 1 gets printed as being at index -3.
│ Consider using `"x#" => axes` syntax or communicating your use case to the Oscar community.
└ @ AbstractAlgebra ~/.julia/dev/AbstractAlgebra/src/misc/VarNames.jl:121

Does this “fix” your issue?

Does this “fix” your issue?

Fine by me. I don't have the rights to properly link the issue in the pull request.

fieker commented
thofma commented

Agree with Claus. Either it is an error or quiet.

For now, we went back to the old behavior. Throwing errors, would mean to have no proper way to display x[1,-1] or x[1,11] at all.

If we change printing later, using $\LaTeX$ syntax seems to be the cleanest thing for me.