Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
 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)
Пример #4
0
    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))
Пример #5
0
    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))
Пример #6
0
    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
Пример #7
0
    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)
Пример #8
0
    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
            )
        )
Пример #9
0
    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))
Пример #10
0
 def y(self):
     """Scalar : This vector's y data."""
     return Scalar(self.data[..., 1])
Пример #11
0
 def x(self):
     """Scalar : This vector's x data."""
     return Scalar(self.data[..., 0])
Пример #12
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(
Пример #13
0
 def d(self):
     return Scalar(self.data[..., 3])
Пример #14
0
 def c(self):
     return Scalar(self.data[..., 2])
Пример #15
0
def test_init(data, expected):
    scalar = Scalar(data)
    assert np.allclose(scalar.data, expected)
Пример #16
0
 def angle(self):
     return Scalar(np.arctan(self.norm.data) * 2)
Пример #17
0
 def angle(self):
     """Scalar : the angle of rotation."""
     return Scalar(2 * np.nan_to_num(np.arccos(np.abs(self.a.data))))
Пример #18
0
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)
Пример #19
0
def scalar(request):
    return Scalar(request.param)
Пример #20
0
 def z(self):
     """Scalar : This vector's z data."""
     return Scalar(self.data[..., 2])
Пример #21
0
 def b(self):
     return Scalar(self.data[..., 1])
Пример #22
0
 def dot(self, other):
     """Scalar : the dot product of this quaternion and the other."""
     return Scalar(np.sum(self.data * other.data, axis=-1))
Пример #23
0
 def norm(self):
     from orix.scalar import Scalar
     return Scalar(np.sqrt(np.sum(np.square(self.data), axis=-1)))
Пример #24
0
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(
Пример #25
0
 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)
Пример #26
0
 def angle(self):
     return Scalar(self.norm.data)
Пример #27
0
 def a(self):
     return Scalar(self.data[..., 0])