You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2014-2015 ACSONE SA/NV (<http://acsone.eu>)
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. """ A trivial immutable array that supports basic arithmetic operations.
  5. >>> a = SimpleArray((1.0, 2.0, 3.0))
  6. >>> b = SimpleArray((4.0, 5.0, 6.0))
  7. >>> t = (4.0, 5.0, 6.0)
  8. >>> +a
  9. SimpleArray((1.0, 2.0, 3.0))
  10. >>> -a
  11. SimpleArray((-1.0, -2.0, -3.0))
  12. >>> a + b
  13. SimpleArray((5.0, 7.0, 9.0))
  14. >>> b + a
  15. SimpleArray((5.0, 7.0, 9.0))
  16. >>> a + t
  17. SimpleArray((5.0, 7.0, 9.0))
  18. >>> t + a
  19. SimpleArray((5.0, 7.0, 9.0))
  20. >>> a - b
  21. SimpleArray((-3.0, -3.0, -3.0))
  22. >>> a - t
  23. SimpleArray((-3.0, -3.0, -3.0))
  24. >>> t - a
  25. SimpleArray((3.0, 3.0, 3.0))
  26. >>> a * b
  27. SimpleArray((4.0, 10.0, 18.0))
  28. >>> b * a
  29. SimpleArray((4.0, 10.0, 18.0))
  30. >>> a * t
  31. SimpleArray((4.0, 10.0, 18.0))
  32. >>> t * a
  33. SimpleArray((4.0, 10.0, 18.0))
  34. >>> a / b
  35. SimpleArray((0.25, 0.4, 0.5))
  36. >>> b / a
  37. SimpleArray((4.0, 2.5, 2.0))
  38. >>> a / t
  39. SimpleArray((0.25, 0.4, 0.5))
  40. >>> t / a
  41. SimpleArray((4.0, 2.5, 2.0))
  42. >>> b / 2
  43. SimpleArray((2.0, 2.5, 3.0))
  44. >>> 2 * b
  45. SimpleArray((8.0, 10.0, 12.0))
  46. >>> b += 2 ; b
  47. SimpleArray((6.0, 7.0, 8.0))
  48. """
  49. import operator
  50. __all__ = ['SimpleArray']
  51. class SimpleArray(tuple):
  52. def _op(self, op, other):
  53. if isinstance(other, tuple):
  54. if len(other) != len(self):
  55. raise TypeError("tuples must have same length for %s" % op)
  56. return SimpleArray(map(op, self, other))
  57. else:
  58. return SimpleArray(map(lambda x: op(x, other), self))
  59. def __add__(self, other):
  60. return self._op(operator.add, other)
  61. __radd__ = __add__
  62. def __pos__(self):
  63. return SimpleArray(map(operator.pos, self))
  64. def __neg__(self):
  65. return SimpleArray(map(operator.neg, self))
  66. def __sub__(self, other):
  67. return self._op(operator.sub, other)
  68. def __rsub__(self, other):
  69. return SimpleArray(other)._op(operator.sub, self)
  70. def __mul__(self, other):
  71. return self._op(operator.mul, other)
  72. __rmul__ = __mul__
  73. def __div__(self, other):
  74. return self._op(operator.div, other)
  75. def __floordiv__(self, other):
  76. return self._op(operator.floordiv, other)
  77. def __truediv__(self, other):
  78. return self._op(operator.truediv, other)
  79. def __rdiv__(self, other):
  80. return SimpleArray(other)._op(operator.div, self)
  81. def __rfloordiv__(self, other):
  82. return SimpleArray(other)._op(operator.floordiv, self)
  83. def __rtruediv__(self, other):
  84. return SimpleArray(other)._op(operator.truediv, self)
  85. def __repr__(self):
  86. return "SimpleArray(%s)" % tuple.__repr__(self)
  87. if __name__ == '__main__':
  88. import doctest
  89. doctest.testmod()