series_class.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """
  2. Contains the base class for series
  3. Made using sequences in mind
  4. """
  5. from sympy.core.expr import Expr
  6. from sympy.core.singleton import S
  7. from sympy.core.cache import cacheit
  8. class SeriesBase(Expr):
  9. """Base Class for series"""
  10. @property
  11. def interval(self):
  12. """The interval on which the series is defined"""
  13. raise NotImplementedError("(%s).interval" % self)
  14. @property
  15. def start(self):
  16. """The starting point of the series. This point is included"""
  17. raise NotImplementedError("(%s).start" % self)
  18. @property
  19. def stop(self):
  20. """The ending point of the series. This point is included"""
  21. raise NotImplementedError("(%s).stop" % self)
  22. @property
  23. def length(self):
  24. """Length of the series expansion"""
  25. raise NotImplementedError("(%s).length" % self)
  26. @property
  27. def variables(self):
  28. """Returns a tuple of variables that are bounded"""
  29. return ()
  30. @property
  31. def free_symbols(self):
  32. """
  33. This method returns the symbols in the object, excluding those
  34. that take on a specific value (i.e. the dummy symbols).
  35. """
  36. return ({j for i in self.args for j in i.free_symbols}
  37. .difference(self.variables))
  38. @cacheit
  39. def term(self, pt):
  40. """Term at point pt of a series"""
  41. if pt < self.start or pt > self.stop:
  42. raise IndexError("Index %s out of bounds %s" % (pt, self.interval))
  43. return self._eval_term(pt)
  44. def _eval_term(self, pt):
  45. raise NotImplementedError("The _eval_term method should be added to"
  46. "%s to return series term so it is available"
  47. "when 'term' calls it."
  48. % self.func)
  49. def _ith_point(self, i):
  50. """
  51. Returns the i'th point of a series
  52. If start point is negative infinity, point is returned from the end.
  53. Assumes the first point to be indexed zero.
  54. Examples
  55. ========
  56. TODO
  57. """
  58. if self.start is S.NegativeInfinity:
  59. initial = self.stop
  60. step = -1
  61. else:
  62. initial = self.start
  63. step = 1
  64. return initial + i*step
  65. def __iter__(self):
  66. i = 0
  67. while i < self.length:
  68. pt = self._ith_point(i)
  69. yield self.term(pt)
  70. i += 1
  71. def __getitem__(self, index):
  72. if isinstance(index, int):
  73. index = self._ith_point(index)
  74. return self.term(index)
  75. elif isinstance(index, slice):
  76. start, stop = index.start, index.stop
  77. if start is None:
  78. start = 0
  79. if stop is None:
  80. stop = self.length
  81. return [self.term(self._ith_point(i)) for i in
  82. range(start, stop, index.step or 1)]