forked from Open-Deep-ML/DML-OpenProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path118.json
More file actions
42 lines (42 loc) · 3 KB
/
Copy path118.json
File metadata and controls
42 lines (42 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{
"id": "118",
"title": "Compute the Cross Product of Two 3D Vectors",
"difficulty": "easy",
"category": "Linear Algebra",
"video": "",
"likes": "0",
"dislikes": "0",
"contributor": [
{
"profile_link": "https://github.com/moe18",
"name": "Moe Chabot"
}
],
"description": "Implement a function to compute the cross product of two 3-dimensional vectors. The cross product of two vectors results in a third vector that is perpendicular to both and follows the right-hand rule. This concept is fundamental in physics, engineering, and 3D graphics.",
"learn_section": "## Understanding the Cross Product\n\nThe **cross product** of two vectors $\\vec{a}$ and $\\vec{b}$ in 3D space is a vector that is perpendicular to both $\\vec{a}$ and $\\vec{b}$.\n\n### Properties\n- Defined only in 3 dimensions.\n- The result $\\vec{c} = \\vec{a} \\times \\vec{b}$ is perpendicular to both $\\vec{a}$ and $\\vec{b}$.\n- Follows the right-hand rule.\n\n### Mathematical Formula\n\nGiven:\n- $\\vec{a} = [a_1, a_2, a_3]$\n- $\\vec{b} = [b_1, b_2, b_3]$\n\nThe cross product is:\n\n$$\n\\vec{a} \\times \\vec{b} = [a_2 b_3 - a_3 b_2,\\ a_3 b_1 - a_1 b_3,\\ a_1 b_2 - a_2 b_1]\n$$\n\n### Use Cases\n- Calculating normals in 3D graphics.\n- Determining torque and angular momentum in physics.\n- Verifying orthogonality in machine learning geometry.\n\n### Example\nFor $\\vec{a} = [1, 0, 0]$ and $\\vec{b} = [0, 1, 0]$:\n\n$$\n\\vec{a} \\times \\vec{b} = [0, 0, 1]\n$$\n\nThe result points in the $z$-axis direction, confirming perpendicularity to both $\\vec{a}$ and $\\vec{b}$.",
"starter_code": "import numpy as np\n\ndef cross_product(a, b):\n # Your code here\n pass",
"solution": "import numpy as np\n\ndef cross_product(a, b):\n \"\"\"\n Compute the cross product of two 3D vectors a and b.\n Parameters:\n a (array-like): A 3-element vector.\n b (array-like): A 3-element vector.\n Returns:\n numpy.ndarray: The cross product vector.\n \"\"\"\n a = np.array(a)\n b = np.array(b)\n\n if a.shape != (3,) or b.shape != (3,):\n raise ValueError(\"Both input vectors must be of length 3.\")\n\n cross = np.array([\n a[1] * b[2] - a[2] * b[1],\n a[2] * b[0] - a[0] * b[2],\n a[0] * b[1] - a[1] * b[0]\n ])\n return cross",
"example": {
"input": "cross_product([1, 0, 0], [0, 1, 0])",
"output": "[0, 0, 1]",
"reasoning": "The cross product of two orthogonal unit vectors [1, 0, 0] and [0, 1, 0] is [0, 0, 1], pointing in the positive z-direction as per the right-hand rule."
},
"test_cases": [
{
"test": "print(cross_product([1, 0, 0], [0, 1, 0]))",
"expected_output": "[0, 0, 1]"
},
{
"test": "print(cross_product([0, 1, 0], [0, 0, 1]))",
"expected_output": "[1, 0, 0]"
},
{
"test": "print(cross_product([1, 2, 3], [4, 5, 6]))",
"expected_output": "[-3, 6, -3]"
},
{
"test": "print(cross_product([1, 0, 0], [1, 0, 0]))",
"expected_output": "[0, 0, 0]"
}
]
}