Linear Algebra Operations

Utility functions for vector and signal-level linear algebra operations.

biomechzoo.linear_algebra_ops.compute_magnitude_data.compute_magnitude_line(x, y, z)[source]
biomechzoo.linear_algebra_ops.compute_magnitude_data.compute_magnitude_data(data, ch_x, ch_y, ch_z, ch_new_name=None)[source]

Compute the magnitude of acceleration data from IMU channels (BiomechZoo format).

Returns the magnitude

biomechzoo.linear_algebra_ops.rectify.compute_magnitude_line(x, y, z)[source]
biomechzoo.linear_algebra_ops.rectify.rectify_data(data, chs)[source]

Take absolute value of channels

biomechzoo.linear_algebra_ops.rectify.rectify_line(yd)[source]
biomechzoo.linear_algebra_ops.kinematics.rotate_dcm_data(data, ch, axis, degrees)[source]

Apply a rotation about a principal axis to one segment’s DCM.

Parameters:
  • data (dict) – Zoo data dictionary containing DCM channels for the segment to be rotated.

  • ch (list[str]) – List of 3 channel names identifying the DCM to rotate, ordered i, j, k (e.g., ['i_LSh', 'j_LSh', 'k_LSh']).

  • axis ({‘X’, ‘Y’, ‘Z’}) – Principal axis to rotate about (case-insensitive).

  • degrees (float) – Rotation angle in degrees.

Returns:

dict – The input data dictionary with the DCM channels updated in place to reflect the applied rotation.

Raises:

ValueError – If ch does not have exactly 3 elements, if all channels do not belong to the same segment, or if axis is not 'X', 'Y', or 'Z'.

Parameters:
Return type:

dict

biomechzoo.linear_algebra_ops.kinematics.quats2euler_data(data, ch_prox, ch_dist, sequence)[source]

Compute Euler angles of the distal segment relative to the proximal segment from quaternion data stored in a zoo data dictionary.

Parameters:
  • data (dict) – Zoo data dictionary containing quaternion channels for the proximal and distal segments.

  • ch_prox (list[str]) – List of 4 channel names for the proximal segment’s quaternion components, ordered W, X, Y, Z (e.g., ['Quat_W_LSh', 'Quat_X_LSh', 'Quat_Y_LSh', 'Quat_Z_LSh']).

  • ch_dist (list[str]) – List of 4 channel names for the distal segment’s quaternion components, ordered W, X, Y, Z (e.g., ['Quat_W_LH', 'Quat_X_LH', 'Quat_Y_LH', 'Quat_Z_LH']).

  • sequence (str) – Euler angle rotation sequence passed to scipy.spatial.transform.Rotation.as_euler(). Case determines intrinsic (uppercase) vs extrinsic (lowercase) rotations (e.g., 'ZXY' for intrinsic, 'zxy' for extrinsic).

Returns:

dict – The input data dictionary updated with three new channels: '<prox>_<dist>_alpha', '<prox>_<dist>_beta', and '<prox>_<dist>_gamma', containing the first, second, and third Euler angles (in degrees) respectively, where <prox> and <dist> are the segment labels extracted from prox_ch and dist_ch.

Raises:

ValueError – If prox_ch or dist_ch do not have exactly 4 elements.

Parameters:
Return type:

dict

References

https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.transform.Rotation.html

biomechzoo.linear_algebra_ops.kinematics.dcms2euler_data(data, ch_prox, ch_dist, sequence)[source]

Compute Euler angles of the distal segment relative to the proximal segment from direction cosine matrices (DCMs) stored in a zoo data dictionary.

Parameters:
  • data (dict) – Zoo data dictionary containing DCM channels for the proximal and distal segments.

  • ch_prox (list[str]) – List of 3 channel names for the proximal segment’s DCM column vectors, ordered i, j, k (e.g., ['i_LSh', 'j_LSh', 'k_LSh']).

  • ch_dist (list[str]) – List of 3 channel names for the distal segment’s DCM column vectors, ordered i, j, k (e.g., ['i_LH', 'j_LH', 'k_LH']).

  • sequence (str) – Euler angle rotation sequence passed to scipy.spatial.transform.Rotation.as_euler(). Case determines intrinsic (uppercase) vs extrinsic (lowercase) rotations (e.g., 'ZXY' for intrinsic, 'zxy' for extrinsic).

Returns:

dict – The input data dictionary updated with three new channels: '<prox>_<dist>_alpha', '<prox>_<dist>_beta', and '<prox>_<dist>_gamma', containing the first, second, and third Euler angles (in degrees) respectively, where <prox> and <dist> are the segment labels extracted from prox_ch and dist_ch.

Raises:

ValueError – If prox_ch or dist_ch do not have exactly 3 elements.

Parameters:
Return type:

dict

References

https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.transform.Rotation.html

biomechzoo.linear_algebra_ops.kinematics.marker2dcm_data(data, seg, origin, marker_1, marker_2)[source]

Compute a right-handed local coordinate system (LCS) from motion capture marker positions and store it as a direction cosine matrix (DCM) in the zoo data dictionary.

Parameters:
  • data (dict) – Zoo data dictionary containing motion capture marker channels.

  • seg (str) – Segment label used to name the output DCM channels (e.g., 'LSh' produces 'i_LSh', 'j_LSh', 'k_LSh').

  • origin (str) – Label of the marker defining the origin of the local coordinate system. Supports both full (e.g., 'LeftShank1') and abbreviated (e.g., 'LShank1') naming conventions.

  • marker_1 (str) – Label of the marker defining the primary axis (i). Same naming conventions as origin.

  • marker_2 (str) – Label of the marker used to define the temporary vector for computing the orthogonal axes via cross product. Same naming conventions as origin.

Returns:

dict – The input data dictionary updated with three new channels: 'i_<seg>', 'j_<seg>', and 'k_<seg>', containing the first, second, and third column vectors of the DCM respectively.

Raises:

ValueError – If the resulting DCM is not orthonormal.

Parameters:
Return type:

dict

Notes

The LCS is constructed as follows:

  • i = unit vector from origin to marker_1

  • j_temp = unit vector from origin to marker_2

  • k = unit vector of i × j_temp

  • j = k × i

Orthonormality is verified before storing.

biomechzoo.linear_algebra_ops.kinematics.quats2dcm_data(data, seg, ch)[source]

Compute a direction cosine matrix (DCM) from quaternion data and store it in the zoo data dictionary.

Parameters:
  • data (dict) – Zoo data dictionary containing quaternion channels for the segment.

  • seg (str) – Segment label used to name the output DCM channels (e.g., 'LSh' produces 'i_LSh', 'j_LSh', 'k_LSh').

  • ch (list[str]) – List of 4 quaternion channel names ordered W, X, Y, Z (e.g., ['Quat_W_LSh', 'Quat_X_LSh', 'Quat_Y_LSh', 'Quat_Z_LSh']).

Returns:

dict – The input data dictionary updated with three new channels: 'i_<seg>', 'j_<seg>', and 'k_<seg>', containing the first, second, and third column vectors of the DCM respectively.

Raises:

ValueError – If ch does not have exactly 4 elements.

Parameters:
Return type:

dict

References

https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.transform.Rotation.html

biomechzoo.linear_algebra_ops.make_unit.make_unit(v)[source]

Normalise a vector or array of vectors to unit length.

Parameters:

v (ndarray) – Input vector or array of vectors. The last axis is treated as the vector dimension.

Returns:

ndarray – Array of the same shape as v where each vector has been divided by its L2 norm, resulting in unit length along the last axis.

Parameters:

v (ndarray)

Return type:

ndarray