<commit_before># helper functions for treating float lists as quaternions
# author: <EMAIL>

import sys

import Vec
import math
import numpy

def id():
    return [0, 0, 0, 1]

def conj(q):
    return [-q[0], -q[1], -q[2], q[3]]

def inv(q):
    return Vec.scal(1 / Vec.norm2(q), conj(q) )

def re(q):
    return q[3]

def im(q):
    return q[:3]

# TODO optimize
def prod(a, b):
    return Vec.sum( Vec.sum( Vec.scal( re(a), im(b) ),
                             Vec.scal( re(b), im(a) )),
                    Vec.cross( im(a), im(b) ) ) + [ re(a) * re(b) - Vec.dot( im(a), im(b) ) ]

# TODO optimize
def rotate(q, x):
    # TODO assert q is unit
    return im( prod(q, prod( x + [0], conj(q))) )


def exp(v):
    """Quaterion exponential."""
    theta = Vec.norm(v)

    if math.fabs(theta) < sys.float_info.epsilon:
        return id()

    s = math.sin(theta / 2)
    c = math.cos(theta / 2)

    return [ v[0] / theta * s,
             v[1] / theta * s,
             v[2] / theta * s,
             c ]

def flip(q):
    """Flip a quaternion to the real positive hemisphere if needed."""
    
    if re(q) < 0:
        return Vec.minus(q)
    else return q

    
def log(q):

    """Quaterion (principal) logarithm. 

    You might want to flip q first to ensure theta is in the [-0, pi]
    range, yielding the equivalent rotation (principal) logarithm.

    """

    half_theta = math.acos( sign * re(q) )

    if math.fabs( half_theta ) < sys.float_info.epsilon:
        return [0, 0, 0]

    return Vec.scal(2 * half_theta / math.sin(half_theta),
                    im(q))



def from_matrix(matrix, isprecise=False):
    """Return quaternion from rotation matrix.
    If isprecise is True, the input matrix is assumed to be a precise rotation
    matrix and a faster algorithm is used.
    """
    M = numpy.array(matrix, dtype=numpy.float64, copy=False)[:4, :4]
    if isprecise:
        q = numpy.empty((4, ))
        t = numpy.trace(M)
        if t > M[3, 3]:<commit_msg>r11215/sofa : Compliant: added comments to python quaternions<commit_after># helper functions for treating float lists as quaternions
# author: <EMAIL>

import sys

import Vec
import math
import numpy

def id():
    """identity"""
    return [0, 0, 0, 1]

def conj(q):
    """conjugate"""
    return [-q[0], -q[1], -q[2], q[3]]

def inv(q):
    """
    inverse 

    If you're dealing with unit quaternions, use conj instead.
    """
    return Vec.scal(1 / Vec.norm2(q), conj(q) )

def re(q):
    """real part"""
    return q[3]

def im(q):
    """imaginary part"""
    return q[:3]

# TODO optimize
def prod(a, b):
    """product"""
    return Vec.sum( Vec.sum( Vec.scal( re(a), im(b) ),
                             Vec.scal( re(b), im(a) )),
                    Vec.cross( im(a), im(b) ) ) + [ re(a) * re(b) - Vec.dot( im(a), im(b) ) ]

# TODO optimize
def rotate(q, x):
    """vector rotation

    rotates x by the rotation represented by q. this is also the
    adjoint map for S^3.

    """
    
    # TODO assert q is unit
    return im( prod(q, prod( x + [0], conj(q))) )


def exp(v):
    """exponential"""
    theta = Vec.norm(v)

    if math.fabs(theta) < sys.float_info.epsilon:
        return id()

    s = math.sin(theta / 2)
    c = math.cos(theta / 2)

    return [ v[0] / theta * s,
             v[1] / theta * s,
             v[2] / theta * s,
             c ]

def flip(q):
    """Flip a quaternion to the real positive hemisphere if needed."""
    
    if re(q) < 0:
        return Vec.minus(q)
    else return q

    
def log(q):

    """(principal) logarithm. 

    You might want to flip q first to ensure theta is in the [-0, pi]
    range, yielding the equivalent rotation (principal) logarithm.

    """

    half_theta = math.acos( sign * re(q) )

    if math.fabs( half_theta ) < sys.float_info.epsilon:
        return [0, 0, 0]

    return Vec.scal(2 * half_theta / math.sin(half_theta),
                    im(q))



def from_matrix(matrix, isprecise=False):
    """Return quaternion from rotation matrix.
    If isprecise is True, the input matrix is assumed to be a precise rotation
    matrix and a faster algorithm is used.
    """
    M = numpy.array(matrix, dtype=numpy.float64, copy=False)[:4, :4]
    if isprecise:
        q = numpy.empty((4, ))
        t = numpy.trace(M)
        if t > M[3, 3]:
{'fraction_non_alphanumeric': 0.14722222222222223, 'fraction_numerical': 0.01574074074074074, 'mean_word_length': 2.191285081240768, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '3748845', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1494, 'n_words': 482}