Skip to content

Commit

Permalink
Further improve betti_number
Browse files Browse the repository at this point in the history
- always return an fmpz, even for `i` negative or larger than 2 * dim(v)
- don't store the odd Betti numbers which are always 0
- use this to reduce the storage for Betti numbers by half
- use `get_attribute!` instead of `has_attribute` + `get_attribute`
- remove redundant call to `set_attribute!`
- don't call `dim(v)` repeatedly and add a type assertion
- initialize `f_vector` via a conversion, to ensure its type is inferred correctly
  • Loading branch information
fingolfin authored and HereAround committed Jan 3, 2022
1 parent 3b37893 commit eb3b079
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions src/ToricVarieties/NormalToricVarieties/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,27 @@
@doc Markdown.doc"""
betti_number(v::AbstractNormalToricVariety, i::Int)
Compute the i-th Betti number of the normal toric variety `v`.
Compute the `i`-th Betti number of the normal toric variety `v`.
"""
function betti_number(v::AbstractNormalToricVariety, i::Int)
# check input
if i > 2*dim(v) || i < 0
return 0
d = dim(v)::Int
if i > 2*d || i < 0 || isodd(i)
return fmpz(0)
end

# extract vector of currently-known Betti numbers (or create it if necessary)
if !has_attribute(v, :betti_number)
betti_numbers = fill(fmpz(-1),2*dim(v)+1)
else
betti_numbers = get_attribute(v, :betti_number)::Vector{fmpz}
end

betti_numbers = get_attribute!(() -> fill(fmpz(-1),d+1), v, :betti_number)::Vector{fmpz}

# compute the Betti number if needed
if betti_numbers[i+1] == -1
if isodd(i)
betti_numbers[i+1] = fmpz(0)
else
k = div(i, 2)
f_vector = Vector{Int}(pm_object(v).F_VECTOR)
pushfirst!(f_vector, 1)
betti_numbers[i+1] = fmpz(sum((-1)^(i-k) * binomial(i,k) * f_vector[dim(v) - i + 1] for i=k:dim(v)))
end
set_attribute!(v, :betti_number, betti_numbers)
k = i >> 1 # i is even, so divide by two and use that as index
if betti_numbers[k+1] == -1
f_vector::Vector{Int} = pm_object(v).F_VECTOR
pushfirst!(f_vector, 1)
betti_numbers[k+1] = fmpz(sum((-1)^(i-k) * binomial(i,k) * f_vector[d - i + 1] for i=k:d))
end

# return result
return betti_numbers[i+1]
return betti_numbers[k+1]
end
export betti_number

0 comments on commit eb3b079

Please sign in to comment.