Cannot sort elements each row matrix in Maple

134 Views Asked by At

I want to sort descending of my matrix each row with maple, $$ A= \begin{bmatrix} 0&-1&2\\-3&0&5\\-1&-5&0 \end{bmatrix} $$

become

$$ A= \begin{bmatrix} 2&0&-1\\5&0&-3\\0&-1&-5 \end{bmatrix}. $$

I typing in maple :

restart:
with(linalg):
A := matrix(3, 3, [0, -1, 2, -3, 0, 5, -1, -5, 0]):
evalm(sort(A, `>`));

But the result same as $A$. How to sort descending each row of matrix $A$?

1

There are 1 best solutions below

1
On

As a question about Maple programming (rather than about mathematics) this question belongs on either www.stackoverflow.com or www.mapleprimes.com .

I'm not sure which version of Maple you're using, but lowercase matrix is deprecated in favour of Matrix (available since Maple 6 released in the year 2000).

Here are two ways to get that, using Matrix,

restart:
A := Matrix(3, 3, [0, -1, 2, -3, 0, 5, -1, -5, 0]);

                 [ 0  -1   2]
                 [          ]
            A := [-3   0   5]
                 [          ]
                 [-1  -5   0]

<seq(sort(A[i,1..-1],`>`), i=1..3)>;

                [2   0  -1]
                [         ]
                [5   0  -3]
                [         ]
                [0  -1  -5]

Matrix(map(sort,convert(A,listlist),`>`));

                [2   0  -1]
                [         ]
                [5   0  -3]
                [         ]
                [0  -1  -5]

If you insist on using the deprecated matrix then you can use the second approach above.

restart;
A := matrix(3, 3, [0, -1, 2, -3, 0, 5, -1, -5, 0]);

                 [ 0  -1   2]
                 [          ]
            A := [-3   0   5]
                 [          ]
                 [-1  -5   0]

matrix(map(sort,convert(A,listlist),`>`));

                [2   0  -1]
                [         ]
                [5   0  -3]
                [         ]
                [0  -1  -5]