<jupyter_start><jupyter_text>Taking Cross Products in Python - Python Functions Objective+ Learn how to define a Python function.+ Define a Python function that computes the cross product of two Cartesian vectors.Cross products are quite prone to numerical errors when carried out by hand.Let us define a Python function that can take cross products between two vectors with Cartesian descriptions:$$\begin{array}{ccc}\vec{r_1} &=& x_1\hat{i} + y_1\hat{j} + z_1\hat{k},\\\vec{r_2} &=& x_2\hat{i} + y_2\hat{j} + z_2\hat{k}\end{array}$$The defintion of the cross product is:$$\vec{r_1}\times \vec{r_2} = \left|\begin{array}{ccc}\hat{i} & \hat{j} & \hat{k}\\x_1 & y_1 & z_1\\x_2 & y_2 & z_2\end{array}\right|=(y_1z_2 - y_2z_1)\hat{i} - (x_1z_2 - x_2z_1)\hat{j} + (x_1y_2 - x_2y_1)\hat{k}.$$Let us define this function in Python.<jupyter_code>import numpy as np

def cross_product(r1, r2):
    """
    Compute the cross product between 3D vectors r1 and r2.
    
    :param r1:   A 3D vector.
    :param r2:   A 3D vector.
    :returns:    A 3D vector representing the cross product of r1 and r2.
    """
    result = np.array([0, 0, 0]) # The result of the calculation
    result[0] = r1[1] * r2[2] - r1[2] * r2[1]    # The i component of the cross product
    result[1] = -(r1[0] * r2[2] - r1[2] * r2[0]) # The j component of the cross product
    result[2] = r1[0] * r2[1] - r1[1] * r2[0]    # The k component of the cross product
    return result<jupyter_output><empty_output><jupyter_text>Let us try it with [Problem 4.B.4](https://youtu.be/2n7xHWWuvlQ) of the lecturebook where we have to calculate the moment $\vec{M_O}$ of the force $\vec{F_{AB}}$ about point $O$ in two different ways.<jupyter_code># Some of the data we need for the calculation
# The force vector
F_AB = np.array([0.36, 0.68, -0.91])  # kN
print('F_AB = ' + str(F_AB) + ' in kN')
# The position vector r_OA
r_OA = np.array([1.6, 0., 2.]) # m
print('r_OA = ' + str(r_OA) + ' in m')
# The position vector r_OB
r_OB = np.array([2.4, 1.5, 0]) # m
print('r_OB = ' + str(r_OB) + ' in m')
# b) Moment about O using r_OA:
M_O_b = cross_product(r_OA, F_AB)
print('M_O_b = ' + str(M_O_b) + ' in kN-m')
# c) Moment about O using r_OB:
M_O_c = cross_product(r_OB, F_AB)
print('M_O_c = ' + str(M_O_c) + ' in kN-m')<jupyter_output>M_O_c = [-1  2  1] in kN-m
{'fraction_non_alphanumeric': 0.15451014242555028, 'fraction_numerical': 0.041001294777729826, 'mean_word_length': 3.6923076923076925, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, '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}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '273171', 'license': 'MIT', 'n_tokens_mistral': 978, 'n_tokens_neox': 938, 'n_words': 278, 'path': 'cross_products_and_python_functions.ipynb', 'repo_name': 'PurdueMechanicalEngineering/me270', 'size': '2317'}