Title: Fixing Non-Commutative Trigonometric Expansion in SymPyThe Problem

20 views
Skip to first unread message

shuvro bhattacharjee

unread,
Jan 26, 2026, 1:01:15 PM (7 days ago) Jan 26
to sympy

SymPy was assuming that all variables behave like regular numbers (where 2 x3 is the same as 3x2).

However, in advanced math (like Matrices or Quantum Physics), the order matters (AxB is not the same as BxA). This is called Non-Commutative math.

SymPy was incorrectly using high-school formulas like:

sin(A+B) = sin(A)cos(B) + sin(B)cos(A)

This formula is wrong for Matrices. It should have stayed as just sin(A+B).


The Solution: The "Wait, Stop!" Rule

I added a "Guard Clause" (a safety check) to the code for sin, cos, and tan.

The logic I added is simple:

Before expanding a formula, the computer now asks: "Are these variables non-commutative?" * If Yes: It stops and leaves the expression alone (Fixed!).

  • If No: It expands it like normal (Standard math still works).

Reply all
Reply to author
Forward
0 new messages