Skip to content
TheChatArchiveThe Chat Archive
Metallama-4-maverick-03-26-experimental· MetaHugging Face LMArena Human Preference 140K

how to rotate a unit vector in mojo

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with DeepSeekdeepseek-v3-0324’s answer.
  1. User

    how to rotate a unit vector in mojo

  2. Meta Assistant

    A very specific and interesting question!

    Mojo is a modern programming language designed for building high-performance, portable, and maintainable applications. It's still a relatively new language, but I'll provide you with an answer based on its current documentation and standard mathematical practices.

    Rotating a unit vector in 3D space involves some basic linear algebra and trigonometry. We'll use the Rodrigues' rotation formula (also known as the axis-angle formula) to achieve this. If you're unfamiliar with the math, don't worry; I'll break it down step by step.

    The Goal: Given a unit vector v (3D vector with length 1) and a rotation defined by an axis k (another 3D unit vector) and an angle θ (in radians), we want to compute the rotated unit vector v_rot.

    Here's the step-by-step process:

    1. Define the inputs:

    • v: the original unit vector ( Float3 in Mojo, which is a vector of 3 Float64 values)
    • k: the rotation axis (another Float3 unit vector)
    • θ (theta): the rotation angle in radians (a single Float64 value)

    2. Recall the Rodrigues' rotation formula:

    The formula rotates a vector v around axis k by angle θ:

    v_rot = v * cos(θ) + (k × v) * sin(θ) + k * (k · v) * (1 - cos(θ))

    where:

    • × denotes the cross product (returns a vector perpendicular to both operands)
    • · denotes the dot product (returns the scalar product of two vectors)
    • * is the scalar multiplication (between a scalar and a vector)

    3. Implement it in Mojo:

    Here's the Mojo code snippet that performs the rotation:

    // Assuming you have a Vector3 (Float3) struct or alias defined,
    // similar to this (adjust according to Mojo's actual std lib):
    struct Float3:
        var x: Float64
        var y: Float64
        var z: Float64
    
    // Helper functions for vector operations
    fn dot(a: Float3, b: Float3) -> Float64:
        return a.x * b.x + a.y * b.y + a.z * b.z
    
    fn cross(a: Float3, b: Float3) -> Float3:
        return Float3(
            x: a.y * b.z - a.z * b.y,
            y: a.z * b.x - a.x * b.z,
            z: a.x * b.y - a.y * b.x
        )
    
    fn scale(v: Float3, s: Float64) -> Float3:
        return Float3(x: v.x * s, y: v.y * s, z: v.z * s)
    
    fn add(a: Float3, b: Float3) -> Float3:
        return Float3(x: a.x + b.x, y: a.y + b.y, z: a.z + b.z)
    
    // Main rotation function
    fn rotate_vector(v: Float3, k: Float3, theta: Float64) -> Float3:
        let cos_theta = cos(theta)
        let sin_theta = sin(theta)
        let k_dot_v = dot(k, v)
        let k_cross_v = cross(k, v)
    
        let part1 = scale(v, cos_theta)
        let part2 = scale(k_cross_v, sin_theta)
        let part3 = scale(k, k_dot_v * (1.0 - cos_theta))
    
        return add(add(part1, part2), part3)
    
    // Example usage:
    fn main() raises:
        // Original unit vector (along X axis)
        let v = Float3(x: 1.0, y: 0.0, z: 0.0)
        // Rotate around Y axis (0, 1, 0) by 90 degrees (π/2 radians)
        let k = Float3(x: 0.0, y: 1.0, z: 0.0)
        let theta = 3.14159265358979323846 / 2.0  # 90 degrees in radians
    
        let v_rotated = rotate_vector(v, k, theta)
        print("Rotated Vector: (\(v_rotated.x), \(v_rotated.y), \(v_rotated.z))")
        # Expected output: Rotated Vector: (0.0, 0.0, 1.0)
        # Our original X-axis vector is now aligned with Z-axis after 90-degree rotation around Y.
    

    Key points:

    1. We defined Float3 as our 3D vector type (adjust if Mojo has a built-in type).
    2. Implemented dot, cross, scale, and add as vector helpers.
    3. The rotate_vector function applies Rodrigues' formula directly.
    4. In main, we demonstrate rotating the unit vector (1, 0, 0) around the Y-axis (0, 1, 0) by 90 degrees (π/2 radians), resulting in (0, 0, 1), which is the unit vector along the Z-axis.

    Notes on Mojo specifics:

    • Ensure you have cos and sin functions available; Mojo should provide these in its standard library (under math or similar module). If not, you'll need to implement or import them.
    • This example assumes Float64 for precision. If Mojo has a Float32 type and you prefer that, adjust accordingly.
    • Error handling (raises in main) is added as per Mojo's syntax for functions that might throw errors.

    That's it! With this explanation and code, you should be able to rotate any unit vector in 3D space using Mojo. Happy coding!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.