Given a Weyl group $W$, Lusztig's $a$-function a from W to the natural numbers is defined as a function constant on two-sided cells and $a(w)=l(w)-2 d(w)$ when $w$ is an involution, where $l(w)$ is the length function and $d(w)$ is the degree of the Kazhdan-Lusztig polynomial $P_{1,w}$. Since every two-sided cell contains an involution the function a is defined on all of W. See for example before conjecture 15 in https://msp.org/pjm/2007/232-2/pjm-v232-n2-p06-s.pdf for this defintion.
My question is how to obtain the $a$-function for the symmetric group as a Weyl group via SAGE.
A more explicit formula in this case is given as follows: Let $x$ be a permutation corresponding to the pair of tableau $(P(x),Q(x))$ by the Robinson-Schendsted correspondence and $shape(Q(x)')=( \lambda_1,...,\lambda_k)$ where $Q(x)'$ is the transposed tableaux. Then $a(x)=\sum\limits_{i=1}^{k}{\binom{\lambda_i}{2}}$, see for example exercise 10 on page 198 in the book by Björner and Brenti "Combinatorics of Coxeter Groups".