| 218 | def __getitem__(self, n): |
| 219 | r""" |
| 220 | Return the coefficient of `x^n = x_1^{n_1} \cdots x_k^{n_k}` where |
| 221 | `n` is a tuple of length `k` and `k` is the number of variables. |
| 222 | |
| 223 | If the number of inputs is not equal to the number of variables, this |
| 224 | raises a ``TypeError``. |
| 225 | |
| 226 | EXAMPLES:: |
| 227 | |
| 228 | sage: P.<x,y,z> = LaurentPolynomialRing(QQ) |
| 229 | sage: f = (y^2 - x^9 - 7*x*y^3 + 5*x*y)*x^-3 + x*z; f |
| 230 | -x^6 + x*z - 7*x^-2*y^3 + 5*x^-2*y + x^-3*y^2 |
| 231 | sage: f[6,0,0] |
| 232 | -1 |
| 233 | sage: f[-2,3,0] |
| 234 | -7 |
| 235 | sage: f[-1,4,2] |
| 236 | 0 |
| 237 | sage: f[1,0,1] |
| 238 | 1 |
| 239 | sage: f[6] |
| 240 | Traceback (most recent call last): |
| 241 | ... |
| 242 | TypeError: Must have exactly 3 inputs |
| 243 | sage: f[6,0] |
| 244 | Traceback (most recent call last): |
| 245 | ... |
| 246 | TypeError: Must have exactly 3 inputs |
| 247 | sage: f[6,0,0,0] |
| 248 | Traceback (most recent call last): |
| 249 | ... |
| 250 | TypeError: Must have exactly 3 inputs |
| 251 | """ |
| 252 | if isinstance(n, slice): |
| 253 | raise TypeError("Multivariate Laurent polynomials are not iterable") |
| 254 | if not isinstance(n, tuple) or len(n) != self.parent().ngens(): |
| 255 | raise TypeError("Must have exactly %s inputs"%self.parent().ngens()) |
| 256 | cdef ETuple t = ETuple(n) |
| 257 | if self._prod is None: |
| 258 | self._compute_polydict() |
| 259 | if t not in self._prod.exponents(): |
| 260 | return self.parent().base_ring().zero_element() |
| 261 | return self._prod[t] |
| 262 | |
| 263 | def __iter__(self): |
| 264 | """ |
| 265 | Iterate through all terms by returning a list of the coefficient and |
| 266 | the corresponding monomial. |
| 267 | |
| 268 | EXAMPLES:: |
| 269 | |
| 270 | sage: P.<x,y> = LaurentPolynomialRing(QQ) |
| 271 | sage: f = (y^2 - x^9 - 7*x*y^3 + 5*x*y)*x^-3 |
| 272 | sage: list(f) # indirect doctest |
| 273 | [(-1, x^6), (1, x^-3*y^2), (5, x^-2*y), (-7, x^-2*y^3)] |
| 274 | """ |
| 275 | if self._prod is None: |
| 276 | self._compute_polydict() |
| 277 | for c, exps in self._prod.list(): |
| 278 | prod = self.parent().one_element() |
| 279 | for i in range(len(exps)): |
| 280 | prod *= self.parent().gens()[i]**exps[i] |
| 281 | yield (c, prod) |
| 282 | |
| 283 | def monomials(self): |
| 284 | """ |
| 285 | Return the list of monomials in ``self``. |
| 286 | |
| 287 | EXAMPLES:: |
| 288 | |
| 289 | sage: P.<x,y> = LaurentPolynomialRing(QQ) |
| 290 | sage: f = (y^2 - x^9 - 7*x*y^3 + 5*x*y)*x^-3 |
| 291 | sage: f.monomials() |
| 292 | [x^6, x^-3*y^2, x^-2*y, x^-2*y^3] |
| 293 | """ |
| 294 | L = [] |
| 295 | if self._prod is None: |
| 296 | self._compute_polydict() |
| 297 | for c, exps in self._prod.list(): |
| 298 | prod = self.parent().one_element() |
| 299 | for i in range(len(exps)): |
| 300 | prod *= self.parent().gens()[i]**exps[i] |
| 301 | L.append(prod) |
| 302 | return L |
| 303 | |
| 304 | def monomial_coefficient(self, mon): |
| 305 | """ |
| 306 | Return the coefficient in the base ring of the monomial ``mon`` in |
| 307 | ``self``, where ``mon`` must have the same parent as ``self``. |
| 308 | |
| 309 | This function contrasts with the function :meth:`coefficient()` |
| 310 | which returns the coefficient of a monomial viewing this |
| 311 | polynomial in a polynomial ring over a base ring having fewer |
| 312 | variables. |
| 313 | |
| 314 | INPUT: |
| 315 | |
| 316 | - ``mon`` - a monomial |
| 317 | |
| 318 | .. SEEALSO:: |
| 319 | |
| 320 | For coefficients in a base ring of fewer variables, see |
| 321 | :meth:`coefficient()`. |
| 322 | |
| 323 | EXAMPLES:: |
| 324 | |
| 325 | sage: P.<x,y> = LaurentPolynomialRing(QQ) |
| 326 | sage: f = (y^2 - x^9 - 7*x*y^3 + 5*x*y)*x^-3 |
| 327 | sage: f.monomial_coefficient(x^-2*y^3) |
| 328 | -7 |
| 329 | sage: f.monomial_coefficient(x^2) |
| 330 | 0 |
| 331 | """ |
| 332 | if mon.parent() != self.parent(): |
| 333 | raise TypeError("Input must have the same parent") |
| 334 | if self._prod is None: |
| 335 | self._compute_polydict() |
| 336 | if (<LaurentPolynomial_mpair>mon)._prod is None: |
| 337 | mon._compute_polydict() |
| 338 | return self.parent().base_ring()( self._prod.monomial_coefficient( |
| 339 | (<LaurentPolynomial_mpair>mon)._prod.dict()) ) |
| 340 | |
| 341 | def constant_coefficient(self): |
| 342 | """ |
| 343 | Return the constant coefficient of ``self``. |
| 344 | |
| 345 | EXAMPLES:: |
| 346 | |
| 347 | sage: P.<x,y> = LaurentPolynomialRing(QQ) |
| 348 | sage: f = (y^2 - x^9 - 7*x*y^2 + 5*x*y)*x^-3; f |
| 349 | -x^6 - 7*x^-2*y^2 + 5*x^-2*y + x^-3*y^2 |
| 350 | sage: f.constant_coefficient() |
| 351 | 0 |
| 352 | sage: f = (x^3 + 2*x^-2*y+y^3)*y^-3; f |
| 353 | x^3*y^-3 + 1 + 2*x^-2*y^-2 |
| 354 | sage: f.constant_coefficient() |
| 355 | 1 |
| 356 | """ |
| 357 | return self[(0,)*self.parent().ngens()] |
| 358 | |