Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __add__(self, other):
if isinstance(other, Vector3d):
return self.__class__(self.data + other.data)
elif isinstance(other, Scalar):
return self.__class__(self.data + other.data[..., np.newaxis])
elif isinstance(other, (int, float)):
return self.__class__(self.data + other)
elif isinstance(other, (list, tuple)):
other = np.array(other)
if isinstance(other, np.ndarray):
return self.__class__(self.data + other[..., np.newaxis])
return NotImplemented
A new vector with entries rotated.
Examples
--------
>>> from math import pi
>>> v = Vector3d((0, 1, 0))
>>> axis = Vector3d((0, 0, 1))
>>> angles = [0, pi/4, pi/2, 3*pi/4, pi]
>>> v.rotate(axis=axis, angle=angles)
"""
from orix.quaternion.rotation import Rotation
from orix.vector.neo_euler import AxAngle
axis = Vector3d.zvector() if axis is None else axis
angle = 0 if angle is None else angle
q = Rotation.from_neo_euler(AxAngle.from_axes_angles(axis, angle))
return q * self
def perpendicular(self):
if np.any(self.x.data == 0) and np.any(self.y.data == 0):
if np.any(self.z.data == 0):
raise ValueError("Contains zero vectors!")
return Vector3d.xvector()
x = -self.y.data
y = self.x.data
z = np.zeros_like(x)
return Vector3d(np.stack((x, y, z), axis=-1))
If multiple vectors are equally close to this one, `tiebreak` will
be used as a secondary comparison. By default equal to (0, 0, 1).
Returns
-------
Vector3d
"""
assert self.size == 1, "`get_nearest` only works for single vectors."
tiebreak = Vector3d.zvector() if tiebreak is None else tiebreak
eps = 1e-9 if inclusive else 0.0
cosines = x.dot(self).data
mask = np.logical_and(-1 - eps < cosines, cosines < 1 + eps)
x = x[mask]
if x.size == 0:
return Vector3d.empty()
cosines = cosines[mask]
verticality = x.dot(tiebreak).data
order = np.lexsort((cosines, verticality))
return x[order[-1]]
def check_vector(obj):
return check(obj, Vector3d)
def perpendicular(self):
if np.any(self.x.data == 0) and np.any(self.y.data == 0):
if np.any(self.z.data == 0):
raise ValueError("Contains zero vectors!")
return Vector3d.xvector()
x = -self.y.data
y = self.x.data
z = np.zeros_like(x)
return Vector3d(np.stack((x, y, z), axis=-1))