| 9 | |
| 10 | |
| 11 | class Dual: |
| 12 | def __init__(self, real, rank): |
| 13 | self.real = real |
| 14 | if isinstance(rank, int): |
| 15 | self.duals = [1] * rank |
| 16 | else: |
| 17 | self.duals = rank |
| 18 | |
| 19 | def __repr__(self): |
| 20 | s = "+".join(f"{dual}E{n}" for n, dual in enumerate(self.duals, 1)) |
| 21 | return f"{self.real}+{s}" |
| 22 | |
| 23 | def reduce(self): |
| 24 | cur = self.duals.copy() |
| 25 | while cur[-1] == 0: |
| 26 | cur.pop(-1) |
| 27 | return Dual(self.real, cur) |
| 28 | |
| 29 | def __add__(self, other): |
| 30 | if not isinstance(other, Dual): |
| 31 | return Dual(self.real + other, self.duals) |
| 32 | s_dual = self.duals.copy() |
| 33 | o_dual = other.duals.copy() |
| 34 | if len(s_dual) > len(o_dual): |
| 35 | o_dual.extend([1] * (len(s_dual) - len(o_dual))) |
| 36 | elif len(s_dual) < len(o_dual): |
| 37 | s_dual.extend([1] * (len(o_dual) - len(s_dual))) |
| 38 | new_duals = [] |
| 39 | for i in range(len(s_dual)): |
| 40 | new_duals.append(s_dual[i] + o_dual[i]) |
| 41 | return Dual(self.real + other.real, new_duals) |
| 42 | |
| 43 | __radd__ = __add__ |
| 44 | |
| 45 | def __sub__(self, other): |
| 46 | return self + other * -1 |
| 47 | |
| 48 | def __mul__(self, other): |
| 49 | if not isinstance(other, Dual): |
| 50 | new_duals = [] |
| 51 | for i in self.duals: |
| 52 | new_duals.append(i * other) |
| 53 | return Dual(self.real * other, new_duals) |
| 54 | new_duals = [0] * (len(self.duals) + len(other.duals) + 1) |
| 55 | for i, item in enumerate(self.duals): |
| 56 | for j, jtem in enumerate(other.duals): |
| 57 | new_duals[i + j + 1] += item * jtem |
| 58 | for k in range(len(self.duals)): |
| 59 | new_duals[k] += self.duals[k] * other.real |
| 60 | for index in range(len(other.duals)): |
| 61 | new_duals[index] += other.duals[index] * self.real |
| 62 | return Dual(self.real * other.real, new_duals) |
| 63 | |
| 64 | __rmul__ = __mul__ |
| 65 | |
| 66 | def __truediv__(self, other): |
| 67 | if not isinstance(other, Dual): |
| 68 | new_duals = [] |
no outgoing calls
no test coverage detected