def __add__(self, other: "Vector") -> "Vector": if not isinstance(other, Vector): raise TypeError( f"Cannot add instances of type {type(other)} and {type(self)}") if len(self) != len(other): raise InvalidSizeError( f"Cannot add vectors of size {len(self)} and {len(other)}") return Vector(i + j for i, j in zip(self, other))
def __pow__(self, exponent: Union[Real, "Vector"]) -> "Vector": if isinstance(exponent, Vector): if len(self) != len(exponent): raise InvalidSizeError( f"Cannot expontiate vectors of size {len(self)} and {len(exponent)}" ) return Vector(i**j for i, j in zip(self, exponent)) else: return Vector(i**exponent for i in self)
def __eq__(self, other: "Vector") -> "Vector": if not isinstance(other, Vector): raise TypeError( f"Cannot compare instances of type {type(other)} and {type(self)}" ) if not len(self) == len(other): raise InvalidSizeError( f"Cannot compare vectors of size {len(self)} and {len(other)}") return all( isclose(i, j, rel_tol=1e-09, abs_tol=1e-04) for i, j in zip(self, other))
def cross(self, other: "Vector") -> "Vector": size_self, size_other = len(self), len(other) if not 1 < size_self < 4 or not 1 < size_other < 4: raise InvalidSizeError( f"Cross product of vector with size {len(self)} and {len(other)} is undefined!" ) u = self.copy() v = other.copy() if size_self < size_other: u.append(0) elif size_self > size_other: v.append(0) if len(u) == 2: u0, u1 = u v0, v1 = v return Vector([u0 * v1 - u1 * v0]) else: u0, u1, u2 = u v0, v1, v2 = v return Vector( [u1 * v2 - u2 * v1, u2 * v0 - u0 * v2, u0 * v1 - u1 * v0])
def from_tuple(cls, tup: Tuple[Real, Real, Real]) -> "Point": if len(tup) != 3: raise InvalidSizeError("length of tuple must be 3") return cls(*tup)