Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement equality for toric varieties #4509

Merged
merged 10 commits into from
Feb 1, 2025
29 changes: 4 additions & 25 deletions docs/src/AlgebraicGeometry/ToricVarieties/NormalToricVarieties.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,10 @@ the affine and non-affine case:

## Equality of Normal Toric Varieties

!!! warning
Equality `==` of normal toric varieties currently checks equality of
memory locations. We recommend using `===`, which always checks
equality of memory locations in OSCAR.

To check that the fans considered as sets of cones are equal, you may use the method below:

```julia
function slow_equal(tv1::NormalToricVariety, tv2::NormalToricVariety)
tv1 === tv2 && return true
ambient_dim(tv1) == ambient_dim(tv2) || return false
f_vector(tv1) == f_vector(tv2) || return false
return Set(maximal_cones(tv1)) == Set(maximal_cones(tv2))
end
```

Polyhedral fans can be stored in Polymake using either rays or
hyperplanes. In the former case, cones are stored as sets of indices of
rays, corresponding to polyhedral hulls, while in the latter case, cones
are stored as sets of indices of hyperplanes, corresponding to
intersections of hyperplanes. Converting between these two formats can
be expensive. In the case where the polyhedral fans of two normal toric
varieties are both stored using rays, the above method `slow_equal` has
computational complexity $O(n \log n)$, where $n$ is the number of
cones.
Equality `==` of normal toric varieties checks equality of the
polyhedral fans (sets of cones). This computes the rays of both of the
toric varieties, which can be expensive if they are not already computed.
Triple-equality `===` always checks equality of memory locations in OSCAR.


## Constructors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,50 @@ end
# Equality
######################

function Base.:(==)(tv1::NormalToricVariety, tv2::NormalToricVariety)
tv1 === tv2 && return true
error("Equality of normal toric varieties is computationally very demanding. More details in the documentation.")
end
@doc raw"""
(==)(X::NormalToricVariety, Y::NormalToricVariety)
paemurru marked this conversation as resolved.
Show resolved Hide resolved

function Base.hash(tv::NormalToricVariety, h::UInt)
return hash(objectid(tv), h)
end
Checks equality of the polyhedral fans as sets of cones.

# Examples
```jldoctest
julia> H = hirzebruch_surface(NormalToricVariety, 0)
Normal toric variety

julia> P1 = projective_space(NormalToricVariety, 1)
Normal toric variety

julia> H == P1 * P1
true
paemurru marked this conversation as resolved.
Show resolved Hide resolved
```
"""
function Base.:(==)(X::NormalToricVariety, Y::NormalToricVariety)
X === Y && return true
ambient_dim(X) == ambient_dim(Y) || return false
n_rays(X) == n_rays(Y) || return false

# p is a permutation such that the i-th ray of X is the p(i)-th ray of Y
p = inv(perm(sortperm(rays(X)))) * perm(sortperm(rays(Y)))

for i in 1:n_rays(X)
rays(X)[i] == rays(Y)[p(i)] || return false
end
@inline rows(Z) = [row(maximal_cones(IncidenceMatrix, Z), i) for i in 1:n_maximal_cones(Z)]
return Set(map(r -> Set(p.(r)), rows(X))) == Set(rows(Y))
end

# @doc raw"""
# hash(X::NormalToricVariety, h::UInt)
#
# Computes a hash of a normal toric variety `X` with the property that, outside of hash collisions, `X_1 == X_2` if and only if `hash(X_1) == hash(X_2)`.
# """
function Base.hash(X::NormalToricVariety, h::UInt)
p = inv(perm(sortperm(rays(X))))
@inline rows(Z) = [row(maximal_cones(IncidenceMatrix, Z), i) for i in 1:n_maximal_cones(Z)]
set_of_maximal_cones = Set(map(r -> Set(p.(r)), rows(X)))
sorted_rays = Vector.([rays(X)[p(i)] for i in 1:n_rays(X)])
paemurru marked this conversation as resolved.
Show resolved Hide resolved
return hash((sorted_rays, set_of_maximal_cones), h)
end

######################
# Display
Expand Down
15 changes: 14 additions & 1 deletion test/AlgebraicGeometry/ToricVarieties/normal_toric_varieties.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@
@testset "Equality of normal toric varieties" begin
@test (p2 === f2) == false
@test p2 === p2
@test_throws ErrorException("Equality of normal toric varieties is computationally very demanding. More details in the documentation.") p2 == f2
@test p2 != f2

X = projective_space(NormalToricVariety, 2)
X = domain(blow_up(X, [3, 4]))
X = domain(blow_up(X, [-2, -3]))
Y = weighted_projective_space(NormalToricVariety, [1, 2, 3])
Y = domain(blow_up(Y, [-1, -1]))
Y = domain(blow_up(Y, [3, 4]))
@test X == Y

Z = projective_space(NormalToricVariety, 2)
X = domain(blow_up(Z, [1, 1]))
Y = domain(blow_up(Z, [1, 2]))
@test X != Y
paemurru marked this conversation as resolved.
Show resolved Hide resolved
end
end
Loading