<commit_before>from __future__ import division
import unittest
import numpy as np

from BDQuaternions import Quaternion
from BDQuaternions import functions as qf


class TestQuaternion(unittest.TestCase):

    def setUp(self):
        self.q1 = Quaternion(np.array([0, 0, 0, 1]))
        self.q2 = Quaternion(np.array([1, 2, 3, 4]))

    def test_quaternion_constructor(self):
        np.testing.assert_allclose(self.q1.quadruple, np.array([0, 0, 0, 1]))
        self.assertRaises(ValueError, Quaternion, np.array([0, 0, 0]))
        self.assertRaises(ValueError, Quaternion, 'xxx')
        self.assertEqual(str(self.q2), str(self.q2.quadruple))
        self.assertNotEqual(self.q1, self.q2)
        with self.assertRaises(ValueError):
            _ = self.q1 == 'a'

    def test_scalar_vector_part(self):
        np.testing.assert_allclose(self.q1.scalar_part(), self.q1.quadruple[0])
        np.testing.assert_allclose(self.q1.vector_part(), self.q1.quadruple[1:])

    def test_multiplication(self):
        r1 = self.q1.scalar_part()
        r2 = self.q2.scalar_part()
        v1 = self.q1.vector_part()
        v2 = self.q2.vector_part()
        r3 = r1 * r2 - np.dot(v1, v2)
        v3 = r1 * v2 + r2 * v1 + np.cross(v1, v2)
        quadruple = np.array(np.hstack((r3, v3)))
        np.testing.assert_allclose((self.q1 * self.q2).quadruple, quadruple)
        np.testing.assert_allclose((self.q1 * 3).quadruple, self.q1.quadruple * 3)
        np.testing.assert_allclose((3 * self.q1).quadruple, self.q1.quadruple * 3)
        with self.assertRaises(ValueError):
            _ = self.q2 * 'x'
        with self.assertRaises(ValueError):
            _ = 'x' * self.q2

    def test_addition(self):
        np.testing.assert_allclose((self.q1 + self.q2).quadruple, np.array([1, 2, 3, 5]))
        np.testing.assert_allclose((self.q1 - self.q2).quadruple, np.array([-1, -2, -3, -3]))
        np.testing.assert_allclose((self.q1 - 3).quadruple, np.array([-3, 0, 0, 1]))
        np.testing.assert_allclose((3 - self.q1).quadruple, np.array([3, 0, 0, -1]))
        np.testing.assert_allclose((3 + self.q1).quadruple, np.array([3, 0, 0, 1]))
        with self.assertRaises(ValueError):
            _ = 'x' + self.q1
        with self.assertRaises(ValueError):
            _ = self.q1 + 'x'
        with self.assertRaises(ValueError):
            _ = 'x' - self.q1
        with self.assertRaises(ValueError):
            _ = self.q1 - 'x'

    def test_conjugation(self):
        np.testing.assert_allclose(self.q2.conjugate().quadruple, np.array([1, -2, -3, -4]))
        self.assertEqual((self.q1 * self.q2).conjugate(), self.q2.conjugate() * self.q1.conjugate())
        self.assertNotEqual((self.q1 * self.q2).conjugate(), self.q1.conjugate() * self.q2.conjugate())

    def test_norm(self):
        np.testing.assert_allclose(self.q1.norm(), 1.0)
        np.testing.assert_allclose((3 * self.q1).norm(), 3.0)
        np.testing.assert_allclose((self.q1 * self.q2).norm(), self.q1.norm() * self.q2.norm())
        np.testing.assert_allclose((self.q2 * self.q1).norm(), self.q1.norm() * self.q2.norm())

    def test_distance(self):
        self.assertEqual(self.q1.distance(self.q2), (self.q1 - self.q2).norm())
        self.assertEqual(self.q2.distance(self.q1), (self.q1 - self.q2).norm())
        self.assertEqual(self.q1.distance(self.q2), self.q2.distance(self.q1))
        self.assertEqual(self.q1.distance(self.q1), 0)
        with self.assertRaises(ValueError):
            self.q1.distance('x')
<commit_msg>more tests.<commit_after>from __future__ import division
import unittest
import numpy as np

from BDQuaternions import Quaternion
from BDQuaternions import functions as qf


class TestQuaternion(unittest.TestCase):

    def setUp(self):
        self.q1 = Quaternion(np.array([0, 0, 0, 1]))
        self.q2 = Quaternion(np.array([1, 2, 3, 4]))

    def test_quaternion_constructor(self):
        np.testing.assert_allclose(self.q1.quadruple, np.array([0, 0, 0, 1]))
        self.assertRaises(ValueError, Quaternion, np.array([0, 0, 0]))
        self.assertRaises(ValueError, Quaternion, 'xxx')
        self.assertEqual(str(self.q2), str(self.q2.quadruple))
        self.assertNotEqual(self.q1, self.q2)
        with self.assertRaises(ValueError):
            _ = self.q1 == 'a'

    def test_scalar_vector_part(self):
        np.testing.assert_allclose(self.q1.scalar_part(), self.q1.quadruple[0])
        np.testing.assert_allclose(self.q1.vector_part(), self.q1.quadruple[1:])

    def test_multiplication(self):
        r1 = self.q1.scalar_part()
        r2 = self.q2.scalar_part()
        v1 = self.q1.vector_part()
        v2 = self.q2.vector_part()
        r3 = r1 * r2 - np.dot(v1, v2)
        v3 = r1 * v2 + r2 * v1 + np.cross(v1, v2)
        v4 = r1 * v2 + r2 * v1 + np.cross(v2, v1)
        quadruple = np.array(np.hstack((r3, v3)))
        np.testing.assert_allclose((self.q1 * self.q2).quadruple, quadruple)
        quadruple = np.array(np.hstack((r3, v4)))
        np.testing.assert_allclose((self.q1.__rmul__(self.q2)).quadruple, quadruple)
        np.testing.assert_allclose((self.q1 * 3).quadruple, self.q1.quadruple * 3)
        np.testing.assert_allclose((3 * self.q1).quadruple, self.q1.quadruple * 3)
        with self.assertRaises(ValueError):
            _ = self.q2 * 'x'
        with self.assertRaises(ValueError):
            _ = 'x' * self.q2

    def test_addition(self):
        np.testing.assert_allclose((self.q1 + self.q2).quadruple, np.array([1, 2, 3, 5]))
        np.testing.assert_allclose((self.q1.__radd__(self.q2)).quadruple, np.array([1, 2, 3, 5]))
        np.testing.assert_allclose((self.q1 - self.q2).quadruple, np.array([-1, -2, -3, -3]))
        np.testing.assert_allclose((self.q1.__rsub__(self.q2)).quadruple, np.array([1, 2, 3, 3]))
        np.testing.assert_allclose((self.q1 - 3).quadruple, np.array([-3, 0, 0, 1]))
        np.testing.assert_allclose((3 - self.q1).quadruple, np.array([3, 0, 0, -1]))
        np.testing.assert_allclose((3 + self.q1).quadruple, np.array([3, 0, 0, 1]))
        np.testing.assert_allclose((self.q1 + 3).quadruple, np.array([3, 0, 0, 1]))
        with self.assertRaises(ValueError):
            _ = 'x' + self.q1
        with self.assertRaises(ValueError):
            _ = self.q1 + 'x'
        with self.assertRaises(ValueError):
            _ = 'x' - self.q1
        with self.assertRaises(ValueError):
            _ = self.q1 - 'x'

    def test_conjugation(self):
        np.testing.assert_allclose(self.q2.conjugate().quadruple, np.array([1, -2, -3, -4]))
        self.assertEqual((self.q1 * self.q2).conjugate(), self.q2.conjugate() * self.q1.conjugate())
        self.assertNotEqual((self.q1 * self.q2).conjugate(), self.q1.conjugate() * self.q2.conjugate())

    def test_norm(self):
        np.testing.assert_allclose(self.q1.norm(), 1.0)
        np.testing.assert_allclose((3 * self.q1).norm(), 3.0)
        np.testing.assert_allclose((self.q1 * self.q2).norm(), self.q1.norm() * self.q2.norm())
        np.testing.assert_allclose((self.q2 * self.q1).norm(), self.q1.norm() * self.q2.norm())

    def test_distance(self):
        self.assertEqual(self.q1.distance(self.q2), (self.q1 - self.q2).norm())
        self.assertEqual(self.q2.distance(self.q1), (self.q1 - self.q2).norm())
        self.assertEqual(self.q1.distance(self.q2), self.q2.distance(self.q1))
        self.assertEqual(self.q1.distance(self.q1), 0)
        with self.assertRaises(ValueError):
            self.q1.distance('x')

{'fraction_non_alphanumeric': 0.16575543405787438, 'fraction_numerical': 0.04147219629283905, 'mean_word_length': 2.772635814889336, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5253245', 'n_tokens_mistral': 2930, 'n_tokens_neox': 2802, 'n_words': 429}