import sympy as sp
from sympy import symbols, Matrix, Rational

def noncommutative_det_for_factorization(n):
    """
    Builds the non‑commutative determinant matrix for factoring n.
    Returns the symbolic determinant (non‑commutative product).
    """
    # Find prime factors (classically, but only to construct the operators)
    # We are not using them in the determinant – they appear as eigenvalues.
    factors = []
    temp = n
    for p in [2,3,5,7,11,13,17,19,23,29,31,37]:
        while temp % p == 0:
            factors.append(p)
            temp //= p
    if temp > 1:
        factors.append(temp)
    
    # Create non‑commuting symbols for the core operator C and cloud operator P
    C, P = sp.symbols('C P', commutative=False)
    # Φ(P) = P^{-1} * (C - 1)  (a simple choice from number theory)
    Phi = P**(-1) * (C - 1)
    
    # Build the 2x2 coupling matrix
    M = Matrix([[C, P],
                [Phi, P]])
    
    # Compute the non‑commutative determinant (Dieudonné)
    # For a 2x2 matrix [[a,b],[c,d]] over a skew field, det_nc = a*d - a*b*a^{-1}*c
    a, b, c, d = M[0,0], M[0,1], M[1,0], M[1,1]
    a_inv = sp.powsimp(a**(-1))  # symbolic inverse
    det_nc = a*d - a*b*a_inv*c
    
    # Simplify using commutation relation: [C, P] = i (set ℏ=1)
    # Replace P*C with C*P + i
    i = sp.I
    det_nc_expanded = det_nc.replace(P*C, C*P + i)
    det_nc_expanded = sp.simplify(det_nc_expanded)
    
    return det_nc_expanded

# Example: factor 15
n = 11
det = noncommutative_det_for_factorization(n)
print(f"Non‑commutative determinant for n={n}:")
sp.pprint(det)
print(f"Is det ≠ 0? {det != 0}")
