<jupyter_start><jupyter_text>Compute $\boldsymbol{J}$ in Kinetic EnergyWe can define the rotational kinetic energy of a single body as$$\frac{1}{2} \boldsymbol{\omega}^{T} \boldsymbol{I} \boldsymbol{\omega}=\frac{1}{2} \operatorname{tr}\left([\boldsymbol{\omega}] \boldsymbol{J}[\boldsymbol{\omega}]^{T}\right),$$where $\boldsymbol{J}$ is a diagonal PD matrix that ensures the equality above holds. The following notebook derives the definition of $\boldsymbol{J}$.<jupyter_code>import numpy
import sympy.vector
from IPython.display import Latex, Math, display
from sympy import *

init_printing()
def hat(x):
    return numpy.array([[0, -x[2], x[1]], [x[2], 0, -x[0]], [-x[1], x[0], 0]],
                       dtype=x.dtype)
omega = numpy.array(symbols("omega_x omega_y omega_z"))
I = numpy.diag(symbols("I_x I_y I_z"))
J = numpy.diag(symbols("J_0 J_1 J_2"))
lhs = omega.T @ I @ omega
display(lhs.collect(omega))
lhs_coeffs = [lhs.coeff(omega[i], 2) for i in range(omega.size)]
display(lhs_coeffs)
rhs = numpy.trace(hat(omega) @ J @ hat(omega).T)
display(rhs.collect(omega))
rhs_coeffs = [rhs.coeff(omega[i], 2) for i in range(omega.size)]
display(rhs_coeffs)
eqs = [
    Eq(lhs_coeff, rhs_coeff)
    for lhs_coeff, rhs_coeff in zip(lhs_coeffs, rhs_coeffs)
]
display(eqs)
J_of_I = solve(eqs, numpy.diag(J).tolist())
J_solved = numpy.diag([J_of_I[numpy.diag(J)[i]] for i in range(3)])
display(Latex("$\\boldsymbol{J}=$"), Matrix(J_solved))
x = numpy.array(symbols("x y z"), dtype=Symbol)
(x.T @ J_solved @ x).collect(numpy.diag(I))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1792573623559539, 'fraction_numerical': 0.012163892445582587, 'mean_word_length': 5.539748953974895, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '613885', 'license': 'MIT', 'n_tokens_mistral': 612, 'n_tokens_neox': 601, 'n_words': 134, 'path': 'notebooks/compute_J.ipynb', 'repo_name': 'ipc-sim/rigid-ipc', 'size': '1562'}