Cross Product of Vector

In the context of computing cross products, a vector refers to an ordered list of numbers that represent a quantity with direction and magnitude.

Some key properties of vectors relevant to cross products:

  • Vectors have a dimensionality, often 2D or 3D represented by an array of numbers.

  • Vectors can be added together element-wise.

  • Vectors can be multiplied by scalars to scale.

  • The cross product takes two vectors as input and outputs a third perpendicular vector.

  • The cross product is only defined for vectors in 3 dimensions.

  • The result of the cross product is a vector orthogonal to the plane spanned by the inputs.

  • The cross product vectors follow the right-hand rule for direction.

So in summary, for cross product examples, a “vector” refers to an ordered n-tuple (often 2 or 3 elements) that models a magnitude and direction in space. The cross product then combines two vector inputs to derive a third perpendicular vector representing their spanning plane. This vector concept is essential for geometric applications like computer graphics.

The cross product of two vectors u and v is another vector perpendicular to both u and v. It gives a vector representation of the plane spanned by u and v.

Java example:

1
2
3
4
5
6
7
8
9
double[] crossProduct(double[] u, double[] v) {
  double[] result = new double[3];

  result[0] = u[1]*v[2] - u[2]*v[1];
  result[1] = u[2]*v[0] - u[0]*v[2];
  result[2] = u[0]*v[1] - u[1]*v[0];

  return result;
}

C++ example:

1
2
3
4
5
6
7
8
9
vector<double> crossProduct(vector<double> u, vector<double> v) {
  vector<double> result(3);

  result[0] = u[1]*v[2] - u[2]*v[1];
  result[1] = u[2]*v[0] - u[0]*v[2];
  result[2] = u[0]*v[1] - u[1]*v[0];

  return result; 
}

Python example:

1
2
3
4
5
6
7
8
def cross_product(u, v):
  result = [0, 0, 0]

  result[0] = u[1]*v[2] - u[2]*v[1]
  result[1] = u[2]*v[0] - u[0]*v[2] 
  result[2] = u[0]*v[1] - u[1]*v[0]

  return result

The cross product is widely used in physics, graphics, and geometry. It provides directionality to vectors.

The cross product of two vectors ( \textbf{A} ) and ( \textbf{B} ) is another vector ( \textbf{C} ) that is perpendicular to both ( \textbf{A} ) and ( \textbf{B} ). The cross product is primarily defined for vectors in three-dimensional space. The magnitude of ( \textbf{C} ) is equal to the area of the parallelogram spanned by ( \textbf{A} ) and ( \textbf{B} ).

The cross product of two vectors ( \vec{A} ) and ( \vec{B} ) results in a third vector ( \vec{C} ) that is perpendicular to the plane containing ( \vec{A} ) and ( \vec{B} ). The direction of ( \vec{C} ) follows the right-hand rule.

Visual Representation:

  1. Place vectors ( \vec{A} ) and ( \vec{B} ) tail-to-tail in a 3D space.
  2. Imagine a plane that contains both ( \vec{A} ) and ( \vec{B} ).
       C
        ^
        |
        |
 A x B  |  
--------|------> A
        |
        |
        V B
  1. The resultant vector ( \vec{C} ) (or ( \vec{A} \times \vec{B} )) is perpendicular to this plane.
  2. The direction follows the right-hand rule: If you curl the fingers of your right hand from ( \vec{A} ) towards ( \vec{B} ), your thumb points in the direction of ( \vec{C} ).

The cross product gives you a vector that is perpendicular to the plane containing the two input vectors. Its direction is determined by the right-hand rule, and its magnitude is equal to the area of the parallelogram spanned by ( \vec{A} ) and ( \vec{B} ). This concept is crucial in physics for torque calculations, in computer graphics for surface normal calculations, and in many other fields.

Solution

Here’s how to implement the cross product operation in Java, C++, and Python.

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public class CrossProduct {
    public static double[] cross(double[] A, double[] B) {
        double[] C = new double[3];
        C[0] = A[1] * B[2] - A[2] * B[1];
        C[1] = A[2] * B[0] - A[0] * B[2];
        C[2] = A[0] * B[1] - A[1] * B[0];
        return C;
    }

    public static void main(String[] args) {
        double[] A = {1, 0, 0};
        double[] B = {0, 1, 0};
        double[] result = cross(A, B);

        for (double num : result) {
            System.out.print(num + " ");
        }
    }
}

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <array>

std::array<double, 3> cross(const std::array<double, 3>& A, const std::array<double, 3>& B) {
    std::array<double, 3> C;
    C[0] = A[1] * B[2] - A[2] * B[1];
    C[1] = A[2] * B[0] - A[0] * B[2];
    C[2] = A[0] * B[1] - A[1] * B[0];
    return C;
}

int main() {
    std::array<double, 3> A = {1, 0, 0};
    std::array<double, 3> B = {0, 1, 0};
    std::array<double, 3> result = cross(A, B);

    for (const auto& num : result) {
        std::cout << num << " ";
    }
    std::cout << std::endl;
}

Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def cross(A, B):
    C = [0, 0, 0]
    C[0] = A[1] * B[2] - A[2] * B[1]
    C[1] = A[2] * B[0] - A[0] * B[2]
    C[2] = A[0] * B[1] - A[1] * B[0]
    return C

A = [1, 0, 0]
B = [0, 1, 0]
result = cross(A, B)
print(result)

Key Takeaways

  • The cross product results in a vector that is perpendicular to both input vectors.
  • Each component of the result vector ( \textbf{C} ) is calculated using specific elements from ( \textbf{A} ) and ( \textbf{B} ).
  • The above implementations are specific to three-dimensional vectors.
  • The order of operands matters: ( \textbf{A} \times \textbf{B} ) is not the same as ( \textbf{B} \times \textbf{A} ).