def dot_outer(self, other): """The outer dot product of a vector with another vector. The dot product for every combination of vectors in `self` and `other` is computed. Returns ------- Scalar Examples -------- >>> v = Vector3d(((0.0, 0.0, 1.0), (1.0, 0.0, 0.0))) # shape = (2, ) >>> w = Vector3d(((0.0, 0.0, 0.5), (0.4, 0.6, 0.0), (0.5, 0.5, 0.5))) # shape = (3, ) >>> v.dot_outer(w) Scalar (2, 3) [[ 0.5 0. 0.5] [ 0. 0.4 0.5]] >>> w.dot_outer(v) # shape = (3, 2) Scalar (3, 2) [[ 0.5 0. ] [ 0. 0.4] [ 0.5 0.5]] """ dots = np.tensordot(self.data, other.data, axes=(-1, -1)) return Scalar(dots)
def angle_with(self, other, use_symmetry=False): """Calculate angles between vectors in `self` and `other`, possibly using symmetrically equivalent vectors to find the smallest angle under symmetry. Vectors must have compatible shapes, and be in the same space (direct or recprocal) and crystal reference frames. Parameters ---------- other : Vector3d or Miller use_symmetry : bool, optional Whether to consider equivalent vectors to find the smallest angle under symmetry. Default is False. Returns ------- Scalar The angle between the vectors, in radians. """ self._compatible_with(other, raise_error=True) if use_symmetry: other2 = other.symmetrise(unique=True) cosines = self.dot_outer(other2).data / ( self.norm.data[..., np.newaxis] * other2.norm.data[np.newaxis, ...]) cosines = np.round(cosines, 9) angles = np.min(np.arccos(cosines), axis=-1) return Scalar(angles) else: return super().angle_with(other)
def dot_outer(self, other): """Scalar : the outer dot product of this rotation and the other.""" cosines = np.abs(super(Rotation, self).dot_outer(other).data) if isinstance(other, Rotation): improper = self.improper.reshape(self.shape + (1,) * len(other.shape)) i = np.logical_xor(improper, other.improper) cosines = np.minimum(~i, cosines) else: cosines[self.improper] = 0 return Scalar(cosines)
def polar(self): r"""Polar spherical coordinate, i.e. the angle :math:`\theta \in [0, \pi]` from the positive z-axis to a point on the sphere, according to the ISO 31-11 standard [SphericalWolfram]_. Returns ------- Scalar """ return Scalar(np.arccos(self.data[..., 2] / self.radial.data))
def angle_with(self, other): """Calculate the angles between vectors in 'self' and 'other' Vectors must have compatible shapes for broadcasting to work. Returns ------- Scalar The angle between the vectors, in radians. """ cosines = np.round(self.dot(other).data / self.norm.data / other.norm.data, 9) return Scalar(np.arccos(cosines))
def angle_with(self, other): """The angle of rotation transforming this rotation to the other. Returns ------- Scalar """ other = Rotation(other) angles = Scalar( np.nan_to_num(np.arccos(2 * self.unit.dot(other.unit).data**2 - 1))) return angles
def azimuth(self): r"""Azimuth spherical coordinate, i.e. the angle :math:`\phi \in [0, 2\pi]` from the positive z-axis to a point on the sphere, according to the ISO 31-11 standard [SphericalWolfram]_. Returns ------- Scalar """ azimuth = np.arctan2(self.data[..., 1], self.data[..., 0]) azimuth += (azimuth < 0) * 2 * np.pi return Scalar(azimuth)
def radial(self): """Radial spherical coordinate, i.e. the distance from a point on the sphere to the origin, according to the ISO 31-11 standard [SphericalWolfram]_. Returns ------- Scalar """ return Scalar( np.sqrt( self.data[..., 0] ** 2 + self.data[..., 1] ** 2 + self.data[..., 2] ** 2 ) )
def dot(self, other): """The dot product of a vector with another vector. Vectors must have compatible shape. Returns ------- Scalar Examples -------- >>> v = Vector3d((0, 0, 1.0)) >>> w = Vector3d(((0, 0, 0.5), (0.4, 0.6, 0))) >>> v.dot(w) Scalar (2,) [ 0.5 0. ] >>> w.dot(v) Scalar (2,) [ 0.5 0. ] """ if not isinstance(other, Vector3d): raise ValueError('{} is not a vector!'.format(other)) return Scalar(np.sum(self.data * other.data, axis=-1))
def y(self): """Scalar : This vector's y data.""" return Scalar(self.data[..., 1])
def x(self): """Scalar : This vector's x data.""" return Scalar(self.data[..., 0])
from orix.scalar import Scalar from orix.vector import Vector3d @pytest.fixture(params=[(1, )]) def scalar(request): return Scalar(request.param) @pytest.mark.parametrize( "data, expected", [ ((5, 3), (5, 3)), ([[1], [2]], [[1], [2]]), (np.array([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5]), (Scalar([-1, 1]), [-1, 1]), ], ) def test_init(data, expected): scalar = Scalar(data) assert np.allclose(scalar.data, expected) @pytest.mark.parametrize("scalar, expected", [(1, -1), ((1, -1), (-1, 1))], indirect=["scalar"]) def test_neg(scalar, expected): neg = -scalar assert np.allclose(neg.data, expected) @pytest.mark.parametrize(
def d(self): return Scalar(self.data[..., 3])
def c(self): return Scalar(self.data[..., 2])
def test_init(data, expected): scalar = Scalar(data) assert np.allclose(scalar.data, expected)
def angle(self): return Scalar(np.arctan(self.norm.data) * 2)
def angle(self): """Scalar : the angle of rotation.""" return Scalar(2 * np.nan_to_num(np.arccos(np.abs(self.a.data))))
def test_stack(data, expected): stack = Scalar.stack(data) assert isinstance(stack, Scalar) assert stack.shape[-1] == len(data) assert np.allclose(stack.data, expected)
def scalar(request): return Scalar(request.param)
def z(self): """Scalar : This vector's z data.""" return Scalar(self.data[..., 2])
def b(self): return Scalar(self.data[..., 1])
def dot(self, other): """Scalar : the dot product of this quaternion and the other.""" return Scalar(np.sum(self.data * other.data, axis=-1))
def norm(self): from orix.scalar import Scalar return Scalar(np.sqrt(np.sum(np.square(self.data), axis=-1)))
def test_check_vector(): vector3 = Vector3d([2, 2, 2]) assert np.allclose(vector3.data, check_vector(vector3).data) def test_neg(vector): assert np.all((-vector).data == -(vector.data)) @pytest.mark.parametrize( "vector, other, expected", [ ([1, 2, 3], Vector3d([[1, 2, 3], [-3, -2, -1]]), [[2, 4, 6], [-2, 0, 2]]), ([1, 2, 3], Scalar([4]), [5, 6, 7]), ([1, 2, 3], 0.5, [1.5, 2.5, 3.5]), ([1, 2, 3], [-1, 2], [[0, 1, 2], [3, 4, 5]]), ([1, 2, 3], np.array([-1, 1]), [[0, 1, 2], [2, 3, 4]]), pytest.param([1, 2, 3], "dracula", None, marks=pytest.mark.xfail), ], indirect=["vector"], ) def test_add(vector, other, expected): s1 = vector + other s2 = other + vector assert np.allclose(s1.data, expected) assert np.allclose(s1.data, s2.data) @pytest.mark.parametrize(
def dot_outer(self, other): """Scalar : the outer dot product of this quaternion and the other.""" dots = np.tensordot(self.data, other.data, axes=(-1, -1)) return Scalar(dots)
def angle(self): return Scalar(self.norm.data)
def a(self): return Scalar(self.data[..., 0])