Anonymous 14035
I use `luacas` to solve equations. For the following equation, its solutions are not arranged in either ascending or descending order. How can I construct a function to sort the solutions in ascending order?
```
\documentclass{article}
\usepackage{luacas}
\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem{ex}{Example}
\begin{document}
\begin{ex}
\begin{CAS}
vars("x")
f = (x-1)*(x-2)*(x+3)
r = ZTable( roots(f) )
\end{CAS}
The set of solutions of the equation $\print{f} = 0$ is
\[\left\{ \lprint{r} \right\}.\]
Here
\[r_1 = \print{r[1]}, \quad r_2 = \print{r[2]}, \quad r_3 = \print{r[3]}.\]
\end{ex}
\begin{ex}
\begin{CAS}
vars("x")
f = x^2 - 7*x - 13
r = ZTable( roots(f) )
\end{CAS}
The set of solutions of the equation $\print{f} = 0$ is
\[\left\{ \lprint{r} \right\}.\]
Here
\[r_1 = \print{r[1]}, \quad r_2 = \print{r[2]}.\]
\end{ex}
\end{document}
```

With rational solutions, I tried
```
\documentclass{article}
\usepackage{luacas}
\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem{ex}{Example}
\begin{document}
\begin{ex}
\begin{CAS}
vars("x")
f = (x-1)*(x-2)*(x+3)*(4*x-7)
r = roots(f)
table.sort(r, function(a,b) return a < b end)
r = ZTable(r)
\end{CAS}
The set of solutions of the equation
\[\print{f} = 0\]
is
\[\left\{ \lprint{r} \right\}.\]
Here
\[r_1 = \print{r[1]}, \quad r_2 = \print{r[2]}, \quad r_3 = \print{r[3]}, \quad r_4 = \print{r[4]}.\]
\end{ex}
\begin{ex}
\begin{CAS}
vars("x")
f = (x^2 - x - 3)*(x^2 - 7*x - 13)
r = roots(f)
r = ZTable(r)
\end{CAS}
The set of solutions of the equation
\[\print{f} = 0\]
is
\[\left\{ \lprint{r} \right\}.\]
Here
\[r_1 = \print{r[1]}, \quad r_2 = \print{r[2]}, \quad r_3 = \print{r[3]}, \quad r_4 = \print{r[4]}.\]
\end{ex}
\end{document}
```

This way does not true with irrational solutions.
Top Answer
Skillmon
The results of `luacas` behave quite nicely in that they print as an expression if you apply `tostring()` on them. In your second code block they print as
```none
7/2 + (1/2 * (101 ^ (1/2)))
7/2 + (-1/2 * (101 ^ (1/2)))
1/2 + (1/2 * (13 ^ (1/2)))
1/2 + (-1/2 * (13 ^ (1/2)))
```
We can let Lua interpret those strings as input using `load()` or `loadstring()` depending on the Lua version (in LuaTeX it's `loadstring()`). With that we can then sort the results according to their approximate values (sorry for splitting that code example up, but that gives nicer syntax highlighting):
```
\documentclass{article}
\usepackage{luacas}
\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem{ex}{Example}
\begin{document}
\begin{ex}
\begin{CAS}
```
```lua
vars("x")
f = (x-1)*(x-2)*(x+3)*(4*x-7)
r = roots(f)
table.sort(r, function(a,b) return a < b end)
r = ZTable(r)
```
```
\end{CAS}
The set of solutions of the equation
\[\print{f} = 0\]
is
\[\left\{ \lprint{r} \right\}.\]
Here
\[
r_1 = \print{r[1]},
\quad r_2 = \print{r[2]},
\quad r_3 = \print{r[3]},
\quad r_4 = \print{r[4]}.
\]
\end{ex}
\begin{ex}
\begin{CAS}
```
```lua
vars("x")
f = (x^2 - x - 3)*(x^2 - 7*x - 13)
r = roots(f)
r = ZTable(r)
local load = load or loadstring
local conv = function(x) return load('return ' .. tostring(x))() end
table.sort(r, function(a, b) return conv(a) < conv(b) end)
```
```
\end{CAS}
The set of solutions of the equation
\[\print{f} = 0\]
is
\[\left\{ \lprint{r} \right\}.\]
Here
\[
r_1 = \print{r[1]},
\quad r_2 = \print{r[2]},
\quad r_3 = \print{r[3]},
\quad r_4 = \print{r[4]}.
\]
\end{ex}
\end{document}
```
