add tag
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}
```

![image.png](/image?hash=1ecb1e2b6dc60e750bd1417e4e34d02aeb5055f9f52be1ffa922b01f3f347fb3)


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}

```

![image.png](/image?hash=cb57d7acfb77db15ae679ca897f767b3a90a61b6fa60fe6ee6183fb756495180)

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}
```


![foo.png](/image?hash=a1e6a90361a0249cbc16f491d16cd4c481841006cd8948c4eff6abfe1d4a19e3)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.