# Symbolic simplificaction without commutativity

I would like to expand the expression:

$$(a_x p_x + a_y p_y + a_z p_z + b m)^2$$

where:

$a_x a_y \neq a_y a_z$

Or generally speaking the objects $a_x,a_y,a_z,b$ are non-commutative for multiplication.

edit retag close merge delete

Sort by » oldest newest most voted

sage: R.<ax,ay,az,px,py,pz> = FreeAlgebra(QQ)
sage: R
Free Algebra on 6 generators (ax, ay, az, px, py, pz) over Rational Field
sage: (ax*px + ay*py + az*pz)^2
ax*px*ax*px + ax*px*ay*py + ax*px*az*pz + ay*py*ax*px + ay*py*ay*py + ay*py*az*pz + az*pz*ax*px + az*pz*ay*py + az*pz*az*pz

more

If FreeAlgebra doesn't provide what you're looking for, note that Maxima also implements noncommutative symbolic algebra, and you can access it from sage with, e.g., the maxima command:

sage: e = maxima('expand((ax . px + ay . py + az . pz)^^2);')
sage: e
(az.pz)^^2+az.pz.ay.py+az.pz.ax.px+(ay.py)^^2+ay.py.az.pz+ay.py.ax.px+(ax.px)^^2+ax.px.az.pz+ax.px.ay.py


Note that this uses Maxima's syntax, which is generally different from Sage's. You can read more about it starting here (SO) or here (Maxima manual).

more

In your particular example, you are doing the computation on the free algebra on ax,ay,az,b over the polynomial ring on px,py,pz,m.

sage: R.<px,py,pz,m>=QQ[]
sage: F.<ax,ay,az,b>=FreeAlgebra(R)
sage: (ax*px+ay*py+az*pz+m*b)^2
px^2*ax^2 + px*py*ax*ay + px*pz*ax*az + px*m*ax*b + px*py*ay*ax + py^2*ay^2 + py*pz*ay*az + py*m*ay*b + px*pz*az*ax + py*pz*az*ay + pz^2*az^2 + pz*m*az*b + px*m*b*ax + py*m*b*ay + pz*m*b*az + m^2*b^2

more