Skip to content

Commit 42e6e68

Browse files
YueRenafkafkafk13
andauthored
Fix attribute handling in tropical linear spaces (#4792)
* TropicalGeometry: stable_intersection, minor optimization moving declaration of variables where they are needed * TropicalGeometry: TropicalLinearSpace, changing handling of attributes * Update src/TropicalGeometry/linear_space.jl Co-authored-by: Anne Frühbis-Krüger <[email protected]> --------- Co-authored-by: Anne Frühbis-Krüger <[email protected]>
1 parent 880fb10 commit 42e6e68

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

src/TropicalGeometry/intersection.jl

+2-3
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@ end
108108

109109
function stable_intersection(TropL1::TropicalLinearSpace{minOrMax,true}, TropL2::TropicalLinearSpace{minOrMax,true}) where minOrMax
110110

111-
plueckerIndices12 = Vector{Int}[]
112-
plueckerVector12 = TropicalSemiringElem{minOrMax}[]
113-
114111
d = dim(TropL1)-codim(TropL2)
115112
if d<=0
116113
# return empty polyhedral complex
@@ -119,6 +116,8 @@ function stable_intersection(TropL1::TropicalLinearSpace{minOrMax,true}, TropL2:
119116
return tropical_linear_space(Sigma,mults,convention(TropL1))
120117
end
121118

119+
plueckerIndices12 = Vector{Int}[]
120+
plueckerVector12 = TropicalSemiringElem{minOrMax}[]
122121
for (I1,p1) in zip(pluecker_indices(TropL1),tropical_pluecker_vector(TropL1))
123122
for (I2,p2) in zip(pluecker_indices(TropL2),tropical_pluecker_vector(TropL2))
124123
I12 = intersect(I1,I2)

src/TropicalGeometry/linear_space.jl

+31-33
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ Min tropical linear space
193193
```
194194
"""
195195
function tropical_linear_space(k::Int, n::Int, plueckerVector::Vector, nu::TropicalSemiringMap=tropical_semiring_map(parent(first(plueckerVector))); weighted_polyhedral_complex_only::Bool=false)
196-
TropL = tropical_linear_space(AbstractAlgebra.combinations(1:n,k), nu.(plueckerVector), weighted_polyhedral_complex_only=weighted_polyhedral_complex_only)
196+
TropL = tropical_linear_space(AbstractAlgebra.combinations(n,k), nu.(plueckerVector), weighted_polyhedral_complex_only=weighted_polyhedral_complex_only)
197197

198198
if !weighted_polyhedral_complex_only
199199
set_attribute!(TropL,:algebraic_pluecker_vector,plueckerVector)
@@ -209,7 +209,7 @@ function compute_pluecker_indices_and_vector(A::MatElem)
209209
n = ncols(A)
210210
k = nrows(A)
211211
@req n>=k "matrix for Pluecker vector cannot have more rows than columns"
212-
plueckerIndices = AbstractAlgebra.combinations(1:n,k)
212+
plueckerIndices = AbstractAlgebra.combinations(n,k)
213213
plueckerVector = AbstractAlgebra.minors(A,k)
214214
nonZeroIndices = findall(!iszero,plueckerVector)
215215
plueckerIndices = plueckerIndices[nonZeroIndices]
@@ -393,26 +393,22 @@ end
393393
pluecker_indices(TropL::TropicalLinearSpace)
394394
395395
Return the Pluecker indices used to construct `TropL`.
396+
Raises an error if neither it, nor any relevant algebraic data nor a tropical matrix is cached.
396397
"""
397398
@attr Vector{Vector{Int}} function pluecker_indices(TropL::TropicalLinearSpace)
398-
# there are two ways to compute the pluecker indices:
399-
# - from the algebraic matrix
400-
# - from the tropical matrix
401-
# (we generally assume that a tropical linear space has at most one of the two)
402-
403-
# check if tropical matrix is available and, if yes, compute the pluecker indices from it
404-
if has_attribute(TropL,:tropical_matrix)
405-
A = tropical_matrix(TropL)
399+
# if any relevant algebraic data is cached, use it to construct the Pluecker indices
400+
if any(has_attribute.(Ref(TropL),[:algebraic_matrix, :algebraic_ideal]))
401+
A = algebraic_matrix(TropL)
406402
plueckerIndices, plueckerVector = compute_pluecker_indices_and_vector(A)
407-
set_attribute!(TropL, :tropical_pluecker_vector, plueckerVector)
403+
set_attribute!(TropL, :algebraic_pluecker_vector, plueckerVector)
408404
return plueckerIndices
409405
end
410406

411-
# otherwise, get the algebraic matrix and compute the pluecker indices from it
412-
# (will rightfully error if algebraic matrix cannot be found or computed)
413-
A = algebraic_matrix(TropL)
407+
# otherwise use the tropical matrix to construct the Pluecker indices
408+
# (will rightfully error if tropical matrix not cached)
409+
A = tropical_matrix(TropL)
414410
plueckerIndices, plueckerVector = compute_pluecker_indices_and_vector(A)
415-
set_attribute!(TropL, :algebraic_pluecker_vector, plueckerVector)
411+
set_attribute!(TropL, :tropical_pluecker_vector, plueckerVector)
416412
return plueckerIndices
417413
end
418414

@@ -421,8 +417,18 @@ end
421417
tropical_pluecker_vector(TropL::TropicalLinearSpace)
422418
423419
Return the tropical Pluecker vector of `TropL`.
420+
Reises an error if neither it, nor any algebraic data nor a tropical matrix is cached.
424421
"""
425422
@attr Vector function tropical_pluecker_vector(TropL::TropicalLinearSpace)
423+
# if any algebraic data is cached, use it to compute the tropical Pluecker vector.
424+
if any(has_attribute.(Ref(TropL),[:algebraic_pluecker_vector, :algebraic_matrix, :algebraic_ideal]))
425+
algebraicPlueckerVector = algebraic_pluecker_vector(TropL)
426+
nu = tropical_semiring_map(TropL)
427+
return nu.(algebraicPlueckerVector)
428+
end
429+
430+
# otherwise use tropical matrix to construct the tropical Pluecker vector
431+
# (will rightfully error if tropical matrix not cached)
426432
A = tropical_matrix(TropL)
427433
plueckerIndices, plueckerVector = compute_pluecker_indices_and_vector(A)
428434
set_attribute!(TropL, :pluecker_indices, plueckerIndices)
@@ -434,6 +440,7 @@ end
434440
algebraic_pluecker_vector(TropL::TropicalLinearSpace)
435441
436442
Return the Pluecker vector over a valued field used to construct `TropL`.
443+
Raises an error, if neither it nor an algebraic matrix nor an algebraic ideal is cached.
437444
"""
438445
@attr Vector function algebraic_pluecker_vector(TropL::TropicalLinearSpace)
439446
A = algebraic_matrix(TropL)
@@ -448,9 +455,8 @@ end
448455
449456
Return the tropical semiring map used to construct `TropL`. Raises an error, if it is not cached.
450457
"""
451-
function tropical_semiring_map(TropL::TropicalLinearSpace)
452-
@req has_attribute(TropL,:tropical_semiring_map) "no tropical semiring map cached"
453-
return get_attribute(TropL, :tropical_semiring_map)::TropicalSemiringMap
458+
@attr TropicalSemiringMap function tropical_semiring_map(TropL::TropicalLinearSpace)
459+
error("no tropical semiring map cached")
454460
end
455461

456462

@@ -459,27 +465,20 @@ end
459465
460466
Return the tropical matrix used to construct `TropL`. Raises an error, if it is not cached.
461467
"""
462-
function tropical_matrix(TropL::TropicalLinearSpace{minOrMax}) where minOrMax
463-
if has_attribute(TropL,:tropical_matrix)
464-
return get_attribute(TropL, :tropical_matrix)::dense_matrix_type(TropicalSemiringElem{minOrMax})
465-
end
466-
@req has_attribute(TropL,:algebraic_matrix) && has_attribute(TropL,:tropical_semiring_map) "neither tropical matrix nor algebraic matrix and tropical semiring map cached"
467-
A = algebraic_matrix(TropL)
468-
nu = tropical_semiring_map(TropL)
469-
return nu.(A)
468+
@attr MatElem function tropical_matrix(TropL::TropicalLinearSpace)
469+
error("no tropical matrix cached")
470470
end
471471

472472

473473
@doc raw"""
474474
algebraic_matrix(TropL::TropicalLinearSpace)
475475
476476
Return the matrix over a valued field used to construct `TropL`.
477+
Raises an error, if neither it nor an algebraic ideal is cached.
477478
"""
478479
@attr MatElem function algebraic_matrix(TropL::TropicalLinearSpace)
479-
if has_attribute(TropL,:algebraic_matrix)
480-
return get_attribute(TropL, :algebraic_matrix)::MatElem
481-
end
482-
@req has_attribute(TropL,:algebraic_ideal) "neither algebraic matrix nor algebraic ideal cached"
480+
# use algebraic ideal to construct the algebraic matrix
481+
# (will rightfully error if algebraic ideal not defined)
483482
I = algebraic_ideal(TropL)
484483
return basis_of_vanishing_of_linear_ideal(I)
485484
end
@@ -491,7 +490,6 @@ end
491490
Return the polynomial ideal over a valued field used to construct `TropL`.
492491
Raises an error, if it is not cached.
493492
"""
494-
function algebraic_ideal(TropL::TropicalLinearSpace)
495-
@req has_attribute(TropL,:algebraic_ideal) "no algebraic ideal cached"
496-
return get_attribute(TropL, :algebraic_ideal)::MPolyIdeal
493+
@attr MPolyIdeal function algebraic_ideal(TropL::TropicalLinearSpace)
494+
error("no algebraic ideal cached")
497495
end

0 commit comments

Comments
 (0)