oscar-system / Oscar.jl

A comprehensive open source computer algebra system for computations in algebra, geometry, and number theory.
https://www.oscar-system.org
Other
345 stars 126 forks source link

Unexpected behaviour: Polynomial rings with multi-index variable names. #3407

Closed HereAround closed 8 months ago

HereAround commented 8 months ago

Describe the bug

The following ring constructor should maybe not work, because the variables defined as return values (here "s", "t" and "u") cannot match the number of generators of the created ring R (6 indeterminates).

julia> R, (s, t, u) = polynomial_ring(QQ, "x" => (1:2, 1:3), "y" => 1:2, "z" => (1:1, 1:1, 1:1))
(Multivariate polynomial ring in 9 variables over QQ, QQMPolyRingElem[x[1, 1] x[1, 2] x[1, 3]; x[2, 1] x[2, 2] x[2, 3]], QQMPolyRingElem[y[1], y[2]], [z[1, 1, 1];;;])

(Aside: The output looks weird and includes a triple semicolon. Of course, we should of course not fumble around with the printing at this point in time. Still, maybe at a future stage, it could be checked if this is truly necessary? Julia weirdness?)

It is surprising that this input is accepted, since there are 9 variables in R but I only provide 3 julia names. So should have expected an error or a warning. But still... since this seems to work, I should expect that s is related to the first, t to the second and u to the third variable of R. Yet, here is what julia tells me:

julia> s
x[1, 1]

julia> t
x[2, 1]

julia> u
x[1, 2]

Is this desired?

@JohnAAbbott suggested the following constructor instead:

julia> R2, s, t, u = polynomial_ring(QQ, "x" => (1:2, 1:3), "y" => 1:2, "z" => (1:1, 1:1, 1:1))
(Multivariate polynomial ring in 9 variables over QQ, QQMPolyRingElem[x[1, 1] x[1, 2] x[1, 3]; x[2, 1] x[2, 2] x[2, 3]], QQMPolyRingElem[y[1], y[2]], [z[1, 1, 1];;;])

Then we find the following:

julia> s
2×3 Matrix{QQMPolyRingElem}:
 x[1, 1]  x[1, 2]  x[1, 3]
 x[2, 1]  x[2, 2]  x[2, 3]

julia> t
2-element Vector{QQMPolyRingElem}:
 y[1]
 y[2]

julia> u
1×1×1 Array{QQMPolyRingElem, 3}:
[:, :, 1] =
 z[1, 1, 1]

(u is a 1 by 1 by 1 array???).

This looks better and is sort-of what one can expect.

To Reproduce Steps to reproduce the behavior, please provide a code snippet that triggers the bug.

using Oscar
# execute the above

Expected behavior

System (please complete the following information): Please paste the output of Oscar.versioninfo(full=true) below. If this does not work, please paste the output of Julia's versioninfo() and your Oscar version.

julia> Oscar.versioninfo(full=true)
OSCAR version 1.0.0-DEV - #AddQSMs, edb952890a -- 2024-02-14 16:29:04 +0100
  combining:
    AbstractAlgebra.jl   v0.37.5
    GAP.jl               v0.10.2
    Hecke.jl             v0.27.0
    Nemo.jl              v0.41.3
    Polymake.jl          v0.11.12
    Singular.jl          v0.22.1
  building on:
    Antic_jll               v0.201.500+0
    Arb_jll                 v200.2300.0+0
    Calcium_jll             v0.401.100+0
    FLINT_jll               v200.900.9+0
    GAP_jll                 v400.1200.200+7
    Singular_jll            v403.214.1400+0
    libpolymake_julia_jll   v0.11.2+0
    libsingular_julia_jll   v0.40.11+0
    polymake_jll            v400.1100.1+0
See `]st -m` for a full list of dependencies.

Julia Version 1.9.2
Commit e4ee485e909 (2023-07-05 09:39 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i7-11370H @ 3.30GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, tigerlake)
  Threads: 1 on 8 virtual cores
Official https://julialang.org/ release

(@JohnAAbbott also tried with OSCAR 0.15.0).

Additional context

Appeared (or currently still does) in one tutorial.

lgoettgens commented 8 months ago

All of this seems right to me. polynomial_ring(QQ, "x" => (1:2, 1:3), "y" => 1:2, "z" => (1:1, 1:1, 1:1)) returns 4 things: the ring, the x vars, the y vars, the z vars. Due to how julia handles tuple unpacking, you ask it only for two things, where the first one gets saved in R and the second one gets further unpacked and the first three things get saved in s, t, u. julia considers it a feature instead of a bug to discard excess objects in tuple unpacking.

the first three indices of the x matrix are indeed (1,1), (2,1) and (1,2) (it would continue with (2,2), (1,3), (2,3)). After all, this is a julia matrix which has column-major indexing.

JohnAAbbott commented 8 months ago

Ah! One does need time to get used to certain "features" of Julia ;-) Thanks for the clarification. When we have time, we'll revise the tutorial about creating polynomial rings. The behaviour of the function polynomial_ring can be confusing.