Exemple #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)
Exemple #2
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)
Exemple #3
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
Exemple #4
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))
Exemple #5
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))
Exemple #6
0
def something(request):
    return Vector3d(request.param)


@pytest.fixture(params=numbers)
def number(request):
    return request.param


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('vector, other, expected', [
    ([1, 2, 3], Vector3d([[1, 2, 3], [-3, -2, -1]]), [[0, 0, 0], [4, 4, 4]]),
    ([1, 2, 3], Scalar([4]), [-3, -2, -1]),
Exemple #7
0
 def c(self):
     return Scalar(self.data[..., 2])
Exemple #8
0
 def d(self):
     return Scalar(self.data[..., 3])
Exemple #9
0
 def a(self):
     return Scalar(self.data[..., 0])
Exemple #10
0
 def b(self):
     return Scalar(self.data[..., 1])
Exemple #11
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))
Exemple #12
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)
Exemple #13
0
 def angle(self):
     return Scalar(self.norm.data)
Exemple #14
0
 def angle(self):
     return Scalar(np.arctan(self.norm.data) * 2)
Exemple #15
0
 def y(self):
     """Scalar : This vector's y data."""
     return Scalar(self.data[..., 1])
Exemple #16
0
 def x(self):
     """Scalar : This vector's x data."""
     return Scalar(self.data[..., 0])
Exemple #17
0
 def norm(self):
     from texpy.scalar import Scalar
     return Scalar(np.sqrt(np.sum(np.square(self.data), axis=-1)))
Exemple #18
0
def test_init(data, expected):
    scalar = Scalar(data)
    assert np.allclose(scalar.data, expected)
Exemple #19
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)
Exemple #20
0
@pytest.fixture(params=[(1, )])
def scalar(request):
    return Scalar(request.param)


@pytest.fixture(params=[(1, 1, 1)])
def vector(request):
    return Vector3d(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('scalar, other, expected', [
    (1, 1, 2),
Exemple #21
0
 def z(self):
     """Scalar : This vector's z data."""
     return Scalar(self.data[..., 2])
Exemple #22
0
 def angle(self):
     """Scalar : the angle of rotation."""
     return Scalar(2 * np.nan_to_num(np.arccos(np.abs(self.a.data))))
Exemple #23
0
def scalar(request):
    return Scalar(request.param)