Skip to content

BugFix: Allow get_kwargs for setting kwargs on __mul__ #499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion orix/quaternion/quaternion.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ def __mul__(self, other: Union[Quaternion, Vector3d]):
m.coordinate_format = other.coordinate_format
return m
else:
return other.__class__(v)
kwargs = other.get_kwargs(v, rotation=self)
return other.__class__(**kwargs)
return NotImplemented

def __neg__(self) -> Quaternion:
Expand Down
4 changes: 4 additions & 0 deletions orix/vector/miller.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ def hkl(self) -> np.ndarray:
"""
return _transform_space(self.data, "c", "r", self.phase.structure.lattice)

def get_kwargs(self, new_data, *args, **kwargs) -> dict:
"""Return the keyword arguments to create the instance."""
return dict(xyz=new_data)

@hkl.setter
def hkl(self, value: np.ndarray):
"""Set the reciprocal lattice vectors."""
Expand Down
51 changes: 35 additions & 16 deletions orix/vector/vector3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,46 +219,54 @@ def polar(self) -> np.ndarray:
# ------------------------ Dunder methods ------------------------ #

def __neg__(self) -> Vector3d:
return self.__class__(-self.data)
kwargs = self.get_kwargs(new_data=-self.data)
return self.__class__(**kwargs)

def __add__(
self, other: Union[int, float, List, Tuple, np.ndarray, Vector3d]
) -> Vector3d:
if isinstance(other, Vector3d):
return self.__class__(self.data + other.data)
kwargs = self.get_kwargs(new_data=self.data + other.data)
return self.__class__(**kwargs)
elif isinstance(other, (int, float)):
return self.__class__(self.data + other)
kwargs = self.get_kwargs(new_data=self.data + other)
return self.__class__(**kwargs)
elif isinstance(other, (list, tuple)):
other = np.array(other)

if isinstance(other, np.ndarray):
return self.__class__(self.data + other[..., np.newaxis])
kwargs = self.get_kwargs(new_data=self.data + other[..., np.newaxis])
return self.__class__(**kwargs)

return NotImplemented

def __radd__(self, other: Union[int, float, List, Tuple, np.ndarray]) -> Vector3d:
if isinstance(other, (int, float)):
return self.__class__(other + self.data)
kwargs = self.get_kwargs(new_data=other + self.data)
return self.__class__(**kwargs)
elif isinstance(other, (list, tuple)):
other = np.array(other)

if isinstance(other, np.ndarray):
return self.__class__(other[..., np.newaxis] + self.data)
kwargs = self.get_kwargs(new_data=other[..., np.newaxis] + self.data)
return self.__class__(**kwargs)

return NotImplemented

def __sub__(
self, other: Union[int, float, List, Tuple, np.ndarray, Vector3d]
) -> Vector3d:
if isinstance(other, Vector3d):
return self.__class__(self.data - other.data)
kwargs = self.get_kwargs(new_data=self.data - other.data)
return self.__class__(**kwargs)
elif isinstance(other, (int, float)):
return self.__class__(self.data - other)
kwargs = self.get_kwargs(self.data - other)
return self.__class__(**kwargs)
elif isinstance(other, (list, tuple)):
other = np.array(other)

if isinstance(other, np.ndarray):
return self.__class__(self.data - other[..., np.newaxis])
kwargs = self.get_kwargs(self.data - other[..., np.newaxis])
return self.__class__(**kwargs)

return NotImplemented

Expand All @@ -282,23 +290,27 @@ def __mul__(
"Try `.dot` or `.cross` instead."
)
elif isinstance(other, (int, float)):
return self.__class__(self.data * other)
kwargs = self.get_kwargs(new_data=self.data * other)
return self.__class__(**kwargs)
elif isinstance(other, (list, tuple)):
other = np.array(other)

if isinstance(other, np.ndarray):
return self.__class__(self.data * other[..., np.newaxis])
kwargs = self.get_kwargs(new_data=self.data * other[..., np.newaxis])
return self.__class__(**kwargs)

return NotImplemented

def __rmul__(self, other: Union[int, float, List, Tuple, np.ndarray]) -> Vector3d:
if isinstance(other, (int, float)):
return self.__class__(other * self.data)
kwargs = self.get_kwargs(new_data=other * self.data)
return self.__class__(**kwargs)
elif isinstance(other, (list, tuple)):
other = np.array(other)

if isinstance(other, np.ndarray):
return self.__class__(other[..., np.newaxis] * self.data)
kwargs = self.get_kwargs(new_data=other[..., np.newaxis] * self.data)
return self.__class__(**kwargs)

return NotImplemented

Expand All @@ -308,12 +320,14 @@ def __truediv__(
if isinstance(other, Vector3d):
raise ValueError("Dividing vectors is undefined")
elif isinstance(other, (int, float)):
return self.__class__(self.data / other)
kwargs = self.get_kwargs(new_data=self.data / other)
return self.__class__(**kwargs)
elif isinstance(other, (list, tuple)):
other = np.array(other)

if isinstance(other, np.ndarray):
return self.__class__(self.data / other[..., np.newaxis])
kwargs = self.get_kwargs(new_data=self.data / other[..., np.newaxis])
return self.__class__(**kwargs)

return NotImplemented

Expand Down Expand Up @@ -731,6 +745,11 @@ def to_polar(
polar = np.rad2deg(polar)
return azimuth, polar, self.radial

def get_kwargs(self, new_data, *args, **kwargs) -> Dict[str, Any]:
"""Return the dictionary of attributes to be used in the
constructor when applying an ufunc."""
return dict(data=new_data)

def in_fundamental_sector(self, symmetry: "Symmetry") -> Vector3d:
"""Project vectors to a symmetry's fundamental sector (inverse
pole figure).
Expand Down