Search code examples
sortinglispmaximanumerical-integration

Sorting list of elements of the form [x=value1, x=value2,...] in maxima


When I use either solve or allroots method I obtain a list of elements that is not sorted. The problem is that I want to sort that list but I can't because it is of the form [x=value1,x=value2,x=value3] and sort function doesn't work well with these x= expressions. Do you how to solve this?

Background

My problem arises in the context of gaussian quadrature while obtaining the roots of Legendre Polynomials. In order to obtain the coefficients of the formula I need to integrate Lagrange polynomials and to do so I need to have the list ordered.


Solution

  • I gather that what you want is to sort by the right-hand side values and obtain just the right-hand side.

    (%i1) allroots (x^3 - 3*x);
    (%o1)      [x = 0.0, x = 1.732050807568877, x = - 1.732050807568877]
    (%i2) sort (map (rhs, %o1));
    (%o2)            [- 1.732050807568877, 0.0, 1.732050807568877]
    

    or equivalently in this case

    (%i3) map (rhs, sort (%o1));
    (%o3)            [- 1.732050807568877, 0.0, 1.732050807568877]
    

    since sort will sort general expressions.