@@ -265,69 +265,57 @@ cdef class fmpq_mpoly(flint_mpoly):
265265
266266 def __getitem__ (self , x ):
267267 """
268- Return the term at index `x` if `x` is an `int`, or the term with the exponent
269- vector `x` if `x` is a tuple of `int`s or `fmpq`s.
268+ Return the coefficient of the term with the exponent vector `x`.
269+ Always returns a value, missing keys will return `0`.
270+ Negative exponents are made positive.
270271
271272 >>> from flint import Ordering
272273 >>> ctx = fmpq_mpoly_ctx.get_context(2, Ordering.lex, 'x')
273274 >>> p = ctx.from_dict({(0, 1): 2, (1, 1): 3})
274- >>> p[1]
275- 2*x1
276275 >>> p[1, 1]
277276 3
278277
279278 """
280279 cdef:
281280 slong nvars = self .ctx.nvars()
282281
283- if isinstance (x, int ):
284- if not 0 <= x < fmpq_mpoly_length(self .val, self .ctx.val):
285- raise IndexError (" term index out of range" )
286- res = create_fmpq_mpoly(self .ctx)
287- fmpq_mpoly_get_term((< fmpq_mpoly> res).val, self .val, x, self .ctx.val)
288- return res
289- elif isinstance (x, tuple ):
290- if len (x) != nvars:
291- raise ValueError (" exponent vector provided does not match number of variables" )
292- res = fmpq()
293- exp_vec = fmpz_vec(x, double_indirect = True )
294- fmpq_mpoly_get_coeff_fmpq_fmpz((< fmpq> res).val, self .val, exp_vec.double_indirect, self .ctx.val)
295- return res
296- else :
297- raise TypeError (" index is not integer or tuple" )
282+ if not isinstance (x, tuple ):
283+ raise TypeError (" Exponent vector index is not a tuple" )
284+ elif len (x) != nvars:
285+ raise ValueError (" Exponent vector provided does not match number of variables" )
286+
287+ res = fmpq()
288+ exp_vec = fmpz_vec(x, double_indirect = True )
289+ fmpq_mpoly_get_coeff_fmpq_fmpz((< fmpq> res).val, self .val, exp_vec.double_indirect, self .ctx.val)
290+ return res
298291
299292 def __setitem__ (self , x , y ):
300293 """
301- Set the coefficient at index `x` to `y` if `x` is an `int`, or the term with
302- the exponent vector `x` if `x` is a tuple of `int`s or `fmpq`s.
294+ Set the coefficient of the term with the exponent vector `x` to `y`.
295+ Will always set a value, missing keys will create a new term.
296+ Negative exponents are made positive.
303297
304298 >>> from flint import Ordering
305299 >>> ctx = fmpq_mpoly_ctx.get_context(2, Ordering.lex, 'x')
306300 >>> p = ctx.from_dict({(0, 1): 2, (1, 1): 3})
307- >>> p[1] = 10
308301 >>> p[1, 1] = 20
309302 >>> p
310- 20*x0*x1 + 10 *x1
303+ 20*x0*x1 + 2 *x1
311304
312305 """
313306 cdef:
314307 slong nvars = self .ctx.nvars()
315308
316309 coeff = any_as_fmpq(y)
317310 if coeff is NotImplemented :
318- raise TypeError (" provided coefficient not coercible to fmpq" )
319-
320- if isinstance (x, int ):
321- if not 0 <= x < fmpq_mpoly_length(self .val, self .ctx.val):
322- raise IndexError (" term index out of range" )
323- fmpq_mpoly_set_term_coeff_fmpq(self .val, x, (< fmpq> coeff).val, self .ctx.val)
324- elif isinstance (x, tuple ):
325- if len (x) != nvars:
326- raise ValueError (" exponent vector provided does not match number of variables" )
327- exp_vec = fmpz_vec(x, double_indirect = True )
328- fmpq_mpoly_set_coeff_fmpq_fmpz(self .val, (< fmpq> coeff).val, exp_vec.double_indirect, self .ctx.val)
329- else :
330- raise TypeError (" index is not integer or tuple" )
311+ raise TypeError (" Provided coefficient not coercible to fmpq" )
312+ elif not isinstance (x, tuple ):
313+ raise TypeError (" Exponent vector index is not a tuple" )
314+ elif len (x) != nvars:
315+ raise ValueError (" Exponent vector provided does not match number of variables" )
316+
317+ exp_vec = fmpz_vec(x, double_indirect = True )
318+ fmpq_mpoly_set_coeff_fmpq_fmpz(self .val, (< fmpq> coeff).val, exp_vec.double_indirect, self .ctx.val)
331319
332320 def __pos__ (self ):
333321 return self
0 commit comments