File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -164,6 +164,23 @@ cdef class flint_mpoly_context(flint_elem):
164164 def gens (self ):
165165 return tuple (self .gen(i) for i in range (self .nvars()))
166166
167+ def variable_to_index (self , var: Union[int , str]):
168+ """ Convert a variable name string or possible index to it's index in the context."""
169+ if isinstance (var, str ):
170+ vars = {x: i for i, x in enumerate (self .names())}
171+ if var not in vars :
172+ raise ValueError (" Variable not in context" )
173+ else :
174+ i = vars [var]
175+ elif isinstance (var, int ):
176+ if not 0 <= var < self .nvars():
177+ raise IndexError (" Generator index out of range" )
178+ i = var
179+ else :
180+ raise TypeError (" Invalid variable type" )
181+
182+ return i
183+
167184 @staticmethod
168185 def create_variable_names (slong nvars , names: str ):
169186 """
Original file line number Diff line number Diff line change @@ -903,20 +903,7 @@ cdef class fmpq_mpoly(flint_mpoly):
903903 """
904904 cdef:
905905 fmpq_mpoly res
906- slong i = 0
907-
908- if isinstance (var, str ):
909- vars = {x: i for i, x in enumerate (self .ctx.names())}
910- if var not in vars :
911- raise ValueError (" variable not in context" )
912- else :
913- i = vars [var]
914- elif isinstance (var, int ):
915- if not 0 <= var < self .ctx.nvars():
916- raise IndexError (" generator index out of range" )
917- i = < slong> var
918- else :
919- raise TypeError (" invalid variable type" )
906+ slong i = self .ctx.variable_to_index(var)
920907
921908 res = create_fmpq_mpoly(self .ctx)
922909
@@ -942,20 +929,7 @@ cdef class fmpq_mpoly(flint_mpoly):
942929 """
943930 cdef:
944931 fmpq_mpoly res
945- slong i = 0
946-
947- if isinstance (var, str ):
948- vars = {x: i for i, x in enumerate (self .ctx.names())}
949- if var not in vars :
950- raise ValueError (" variable not in context" )
951- else :
952- i = vars [var]
953- elif isinstance (var, int ):
954- if not 0 <= var < self .ctx.nvars():
955- raise IndexError (" generator index out of range" )
956- i = < slong> var
957- else :
958- raise TypeError (" invalid variable type" )
932+ slong i = self .ctx.variable_to_index(var)
959933
960934 res = create_fmpq_mpoly(self .ctx)
961935
Original file line number Diff line number Diff line change @@ -915,20 +915,7 @@ cdef class fmpz_mpoly(flint_mpoly):
915915 """
916916 cdef:
917917 fmpz_mpoly res
918- slong i = 0
919-
920- if isinstance (var, str ):
921- vars = {x: i for i, x in enumerate (self .ctx.names())}
922- if var not in vars :
923- raise ValueError (" variable not in context" )
924- else :
925- i = vars [var]
926- elif isinstance (var, int ):
927- if not 0 <= var < self .ctx.nvars():
928- raise IndexError (" generator index out of range" )
929- i = < slong> var
930- else :
931- raise TypeError (" invalid variable type" )
918+ slong i = self .ctx.variable_to_index(var)
932919
933920 res = create_fmpz_mpoly(self .ctx)
934921
@@ -955,20 +942,7 @@ cdef class fmpz_mpoly(flint_mpoly):
955942 cdef:
956943 fmpz_mpoly res
957944 fmpz scale
958- slong i = 0
959-
960- if isinstance (var, str ):
961- vars = {x: i for i, x in enumerate (self .ctx.names())}
962- if var not in vars :
963- raise ValueError (" variable not in context" )
964- else :
965- i = vars [var]
966- elif isinstance (var, int ):
967- if not 0 <= var < self .ctx.nvars():
968- raise IndexError (" generator index out of range" )
969- i = < slong> var
970- else :
971- raise TypeError (" invalid variable type" )
945+ slong i = self .ctx.variable_to_index(var)
972946
973947 res = create_fmpz_mpoly(self .ctx)
974948
You can’t perform that action at this time.
0 commit comments