2626# https://www.gnu.org/licenses/
2727# ****************************************************************************
2828
29+ from sage .rings .infinity import infinity
30+ from sage .rings .rational_field import QQ
2931from sage .structure .sage_object import SageObject
3032from sage .structure .unique_representation import UniqueRepresentation
3133
32- from sage .rings .rational_field import QQ
33- from sage .rings .infinity import infinity
34-
3534
3635class TropicalVariety (UniqueRepresentation , SageObject ):
3736 r"""
@@ -188,10 +187,11 @@ def __init__(self, poly):
188187 """
189188 import operator
190189 from itertools import combinations
191- from sage .symbolic .ring import SR
192- from sage .symbolic .relation import solve
190+
193191 from sage .arith .misc import gcd
194192 from sage .rings .semirings .tropical_mpolynomial import TropicalMPolynomial
193+ from sage .symbolic .relation import solve
194+ from sage .symbolic .ring import SR
195195
196196 if not isinstance (poly , TropicalMPolynomial ):
197197 raise ValueError (f"{ poly } is not a multivariate tropical polynomial" )
@@ -448,9 +448,10 @@ def _components_intersection(self):
448448 5: [((0, t2, 0), {0 <= t2}), ((1/2*t2, t2, t2), {t2 <= 0})]}
449449 """
450450 import operator
451+
451452 from sage .functions .min_max import max_symbolic , min_symbolic
452- from sage .symbolic .relation import solve
453453 from sage .sets .set import Set
454+ from sage .symbolic .relation import solve
454455
455456 def update_result (result ):
456457 sol_param = solve (new_expr , vars )
@@ -592,19 +593,20 @@ def weight_vectors(self):
592593 sage: all(a == vector([0,0,0,0]) for a in [sum(lst) for lst in vec])
593594 True
594595 """
595- from sage .symbolic .ring import SR
596- from sage .symbolic .relation import solve
597- from sage .calculus .functional import diff
596+ from itertools import combinations
597+
598598 from sage .arith .misc import gcd
599+ from sage .calculus .functional import diff
599600 from sage .matrix .constructor import matrix
600601 from sage .modules .free_module_element import vector , zero_vector
601- from itertools import combinations
602+ from sage .symbolic .relation import solve
603+ from sage .symbolic .ring import SR
602604
603605 dim = self .dimension ()
604606 t = SR .var ('t' )
605- t_vars = [SR .var ('t{}' . format ( i ) ) for i in range (dim )]
606- u_vars = [SR .var ('u{}' . format ( i ) ) for i in range (dim )]
607- convert_tu = { ti : ui for ti , ui in zip (t_vars , u_vars )}
607+ t_vars = [SR .var (f 't{ i } ' ) for i in range (dim )]
608+ u_vars = [SR .var (f 'u{ i } ' ) for i in range (dim )]
609+ convert_tu = dict ( zip (t_vars , u_vars ))
608610 CI = self ._components_intersection ()
609611 unique_line = set ()
610612 index_line = {}
@@ -657,9 +659,9 @@ def weight_vectors(self):
657659 if is_unique :
658660 new_eqn = tuple ([eq .subs (convert_tu ) for eq in eqn ])
659661 cdns = line [1 ]
660- new_cdn = set ([ cdn .subs (convert_tu ) for cdn in cdns ])
662+ new_cdn = { cdn .subs (convert_tu ) for cdn in cdns }
661663 unique_line .add (new_eqn )
662- index_line [index ] = tuple ([ new_eqn , new_cdn ] )
664+ index_line [index ] = ( new_eqn , new_cdn )
663665 line_comps [index ] = [i ]
664666 index += 1
665667 else :
@@ -676,9 +678,7 @@ def weight_vectors(self):
676678 for v in l .variables ():
677679 all_var .add (v )
678680 for vpar in all_var :
679- par_drv = []
680- for l in line :
681- par_drv .append (QQ (diff (l , vpar )))
681+ par_drv = [QQ (diff (l , vpar )) for l in line ]
682682 par_drv = vector (par_drv )
683683 dir_vecs .append (par_drv )
684684
@@ -688,32 +688,24 @@ def weight_vectors(self):
688688 surface = self ._hypersurface [i ][0 ]
689689 drv_vectors = []
690690 for vpar in self ._vars :
691- temp_vec = []
692- for s in surface :
693- temp_vec .append (QQ (diff (s , vpar )))
691+ temp_vec = [QQ (diff (s , vpar )) for s in surface ]
694692 temp_vec = vector (temp_vec )
695693 drv_vectors .append (temp_vec )
696694 temp = [t_vars ]
697- for vec in drv_vectors :
698- temp .append (vec )
695+ temp .extend (drv_vectors )
699696 vec_matrix = matrix (SR , temp )
700697 normal_vec = vec_matrix .det ()
701- temp_nor = []
702- for tvar in t_vars :
703- temp_nor .append (QQ (diff (normal_vec , tvar )))
698+ temp_nor = [QQ (diff (normal_vec , tvar )) for tvar in t_vars ]
704699 normal_vec = vector (temp_nor )
705700 normal_vec *= 1 / gcd (normal_vec )
706701
707702 # Calculate the weight vector
708703 temp_final = [t_vars ]
709- for v in dir_vecs :
710- temp_final .append (v )
704+ temp_final .extend (dir_vecs )
711705 temp_final .append (normal_vec )
712706 vec_matrix = matrix (SR , temp_final )
713707 weight_vec = vec_matrix .det ()
714- temp_weight = []
715- for tvar in t_vars :
716- temp_weight .append (QQ (diff (weight_vec , tvar )))
708+ temp_weight = [QQ (diff (weight_vec , tvar )) for tvar in t_vars ]
717709 weight_vec = vector (temp_weight )
718710 order = self ._hypersurface [i ][2 ]
719711 weight_vec *= order
@@ -722,7 +714,7 @@ def weight_vectors(self):
722714 balance = False
723715 for i in range (1 , len (WV [k ])+ 1 ):
724716 for j in combinations (range (len (WV [k ])), i ):
725- test_vectors = [ v for v in WV [k ]]
717+ test_vectors = list ( WV [k ])
726718 for idx in j :
727719 test_vectors [idx ] = - test_vectors [idx ]
728720 if sum (test_vectors ) == zero_vector (QQ , dim ):
@@ -791,8 +783,8 @@ def _axes(self):
791783 sage: p2.tropical_variety()._axes()
792784 [[-1, 2], [-1, 2], [-1, 2]]
793785 """
794- from sage .symbolic .relation import solve
795786 from sage .arith .srange import srange
787+ from sage .symbolic .relation import solve
796788
797789 if not self ._hypersurface :
798790 return [[- 1 , 1 ], [- 1 , 1 ], [- 1 , 1 ]]
@@ -911,8 +903,8 @@ def _polygon_vertices(self):
911903 7: {(-1/2, -1, -1), (-1/2, 2, -1), (0, 0, 0), (0, 2, 0)},
912904 8: {(1, 1, 1), (1, 2, 1), (2, 1, 1), (2, 2, 1)}}
913905 """
914- from sage .symbolic .relation import solve
915906 from sage .sets .real_set import RealSet
907+ from sage .symbolic .relation import solve
916908
917909 poly_verts = {i : set () for i in range (self .number_of_components ())}
918910 axes = self ._axes ()
@@ -1057,8 +1049,9 @@ def plot(self, color='random'):
10571049 sphinx_plot(p2.tropical_variety().plot())
10581050 """
10591051 from random import random
1060- from sage . plot . graphics import Graphics
1052+
10611053 from sage .geometry .polyhedron .constructor import Polyhedron
1054+ from sage .plot .graphics import Graphics
10621055
10631056 if color == 'random' :
10641057 colors = []
@@ -1283,17 +1276,17 @@ def _vertices_components(self):
12831276 if lower != - infinity :
12841277 x = parametric_function [0 ].subs (** {str (v ): lower })
12851278 y = parametric_function [1 ].subs (** {str (v ): lower })
1286- if (x ,y ) not in comp_vert :
1287- comp_vert [(x ,y )] = [(i , 1 )]
1279+ if (x , y ) not in comp_vert :
1280+ comp_vert [(x , y )] = [(i , 1 )]
12881281 else :
1289- comp_vert [(x ,y )].append ((i , 1 ))
1282+ comp_vert [(x , y )].append ((i , 1 ))
12901283 if upper != infinity :
12911284 x = parametric_function [0 ].subs (** {str (v ): upper })
12921285 y = parametric_function [1 ].subs (** {str (v ): upper })
1293- if (x ,y ) not in comp_vert :
1294- comp_vert [(x ,y )] = [(i , - 1 )]
1286+ if (x , y ) not in comp_vert :
1287+ comp_vert [(x , y )] = [(i , - 1 )]
12951288 else :
1296- comp_vert [(x ,y )].append ((i , - 1 ))
1289+ comp_vert [(x , y )].append ((i , - 1 ))
12971290 return comp_vert
12981291
12991292 def weight_vectors (self ):
@@ -1334,9 +1327,9 @@ def weight_vectors(self):
13341327 (-1, 0): [(-1, 0), (0, -1), (1, 1)],
13351328 (3, 4): [(-1, -1), (0, 1), (1, 0)]}
13361329 """
1330+ from sage .arith .misc import gcd
13371331 from sage .calculus .functional import diff
13381332 from sage .modules .free_module_element import vector
1339- from sage .arith .misc import gcd
13401333
13411334 if not self ._vertices_components ():
13421335 return {}
@@ -1636,10 +1629,9 @@ def plot(self):
16361629 + R(4)*x**3*y + x**2*y**2 + R(2)*x*y**3 + y**4)
16371630 sphinx_plot(p3.tropical_variety().plot())
16381631 """
1639- from sage .plot .plot import plot
1640- from sage .plot .text import text
16411632 from sage .plot .graphics import Graphics
1642- from sage .plot .plot import parametric_plot
1633+ from sage .plot .plot import parametric_plot , plot
1634+ from sage .plot .text import text
16431635
16441636 if not self ._hypersurface :
16451637 return plot (lambda x : float ('nan' ), {- 1 , 1 })
0 commit comments