In SymPy, trigonometric functions like sin, cos, and tan have an 
.expand(trig=True) method. This method applies standard trigonometric 
identities (like the addition formula).

However, there was a bug where these identities were being applied to 
*non-commutative* symbols (like Matrices or Quantum Operators). In 
non-commutative algebra, the standard identity:
$$\sin(A+B) = \sin(A)\cos(B) + \cos(A)\sin(B)$$

is *mathematically incorrect* because it assumes $AB = BA$. Applying this 
expansion to non-commutative objects leads to wrong results in physics and 
linear algebra.
*The Evidence*

If you define $A$ and $B$ as non-commutative, SymPy should "do nothing" 
when asked to expand, but it was incorrectly simplifying them:
Python
A, B = symbols('A B', commutative=False) print(expand(sin(A + B), trig=True)) 
# BUGGY OUTPUT: sin(A)*cos(B) + sin(B)*cos(A) # CORRECT OUTPUT: sin(A + B) 
*The Solution*

The fix involved adding a "Guard Clause" to the _eval_expand_trig method in 
the sin, cos, and tan classes. This check ensures that if the argument is 
non-commutative, the function returns itself instead of applying the 
commutative identity.

*File:* sympy/functions/elementary/trigonometric.py

*The Fix:*
Python
def _eval_expand_trig(self, **hints): arg = self.args[0] # Check if symbols 
commute. If not, standard identities do not apply. if arg.is_commutative is 
False: return self # ... rest of the expansion logic ... 
*Verification*

I wrote a comprehensive test suite to ensure the fix works across all 
primary trig functions while ensuring that standard (commutative) math is 
*not* broken:

   1. 
   
   *Non-Commutative Test:* expand(sin(A+B)) returns sin(A+B) (Passed).
   2. 
   
   *Commutative Test:* expand(sin(x+y)) still returns the standard 
   expansion (Passed).
   3. 
   
   *Mixed Case:* expand(sin(A+x)) is recognized as non-commutative and 
   stays unexpanded (Passed).
   
*Impact*

This fix ensures mathematical consistency across SymPy. By making the 
Trigonometry module respect the is_commutative flag (just like the 
Exponential and Power modules already do), we prevent silent errors in 
advanced mathematical computations.

should i make PR for this??


-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/sympy/35c37c1a-7eb8-459f-ac22-ae28cfc0f05en%40googlegroups.com.

Reply via email to