Numpy Examples

SVD Example 1:

import numpy as np

def compute_transformation_matrix(G, L):
    """Compute R from the equation G = R L R^T using SVD when L and G are not positive definite."""

    # Compute SVD of L
    U_L, Sigma_L, V_L_T = np.linalg.svd(L)
    Sigma_L = np.diag(Sigma_L)  # Convert to diagonal matrix

    # Compute SVD of G
    U_G, Sigma_G, V_G_T = np.linalg.svd(G)
    Sigma_G = np.diag(Sigma_G)  # Convert to diagonal matrix

    # Compute R
    R = U_G @ np.sqrt(Sigma_G) @ V_G_T @ V_L_T.T @ np.linalg.inv(np.sqrt(Sigma_L)) @ U_L.T
    return R

# Example Matrices (L and G are not positive definite)
G = np.array([[0.49469, -0.21603, 0], 
              [-0.21603, -0.47129, 0], 
              [0, 0, 0]])

L = np.array([[-0.41930, -0.30689, 0], 
              [-0.30689, 0.4427, 0], 
              [0, 0, 1]])

# Compute R
R = compute_transformation_matrix(G, L)
print("Computed R matrix:")
print(R)
import numpy as np

def transform_stress_to_global(Sx_local, Sy_local, Sxy_local, theta):
    """
    Transforms stress from local to global coordinates.

    Parameters:
    Sx_local, Sy_local, Sxy_local : float
        Stresses in local coordinate system.
    theta : float
        Rotation angle in degrees (from local to global system).

    Returns:
    (Sx_global, Sy_global, Sxy_global) : tuple
        Transformed stresses in the global coordinate system.
    """
    # Convert theta to radians
    theta_rad = np.radians(theta)

    # Compute transformation matrix R
    c = np.cos(theta_rad)
    s = np.sin(theta_rad)

    R = np.array([
        [c**2, s**2,  2*c*s],
        [s**2, c**2, -2*c*s],
        [-c*s, c*s,   c**2 - s**2]
    ])

    # Local stress vector
    S_local = np.array([Sx_local, Sy_local, Sxy_local])

    # Compute global stresses
    S_global = R @ S_local

    return S_global[0], S_global[1], S_global[2]

# Example Usage
Sx_local, Sy_local, Sxy_local = 100, 50, 30  # Local stresses
theta = 45  # Rotation angle in degrees

Sx_global, Sy_global, Sxy_global = transform_stress_to_global(Sx_local, Sy_local, Sxy_local, theta)

print(f"Global Stress Sx: {Sx_global:.2f}")
print(f"Global Stress Sy: {Sy_global:.2f}")
print(f"Global Shear Stress Sxy: {Sxy_global:.2f}")