SortProducts - Maple Help

Physics[SortProducts] - sort the operands of the products of a given expression according to an indicated ordering

 Calling Sequence SortProducts(ee, ordering, {totheleft, totheright, useanticommutator, usecommutator, useinert})

Parameters

 ee - any algebraic expressions containing products with non-commutative operands, or any valid Maple object containing such expressions ordering - a list indicating the desired ordering for (some or all of) the operands in the products in ee totheleft = ... - optional, can be true, false, or the string "not given" (default). It indicates where the desired ordering of the operands in products should be placed: to the left when true, to the right when false, or in place when "not given" totheright = ... - optional, can be true, false, or the string "not given" (default). It indicates where the desired ordering of the operands should be placed: to the right when true, to the left when false, or in place when "not given" useanticommutator = ... - optional, can be true, false, or the string "not given" (default). It forces reordering any two product operands that do not commute, by using A*B = - B*A + AntiCommutator(A, B), even if the value of AntiCommutator(A, B) is not known to the system. usecommutator = ... - optional, can be true, false, or the string "not given" (default). It forces reordering any two product operands that do not commute, by using A*B = B*A + Commutator(A, B), even if the value of Commutator(A, B) is not known to the system. useinert = ... - optional, can be true, false, or the string "not given" (default). It forces using the inert forms %Commutator and %AntiCommutator regardless of whether the value of the corresponding Commutator and AntiCommutator is known to the system.

Description

 • SortProducts receives an expression involving products and sorts the operands of these products according to the ordering indicated as the second argument, a list containing some or all of the operands of the product. The sorting of operands performed automatically takes into account any algebra rules set using Setup.
 • By default, the sorting between two operands A and B is performed when the value of their Commutator or AntiCommutator is known to the system (can be resolved). Note that if a Commutator can be resolved, so is the AntiCommutator and viceversa. To have the sorting performed even when the Commutator cannot be resolved, use the options usecommutator or useanticommutator, in which case the output will respectively contain an unresolved (unevaluated) Commutator or AntiCommutator. The options usecommutator and useanticommutator cannot be used simultaneously.
 • When the options usecommutator or useanticommutator are used, commutators or anticommutators introduced by SortProducts will be expressed using the active Commutator and AntiCommutator commands unless the operands of these commutators or anticommutators involve products, in which case they will be represented using the inert %Commutator or %AntiCommutator commands. This has the purpose of allowing for further manipulations of the output without having these commutators introduced by SortProducts automatically expanded by the corresponding active command (for details on this automatic expansion, see Commutator).
 • To override that default behavior, you can pass the optional argument useinert, or useinert = true. In that case, all the Commutators and AntiCommutators introduced by SortProducts will be expressed using the inert commands %Commutator or %AntiCommutator, regardless of whether these commutators can be resolved.
 • Equivalently, passing useinert = false all the Commutators and AntiCommutators introduced by SortProducts will be expressed in active form, using the Commutator or AntiCommutator commands, regardless of whether their operands involve products, in which case their expansion will be automatically performed as explained in the help page for Commutator.
 • SortProducts sorts the operands of the products 'in place', optionally positioning, when possible, the sorted operands to the left or to the right of the unsorted operands, when respectively passing the optional arguments totheleft or totheright. The option totheright cannot be used simultaneously with either of usecommutator and useanticommutator; so when using this option the sorting of say A and B is performed only if their Commutator can be resolved.
 • SortProducts is relevant mainly to sort the operands in noncommutative products expressed using the Physics:-* product operator, although it can also sort the operands in commutative products expressed with Maple's standard product operator, *. Note that the sorting of operands in commutative products within a sum may be reverted by the Maple kernel.

Examples

 > $\mathrm{with}\left(\mathrm{Physics}\right):$

Consider the product of the commutative $a,b,c,d$

 > $P≔abcd$
 ${P}{≔}{a}{}{b}{}{c}{}{d}$ (1)

Reorder the operands $c$ and $b$ "in place".

 > $\mathrm{SortProducts}\left(P,\left[c,b\right]\right)$
 ${a}{}{c}{}{b}{}{d}$ (2)

Sort the operands $b$ and $c$ and put them to the left, then to the right

 > $\mathrm{SortProducts}\left(P,\left[c,b\right],\mathrm{totheleft}\right)$
 ${c}{}{b}{}{a}{}{d}$ (3)
 > $\mathrm{SortProducts}\left(P,\left[c,b\right],\mathrm{totheright}\right)$
 ${a}{}{d}{}{c}{}{b}$ (4)

Set a prefix identifying noncommutative variables and related algebra rules for some of them, such that $\mathrm{Z1},\mathrm{Z2},\mathrm{Z3}$ and $\mathrm{Z4}$ commute between themselves, but none of them commute with $\mathrm{Z5}$.

 > $\mathrm{Setup}\left(\mathrm{noncommutativeprefix}=Z,\mathrm{%Commutator}\left(\mathrm{Z1},\mathrm{Z2}\right)=0,\mathrm{%Commutator}\left(\mathrm{Z1},\mathrm{Z3}\right)=0,\mathrm{%Commutator}\left(\mathrm{Z2},\mathrm{Z3}\right)=0,\mathrm{%Commutator}\left(\mathrm{Z3},\mathrm{Z4}\right)=0,\mathrm{%Commutator}\left(\mathrm{Z2},\mathrm{Z4}\right)=0,\mathrm{%Commutator}\left(\mathrm{Z1},\mathrm{Z4}\right)=0\right)$
 $\left[{\mathrm{algebrarules}}{=}\left\{{\mathrm{%Commutator}}{}\left({\mathrm{Z1}}{,}{\mathrm{Z2}}\right){=}{0}{,}{\mathrm{%Commutator}}{}\left({\mathrm{Z1}}{,}{\mathrm{Z3}}\right){=}{0}{,}{\mathrm{%Commutator}}{}\left({\mathrm{Z1}}{,}{\mathrm{Z4}}\right){=}{0}{,}{\mathrm{%Commutator}}{}\left({\mathrm{Z2}}{,}{\mathrm{Z3}}\right){=}{0}{,}{\mathrm{%Commutator}}{}\left({\mathrm{Z2}}{,}{\mathrm{Z4}}\right){=}{0}{,}{\mathrm{%Commutator}}{}\left({\mathrm{Z3}}{,}{\mathrm{Z4}}\right){=}{0}\right\}{,}{\mathrm{noncommutativeprefix}}{=}\left\{{Z}\right\}\right]$ (5)
 > $P≔\mathrm{Z1}\mathrm{Z2}\mathrm{Z3}\mathrm{Z4}$
 ${P}{≔}{\mathrm{Z1}}{}{\mathrm{Z2}}{}{\mathrm{Z3}}{}{\mathrm{Z4}}$ (6)

Sort Z2 and Z3 "in place".

 > $\mathrm{SortProducts}\left(P,\left[\mathrm{Z3},\mathrm{Z2}\right]\right)$
 ${\mathrm{Z1}}{}{\mathrm{Z3}}{}{\mathrm{Z2}}{}{\mathrm{Z4}}$ (7)
 > $\mathrm{SortProducts}\left(P,\left[\mathrm{Z3},\mathrm{Z2}\right],\mathrm{totheright}\right)$
 ${\mathrm{Z1}}{}{\mathrm{Z4}}{}{\mathrm{Z3}}{}{\mathrm{Z2}}$ (8)

The value of the commutator between Z1 and Z5 is not known to the system, so by default they are not sorted:

 > $\mathrm{SortProducts}\left(\mathrm{Z1}\mathrm{Z5},\left[\mathrm{Z5},\mathrm{Z1}\right]\right)$
 ${\mathrm{Z1}}{}{\mathrm{Z5}}$ (9)

Force their sorting using their commutator or anticommutator

 > $\mathrm{SortProducts}\left(\mathrm{Z1}\mathrm{Z5},\left[\mathrm{Z5},\mathrm{Z1}\right],\mathrm{usecommutator}\right)$
 ${\mathrm{*}}{}\left({\mathrm{Z5}}{,}{\mathrm{Z1}}\right){+}{\mathrm{Commutator}}{}\left({\mathrm{Z1}}{,}{\mathrm{Z5}}\right)$ (10)
 > $\mathrm{expand}\left(\right)$
 ${\mathrm{Z1}}{}{\mathrm{Z5}}$ (11)
 > $\mathrm{SortProducts}\left(\mathrm{Z1}\mathrm{Z5},\left[\mathrm{Z5},\mathrm{Z1}\right],\mathrm{useanticommutator}\right)$
 ${-}{\mathrm{*}}{}\left({\mathrm{Z5}}{,}{\mathrm{Z1}}\right){+}{\mathrm{AntiCommutator}}{}\left({\mathrm{Z1}}{,}{\mathrm{Z5}}\right)$ (12)
 > $\mathrm{expand}\left(\right)$
 ${\mathrm{Z1}}{}{\mathrm{Z5}}$ (13)

Enter the product of $P$ with $\mathrm{Z5}$ at the end (so, to the right of P) and sort it with $\mathrm{Z5}$ to the left. This is a case where $\mathrm{Z5}$ does not commute with any of the other operands. Compare the results with and without the option usecommutator,

 > $P≔P\mathrm{Z5}$
 ${P}{≔}{\mathrm{Z1}}{}{\mathrm{Z2}}{}{\mathrm{Z3}}{}{\mathrm{Z4}}{}{\mathrm{Z5}}$ (14)
 > $\mathrm{SortProducts}\left(P,\left[\mathrm{Z5},\mathrm{Z1}\right]\right)$
 ${\mathrm{Z1}}{}{\mathrm{Z2}}{}{\mathrm{Z3}}{}{\mathrm{Z4}}{}{\mathrm{Z5}}$ (15)
 > $\mathrm{SortProducts}\left(P,\left[\mathrm{Z5},\mathrm{Z1}\right],\mathrm{usecommutator}\right)$
 ${\mathrm{*}}{}\left({\mathrm{Z5}}{,}{\mathrm{Z1}}{,}{\mathrm{Z2}}{,}{\mathrm{Z3}}{,}{\mathrm{Z4}}\right){-}{\mathrm{%Commutator}}{}\left({\mathrm{Z5}}{,}{\mathrm{*}}{}\left({\mathrm{Z1}}{,}{\mathrm{Z2}}{,}{\mathrm{Z3}}{,}{\mathrm{Z4}}\right)\right)$ (16)
 > $\mathrm{SortProducts}\left(P,\left[\mathrm{Z5},\mathrm{Z3}\right],\mathrm{usecommutator}\right)$
 ${\mathrm{*}}{}\left({\mathrm{Z1}}{,}{\mathrm{Z2}}{,}{\mathrm{*}}{}\left({\mathrm{Z5}}{,}{\mathrm{Z3}}{,}{\mathrm{Z4}}\right){-}{\mathrm{%Commutator}}{}\left({\mathrm{Z5}}{,}{\mathrm{*}}{}\left({\mathrm{Z3}}{,}{\mathrm{Z4}}\right)\right)\right)$ (17)

Note, in the last two examples, the introduction of an inert commutator, displayed with its brackets in grey, using the %Commutator command. You can activate those inert computations using value, in which case that commutator will be expanded because one of its operands is a product:

 > $\mathrm{value}\left(\right)$
 ${\mathrm{*}}{}\left({\mathrm{Z1}}{,}{\mathrm{Z2}}{,}{\mathrm{*}}{}\left({\mathrm{Z5}}{,}{\mathrm{Z3}}{,}{\mathrm{Z4}}\right){-}{\mathrm{*}}{}\left({\mathrm{Z3}}{,}{\mathrm{Commutator}}{}\left({\mathrm{Z5}}{,}{\mathrm{Z4}}\right)\right){-}{\mathrm{*}}{}\left({\mathrm{Commutator}}{}\left({\mathrm{Z5}}{,}{\mathrm{Z3}}\right){,}{\mathrm{Z4}}\right)\right)$ (18)

The input to SortProducts doesn't need to be a product, it could be any algebraic expression, in which case all its products will be sorted. For example,

 > $\mathrm{ee}≔\mathrm{Z5}+{\left(\mathrm{Z1}\mathrm{Z5}\right)}^{2}$
 ${\mathrm{ee}}{≔}{\mathrm{Z5}}{+}{\left({\mathrm{Z1}}{}{\mathrm{Z5}}\right)}^{{2}}$ (19)
 > $\mathrm{SortProducts}\left(\mathrm{ee},\left[\mathrm{Z5},\mathrm{Z1}\right],\mathrm{usecommutator}\right)$
 ${\mathrm{Z5}}{+}{\mathrm{^}}{}\left({\mathrm{*}}{}\left({\mathrm{Z5}}{,}{\mathrm{Z1}}\right){+}{\mathrm{Commutator}}{}\left({\mathrm{Z1}}{,}{\mathrm{Z5}}\right){,}{2}\right)$ (20)

Set an algebra rule for the commutator of $\mathrm{Z1}$ and $\mathrm{Z5}$, for example:

 > $\mathrm{Setup}\left(\mathrm{%Commutator}\left(\mathrm{Z1},\mathrm{Z5}\right)=\mathrm{Z6}\right)$
 $\left[{\mathrm{algebrarules}}{=}\left\{{\mathrm{%Commutator}}{}\left({\mathrm{Z1}}{,}{\mathrm{Z2}}\right){=}{0}{,}{\mathrm{%Commutator}}{}\left({\mathrm{Z1}}{,}{\mathrm{Z3}}\right){=}{0}{,}{\mathrm{%Commutator}}{}\left({\mathrm{Z1}}{,}{\mathrm{Z4}}\right){=}{0}{,}{\mathrm{%Commutator}}{}\left({\mathrm{Z1}}{,}{\mathrm{Z5}}\right){=}{\mathrm{Z6}}{,}{\mathrm{%Commutator}}{}\left({\mathrm{Z2}}{,}{\mathrm{Z3}}\right){=}{0}{,}{\mathrm{%Commutator}}{}\left({\mathrm{Z2}}{,}{\mathrm{Z4}}\right){=}{0}{,}{\mathrm{%Commutator}}{}\left({\mathrm{Z3}}{,}{\mathrm{Z4}}\right){=}{0}\right\}\right]$ (21)
 > $P≔\mathrm{Z1}\mathrm{Z5}$
 ${P}{≔}{\mathrm{Z1}}{}{\mathrm{Z5}}$ (22)
 > $\mathrm{SortProducts}\left(P,\left[\mathrm{Z5},\mathrm{Z1}\right]\right)$
 ${\mathrm{Z5}}{}{\mathrm{Z1}}{+}{\mathrm{Z6}}$ (23)

The result above got computed using the algebra rule set. In some cases, however, instead of the value of the commutator (algebra rule), it is preferred the actual commutator, unevaluated. For these purposes pass the useinert option

 > $\mathrm{SortProducts}\left(P,\left[\mathrm{Z5},\mathrm{Z1}\right],\mathrm{useinert}\right)$
 ${\mathrm{*}}{}\left({\mathrm{Z5}}{,}{\mathrm{Z1}}\right){+}{\mathrm{%Commutator}}{}\left({\mathrm{Z1}}{,}{\mathrm{Z5}}\right)$ (24)
 > $\mathrm{SortProducts}\left(P,\left[\mathrm{Z5},\mathrm{Z1}\right],\mathrm{useanticommutator},\mathrm{useinert}\right)$
 ${-}{\mathrm{*}}{}\left({\mathrm{Z5}}{,}{\mathrm{Z1}}\right){+}{\mathrm{%AntiCommutator}}{}\left({\mathrm{Z1}}{,}{\mathrm{Z5}}\right)$ (25)

Equivalently, when the value of the commutator is not known to the system and there are products in the operands of the commutators introduced, instead of the default inert representations one may prefer an active representation using the Commutator or AntiCommutator commands. For that purpose pass the option useinert = false

 > $P≔\mathrm{Z2}\mathrm{Z3}\mathrm{Z5}$
 ${P}{≔}{\mathrm{Z2}}{}{\mathrm{Z3}}{}{\mathrm{Z5}}$ (26)
 > $\mathrm{SortProducts}\left(P,\left[\mathrm{Z5},\mathrm{Z2}\right],\mathrm{usecommutator}\right)$
 ${\mathrm{*}}{}\left({\mathrm{Z5}}{,}{\mathrm{Z2}}{,}{\mathrm{Z3}}\right){-}{\mathrm{%Commutator}}{}\left({\mathrm{Z5}}{,}{\mathrm{*}}{}\left({\mathrm{Z2}}{,}{\mathrm{Z3}}\right)\right)$ (27)
 > $\mathrm{SortProducts}\left(P,\left[\mathrm{Z5},\mathrm{Z2}\right],\mathrm{usecommutator},\mathrm{useinert}=\mathrm{false}\right)$
 ${\mathrm{*}}{}\left({\mathrm{Z5}}{,}{\mathrm{Z2}}{,}{\mathrm{Z3}}\right){-}{\mathrm{*}}{}\left({\mathrm{Z2}}{,}{\mathrm{Commutator}}{}\left({\mathrm{Z5}}{,}{\mathrm{Z3}}\right)\right){-}{\mathrm{*}}{}\left({\mathrm{Commutator}}{}\left({\mathrm{Z5}}{,}{\mathrm{Z2}}\right){,}{\mathrm{Z3}}\right)$ (28)
 > 

Compatibility

 • The Physics[SortProducts] command was introduced in Maple 2019.